# Select

> Picks one value from a short list.

A select picks one value from a short list that opens in a popup.

## When to use

- Use it for 4 to 15 options.
- Use radio buttons for 2 or 3 options, so all are visible at once.
- Use a [Combobox](https://design.sosialoka.tech/components/combobox/) when the list is long or people search it.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/select/_

## Anatomy

1. Trigger showing the value
2. Chevron
3. List of options
4. Selected option with a check

A trigger on `radius-md` showing the chosen value or a placeholder, and a popup on `popover` with `radius-lg` and `elevation` from `shadow-md`. The popup renders outside the page flow, so it is never clipped by a card.

## Variants

There is one style, in two trigger sizes: `default` and `sm`.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `items` | children (`SelectItem`) | `options` | list | none | The options. In React, Base UI needs `items` on the root. |
| `value` / `defaultValue` | `v-model` / `default-value` | `initialValue` | string | none | The chosen value. |
| `onValueChange` | `@update:model-value` | `onChanged` | function | none | Runs when the value changes. |
| placeholder item (`value: null`) | `placeholder` on `SelectValue` | `placeholder` | text | none | Shown before a choice. |
| `disabled` | `disabled` | `enabled: false` | boolean | `false` | Blocks opening. |

## Code

**React**
```tsx
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"

const products = [
  { label: "Pilih produk", value: null },
  { label: "Loka Musik", value: "lokamusik" },
  { label: "Cover Clearance", value: "coverclearance" },
  { label: "Lokasign", value: "lokasign" },
  { label: "My Loka Link", value: "mylokalink" },
]

export default function SelectDemo() {
  return (
    <Select items={products}>
      <SelectTrigger className="w-56" aria-label="Produk">
        <SelectValue />
      </SelectTrigger>
      <SelectContent>
        <SelectGroup>
          {products
            .filter((p) => p.value !== null)
            .map((p) => (
              <SelectItem key={p.value} value={p.value}>
                {p.label}
              </SelectItem>
            ))}
        </SelectGroup>
      </SelectContent>
    </Select>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"

const products = [
  { label: "Loka Musik", value: "lokamusik" },
  { label: "Cover Clearance", value: "coverclearance" },
  { label: "Lokasign", value: "lokasign" },
  { label: "My Loka Link", value: "mylokalink" },
]
</script>

<template>
  <Select>
    <SelectTrigger class="w-56" aria-label="Produk">
      <SelectValue placeholder="Pilih produk" />
    </SelectTrigger>
    <SelectContent>
      <SelectGroup>
        <SelectItem v-for="p in products" :key="p.value" :value="p.value">
          {{ p.label }}
        </SelectItem>
      </SelectGroup>
    </SelectContent>
  </Select>
</template>
```

**Flutter**
```dart
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

const products = {
  'lokamusik': 'Loka Musik',
  'coverclearance': 'Cover Clearance',
  'lokasign': 'Lokasign',
  'mylokalink': 'My Loka Link',
};

class SelectDemo extends StatelessWidget {
  const SelectDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: const BoxConstraints(minWidth: 224),
      child: ShadSelect<String>(
        placeholder: const Text('Pilih produk'),
        options: products.entries
            .map((e) => ShadOption(value: e.key, child: Text(e.value)))
            .toList(),
        selectedOptionBuilder: (context, value) => Text(products[value]!),
        onChanged: (value) {},
      ),
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/select.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/select.json
```

## Accessibility

- The trigger has `role="combobox"` and opens with Enter, Space or the arrow keys.
- Arrow keys move through options; typing jumps to a matching option; Escape closes.
- Give the trigger a label, or an `aria-label` when no visible label fits.
