⚡️🌳 A React search bar and hook that utilizes a trie tree for super fast results
There are two sets of instructions. The first installation is for getting the demo project running on your machine. The second installation is for incoperating the search bar or hook into an existing project.
If you would like to utilize the trie tree features without using the search bar and create your own use case. I highly recommend you just the autocomplete hook.
-
Refer to the getting started description first!
-
Clone the repository:
git clone https://github.com/alexbarksdale/react-autocomplete.git
- You now need to install all of the necessary packages for the project, type the following in your terminal:
npm install
- This project comes with a demo application and you can view it with:
npm start
-
Refer to the getting started description first!
-
Clone the repository:
git clone https://github.com/alexbarksdale/react-autocomplete.git
-
Navigate into
src/assets/
and taketrietree.ts
andtrietreenode.ts
into your desired location. -
OPTION 1: If you would like to use the search bar, navigate to
src/components/
and takeSearchBar.tsx
into your desired location.‼️ This component uses styled-components and react-icon. React-icon is only for the 🔍 icon so if you would like to remove it, find it with cmd + f (ctrl + f for windows) and search forSearchIcon
and remove it. However, you must install styled-components with:npm i styled-components @types/styled-components
andnpm i react-icons
if you would like to keep the 🔍 icon. -
OPTION 2: If you would like to use the React autocomplete hook, navigate to
src/components/
and takeuseAutocomplete.ts
into your desired location.
Prop | Type | Required | Description |
---|---|---|---|
placeholder |
String | Customize the placeholder of the component. | |
type |
String | Change the input type. Default: type="text" | |
corpus |
Array[string] | Supply a corpus to initialize the tree with words or phrases. | |
onChange |
Function | Retrieve the input value from the component back to the parent. | |
disableTermSubmit |
Boolean | Disable adding a new term to the tree on submit. |
import { SearchBar } from './SearchBar';
<SearchBar
placeholder="Search menu..."
corpus={menuItems}
onChange={(searchTerm: string) => setTerm(searchTerm)}
disableTermSubmit
/>
<SearchBar placeholder="Example" />
<SearchBar type="text" />
<SearchBar corpus={['Pizza', 'Pasta', 'Potato']} />
<SearchBar onChange={(s: string) => setState(s)} />
<SearchBar disableTermSubmit />
- Usage:
const [completions] = useAutocomplete(arg1, arg2)
- Returns: An array of strings containing completions from a given prefix.
Arg | Type | Required | Description |
---|---|---|---|
arg1 |
String | ✓ | Used to find a prefix in the tree |
arg2 |
Array[string] | Supply a corpus to initialize the tree with words or phrases. |
import React, { useState} from 'react';
import { useAutocomplete } from './useAutocomplete';
const corpus = ['Pizza', 'Pasta', 'Potato'];
const [searchTerm, setSearchTerm] = useState('');
const [completions] = useAutocomplete(searchTerm, corpus);
export function Demo(): JSX.Element {
return (
{completions.map((item, i) => {
return (
<ul>
<li key={i}>{item}</li>
</ul>
);
})}
);
}