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

Inputs automatically submit the form upon Enter, no way to opt out #764

Closed
jaredcwhite opened this issue May 27, 2022 · 3 comments
Closed

Comments

@jaredcwhite
Copy link
Contributor

Normally there's a way to add event handling for standard HTML inputs to cancel the event when the Enter key is pressed, so as to avoid submitting the form or otherwise handle the event manually.

It doesn't appear sl-input has that ability. It handles the underlying input within the shadow DOM and triggers a form submit immediately, bypassing any user-level event handling.

The particular line where this occurs:

this.formSubmitController.submit();

@jaredcwhite
Copy link
Contributor Author

jaredcwhite commented May 27, 2022

I figured out a workaround — if you use event capturing and call stopPropagation() it'll cancel the event down in the shadow DOM.

form.querySelector("sl-input").addEventListener("keydown", event => {
  if (event.key === 'Enter') event.stopPropagation()
}, true)

Feel free to close if you think that's sufficient.

@claviska
Copy link
Member

There was no way to cancel the keydown event because the binding happens in the component. Wrapping the submit logic in setTimeout() will wait a cycle, which is enough time for other listeners to cancel the event if needed. For good measure, I also added a test to prevent regressions.

@jaredcwhite
Copy link
Contributor Author

Cool, thanks for digging into that!

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

No branches or pull requests

2 participants