Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SaveButton Breaking Change Regarding FormContext #5842

Merged
merged 1 commit into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions packages/ra-ui-materialui/src/button/SaveButton.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ describe('<SaveButton />', () => {
<TestContext>
<ThemeProvider theme={theme}>
<SaveContextProvider value={saveContextValue}>
<FormContextProvider value={formContextValue}>
<SaveButton {...invalidButtonDomProps} />
</FormContextProvider>
<SaveButton {...invalidButtonDomProps} />
</SaveContextProvider>
</ThemeProvider>
</TestContext>
Expand All @@ -78,9 +76,7 @@ describe('<SaveButton />', () => {
<TestContext>
<ThemeProvider theme={theme}>
<SaveContextProvider value={saveContextValue}>
<FormContextProvider value={formContextValue}>
<SaveButton disabled={true} />
</FormContextProvider>
<SaveButton disabled={true} />
</SaveContextProvider>
</ThemeProvider>
</TestContext>
Expand All @@ -92,9 +88,7 @@ describe('<SaveButton />', () => {
const { getByLabelText } = render(
<TestContext>
<SaveContextProvider value={saveContextValue}>
<FormContextProvider value={formContextValue}>
<SaveButton submitOnEnter />
</FormContextProvider>
<SaveButton submitOnEnter />
</SaveContextProvider>
</TestContext>
);
Expand All @@ -107,9 +101,7 @@ describe('<SaveButton />', () => {
const { getByLabelText } = render(
<TestContext>
<SaveContextProvider value={saveContextValue}>
<FormContextProvider value={formContextValue}>
<SaveButton submitOnEnter={false} />
</FormContextProvider>
<SaveButton submitOnEnter={false} />
</SaveContextProvider>
</TestContext>
);
Expand Down Expand Up @@ -144,12 +136,7 @@ describe('<SaveButton />', () => {
const { getByLabelText } = render(
<TestContext>
<SaveContextProvider value={saveContextValue}>
<FormContextProvider value={formContextValue}>
<SaveButton
handleSubmitWithRedirect={onSubmit}
saving
/>
</FormContextProvider>
<SaveButton handleSubmitWithRedirect={onSubmit} saving />
</SaveContextProvider>
</TestContext>
);
Expand All @@ -168,12 +155,10 @@ describe('<SaveButton />', () => {
dispatchSpy = jest.spyOn(store, 'dispatch');
return (
<SaveContextProvider value={saveContextValue}>
<FormContextProvider value={formContextValue}>
<SaveButton
handleSubmitWithRedirect={onSubmit}
invalid
/>
</FormContextProvider>
<SaveButton
handleSubmitWithRedirect={onSubmit}
invalid
/>
</SaveContextProvider>
);
}}
Expand Down
26 changes: 21 additions & 5 deletions packages/ra-ui-materialui/src/button/SaveButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,37 @@ const SaveButton: FC<SaveButtonProps> = 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(
'<SaveButton onSave> prop is deprecated, use the onSuccess prop instead.'
);
if (!saveContext || !saveContext.setOnSave) {
console.warn(
'Using <SaveButton> outside a FormContext is deprecated.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is deprecated in this method ^^.

);
}
}
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 <SaveButton> outside a FormContext is deprecated.'
);
} else {
// we reset to the Form default save function
saveContext.setOnSave();
}
}
if (onSuccess) {
setOnSuccess(onSuccess);
Expand Down