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

feat/Autocomplete Generics #2021

Merged
merged 11 commits into from
Sep 18, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@
type TransitionIn = Transition;
type TransitionOut = Transition;
type Value = unknown;
type Meta = unknown;
</script>

<script
lang="ts"
generics="Value = unknown, TransitionIn extends Transition = SlideTransition, TransitionOut extends Transition = SlideTransition"
generics="Value = unknown, Meta = unknown,
TransitionIn extends Transition = SlideTransition, TransitionOut extends Transition = SlideTransition"
>
import { createEventDispatcher } from 'svelte';
// import { flip } from 'svelte/animate';
// import {slide} from 'svelte/transition';

// Types
import type { AutocompleteOption } from './types.js';
type Option = AutocompleteOption<Value, Meta>;
AdrianGonz97 marked this conversation as resolved.
Show resolved Hide resolved

// Event Dispatcher
type AutocompleteEvent = {
selection: AutocompleteOption<Value>;
selection: Option;
};
const dispatch = createEventDispatcher<AutocompleteEvent>();

Expand All @@ -35,9 +38,9 @@
export let input: Value | undefined = undefined;
/**
* Define values for the list.
* @type {AutocompleteOption<Value>[]}
* @type {Option[]}
*/
export let options: AutocompleteOption<Value>[] = [];
export let options: Option[] = [];
/** Limit the total number of suggestions. */
export let limit: number | undefined = undefined;
/**
Expand Down Expand Up @@ -114,7 +117,7 @@
listedOptions = _options;
}

function filterOptions(): AutocompleteOption<Value>[] {
function filterOptions(): Option[] {
// Create a local copy of options
let _options = [...listedOptions];
// Filter options
Expand All @@ -129,8 +132,8 @@
return _options;
}

function onSelection(option: AutocompleteOption<Value>) {
/** @event {AutocompleteOption<Value>} selection - Fire on option select. */
function onSelection(option: Option) {
/** @event {Option} selection - Fire on option select. */
dispatch('selection', option);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/skeleton/src/lib/components/Autocomplete/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Autocomplete Types

export interface AutocompleteOption<Value = unknown> {
export interface AutocompleteOption<Value = unknown, Meta = unknown> {
/** Provide a unique display label per option. Supports HTML. */
label: string;
/** Provide a unique option value. */
value: Value;
/** Provide a comma separated list of keywords. */
keywords?: any;
keywords?: string;
/** Pass arbitrary data per option. */
meta?: any;
meta?: Meta;
HugeLetters marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@

let inputDemo = '';
let inputAllowlist = '';
type FlavorOption = AutocompleteOption<string>;
const flavorOptions = [
AdrianGonz97 marked this conversation as resolved.
Show resolved Hide resolved
type FlavorOption = AutocompleteOption<string, { healthy: boolean }>;
const flavorOptions: FlavorOption[] = [
{ label: 'Vanilla', value: 'vanilla', keywords: 'plain, basic', meta: { healthy: false } },
{ label: 'Chocolate', value: 'chocolate', keywords: 'dark, white', meta: { healthy: false } },
{ label: 'Strawberry', value: 'strawberry', keywords: 'fruit', meta: { healthy: true } },
{ label: 'Neapolitan', value: 'neapolitan', keywords: 'mix, strawberry, chocolate, vanilla', meta: { healthy: false } },
{ label: 'Pineapple', value: 'pineapple', keywords: 'fruit', meta: { healthy: true } },
{ label: 'Peach', value: 'peach', keywords: 'fruit', meta: { healthy: true } }
] satisfies FlavorOption[];
];
const flavorAllowlist = ['neapolitan', 'pineapple', 'peach'];
let flavorDenylist = ['vanilla', 'chocolate'];

Expand Down