Skip to content

Commit

Permalink
refactor: move the debounce to the filter object
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jan 16, 2025
1 parent 7f497fc commit 09b4fd9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
9 changes: 8 additions & 1 deletion packages/core/src/collections/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ export interface FilterContext<TValue> {

export interface FilterFn {
(context: FilterContext<unknown>): boolean;
debounceMs: number;
}

export interface FilterOptions {
caseSensitive?: boolean;
debounceMs?: number;
}

export function useDefaultFilter(options: FilterOptions = {}) {
const { caseSensitive = false } = options;
const { caseSensitive = false, debounceMs = 100 } = options;

const withCaseSensitive = caseSensitive ? (value: string) => value : (value: string) => value.toLowerCase();

Expand All @@ -35,6 +37,11 @@ export function useDefaultFilter(options: FilterOptions = {}) {
return withCaseSensitive(option.label) === withCaseSensitive(search);
};

contains.debounceMs = debounceMs;
startsWith.debounceMs = debounceMs;
endsWith.debounceMs = debounceMs;
equals.debounceMs = debounceMs;

return {
contains,
startsWith,
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/useComboBox/useComboBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ export interface ComboBoxCollectionOptions {
* The filter function to use.
*/
filter: FilterFn;

/**
* The debounce time in milliseconds for the filter function.
*/
debounceMs?: number;
}

export function useComboBox<TOption, TValue = TOption>(
Expand Down Expand Up @@ -336,7 +331,7 @@ export function useComboBox<TOption, TValue = TOption>(
});
}

watch(inputValue, debounce(collectionOptions?.debounceMs ?? 100, updateHiddenState));
watch(inputValue, debounce(filter.debounceMs, updateHiddenState));
}

return exposeField(
Expand Down
1 change: 1 addition & 0 deletions packages/playground/src/components/ComboBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const props = defineProps<Props>();
const { contains } = useDefaultFilter({
caseSensitive: false,
debounceMs: 200,
});
const { inputProps, listBoxProps, labelProps, buttonProps, errorMessageProps, errorMessage, descriptionProps } =
Expand Down

0 comments on commit 09b4fd9

Please sign in to comment.