I am using the react-autocomplete npm package to build the autocomplete functionality in my Reactjs app. So far I have been able to render dropdown items that match the user's input, as you see in the documentation of this package. However, I would like to generate a default text in the dropdown as No results found whenever user's input do not match any of the dropdown items. And to be honest I am really struggling with it.
I tried adding custom function to the shouldItemRender to change the state and dropdown items accordingly but it does not help either. Here is the snippet of my code -
const suggestions= [{id: 100 , text: "Aluminium extracts" }, {id: 101 , text: "Copper extracts" }]
<Autocomplete
getItemValue={(item) => item.text}
items={ suggestions }
renderItem={(item, isHighlighted) => {
return <div> {item.text} </div>)
}
}
shouldItemRender={(item, value) => item.text.toLowerCase().indexOf(value.toLowerCase()) > -1}
// shouldItemRender={(item, value) => handleRender(item, value) }
value={value}
onChange={(e, newValue) => { setValue(e.target.value) }}
onSelect={(v) => handleInput(v)}
inputProps={{ placeholder: "start typing"}}
/>
I will appreciate your help if you can help me accomplish this. Thank you.
Use
renderMenuto check whether a match found or not. If there is no match found then show the No match found, if match found then return the items andrenderItemwill render the items list.Check this link for better understanding. https://github.com/reactjs/react-autocomplete/blob/master/examples/custom-menu/app.js