sickdyd / react-search-autocomplete

A search box that filters the provided array of objects
https://sickdyd.github.io/react-search-autocomplete
MIT License
218 stars 82 forks source link

How to trigger event pressing Enter key on search #38

Closed hoanghuychh closed 2 years ago

hoanghuychh commented 2 years ago

Hi I'm using ur package but I can't handle the event press enter key, Do you have any suggestions for me?

sickdyd commented 2 years ago

@hoanghuychh Hello! I need more context: what are you trying to achieve?

hoanghuychh commented 2 years ago

@sickdyd Thank you for your reply, I want to press Enter key to do a search action and can I do it, like the bar search's google?

sickdyd commented 2 years ago

@hoanghuychh I think you mean to use the onSearch callback. Please check the readme file for more.

hoanghuychh commented 2 years ago

Yes I used it but the onSearch can't get keydown Enter event or any onKeyDown event :((

sickdyd commented 2 years ago

@hoanghuychh Ah, now I understand: you want to know what key has been pressed. There is no way to do it right now. Can you tell me why you want to know which key has been pressed?

This PR is implementing arrow key navigation so it might help you out: https://github.com/sickdyd/react-search-autocomplete/pull/34

Alex-Licea commented 2 years ago

Hi!

I tried to achieve the same behaviour. My solution was to wrap the autocomplete in a form with a onSubmit callback and store the input value in a state which is updated every onSearch call.

When you press “return” key, use the onSubmit callback to use your stored input value.

sickdyd commented 2 years ago

@Alex-Licea Thank you, that seems like a good solution for this issue.