Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combobox: Fuzzy search #2296

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dompurify": "^3.1.3",
"dotenv": "^16.0.3",
"formik": "^2.2.9",
"fuse.js": "^7.0.0",
"hex-color-regex": "^1.1.0",
"history": "^5.3.0",
"i18next": "^22.1.4",
Expand Down
45 changes: 26 additions & 19 deletions src/components/forms/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Alert } from '../Alert';
import { useColorScheme } from '$app/common/colors';
import { styled } from 'styled-components';
import { Spinner } from '../Spinner';
import Fuse from 'fuse.js';

export interface Entry<T = any> {
id: number | string;
Expand Down Expand Up @@ -66,6 +67,7 @@ export interface ComboboxStaticProps<T = any> {
isDataLoading?: boolean;
onInputValueChange?: (value: string) => void;
compareOnlyByValue?: boolean;
onFilter?: (entries: Entry<T>[]) => unknown;
}

export type Nullable<T> = T | null;
Expand Down Expand Up @@ -107,6 +109,7 @@ export function Combobox<T = any>({
onEmptyValues,
onFocus,
onInputValueChange,
onFilter,
}: ComboboxStaticProps<T>) {
const [inputValue, setInputValue] = useState(
String(inputOptions.value ?? '')
Expand All @@ -122,15 +125,9 @@ export function Combobox<T = any>({
let filteredOptions =
inputValue === ''
? entries
: entries.filter(
(entry) =>
entry.label?.toLowerCase()?.includes(inputValue?.toLowerCase()) ||
entry.value
?.toString()
?.toLowerCase()
?.includes(inputValue?.toLowerCase()) ||
entry.searchable.toLowerCase().includes(inputValue?.toLowerCase())
);
: new Fuse(entries, { keys: ['id', 'label', 'searchable'] })
.search(inputValue)
.map((v) => v.item);

filteredOptions = filteredOptions.filter((entry) =>
exclude.length > 0 ? !exclude.includes(entry.value) : true
Expand Down Expand Up @@ -291,6 +288,10 @@ export function Combobox<T = any>({

useDebounce(
() => {
if (onFilter) {
onFilter(filteredOptions);
}

if (!onEmptyValues) {
return;
}
Expand Down Expand Up @@ -471,6 +472,7 @@ export function ComboboxStatic<T = any>({
clearInputAfterSelection,
isDataLoading,
compareOnlyByValue,
onFilter,
}: ComboboxStaticProps<T>) {
const [t] = useTranslation();
const [selectedValue, setSelectedValue] = useState<Entry | null>(null);
Expand All @@ -480,15 +482,9 @@ export function ComboboxStatic<T = any>({
let filteredValues =
query === ''
? entries
: entries.filter(
(entry) =>
entry.label?.toLowerCase()?.includes(query?.toLowerCase()) ||
entry.value
?.toString()
?.toLowerCase()
?.includes(query?.toLowerCase()) ||
entry.searchable.toLowerCase().includes(query?.toLowerCase())
);
: new Fuse(entries, { keys: ['id', 'label', 'value', 'searchable'] })
.search(query)
.map((v) => v.item);

filteredValues = filteredValues.filter((entry) =>
exclude.length > 0 ? !exclude.includes(entry.value) : true
Expand All @@ -509,6 +505,10 @@ export function ComboboxStatic<T = any>({

useDebounce(
() => {
if (onFilter) {
onFilter(filteredValues);
}

if (!onEmptyValues) {
return;
}
Expand Down Expand Up @@ -836,6 +836,7 @@ export function ComboboxAsync<T = any>({
const [entries, setEntries] = useState<Entry<T>[]>([]);
const [url, setUrl] = useState(endpoint);
const [enableQuery, setEnableQuery] = useState<boolean>(false);
const [filtered, setFiltered] = useState<Entry<T>[]>([]);

useEffect(() => {
setUrl(endpoint);
Expand Down Expand Up @@ -973,7 +974,11 @@ export function ComboboxAsync<T = any>({
setUrl((c) => {
const url = new URL(c);

url.searchParams.set('filter', query);
console.log({ query, filtered });

if (filtered.length === 0 || query === '') {
url.searchParams.set('filter', query);
}

return url.href;
});
Expand Down Expand Up @@ -1001,6 +1006,7 @@ export function ComboboxAsync<T = any>({
onInputValueChange={onInputValueChange}
onEmptyValues={onEmptyValues}
compareOnlyByValue={compareOnlyByValue}
onFilter={setFiltered}
/>
);
}
Expand All @@ -1025,6 +1031,7 @@ export function ComboboxAsync<T = any>({
isDataLoading={isLoading}
onInputValueChange={onInputValueChange}
compareOnlyByValue={compareOnlyByValue}
onFilter={setFiltered}
/>
);
}
Loading