Submitting form when pressing 'enter' key
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.
You don't necessarily have to have a form at all. The component can work just in JavaScript, and not use html forms. If your design does require having a form, you can call preventDefault in the JavaScript. https://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted
You don't necessarily have to have a form at all. The component can work just in JavaScript, and not use html forms.
Yeah, but in my case i have to use it inside a form (i have an integration with redux-form and i need the field that uses react-autosuggest to have its 'state' managed by the redux-form).
Here's some code:
render() {
const {placeholder, suggestions, onChange, onBlur, onFocus} = this.props;
const inputProps = {
placeholder: placeholder || "Selecione...",
value: this.props.selected,
onChange: this.onChange.bind(this),
onFocus: this.onFocus.bind(this)
};
const showSugggestions = this.state.showSugggestions;
return (
<ACFieldTemplate {...this.props}>
<Autosuggest
suggestions={suggestions}
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested.bind(this)}
onSuggestionsClearRequested={this.onSuggestionsClearRequested.bind(this)}
getSuggestionValue={this.getSuggestionValue.bind(this)}
renderSuggestion={this.renderSuggestion.bind(this)}
onSuggestionSelected={this.onSuggestionSelected.bind(this)}
inputProps={inputProps}
alwaysRenderSuggestions={showSugggestions}
/>
</ACFieldTemplate>
);
}
I've tried to use e.preventDefault on both onChange and onSuggestionSelected methods but it doesn't seem to work.
@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}
/>
);
}
@somaholiday That's it!
I was expecting to find a higher level solution, something like:
return (
<Autosuggest
...
keyYouShouldPressToTriggerSubmit={keyCode}
/>
and not a such low level one, but anyways...problem solved.
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.