Autocomplete
Autocomplete is used to search and pick a value from a list of options.
Usage
import { Autocomplete } from 'stwui';
import Autocomplete from 'stwui/autocomplete';
<script lang="ts">
import { Autocomplete } from 'stwui';
let value: string;
let options = ['Option 1', 'Option 2', 'Option 3'];
function filter(e: Event) {
const target = e.target as HTMLInputElement;
filtered = options.filter((opt) => opt.toLowerCase().includes(target.value.toLowerCase()));
}
function filterOptions(option: string) {
if (option) {
filtered = options.filter((opt) => opt.toLowerCase().includes(option.toLowerCase()));
} else {
filtered = options;
}
}
$: filterOptions(value);
</script>
<Autocomplete
name="autocomplete"
placeholder="Basic"
bind:value={value}
on:input={filter}
{options}
>
<Autocomplete.Label slot="label">Label</Autocomplete.Label>
<Autocomplete.Options slot="options">
{#if filtered.length > 0}
{#each filtered as option}
<Autocomplete.Options.Option {option} />
{/each}
{:else}
<Autocomplete.Options.EmptyOption />
{/if}
</Autocomplete.Options>
</Autocomplete>
<script lang="ts">
import { Autocomplete } from 'stwui';
let value: string;
let options = ['Option 1', 'Option 2', 'Option 3'];
function filter(e: Event) {
const target = e.target as HTMLInputElement;
filtered = options.filter((opt) => opt.toLowerCase().includes(target.value.toLowerCase()));
}
function filterOptions(option: string) {
if (option) {
filtered = options.filter((opt) => opt.toLowerCase().includes(option.toLowerCase()));
} else {
filtered = options;
}
}
$: filterOptions(value);
</script>
<Autocomplete
name="autocomplete"
placeholder="Basic"
bind:value={value}
on:input={filter}
{options}
>
<Autocomplete.Label slot="label">Label</Autocomplete.Label>
<Autocomplete.Options slot="options">
{#if filtered.length > 0}
{#each filtered as option}
<Autocomplete.Options.Option {option} />
{/each}
{:else}
<Autocomplete.Options.EmptyOption />
{/if}
</Autocomplete.Options>
</Autocomplete>
With Leading
<script lang="ts">
import { Autocomplete } from 'stwui';
let value: string;
let options = ['Option 1', 'Option 2', 'Option 3'];
let email = 'svg-path';
function filter(e: Event) {
const target = e.target as HTMLInputElement;
filtered = options.filter((opt) => opt.toLowerCase().includes(target.value.toLowerCase()));
}
function filterOptions(option: string) {
if (option) {
filtered = options.filter((opt) => opt.toLowerCase().includes(option.toLowerCase()));
} else {
filtered = options;
}
}
$: filterOptions(value);
</script>
<Autocomplete
name="autocomplete"
bind:value={value}
on:input={filter}
{options}
>
<Autocomplete.Label slot="label">Label</Autocomplete.Label>
<Autocomplete.Leading slot="leading" data={email} />
<Autocomplete.Options slot="options">
{#if filtered.length > 0}
{#each filtered as option}
<Autocomplete.Options.Option {option} />
{/each}
{:else}
<Autocomplete.Options.EmptyOption />
{/if}
</Autocomplete.Options>
</Autocomplete>
You're doing it wrong!
<script lang="ts">
import { Autocomplete } from 'stwui';
let value: string;
let options = ['Option 1', 'Option 2', 'Option 3'];
let email = 'svg-path';
function filter(e: Event) {
const target = e.target as HTMLInputElement;
filtered = options.filter((opt) => opt.toLowerCase().includes(target.value.toLowerCase()));
}
function filterOptions(option: string) {
if (option) {
filtered = options.filter((opt) => opt.toLowerCase().includes(option.toLowerCase()));
} else {
filtered = options;
}
}
$: filterOptions(value);
let error: string | undefined = "You're doing it wrong!";
$: if (value && value.length > 0) {
error = undefined;
} else {
error = "You're doing it wrong!";
}
</script>
<Autocomplete
name="autocomplete"
bind:value={value}
on:input={filter}
{options}
{error}
>
<Autocomplete.Label slot="label">Label</Autocomplete.Label>
<Autocomplete.Leading slot="leading" data={email} />
<Autocomplete.Options slot="options">
{#if filtered.length > 0}
{#each filtered as option}
<Autocomplete.Options.Option {option} />
{/each}
{:else}
<Autocomplete.Options.EmptyOption />
{/if}
</Autocomplete.Options>
</Autocomplete>
<script lang="ts">
import { Autocomplete } from 'stwui';
let value: string;
let options = ['Option 1', 'Option 2', 'Option 3'];
let email = 'svg-path';
function filter(e: Event) {
const target = e.target as HTMLInputElement;
filtered = options.filter((opt) => opt.toLowerCase().includes(target.value.toLowerCase()));
}
function filterOptions(option: string) {
if (option) {
filtered = options.filter((opt) => opt.toLowerCase().includes(option.toLowerCase()));
} else {
filtered = options;
}
}
$: filterOptions(value);
</script>
<Autocomplete
name="autocomplete"
bind:value={value}
on:input={filter}
{options}
disabled
>
<Autocomplete.Label slot="label">Label</Autocomplete.Label>
<Autocomplete.Leading slot="leading" data={email} />
<Autocomplete.Options slot="options">
{#if filtered.length > 0}
{#each filtered as option}
<Autocomplete.Options.Option {option} />
{/each}
{:else}
<Autocomplete.Options.EmptyOption />
{/if}
</Autocomplete.Options>
</Autocomplete>
With Any Value
<script lang="ts">
import { Autocomplete } from 'stwui';
let value = 'I am not in the list!';
let options = ['Option 1', 'Option 2', 'Option 3'];
function filter(e: Event) {
const target = e.target as HTMLInputElement;
filtered = options.filter((opt) => opt.toLowerCase().includes(target.value.toLowerCase()));
}
function filterOptions(option: string) {
if (option) {
filtered = options.filter((opt) => opt.toLowerCase().includes(option.toLowerCase()));
} else {
filtered = options;
}
}
$: filterOptions(value);
</script>
<Autocomplete
name="autocomplete"
placeholder="Basic"
bind:value={value}
on:input={filter}
allowNonListValue
>
<Autocomplete.Options slot="options">
{#if filtered.length > 0}
{#each filtered as option}
<Autocomplete.Options.Option {option} />
{/each}
{:else}
<Autocomplete.Options.EmptyOption />
{/if}
</Autocomplete.Options>
</Autocomplete>
<script lang="ts">
import { Autocomplete } from 'stwui';
let value: string;
let options = ['Option 1', 'Option 2', 'Option 3', 'Option 4', 'Option 5', 'Option 6'];
function filter(e: Event) {
const target = e.target as HTMLInputElement;
filtered = options.filter((opt) => opt.toLowerCase().includes(target.value.toLowerCase()));
}
function filterOptions(option: string) {
if (option) {
filtered = options.filter((opt) => opt.toLowerCase().includes(option.toLowerCase()));
} else {
filtered = options;
}
}
$: filterOptions(value);
</script>
<Autocomplete
name="autocomplete"
placeholder="Basic"
bind:value={value}
on:input={filter}
{options}
mobile
>
<Autocomplete.Label slot="label">Label</Autocomplete.Label>
<Autocomplete.Options slot="options">
{#if filtered.length > 0}
{#each filtered as option}
<Autocomplete.Options.Option {option} />
{/each}
{:else}
<Autocomplete.Options.EmptyOption />
{/if}
</Autocomplete.Options>
</Autocomplete>
Autocomplete Props
name | string | nanoid() |
error | string | |
placeholder | string | |
value | string | |
allowNonListValue | boolean | false |
disabled | boolean | false |
options | string[] | [] |
mobile | boolean | false |
Autocomplete Slots
label | <Autocomplete.Label slot="label" /> |
leading | <Autocomplete.Leading slot="leading" /> |
options | <Autocomplete.Options slot="options" /> |
Autocomplete.Leading Props
data | string (IconData) | |
viewBox | string | 0 0 24 24 |
size | string | 24px |
width | string | 24px |
height | string | 24px |
color | string | currentColor |
stroke | string | undefined | |
fill | string | currentColor |
Autocomplete.Options Slots
default | <Autocomplete.Options.Option /> |
default | <Autocomplete.Options.EmptyOption /> |
Autocomplete.Options.Option Props
Autocomplete.Options.EmptyOption Slots
Autocomplete Class Identifiers
stwi-autocomplete |
stwui-autocomplete-wrapper |
stwui-autocomplete-trigger |
stwui-autocomplete-input |
stwui-autocomplete-leading |
stwui-autocomplete-clear-wrapper |
stwui-autocomplete-error-wrapper |
stwui-autocomplete-mobile-drawer |
stwui-autocomplete-mobile-input |
stwui-autocomplete-mobile-options |
stwui-autocomplete-error |
stwui-autocomplete-empty-option |
stwui-autocomplete-label |
stwui-autocomplete-option |
stwui-autocomplete-options |