moroshko / react-autosuggest

WAI-ARIA compliant React autosuggest component
http://react-autosuggest.js.org
MIT License
5.97k stars 586 forks source link

Add onEnter event outside suggestion event #565

Open zeroseed opened 5 years ago

zeroseed commented 5 years ago

Hi , can we add onEnter event to the input that triggered even when suggestion aren't selected? for example when the input are empty , i would like to press enter, then when value are empty i want to trigger my custom event maybe for resetting content for my API

Thank you

mbenadda commented 5 years ago

Hey @zeroseed,

I'm not the maintainer but I just wanted to let you know you can already do this through inputProps. You can just add an onKeyDown prop to your inputProps and it will be called.

You can then match you use-case easily:

// ... input props
onKeyDown: (event) => {
  if (event.keyCode === 13 /* enter */ && !value /* or whatever variable you store the input content in */) {
    // Handle your onEnter logic
  }
},
// ... more input props

As you can see here and there, react-autosuggest makes sure it does not ignore your own handlers.

The only restriction is your handler will be called after the regular react-autosuggest logic. I'm not sure this alone warrants the addition of a specific prop for this use case.

Hope this helps 😅