react-search-highlight
react-search-highlight is a light weight react package to highlight static/dynamic search for auto completion
Installation
install it using npm or yarn to include it in your own React project
You will also need to import css modules in root your project before using it. dist/react-search-highlight.cjs.development.css
npm install react-search-highlight
or:
yarn add react-search-highlight
Usage
You can either use the hook:
import {
PopOverList,
PopOverOption,
PopOverOptionText,
Props,
ReactSearchHighlightProvider,
SearchBar,
STRING_MATCHING,
useReactSearchHighlight,
Wrapper
} from 'react-search-highlight';
import 'react-search-highlight/dist/react-search-highlight.cjs.development.css';
import TEST_DATA from '../data.json';
const Template = () => {
const {suggestions, isResultsEmpty} = useReactSearchHighlight();
return (
<Wrapper>
<SearchBar
data={TEST_DATA} // need to provide data here
keysToSearch={['heading', 'title']} // keys to search from data object
placeholder="search docs"
matchingAlgorithm={STRING_MATCHING}
ref={ref}
/>
<PopOverList>
{suggestions?.map((item, index) => (
<PopOverOption
optionIndex={index}
key={index}
onClick={() => alert(index)}
>
⚡️
<PopOverOptionText as="h3" value={item.heading} />
<PopOverOptionText as="h5" value={item.title} />
</PopOverOption>
))}
{isResultsEmpty && <h3>No results found</h3>}
</PopOverList>
</Wrapper>
);
};
export const Custom = () => {
return (
<ReactSearchHighlightProvider>
<Template />
</ReactSearchHighlightProvider>
);
};
Or with the wrapper component
import {
PopOverList,
PopOverOption,
PopOverOptionText,
Props,
SearchBar,
Wrapper
} from 'react-search-highlight';
import 'react-search-highlight/dist/react-search-highlight.cjs.development.css';
import TEST_DATA from '../data.json';
export const Default = args => {
return (
<Wrapper>
{({suggestions}) => {
return (
<>
<SearchBar
data={TEST_DATA} // need to provide data here
keysToSearch={['heading', 'title']} // keys to search from data object
placeholder="search docs"
/>
<PopOverList>
{suggestions?.map((item, index) => (
<PopOverOption
optionIndex={index}
key={index}
onClick={() => alert(index)}
>
⚡️
<PopOverOptionText as="h3" value={item.heading} />
<PopOverOptionText as="h5" value={item.title} />
</PopOverOption>
))}
</PopOverList>
</>
);
}}
</Wrapper>
);
};
If you want to use it with modal
import {Modal} from 'react-search-highlight';
export const WithModal = () => {
return (
<div>
<h1>Modal is open</h1>
<Modal>
<Default />
</Modal>
</div>
);
};
Data must be provided as array of object format in <SearchBar/>
component data
prop, additionally keysToSearch
prop must be a array of keys to search form data object.
const data = [
{
title:....,
name:.....,
age:......,
},
....
]
const keysToSearch = ['title','name']
<SearchBar
data={data}
keysToSearch={keysToSearch}
/>
🔨 API
useReactSearchHighlight
can be used with ReactSearchHighlightProvider
and it can be used throughout the component to access the context values. Note that whenever you are using it you must wrap the entire component using ReactSearchHighlightProvider
.
const {
suggestions,
isLoading,
input,
startLoading,
endLoading,
isResultsEmpty,
resetState
} = useReactSearchHighlight();
You can also access these values using wrapper component
<Wrapper>
{({suggestions, isLoading, input, startLoading, endLoading, isResultsEmpty, resetState}) => {
return (
.....
);
}}
</Wrapper>
<SearchBar/>
Props:
data: any[];
keysToSearch?: string[];
inputAlgorithm?: typeof DEBOUNCE | typeof THROTTLE | typeof DEFAULT;
inputAlgorithmTimeout?: number;
matchingAlgorithm?: typeof CHARACTER_MATCHING | typeof STRING_MATCHING;
value?: string;
onChange?: (e:React.ChangeEvent<HTMLInputElement>) => void
initialValue?: string
PrefixIcon?: React.FC
<PopOverOption/>
Props:
children: ReactNode;
optionIndex: number;
<PopOverOptionText/>
Props:
value: string;
as: string;
🐛 Bug Reporting
The Library is in developing stage
- Feel free to Open an issue on GitHub to request any additional features you might need for your use case.
- Connect with me on LinkedIn. I'd love ❤️️ to hear where you are using this library.
📜 License
This software is open source, licensed under the MIT License.