From 59f87d01fa8636b4986be20b11af239546ccd763 Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Mon, 1 Feb 2021 17:15:48 +0100 Subject: [PATCH] Fix SaveButton Breaking Change Regarding FormContext --- .../src/button/SaveButton.spec.tsx | 33 +++++-------------- .../src/button/SaveButton.tsx | 26 ++++++++++++--- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/packages/ra-ui-materialui/src/button/SaveButton.spec.tsx b/packages/ra-ui-materialui/src/button/SaveButton.spec.tsx index e2f61127212..17b57bc6469 100644 --- a/packages/ra-ui-materialui/src/button/SaveButton.spec.tsx +++ b/packages/ra-ui-materialui/src/button/SaveButton.spec.tsx @@ -57,9 +57,7 @@ describe('', () => { - - - + @@ -78,9 +76,7 @@ describe('', () => { - - - + @@ -92,9 +88,7 @@ describe('', () => { const { getByLabelText } = render( - - - + ); @@ -107,9 +101,7 @@ describe('', () => { const { getByLabelText } = render( - - - + ); @@ -144,12 +136,7 @@ describe('', () => { const { getByLabelText } = render( - - - + ); @@ -168,12 +155,10 @@ describe('', () => { dispatchSpy = jest.spyOn(store, 'dispatch'); return ( - - - + ); }} diff --git a/packages/ra-ui-materialui/src/button/SaveButton.tsx b/packages/ra-ui-materialui/src/button/SaveButton.tsx index 67227880609..f524d4c1ce4 100644 --- a/packages/ra-ui-materialui/src/button/SaveButton.tsx +++ b/packages/ra-ui-materialui/src/button/SaveButton.tsx @@ -81,21 +81,37 @@ const SaveButton: FC = props => { const classes = useStyles(props); const notify = useNotify(); const translate = useTranslate(); - const { setOnSave } = useFormContext(); + const saveContext = useFormContext(); const { setOnSuccess, setOnFailure, setTransform } = useSaveContext(props); const handleClick = event => { // deprecated: use onSuccess and transform instead of onSave if (typeof onSave === 'function') { if (process.env.NODE_ENV !== 'production') { - console.log( + console.warn( ' prop is deprecated, use the onSuccess prop instead.' ); + if (!saveContext || !saveContext.setOnSave) { + console.warn( + 'Using outside a FormContext is deprecated.' + ); + } + } + if (saveContext && saveContext.setOnSave) { + saveContext.setOnSave(onSave); } - setOnSave(onSave); } else { - // we reset to the Form default save function - setOnSave(); + if ( + (process.env.NODE_ENV !== 'production' && !saveContext) || + !saveContext.setOnSave + ) { + console.warn( + 'Using outside a FormContext is deprecated.' + ); + } else { + // we reset to the Form default save function + saveContext.setOnSave(); + } } if (onSuccess) { setOnSuccess(onSuccess);