<ReactSearchAutocomplete>
A <ReactSearchAutocomplete>
is a fully customizable search box where the user can type text and filter the results. It relies on Fuse.js v6.5.3 for the fuzzy search. Check out their website to see the options (you can pass them to this component).
Click here to see a demo.
Demo source.
Installing
$ npm install react-search-autocomplete
or
$ yarn add react-search-autocomplete
Exports
The default export is <ReactSearchAutocomplete>
.
To use it:
import { ReactSearchAutocomplete } from 'react-search-autocomplete'
React Search Autocomplete Usage
import React from 'react'
import './App.css'
import { ReactSearchAutocomplete } from 'react-search-autocomplete'
function App() {
const items = [
{
id: 0,
name: 'Cobol'
},
{
id: 1,
name: 'JavaScript'
},
{
id: 2,
name: 'Basic'
},
{
id: 3,
name: 'PHP'
},
{
id: 4,
name: 'Java'
}
]
const handleOnSearch = (string, results) => {
console.log(string, results)
}
const handleOnHover = (result) => {
console.log(result)
}
const handleOnSelect = (item) => {
console.log(item)
}
const handleOnFocus = () => {
console.log('Focused')
}
const formatResult = (item) => {
return (
<>
<span style={{ display: 'block', textAlign: 'left' }}>id: {item.id}</span>
<span style={{ display: 'block', textAlign: 'left' }}>name: {item.name}</span>
</>
)
}
return (
<div className="App">
<header className="App-header">
<div style={{ width: 400 }}>
<ReactSearchAutocomplete
items={items}
onSearch={handleOnSearch}
onHover={handleOnHover}
onSelect={handleOnSelect}
onFocus={handleOnFocus}
autoFocus
formatResult={formatResult}
/>
</div>
</header>
</div>
)
}
export default App
With TypeScript
type Item = {
id: number;
name: string;
}
<ReactSearchAutocomplete<Item> ... />
<ReactSearchAutocomplete>
Props:
{
items,
fuseOptions,
resultStringKeyName,
inputSearchString,
inputDebounce,
onSearch,
onHover,
onSelect,
onFocus,
onClear,
showIcon,
showClear,
maxResults,
placeholder,
autoFocus,
styling,
formatResult,
showNoResults,
showNoResultsText,
showItemsOnFocus,
maxLength,
className,
}
License
MIT