# Select

A form field for choosing one option from a short list.

```tsx
import { SelectField, Combobox } from '@halo-ds/react';
```

## When to use it
Use a Select when people pick one value that is saved with a form, like their country or a time zone. It looks like an Input with an arrow; clicking it opens a list built from Menu and Menu items, with a check on the current choice. For actions that happen straight away, use a Menu. It works for long lists people know, like countries: typing a letter jumps to a match. When people need to search by part of a word, use a Combobox.

## Do
- Use a Select for one choice from a list people know, like countries or time zones. Typing a letter jumps to a match.
- Order options in a way people expect: alphabetical, by size or by date.
- Pick a sensible default when there is one.
- Give every Select a label, even in a row of filters.

## Don't
- Use a Select for 2 or 3 options; show Radio buttons instead.
- Put actions in a Select; use a Menu.
- Use a Select when people pick more than one or search by part of a name; use a Combobox.
- Leave the placeholder as the only explanation.

## Choosing a value or doing an action?
Both open the same Menu list. What differs is what happens when people pick an option.
- **Choosing a value: Select**: The choice stays in the field and is saved with the form, like a country. The list shows a check mark on it.
- **Doing an action: Button with a menu**: Picking an option does something straight away, like exporting a file. Nothing stays selected, and it opens from a Button, not a field.

## Props
### SelectField props
| Prop | Type | What it does |
| --- | --- | --- |
| `fieldClassName` | `string \| undefined` |  |
| `fieldClassNames` | `Partial<Record<FieldPart, string>> \| undefined` |  |
| `fieldStyles` | `Partial<Record<FieldPart, CSSProperties>> \| undefined` |  |
| `defaultValue` | `string \| undefined` |  |
| `className` | `string \| undefined` |  |
| `id` | `string \| undefined` |  |
| `aria-label` | `string \| undefined` |  |
| `icon` | `ReactNode` | Icon at the start of the box, shown when the chosen option has none. Figma: L icon. |
| `name` | `string \| undefined` | Form field name. A hidden input carries the value. |
| `classNames` | `Partial<Record<SelectPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<SelectPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
| `size` | `"sm" \| "md" \| "lg" \| undefined` | 32, 40 or 48px. Figma: Size. |
| `disabled` | `boolean \| undefined` |  |
| `placeholder` | `string \| undefined` |  |
| `required` | `boolean \| undefined` |  |
| `value` | `string \| undefined` |  |
| `invalid` | `boolean \| undefined` |  |
| `options` (required) | `SelectOption[]` |  |
| `onValueChange` | `((value: string) => void) \| undefined` |  |
| `label` | `ReactNode` |  |
| `error` | `ReactNode` | An error message. Replaces the hint, turns red and marks the control invalid. The control shows the warning icon, so the message has none. Figma: State=Error. |
| `hint` | `ReactNode` | Help text under the control. Figma: Hint. |
| `optional` | `boolean \| undefined` |  |
| `info` | `ReactNode` |  |
It also takes the usual HTML attributes for its element.

### Combobox props
| Prop | Type | What it does |
| --- | --- | --- |
| `multiple` | `false \| undefined` | One value, or several shown as Tags. Figma: Type. |
| `value` | `string \| null \| undefined` |  |
| `defaultValue` | `string \| null \| undefined` |  |
| `onValueChange` | `((value: string \| null) => void) \| undefined` |  |
| `options` (required) | `ComboboxOption[]` |  |
| `placeholder` | `string \| undefined` |  |
| `size` | `"sm" \| "md" \| "lg" \| undefined` |  |
| `invalid` | `boolean \| undefined` |  |
| `disabled` | `boolean \| undefined` |  |
| `onCreate` | `((text: string) => void) \| undefined` | Lets people add what they typed as a new option. Called with the text. |
| `emptyText` | `string \| undefined` | Shown when nothing matches. |
| `id` | `string \| undefined` |  |
| `aria-label` | `string \| undefined` |  |
| `aria-describedby` | `string \| undefined` |  |
| `aria-invalid` | `true \| undefined` |  |
| `required` | `boolean \| undefined` |  |
| `className` | `string \| undefined` |  |
| `name` | `string \| undefined` | Form field name. Hidden inputs carry the value, one per chosen option. |
| `classNames` | `Partial<Record<ComboboxPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<ComboboxPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { Combobox, SelectField } from '@halo-ds/react';

const countries = [
  { value: 'gh', label: 'Ghana' },
  { value: 'ke', label: 'Kenya' },
  { value: 'ng', label: 'Nigeria' },
  { value: 'za', label: 'South Africa' },
];

export default function Example() {
  return (
    <div style={{ display: 'grid', gap: 20, width: 320 }}>
      <SelectField label="Country" placeholder="Choose a country" options={countries} />
      <Combobox multiple aria-label="Countries" placeholder="Search countries" options={countries} defaultValue={['ng']} />
    </div>
  );
}
```

### Sizes and states
Small and large, an error, and disabled.
```tsx
import { Select, SelectField } from '@halo-ds/react';

const fruit = [
  { value: 'apple', label: 'Apple' },
  { value: 'mango', label: 'Mango' },
  { value: 'pear', label: 'Pear' },
];

export default function Demo() {
  return (
    <div className="demo-stack" style={{ width: 300 }}>
      <Select aria-label="Small" size="sm" options={fruit} placeholder="Small" />
      <Select aria-label="Large" size="lg" options={fruit} defaultValue="mango" />
      <SelectField label="Fruit" options={fruit} error="Choose a fruit to continue." />
      <SelectField label="Fruit" options={fruit} defaultValue="pear" disabled />
    </div>
  );
}
```

### Combobox
Type to filter the list, then choose one.
```tsx
import { ComboboxField } from '@halo-ds/react';

const fruit = [
  { value: 'apple', label: 'Apple' },
  { value: 'banana', label: 'Banana' },
  { value: 'mango', label: 'Mango' },
  { value: 'pear', label: 'Pear' },
];

export default function Demo() {
  return (
    <div style={{ width: 300, minHeight: 240 }}>
      <ComboboxField label="Fruit" options={fruit} placeholder="Search fruit" />
    </div>
  );
}
```

### Several values
Choices show as tags, and people can add new ones. Backspace removes the last tag.
```tsx
import { useState } from 'react';
import { ComboboxField } from '@halo-ds/react';

export default function Demo() {
  const [options, setOptions] = useState([
    { value: 'apple', label: 'Apple' },
    { value: 'mango', label: 'Mango' },
    { value: 'pear', label: 'Pear' },
  ]);
  const [value, setValue] = useState(['apple', 'mango']);
  return (
    <div style={{ width: 320, minHeight: 240 }}>
      <ComboboxField
        label="Fruit"
        multiple
        options={options}
        value={value}
        onValueChange={setValue}
        onCreate={(text) => {
          const v = text.toLowerCase();
          setOptions([...options, { value: v, label: text }]);
          setValue([...value, v]);
        }}
      />
    </div>
  );
}
```

### People
Options can have an avatar, which shows in the list and on the tag.
```tsx
import { Avatar, ComboboxField } from '@halo-ds/react';

const people = ['Ada Obi', 'Jonas Schmidt', 'Lara Pinto'].map((name) => ({
  value: name,
  label: name,
  avatar: <Avatar size="xs" name={name} />,
}));

export default function Demo() {
  return (
    <div style={{ width: 320, minHeight: 240 }}>
      <ComboboxField label="Assign to" multiple options={people} defaultValue={['Ada Obi']} placeholder="Choose people" />
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/select
