Filter a list for the given text and provide the formatted results with matched text to be boldable
- Filter any list by a text field
- Framework agnostic, can be used in React, Angular, Vue etc.
- Flexible so that any styles can be applied
- Bolded text works for case insensitive matches
- Install package by running
npm install search-list-text
oryarn add search-list-text
- Import the method to your project
import { searchList } from 'search-list-text';
- Use the method for example:
searchList(list, {searchText: searchText, boldClassName: 'bold', labelKey: 'label'})
import React, { useState } from "react";
import "./styles.css";
import { searchList } from "search-list-text";
function App() {
const [searchText, setSearchText] = useState("");
const list = [
{
label: "Nike Nike",
value: "nike"
},
{
label: "Addidas",
value: "addidas"
},
{
label: "Puma",
value: "puma"
},
{
label: "Niomi",
value: "niomi"
}
];
const changeList = (event) => {
setSearchText(event.currentTarget.value);
};
const filteredList = searchList(list, {
searchText: searchText,
boldClassName: "bold",
labelKey: "label"
});
return (
<div>
<input type="text" onChange={changeList} />
<ul className="list">
{filteredList.map((item) => {
return <li dangerouslySetInnerHTML={{ __html: item.label }} />;
})}
</ul>
</div>
);
}
export default App;