afcapel / stimulus-autocomplete

Stimulus autocomplete component
MIT License
482 stars 62 forks source link

Dont submit form when pressing Enter #146

Open brentgreeff opened 9 months ago

brentgreeff commented 9 months ago

When the input used for searching is part of a form with other fields, its very easy to use the Enter to select an option, but then also to press Enter and submit the whole form, which I did accidentally many times.

data-autocomplete-submit-on-enter-value="false" - does not do what I would expect.

input[type="text" data-autocomplete-target="input" data-action="keydown.enter->autocomplete#bad"]
class CustomAutocomplete extends Autocomplete {

  bad(e) {
    e.preventDefault()
  }
}
application.register('autocomplete', CustomAutocomplete)

This worked for me.