-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement fuzzy matching for combobox dropdowns to sort based on best match instead of alphabetical. resolves #904
- Loading branch information
Showing
6 changed files
with
45 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
libs/shared/ui-utils/src/lib/hooks/useFuzzySearchFilter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Fuse, { IFuseOptions } from 'fuse.js'; | ||
import { useMemo, useState } from 'react'; | ||
import { useDebounce } from './useDebounce'; | ||
|
||
const DEFAULT_OPTIONS: IFuseOptions<unknown> = { | ||
includeScore: true, | ||
findAllMatches: true, | ||
ignoreLocation: true, | ||
includeMatches: true, | ||
keys: ['label', 'value', 'secondaryLabel', 'tertiaryLabel'], | ||
}; | ||
|
||
export function useFuzzySearchFilter<T extends object>(items: T[], filter: string, options: IFuseOptions<unknown> = DEFAULT_OPTIONS) { | ||
const fuse = useMemo(() => new Fuse<T>(items, { ...options }), [items, options]); | ||
|
||
const filterText = useDebounce(filter, 300); | ||
const [visibleItems, setVisibleItems] = useState(items); | ||
|
||
useMemo(() => { | ||
if (filterText) { | ||
const result = fuse.search(filterText); | ||
setVisibleItems(result.map(({ item }) => item)); | ||
} else { | ||
setVisibleItems(items); | ||
} | ||
}, [filterText, fuse, items]); | ||
|
||
return visibleItems; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters