hotwired / stimulus

A modest JavaScript framework for the HTML you already have
https://stimulus.hotwired.dev/
MIT License
12.7k stars 424 forks source link

Email autocomplete on `<input>` triggers `keydown.esc` data-action event #743

Open marcoroth opened 10 months ago

marcoroth commented 10 months ago

If you setup a data-action with a keydown.esc event it will trigger the action whenever you autocomplete the <input> with a selection from the browser popup:

<form data-controller="modal" data-action="keydown.esc@window->modal#close">
  <input type="email" autocomplete="email" placeholder="Email">
</form>

Here's a codepen to demonstrate the behaviour: https://codepen.io/marcoroth/pen/vYbPNeE?editors=1010

It looks like it's not only specific to the esc modifier, as it also happens for any other modifier or the plain keydown event as well. It seems like the autocomplete in browsers is being simulated by emitting a series of keydown, keyup and change events to mimic user input.

Maybe we can detect if an event wasn't actual human input and then prevent the action from being run.

/cc @tylerklose

rik commented 10 months ago

In Safari, the event emitted is of type CustomEvent, not KeyboardEvent. And that's messing up the key filtering: https://github.com/hotwired/stimulus/blob/349dc165611238f8ba5b316d86ec6536c58b2a2c/src/core/binding.ts#L89-L91

rik commented 10 months ago

I couldn't get Chrome to autocomplete emails so I couldn't test the type of Events it emits in this situation.

tsmd commented 10 months ago

Hi, @marcoroth, I have submitted a pull request to fix the issue you raised in #713. I would be grateful if you could do a review if you could.

alexander-schranz commented 7 months ago

I can confirm this issue is happening also for other inputs when using Safari:

safari-test