Select

Custom dropdown select with keyboard navigation and accessibility.

uiforminteractive

Installation

Copy the component file into your project's src/lib/components/ directory.

templates/Select.svelte

Usage

<script>
  import Select from '$lib/components/Select.svelte';

  let country = $state('');
  let theme = $state('dark');
  let priority = $state('');
</script>

<!-- Basic select -->
<Select
  bind:value={country}
  label="Country"
  placeholder="Select a country"
  options={[
    { value: 'us', label: 'United States' },
    { value: 'uk', label: 'United Kingdom' },
    { value: 'ca', label: 'Canada' },
    { value: 'au', label: 'Australia' }
  ]}
/>

<!-- Select with default value -->
<Select
  bind:value={theme}
  label="Theme"
  options={[
    { value: 'light', label: 'Light' },
    { value: 'dark', label: 'Dark' },
    { value: 'auto', label: 'Auto' }
  ]}
/>

<!-- Select with helper text -->
<Select
  bind:value={priority}
  label="Priority"
  placeholder="Select priority level"
  helperText="Choose the urgency level for this task"
  options={[
    { value: 'low', label: 'Low' },
    { value: 'medium', label: 'Medium' },
    { value: 'high', label: 'High' },
    { value: 'urgent', label: 'Urgent' }
  ]}
/>

<!-- Select with disabled option -->
<Select
  label="Plan"
  placeholder="Choose a plan"
  options={[
    { value: 'free', label: 'Free' },
    { value: 'pro', label: 'Pro' },
    { value: 'enterprise', label: 'Enterprise', disabled: true }
  ]}
/>

<!-- Select with error and required -->
<Select
  bind:value={country}
  label="Country"
  placeholder="Select your country"
  required
  error={country === '' ? 'Country is required' : ''}
  options={[
    { value: 'us', label: 'United States' },
    { value: 'uk', label: 'United Kingdom' },
    { value: 'ca', label: 'Canada' }
  ]}
/>

Props

PropTypeDefaultDescription
valuestring | number''Selected value (bindable)
optionsArray<{value: string | number, label: string, disabled?: boolean}>[]Array of options to display in the select
labelstring''Label text displayed above the select
placeholderstring'Select an option'Placeholder text shown when no value is selected
disabledbooleanfalseWhether the select is disabled
requiredbooleanfalseWhether the select is required (adds asterisk to label)
errorstring''Error message to display below select
helperTextstring''Helper text displayed below select when no error
idstring''Select id attribute (auto-generated if not provided)
namestring''Select name attribute for form submission
classstring''Additional CSS classes for the select button

Files