autocomplete icon indicating copy to clipboard operation
autocomplete copied to clipboard

Prevent submit on enter only when the autocomplete is displayed

Open ofrias opened this issue 4 years ago • 2 comments

The preventSubmit setting should only be applied when the autocomplete list is visible. If the enter is pressed while the focus is on the input field the form should be submited.

Sample code:

              var isOpen = containerDisplayed();
              if (selected) {
                  settings.onSelect(selected, input);
                  clear();
              }
              if (preventSubmit && isOpen) {
                  ev.preventDefault();
              }

ofrias avatar May 25 '21 10:05 ofrias

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 25 '22 21:05 stale[bot]

I think this is still active, my PR #89 should fix this.

Taitava avatar May 26 '22 10:05 Taitava

Unfortunately this issue is not fixed in the current version. You can reproduce it by including the input in a form and pressing enter while the input is focused (without the autocomplete list visible). The browser won't post the form (because of the ev.preventDefault() call) but it should. The sample code is still applicable to the current version and fixes the issue.

ofrias avatar Mar 17 '23 07:03 ofrias

@ofrias Have you tried if #89 fixes it for you?

Taitava avatar Mar 18 '23 04:03 Taitava

Oops, I didn't noticed the PR was not merged in trunk yet. I have just tried and yes, it fixes it too.

ofrias avatar Mar 18 '23 07:03 ofrias

@ofrias @Taitava

You can now use the following option to prevent form submissions only when an item is selected in autocomplete:

autocomplete({
    ...,
    preventSubmit: PreventSubmit.OnSelect
});

You also need to install the following version:

npm install --save-dev [email protected]

I will close the issue, but if something is not working as expected - let me know.

denis-taran avatar Jul 22 '23 13:07 denis-taran

@kraaden so, is PR #89 redundant now? 🙂

Taitava avatar Jul 23 '23 16:07 Taitava

@Taitava Yes, thanks! I closed the PR

denis-taran avatar Jul 23 '23 17:07 denis-taran