-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Adding type="button"
to SaveButton
overrides custom onSubmit
behaviour on TabbedForm
#7669
Comments
Hi! |
@slax57 I have this problem too. Looking at the code of SaveButton, your remarks kind of make sense. To be sure what you mean I have setup a codesandbox that reproduce the problem. In the sandbox. I added 2 SimpleForm's, each with its own Toolbar and SaveButton. The first one is a regular SaveButton the second has SaveButton with type="button". You can see how the submitForm function in the second one is not called. How do you propose exactly to fix that? https://codesandbox.io/s/focused-ellis-xvjqkj?file=/src/customRouteNoLayout.tsx I'm guessing, I should wrap the SimpleForm with a SaveContextProvider... |
Yep, got the solution. import * as React from "react";
import {
Toolbar,
SaveButton,
SimpleForm,
TextInput,
SaveContextProvider
} from "react-admin";
const MyToolbarWithSubmitSave = () => (
<Toolbar>
<SaveButton label="submit save" />
</Toolbar>
);
const MyToolbarWithButtonSave = () => (
<Toolbar>
<SaveButton label="button save" type="button" />
</Toolbar>
);
const CustomRouteNoLayout = ({ title = "SimpleForm" }) => {
const submitForm = (formData) => {
console.log("submitForm", formData);
};
return (
<div>
<h4>Simpleform with regular submit SaveButton (submitForm is called)</h4>
<SimpleForm onSubmit={submitForm} toolbar={<MyToolbarWithSubmitSave />}>
<TextInput source="title" />
</SimpleForm>
<h4>Simpleform with type=button SaveButton (submitForm is NOT called)</h4>
<SimpleForm onSubmit={submitForm} toolbar={<MyToolbarWithButtonSave />}>
<TextInput source="title" />
</SimpleForm>
<h4>
Simpleform with type=button SaveButton wrapped in a SaveContextProvider
(submitForm is called)
</h4>
<SaveContextProvider value={{ save: submitForm }}>
<SimpleForm toolbar={<MyToolbarWithButtonSave />}>
<TextInput source="title" />
</SimpleForm>
</SaveContextProvider>
</div>
);
}; |
@christiaanwesterbeek Actually the solution I had in mind was to call the Now, after reading through this issue a 2nd time, I realize it could be nice to have the SaveButton also have a look at the context to see if a custom The original idea was that |
Yes, and the reason I wanted to add
|
I suggest we fix this via documentation: in the section about submit on enter https://marmelab.com/react-admin/EditTutorial.html#submit-on-enter We should warn that this approach isn't compatible with a custom onSubmit, and that developers should pass an |
What you were expecting:
Adding
type="button"
to the SaveButton simple changes the button's behaviour to not fire submit on enter, as per the documentationWhat happened instead:
Changing the
type
property seems to have additional side-effects. It doesn't seem to respect the a customonSubmit
function passed through to the form component. Instead it seems to revert to the defaultonSubmit
behaviourSteps to reproduce:
Access the repro
Navigate to edit post page
Change the text value
Press save
Observe that the default save behaviour is used, instead of our custom
onSubmit
Related code:
Other information:
Environment
The text was updated successfully, but these errors were encountered: