question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prevent submit event from firing when enter is pressed on search input

See original GitHub issue

Problem

If you have a <PowerSelectMultiple /> with searchEnabled inside a <form> and press <kbd> Enter </kbd> while searching or choosing with keyboard, the submit event gets triggered on the <form> causing an unexpected UX (submitting the form…)

After a long debugging session I noticed something that is not entirely a bug from ember-power-select but how browsers handle form submit events: submit events get triggered on the <form> element and can’t be stopped from being executed when any button with type submit gets pressed or <kbd> Enter </kbd> is pressed while on any of the form’s inputs , i.e. stopPropagation on <kbd> Enter </kbd> doesn’t work at all.

Having

<form {{on "submit" this.onSubmit}}>
 <PowerSelectMultiple
    @options={{array "1" "2" "3"}}
    @selected={{this.selected}}
    @onChange={{this.onChange}}
    @searchEnabled={{true}}
  as |option|>
    {{option}}
 </PowerSelectMultiple>
  <button type="submit">Submit</button>
</form>

I have two possible solutions in mind:

  1. One way to solve this and probably the most a11y friendly, is to wrap this search input inside its own <form> and so prevent the submit event to be triggered on the parent form.
  2. Use the form attribute to fake the browser that this input is meant to submit the fake form with id “fake-form-power-select-form”
<input form="fake-form-power-select-form" />

What do you think?

Something like this

 <form
          class="euiComboBox__input"
          style="font-size: 14px; display: inline-block;"
          {{on "submit" (prevent-default)}}
        >
          <input
            class="ember-power-select-trigger-multiple-input euiComboBox__input"
            autocomplete="off"
            autocorrect="off"
            autocapitalize="off"
            spellcheck={{false}}
            id="ember-power-select-trigger-multiple-input-{{@select.uniqueId}}"
            value={{@select.searchText}}
            aria-controls={{@listboxId}}
            style={{this.triggerMultipleInputStyle}}
            placeholder={{this.maybePlaceholder}}
            disabled={{@select.disabled}}
            tabindex={{@tabindex}}
            {{on "focus" @onFocus}}
            {{on "blur" @onBlur}}
            {{on "input" this.handleInput}}
            {{on "keydown" this.handleKeydown}}
            {{did-insert this.storeInputStyles}}
          />
  </form>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cibernoxcommented, May 3, 2021

I should be releasing a patch version at some point today

1reaction
cibernoxcommented, May 1, 2021

I opened a PR using the second option (adding form="non-existent-form"). I like that better because it’s zero-javascript and i’m not sure nested forms are legal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent users from submitting a form by hitting Enter
Disallow enter key anywhere​​ $(document). on("keydown", "form", function(event) { return event. key != "Enter"; });
Read more >
The Enter Key should Submit Forms, Stop Suppressing it
This sets up a keypress event handler that prevents the default action (implicit submission) from occurring when the enter key is pressed.
Read more >
Prevent form submission on Enter key press | Example Code
Use preventDefault() event method to Prevent form submission on the "Enter" key pressed in JavaScript. Enter key keycode is 13, so you can....
Read more >
X-On - Alpine.js
Here's an example of listening for the Enter key inside an input element. ... preventDefault() inside a listener on the browser event object....
Read more >
Event.preventDefault() - Web APIs | MDN
The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found