# Selects

`DInputSelect` for a short list in a native-looking select; `DSelect` for search, multiple selection or custom options. Exact props: `widgets-get-component-props`.

---

## DInputSelect

Works with a list of any shape. `valueExtractor` and `labelExtractor` read each item; `value` is the extracted value; `onChange` receives the **selected item**:

```tsx
<DInputSelect
  label={t('transfer.account')}
  options={accounts}
  valueExtractor={(account) => account.id}
  labelExtractor={(account) => account.name}
  value={selectedId}
  onChange={(account) => setSelectedId(account.id)}
/>
```

Use it when the list is short and does not need search.

---

## DSelect

Built on react-select: options, value and `onChange` are react-select props, forwarded by `DSelect`. By default each option is an object with `label` and `value`, and both `value` and `onChange` work with **option objects**, not with the raw value:

```tsx
const options = [
  { value: 'savings', label: t('accounts.savings') },
  { value: 'checking', label: t('accounts.checking') },
];

<DSelect
  label={t('accounts.type')}
  options={options}
  value={options.find((option) => option.value === type) ?? null}
  onChange={(option) => setType(option?.value ?? '')}
/>
```

| Prop | Use |
|------|-----|
| `label`, `hint`, `invalid`, `valid`, `floatingLabel` | As in the other inputs |
| `searchable` | Lets the user type to filter |
| `multi` | Multiple selection; `value` and `onChange` work with an array of options |
| `clearable` | Shows a button to clear the value |
| `loading`, `disabled`, `placeholder` | State |
| `components` | Replaces parts of the select (see below) |

### Custom option rendering

The subcomponents are meant for `components`:

| Accessor | Renders |
|----------|---------|
| `DSelect.OptionIcon`, `DSelect.OptionEmoji`, `DSelect.OptionCheck` | An option with an icon, an emoji or a check mark |
| `DSelect.SingleValueIconText`, `DSelect.SingleValueEmoji`, `DSelect.SingleValueEmojiText` | The selected value with an icon or an emoji |
| `DSelect.Placeholder`, `DSelect.DropdownIndicator`, `DSelect.ClearIndicator`, `DSelect.LoadingIndicator`, `DSelect.MultiValueRemove` | The corresponding react-select parts |

Read the fields each one expects with `widgets-get-component-props` (for example `DSelect.OptionIcon`).

---

## Common mistakes

| Wrong | Right |
|-------|-------|
| `value="savings"` on `DSelect` | `value={options.find((option) => option.value === 'savings') ?? null}` |
| `onChange={setType}` on `DSelect` (stores the option object) | `onChange={(option) => setType(option?.value ?? '')}` |
| Keeping option objects in state | Keep the raw value and look the option up when rendering |
| `DSelect` for three static choices | `DInputSelect`, or `DInputCheck` with `type="radio"` |
