-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Prevent submit from propagating. #1336
Prevent submit from propagating. #1336
Conversation
Trying to understand your use case -- forms should not be nested anyway, so maybe there is another solution to this? |
I have a use case: Take the following schema property
There is an added issue with the fact that the |
Yes, I agree, being able to override the default tag created so it is not a If that happens, though, is there any additional need for preventing submit form from propagating? (given that we won't need to have nested |
For my use case, the forms were not nested in the html dom; only the react dom. I was using a custom ArrayFieldTemplate that adds in a control for each row that opens a modal in a react portal. The modal has a form where the user can update some aspects of the row, such as entering a new position. On submitting the form, what I was seeing was the Form.js onSubmit getting called twice-- once targeting the modal form (expected), and again targeting the parent form (unexpected). This was creating an odd race condition where the child form's submit would update the form data (such as changing the row's position); and then the parent form's onSubmit would run with the stale data still in its state, essentially reverting the change. |
I see, makes sense, thanks! |
It seems like it's not a good idea to stop event propagation, though. https://css-tricks.com/dangers-stopping-event-propagation/ Since the event we're trying to prevent (parent form submission) is also controlled by us in rjsf, could we perhaps alternatively do this by adding some logic in |
Thanks for the change. All right -- seems like the logic is sufficiently complicated that it needs a test. Could you add a test with a Form-in-Form scenario? |
test/Form_test.js
Outdated
const arraySubmit = arrayForm.querySelector(".array-form-submit"); | ||
|
||
arraySubmit.click(); | ||
sinon.assert.calledOnce(onSubmit); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we can get more specificity on what exactly called onSubmit
-- can you create two functions, outerOnSubmit
and innerOnSubmit
, then test to see if outerOnSubmit
is never called and innerOnSubmit
is called?
Looks good -- thanks! |
Reasons for making this change
When forms are nested in the react DOM, child submit events will otherwise propagate to the parent form. This can result in unexpected behavior, such as stale data on the parent form if submission of the child triggers an update to the parent's data.
Checklist