This is a simple autocomplete component that allows you to search for a list of items and select one of them.
v20.10.0
- Run
npm install - Run
npm run storybook - Open your browser and go to
http://localhost:6006/
- Run
npm install - Run
npm run test
- Run
npm install - Run
npm run build - Run
npm link - In your project run
npm link auto-complete - This is an example of how to use the component:
import { AutoComplete } from "auto-complete";
import { useEffect, useRef, useState } from "react";
type StarWarsCharacter = {
name: string;
};
function App() {
const refElement = useRef<HTMLInputElement>(null);
const [starWarsCharacters, setStarWarsCharacters] = useState<string[]>([]);
useEffect(() => {
fetch("https://swapi.dev/api/people/?format=json")
.then((response) => response.json())
.then((data) => {
setStarWarsCharacters(
data.results.map((character: StarWarsCharacter) => character.name),
);
});
}, []);
useEffect(() => {
refElement.current?.focus();
}, []);
return (
<AutoComplete
placeholder="Type a Star Wars character"
options={starWarsCharacters}
onSelectOption={(option) => console.log(option)}
ref={refElement}
/>
);
}
export default App;