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.

Submitting form when pressing 'enter' key

See original GitHub issue

Hey guys

I noticed that when you use the enter key to select an option in the component (react-autosuggest), the form that holds that component gets submitted, does anyone know how to disable or change this behavior?

happening on version ^9.3.1.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

11reactions
rpiaggiocommented, Apr 24, 2018

In case it helps anyone:

We managed to keep form submission on ENTER but prevent it if ENTER was used to choose a suggestion by keeping track of whether an option is selected or not via onSuggestionHighlighted. If an option is selected, then ENTER is prevented from submitting the form as described above.

6reactions
somaholidaycommented, Aug 1, 2017

@charlesrochati I prevented submit of our form (also using redux-form) by passing an onKeyDown handler in to the inputProps prop on Autosuggest:

onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
  switch (event.keyCode) {
  ...
    case KeyCodes.ENTER: {
      event.preventDefault();
    }
  ...
  }
};

render() {
  ...

  const inputProps = {
    ...
    onKeyDown: this.onKeyDown,
  };

  return (
    <Autosuggest
      suggestions={suggestions}
      onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
      onSuggestionsClearRequested={this.onSuggestionsClearRequested}
      onSuggestionSelected={this.onSuggestionSelected}
      onSuggestionHighlighted={this.onSuggestionHighlighted}
      getSuggestionValue={this.getSuggestionValue}
      renderSuggestion={this.renderSuggestion}
      inputProps={inputProps}
      highlightFirstSuggestion={true}
      theme={autosuggestTheme}
    />
  );
} 
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to submit a form when the return key is pressed?
Every time a key is pressed, function enter() will be called. If the key pressed matches the enter key (13), then sendform() will...
Read more >
The Enter Key should Submit Forms, Stop Suppressing it
When filling out a form, pressing enter in a textbox should submit the form. This is known as an implicit form submission. Despite...
Read more >
Submit a form with Enter key with JavaScript/jQuery
The idea is to bind an event handler to the keyup JavaScript event using jQuery's .keyup(handler) method and use that handler to submit...
Read more >
How to submit a form on Enter button using jQuery
Given an HTML form and the task is to submit the form after clicking the 'Enter' button using jQuery. To submit the form...
Read more >
How To Trigger Button Click on Enter - W3Schools
Trigger a button click on keyboard "enter" with JavaScript. Trigger a Button Click on Enter. Press the "Enter" key inside the input field...
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