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

Autocomplete Complete #1045

Merged
merged 36 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b29f9b9
Auto Complete component stubbed out
Feb 20, 2023
b8677c0
chore/ updated link
Feb 21, 2023
e4dd99b
Pulled inline styles out into props, added props for whitelist and mode.
Feb 21, 2023
05d395a
Removed old docshell code as its getting reworked.
Feb 21, 2023
a047206
Fuzzy and exclude search implemented
Feb 23, 2023
d8c1f6e
added data-testid
Feb 23, 2023
1c6e85b
chore: Added todo
Feb 25, 2023
0fa8111
Merge branch 'dev' of github.com:JustBarnt/skeleton into feat/autocom…
Feb 25, 2023
24fc1da
Merge branch 'skeletonlabs:dev' into feat/autocomplete
JustBarnt Feb 25, 2023
2e750c8
Updated branch name
Feb 25, 2023
41583ae
Merge branch 'feat/autocomplete' of github.com:JustBarnt/skeleton int…
Feb 25, 2023
27d7194
Did not mean to commit this.
Feb 25, 2023
9c83c6d
feat: More progress on Autocomplete, created autoComplete action
JustBarnt Feb 26, 2023
da20378
Audit and changes by Chris
endigo9740 Feb 28, 2023
9856951
Updated whitelist, aria-labels
JustBarnt Mar 7, 2023
3a84898
Merge branch 'skeletonlabs:dev' into feat/autocomplete
JustBarnt Mar 7, 2023
7e7a8bf
Fixed tabindex
JustBarnt Mar 8, 2023
949fa3d
Merge branch 'feat/autocomplete' of github.com:JustBarnt/skeleton int…
JustBarnt Mar 8, 2023
a8d9f27
Merge branch 'dev' into feat/autocomplete
endigo9740 Mar 15, 2023
2257c2e
Merge latest dev changes, update autocomplete doc page
endigo9740 Mar 15, 2023
63e7949
.
endigo9740 Mar 15, 2023
b1e6309
Resolve casing
endigo9740 Mar 15, 2023
b74d6eb
Rewrote filtering functionality. Items now remain in the llist even w…
JustBarnt Mar 17, 2023
2a937b5
Modifications by Chris
endigo9740 Mar 20, 2023
757f414
started docs, removed substring conditional
JustBarnt Mar 21, 2023
6ffb41f
completed first draft of doc writting
Mar 21, 2023
7c2d326
Spelling fixes.
Mar 21, 2023
20d7bda
Fixed issue with whitelist returning values that "truthly" matched in…
Mar 21, 2023
f3307e8
spelling and grammar corrections.
Mar 21, 2023
9207b0d
Refactored by Chris
endigo9740 Mar 21, 2023
bfad7ce
Additonal refactoring by Chris
endigo9740 Mar 21, 2023
f6afcff
Document the input prop
endigo9740 Mar 21, 2023
1c133db
Corrected misspelling of blacklist, simplified optionValues but just …
Mar 22, 2023
b49d667
Finalize the autocomplete component
endigo9740 Mar 22, 2023
e44bff2
Input chip demo docs
endigo9740 Mar 22, 2023
855642e
Drop aria apg link
endigo9740 Mar 22, 2023
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
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ module.exports = {
browser: true,
es2017: true,
node: true
}
},
};
1 change: 1 addition & 0 deletions src/docs/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const menuNavLinks: any = [
{ href: '/components/app-bar', label: 'App Bar', keywords: 'header, top, bar, title' },
{ href: '/components/app-rail', label: 'App Rail', keywords: 'nav, navigation, tile, sidebar' },
{ href: '/components/app-shell', label: 'App Shell', keywords: 'layout, header, footer, sidebar, page, content' },
{ href: '/components/autocomplete', label: 'Autocomplete', keywords: 'input, filter, fuzzy, auto, complete, suggest' },
{ href: '/components/avatars', label: 'Avatars', keywords: 'image, initial, filter' },
{ href: '/components/conic-gradients', label: 'Conic Gradients', keywords: 'chart, graph, circle, pie, spinner, legend' },
{ href: '/components/file-buttons', label: 'File Buttons', keywords: 'upload, form, input, file, media' },
Expand Down
112 changes: 112 additions & 0 deletions src/lib/components/Autocomplete/Autocomplete.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { slide } from 'svelte/transition';
import { flip } from 'svelte/animate';
const dispatch = createEventDispatcher();

// Types
import type { AutocompleteOption } from './types';

// Props
/**
* Bind the input value.
* @type {unknown}
*/
export let input: unknown = undefined;
/**
* Define values for the list
* @type {AutocompleteOption[]}
*/
export let options: AutocompleteOption[] = [];
/**
* Provide whitelisted values
* @type {unknown[]}
*/
export let whitelist: unknown[] = [];
/**
* Provide blacklist values
* @type {unknown[]}
*/
export let blacklist: unknown[] = [];
/** Provide a HTML markup to display when no match is found. */
export let emptyState: string = 'No Results Found.';
/** Set the animation duration. Use zero to disable. */
export let duration: number = 200;
// Props (region)
/** Provide arbitrary classes to nav element. */
export let regionNav: string = '';
/** Provide arbitrary classes to each list. */
export let regionList: string = 'list-nav';
/** Provide arbitrary classes to each list item. */
export let regionItem: string = '';
/** Provide arbitrary classes to each button. */
export let regionButton: string = 'w-full';
/** Provide arbitrary classes to empty message. */
export let regionEmpty: string = 'text-center';

// Local
let listedOptions = options;

// Whitelist Options
function whitelistOptions(): void {
if (!whitelist.length) return;
listedOptions = [...options].filter((option: AutocompleteOption) => whitelist.includes(option.value));
}

// Blacklist Options
function blacklistOptions(): void {
if (!blacklist.length) return;
const toBlacklist = new Set(blacklist);
listedOptions = [...options].filter((option: AutocompleteOption) => !toBlacklist.has(option.value));
}

function filterOptions(): AutocompleteOption[] {
// Create a local copy of options
let _options = [...listedOptions];
// Filter options
_options = _options.filter((option: AutocompleteOption) => {
// Format the input search value
const inputFormatted = String(input).toLowerCase().trim();
// Format the option
let optionFormatted = JSON.stringify([option.label, option.value, option.keywords]).toLowerCase();
// Check Match
if (optionFormatted.includes(inputFormatted)) return option;
});
return _options;
}

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

// State
$: if (whitelist) whitelistOptions();
$: if (blacklist) blacklistOptions();
$: optionsFiltered = input ? filterOptions() : listedOptions;
// Reactive
$: classsesBase = `${$$props.class ?? ''}`;
$: classesNav = `${regionNav}`;
$: classesList = `${regionList}`;
$: classesItem = `${regionItem}`;
$: classesButton = `${regionButton}`;
$: classesEmtpy = `${regionEmpty}`;
</script>

<div class="autocomplete {classsesBase}" data-testid="autocomplete">
{#if optionsFiltered.length > 0}
<nav class="autocomplete-nav {classesNav}">
<ul class="autocomplete-list {classesList}">
{#each optionsFiltered as option, i (option)}
<li class="autocomplete-item {classesItem}" animate:flip={{ duration }} transition:slide|local={{ duration }}>
<button class="autocomplete-button {classesButton}" type="button" on:click={() => onSelection(option)} on:click on:keypress>
{@html option.label}
</button>
</li>
{/each}
</ul>
</nav>
{:else}
<div class="autocomplete-empty {classesEmtpy}">{emptyState}</div>
{/if}
</div>
12 changes: 12 additions & 0 deletions src/lib/components/Autocomplete/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Autocomplete Types

export interface AutocompleteOption {
/** provide a unique display label per option. Supports HTML. */
label: string;
/** Provide a unique option value. */
value: unknown;
/** Provide a comma seperated list of keywords. */
keywords?: any;
/** Pass arbitrary data per option. */
meta?: any;
}
28 changes: 15 additions & 13 deletions src/lib/components/InputChip/InputChip.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
const dispatch = createEventDispatcher();

// Props
/** Bind the input value. */
export let input: string = '';
/**
* Set a unique select input name.
* @type {string}
Expand Down Expand Up @@ -59,27 +61,27 @@
const cInputField = 'unstyled bg-transparent border-0 !ring-0 p-0 w-full';

// Local
let inputValue = '';
// let input = '';
let inputValid = true;

function onInputHandler(): void {
inputValid = true;
}

function validate(): boolean {
if (!inputValue) return false;
if (!input) return false;
// Custom validation
if (validation !== undefined && !validation(inputValue)) return false;
if (validation !== undefined && !validation(input)) return false;
// Maxiumum
if (max !== -1 && value.length >= max) return false;
// Minimum Character Length
if (minlength !== -1 && inputValue.length < minlength) return false;
if (minlength !== -1 && input.length < minlength) return false;
// Maxiumum Character Length
if (maxlength !== -1 && inputValue.length > maxlength) return false;
if (maxlength !== -1 && input.length > maxlength) return false;
// Whitelist (if available)
if (whitelist.length > 0 && !whitelist.includes(inputValue)) return false;
if (whitelist.length > 0 && !whitelist.includes(input)) return false;
// Value is unique
if (allowDuplicates === false && value.includes(inputValue)) return false;
if (allowDuplicates === false && value.includes(input)) return false;
// State is valid
return true;
}
Expand All @@ -90,15 +92,15 @@
inputValid = validate();
if (inputValid === false) return;
// Format: trim value
inputValue = inputValue.trim();
input = input.trim();
// Format: to lowercase (if enabled)
inputValue = allowUpperCase ? inputValue : inputValue.toLowerCase();
input = allowUpperCase ? input : input.toLowerCase();
// Append value to array
value = [...value, inputValue];
value = [...value, input];
/** @event {{ event: Event, chipIndex: number, chipValue: string }} add - Fires when a chip is added. */
dispatch('add', { event, chipIndex: value.length - 1, chipValue: inputValue });
dispatch('add', { event, chipIndex: value.length - 1, chipValue: input });
// Clear input value
inputValue = '';
input = '';
}

function removeChip(event: Event, chipIndex: number, chipValue: string): void {
Expand Down Expand Up @@ -131,7 +133,7 @@
<form on:submit={addChip}>
<input
type="text"
bind:value={inputValue}
bind:value={input}
placeholder={$$restProps.placeholder ?? 'Enter values...'}
class="input-chip-field {classesInputField}"
on:input={onInputHandler}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Types ---

export type { AutocompleteOption } from './components/Autocomplete/types';
export type { ConicStop } from './components/ConicGradient/types';
export type { DrawerSettings } from './utilities/Drawer/types';
export type { ModalSettings, ModalComponent } from './utilities/Modal/types';
Expand Down Expand Up @@ -70,6 +71,7 @@ export { default as AppBar } from './components/AppBar/AppBar.svelte';
export { default as AppRail } from './components/AppRail/AppRail.svelte';
export { default as AppRailTile } from './components/AppRail/AppRailTile.svelte';
export { default as AppShell } from './components/AppShell/AppShell.svelte';
export { default as Autocomplete } from './components/Autocomplete/Autocomplete.svelte';
export { default as Avatar } from './components/Avatar/Avatar.svelte';
export { default as ConicGradient } from './components/ConicGradient/ConicGradient.svelte';
export { default as FileButton } from './components/FileButton/FileButton.svelte';
Expand Down
2 changes: 2 additions & 0 deletions src/lib/styles/elements/lists.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
@apply px-4 py-2;
/* Hover */
@apply bg-primary-hover-token;
/* Focus */
@apply focus:!variant-filled-primary outline-none;
/* Cursor */
@apply cursor-pointer;
/* Theme: Rounded */
Expand Down
Loading