React-autosuggest: Add onEnter event outside suggestion event

Created on 3 Sep 2018  路  1Comment  路  Source: moroshko/react-autosuggest

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

Most helpful comment

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 馃槄

>All comments

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 馃槄

Was this page helpful?
0 / 5 - 0 ratings