0.0.1 • Published 4 years ago

react-suggestbar v0.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

react-suggestbar

A simple searchbar with suggestion as you type

live demo

source

Summary

Installing

$ npm install react-suggestbar

Description

It's a simple component that shows you suggestions as you type.

The suggestions are hidden when the focus is no longer contains in the <div/> container.

The user can use them to autocomplete the <input/>.

Usage

import React from "react";
import SuggestBar from "react-suggestbar";

const fruits = [
	"apple",
	"apricot",
	"banana",
	"blackberry",
	"blueberry",
	...
];

function Component() {
	const [suggestions, setSuggestions] = useState([]);
	const [search, setSearch] = useState("");

	const change = (evt) => {
		let value = evt.target.value;

		setSearch(value);
		if (value === "") {
			setSuggestions([]);
		} else {
			setSuggestions(fruits.filter((fruit) => fruit.startsWith(value)));
		}
	};

	const submit = () => {
		console.log(`Submit: ${search}`);
		setSearch("");
	};

	const click = (suggestion) => {
		console.log(`Accept suggestion: ${suggestion}`);
		setSearch("");
	};

	return (
		<SuggestBar
			inputValue={search}
			onInputChange={change}
			onInputSubmit={submit}
			suggestData={suggestions}
			onSuggestClick={click}
		/>
	);
}

export default Component;

Props

namerequiredtypedefaultdescription
inputValueyesanyThe value attribute of the <input/>
inputTypenostring"text"The type attribute of the <input/>
inputPlaceholdernostring""The placeholder attribute of the <input/>
onInputChangeyes(event: React.ChangeEvent) => voidThe event handler for the onChange event listener of the <input/>
onInputSubmityesVoidFunctionThe event handler for the onSubmit event listener of the <input/> and the <button/>
submitBtnnoReact.ReactNode"Ok"The content (children) of the <button/>
suggestDatayesstring[]The list of suggestions the suggestbar must show
onSuggestClickyes(suggestion: string) => voidThe event handler for the onClick event listener of each suggestion
containerClassNamenostringA CSS className for the <div/> container
inputClassNamenostringA CSS className for the <input/>
submitBtnClassNamenostringA CSS className for the submit <button/>
suggestContainerClassNamenostringA CSS className for the <div/> container of the suggestions
suggestClassNamenostringA CSS className for each suggestion (<button/>)

LICENSE

MIT