Skip to content

Commit

Permalink
Merge pull request #284 from 10up/fix/refactor-use-filtered-list-to-ts
Browse files Browse the repository at this point in the history
Fix: refactor use filtered list TypeScript
  • Loading branch information
fabiankaegy committed Jan 23, 2024
2 parents c41f88e + 3818e11 commit 307dce9
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ const fuzzy = new uFuzzy();
* @param {string?} property name of the prop
* @returns {Array} filtered list
*/
export function useFilteredList(list = [], searchTerm = '', property = 'name') {
export function useFilteredList<TListItem extends {[key: string]: string}>(list: TListItem[] = [], searchTerm = '', property: keyof TListItem = 'name') {
const [filteredList, setFilteredList] = useState(list);

const propertyList = useMemo(() => list.map((item) => item[property]), [list, property]);

const filterList = useCallback(
(searchTerm) => {
(searchTerm: string) => {
const matchedNames = fuzzy.filter(propertyList, searchTerm);
return matchedNames.map((index) => list[index]);
const results = matchedNames?.map((index) => list[index]) || [];
return results;
},
[propertyList, list],
);
Expand Down

0 comments on commit 307dce9

Please sign in to comment.