afcapel / stimulus-autocomplete

Stimulus autocomplete component
MIT License
478 stars 61 forks source link

Dont submit form when pressing Enter #146

Open brentgreeff opened 8 months ago

brentgreeff commented 8 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.