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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip events that already have default prevented #27

Merged
merged 1 commit into from
Jun 17, 2022

Conversation

dgreif
Copy link
Contributor

@dgreif dgreif commented Jun 17, 2022

There may be scenarios where you want to do client-side validation or have some other specialty handler for a form. In these scenarios, it would be nice to have an escape hatch that prevents remoteForm from handling the event.

There are also some rare (but possible) edge cases with code bundlers where an app could double load this module and execute it twice, creating two registered listeners for form submit events. In this scenario, both instances of remoteForm would trigger their handlers and send the fetch request, causing a double submission.

In both cases, a solution is to check if the event has already been handled by checking event.defaultPrevented. This allows custom handlers to prevent further action with the event, and handles the double load scenario because we already call preventDefault within remoteForm.

This should fix an outstanding issue in github/github where comment creation and other actions can be executed twice.

Note: We may need to consider this a breaking change as it could prevent forms from submitting in apps that are already preventing default somewhere. Not sure how we typically handle unintended side-effects like this 馃槄

@dgreif dgreif requested a review from a team as a code owner June 17, 2022 05:08
@dgreif dgreif requested a review from koddsson June 17, 2022 05:08
Copy link
Contributor

@koddsson koddsson left a comment

Choose a reason for hiding this comment

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

LGTM

@koddsson koddsson merged commit 35eee19 into main Jun 17, 2022
@koddsson koddsson deleted the dg-skip-prevented-events branch June 17, 2022 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants