I have created a typeAhead component which is an input field with a datalist bound to it in a React app. I listen for the "onChange" event on the input field which fetches the matching typeAhead results and this works fine.
So now I am trying to add some listeners for executing the search which should happen when an option is selected from the dropdown or when "enter" key is pressed. I tried listening for the "onSelect" event but it never seems to get fired. And I'm not sure how to listen for the "enter" key event either.
render() {
return(
<div>
<input type="text" id="searchfld" list="data" placeholder="Search..." onChange={getTypeAheadResults} />
<datalist id="data" onChange= {executesearch} onSelect={executeSearch}></datalist>
</div>
);
}
const getTypeAheadResults= () => {
//fires
}
const executesearch = () => {
//does not fire
}
any help helps, thanks!
EDIT: Tharaka Wijebandara's answer solves the "enter" key issue. still trying to figure out how to listen for datalist selection.
just to reiterate -- when the user types something in the search field, I fetch the query results and add them to the datalist. This is working fine.
What I am trying to accomplish now is to call my executeSearch function when an option is selected from the datalist.
datalistcomponent is returning? It's hard to debug without knowing