diff --git a/.changeset/plenty-drinks-tan.md b/.changeset/plenty-drinks-tan.md new file mode 100644 index 000000000..4b34c3b44 --- /dev/null +++ b/.changeset/plenty-drinks-tan.md @@ -0,0 +1,5 @@ +--- +"@skeletonlabs/skeleton": minor +--- + +feat: Autocomplete now accepts custom filter function using the prop `filter`. diff --git a/packages/skeleton/src/lib/components/Autocomplete/Autocomplete.svelte b/packages/skeleton/src/lib/components/Autocomplete/Autocomplete.svelte index 6183b9421..c33b983c9 100644 --- a/packages/skeleton/src/lib/components/Autocomplete/Autocomplete.svelte +++ b/packages/skeleton/src/lib/components/Autocomplete/Autocomplete.svelte @@ -66,6 +66,11 @@ export let regionButton = 'w-full'; /** Provide arbitrary classes to empty message. */ export let regionEmpty = 'text-center'; + /** + * Provide a custom filter function. + * @type {() => AutocompleteOption[]} + */ + export let filter: () => Option[] = filterOptions; // Props (transition) /** @@ -139,7 +144,7 @@ // State $: filterByAllowDeny(allowlist, denylist); - $: optionsFiltered = input ? filterOptions() : listedOptions; + $: optionsFiltered = input ? filter() : listedOptions; $: sliceLimit = limit ?? optionsFiltered.length; // Reactive $: classesBase = `${$$props.class ?? ''}`; diff --git a/sites/skeleton.dev/src/routes/(inner)/components/autocomplete/+page.svelte b/sites/skeleton.dev/src/routes/(inner)/components/autocomplete/+page.svelte index ff4f8f998..279dd2526 100644 --- a/sites/skeleton.dev/src/routes/(inner)/components/autocomplete/+page.svelte +++ b/sites/skeleton.dev/src/routes/(inner)/components/autocomplete/+page.svelte @@ -81,6 +81,19 @@ function onPopupDemoSelect(event: CustomEvent): void { inputPopupDemo = event.detail.label; } + + function customFilterFunction(): FlavorOption[] { + // Create a local copy of options + let _options = [...flavorOptions]; + // Filter options + _options = _options.filter((option) => { + // Format the input search value + const inputFormatted = String(inputDemo).toLowerCase().trim(); + // Check Match + if (option.value.toLowerCase().trim().includes(inputFormatted)) return option; + }); + return _options; + } @@ -220,6 +233,41 @@ const flavorOptions: AutocompleteOption[] = [ + +
+

Custom Filter

+

Provide a custom filter function using the prop filter.

+ + +
+ +
+ +
+
+
+ + [] { + // Create a local copy of options + let _options = [...flavorOptions]; + // Filter options + return _options.filter((option) => { + // Format the input and option values + const inputFormatted = String(inputValue).toLowerCase().trim(); + const optionFormatted = option.value.toLowerCase().trim(); + // Check Match with value only + if (optionFormatted.includes(inputFormatted)) return option; + }); +} + `} + /> + `} /> + +
+

Input Chip