# Select

Source: https://ecosphere.dev/docs/forms/select (Ecosphere 1.5). Install `vue-ecosphere` for Vue 3 or `react-ecosphere` for React 18.2+/19: same components, props, markup and stylesheet.

A single choice from a short list: a combobox trigger opening a listbox under it.

`import { EcoSelect } from "vue-ecosphere"`

## Closed, with a label

Vue (vue-ecosphere):

```vue
<EcoSelect v-model="region" label="Region" :options="regions" first />
```

React (react-ecosphere):

```tsx
const [region, setRegion] = useState<string | null>("na");

<EcoSelect value={region} onChange={setRegion} label="Region" options={regions} first />
```

## Nothing chosen yet

Vue (vue-ecosphere):

```vue
<EcoSelect v-model="owner" label="Owner" required :options="owners"
  placeholder="Choose an owner" hint="The owner gets every reply on this request." first />
```

React (react-ecosphere):

```tsx
const [owner, setOwner] = useState<string | null>(null);

<EcoSelect value={owner} onChange={setOwner} label="Owner" required options={owners}
  placeholder="Choose an owner" hint="The owner gets every reply on this request." first />
```

## Chosen, highlighted, disabled

Open it and use the arrow keys: the highlight skips Lyon, which stays listed with its reason.

Vue (vue-ecosphere):

```vue
<EcoSelect v-model="site" label="Site" :options="[
  { label: 'Amsterdam · 6 bays', value: 'ams' },
  …
  { label: 'Lyon', value: 'lyo', disabled: true, hint: 'Closed for refit' },
  { label: 'Rotterdam · 8 bays', value: 'rtm' },
]" first />
```

React (react-ecosphere):

```tsx
const [site, setSite] = useState<string | null>("lis");

<EcoSelect value={site} onChange={setSite} label="Site" options={[
  { label: "Amsterdam · 6 bays", value: "ams" },
  …
  { label: "Lyon", value: "lyo", disabled: true, hint: "Closed for refit" },
  { label: "Rotterdam · 8 bays", value: "rtm" },
]} first />
```

## Searchable and clearable

For a long list: type to filter by name or team, ↑ ↓ to move, Enter to choose. The × clears the choice; so do Delete and Backspace on the trigger.

Vue (vue-ecosphere):

```vue
<EcoSelect v-model="assignee" label="Assignee" :options="people"
  searchable search-placeholder="Search people or teams" clearable clear-label="Clear assignee"
  placeholder="Choose an assignee" first />
```

React (react-ecosphere):

```tsx
const [assignee, setAssignee] = useState<string | null>("grace");

<EcoSelect value={assignee} onChange={setAssignee} label="Assignee" options={people}
  searchable searchPlaceholder="Search people or teams" clearable clearLabel="Clear assignee"
  placeholder="Choose an assignee" first />
```

## Server-side search

With filter set to false the list shows the options as given: answer @search by loading and replacing them. The trigger keeps the chosen label while the results leave it out.

Vue (vue-ecosphere):

```vue
<EcoSelect v-model="hub" label="Hub airport" :options="hubOptions"
  searchable :filter="false" :loading="hubLoading" @search="searchHubs"
  search-placeholder="Search airports" empty-text="No airport by that name" clearable
  placeholder="Choose an airport" first />

// searchHubs(query): set hubLoading, fetch, then replace hubOptions and clear hubLoading.
// An empty query (the list closed) asks for the default list again.
```

React (react-ecosphere):

```tsx
const [hub, setHub] = useState<string | null>(null);

<EcoSelect value={hub} onChange={setHub} label="Hub airport" options={hubOptions}
  searchable filter={false} loading={hubLoading} onSearch={searchHubs}
  searchPlaceholder="Search airports" emptyText="No airport by that name" clearable
  placeholder="Choose an airport" first />

// searchHubs(query): set hubLoading, fetch, then replace hubOptions and clear hubLoading.
// An empty query (the list closed) asks for the default list again.
```

## Small, in a toolbar

Vue (vue-ecosphere):

```vue
<span id="periodL" class="caption">Period</span>
<EcoSelect v-model="period" size="sm" labelledby="periodL" :options="periods" style="width: 168px" />
```

React (react-ecosphere):

```tsx
const [period, setPeriod] = useState<string | null>("90d");

<span id="periodL" className="caption">Period</span>
<EcoSelect value={period} onChange={setPeriod} size="sm" labelledby="periodL" options={periods} style={{ width: 168 }} />
```

## Disabled, with the reason

Vue (vue-ecosphere):

```vue
<EcoSelect :model-value="'team'" label="Plan" disabled
  :options="[{ label: 'Team · 25 seats', value: 'team' }]"
  hint="Only an account owner can change the plan." first />
```

React (react-ecosphere):

```tsx
<EcoSelect value="team" label="Plan" disabled
  options={[{ label: "Team · 25 seats", value: "team" }]}
  hint="Only an account owner can change the plan." first />
```

## Invalid

Choose a term to clear the error.

Vue (vue-ecosphere):

```vue
<EcoSelect v-model="term" label="Billing term" required :options="terms"
  placeholder="Choose a term" :error="term ? undefined : 'Choose a billing term to continue.'" first />
```

React (react-ecosphere):

```tsx
const [term, setTerm] = useState<string | null>(null);

<EcoSelect value={term} onChange={setTerm} label="Billing term" required options={terms}
  placeholder="Choose a term" error={term ? undefined : "Choose a billing term to continue."} first />
```

## API

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `v-model` | `string \| number \| null` | null | The chosen option's value. |
| `v-model:open` | `boolean` | false | Whether the listbox is open. |
| `options` | `EcoOption[]` | — | { label, value, hint?, disabled? } — a disabled option stays listed with a hint saying why. |
| `placeholder` | `string` | "Choose an option" | Say what to choose (data-placeholder while empty). |
| `size` | `"md" \| "sm"` | "md" | sm (32px) for toolbars; md inside forms to line up with input. |
| `labelledby` | `string` | — | Id of an external label when there is no label prop. |
| `label / required / hint / error / invalid / first / disabled / id` | `—` | — | Field props. The error sets aria-invalid on the trigger. |
| `searchable` | `boolean` | false | A menu__find search box at the top of the open list. It takes focus when the list opens and is the combobox (aria-activedescendant, aria-autocomplete="list"); the trigger stays a button. The query resets when the list closes. |
| `searchPlaceholder` | `string` | "Search" | Placeholder of the search box. |
| `filter` | `boolean \| (option, query) => boolean` | true | true matches label and hint (case-insensitive); false leaves options as given, for server-side search; or your own test. The query arrives trimmed. |
| `clearable / clearLabel` | `boolean / string` | false / "Clear" | A × before the chevron while a value is chosen; it (and Delete or Backspace on the closed trigger) sets the model to null without opening the list. Only where no answer is valid. |
| `loading / loadingText` | `boolean / string` | false / "Loading options…" | The open list shows a status row instead of the options; the listbox is aria-busy. |
| `emptyText` | `string` | "No matches" / "No options" | Shown in menu__empty when nothing is listed: the first default while a query is typed, the second otherwise. |
| `@search` | `(query: string)` | — | The query as it is typed, and "" when the list closes. Pair with :filter="false" and replace options. |
| `@clear` | `()` | — | The × or Delete/Backspace cleared the value. |
| `#value / #option` | `slot` | — | Custom trigger text / option content. |
| `Keyboard` | `—` | — | ↑ ↓ Home End move, Enter/Space choose, Escape closes, typing jumps to a match. Searchable: typing on the trigger opens the list and searches; in the box, ↑ ↓ Home End move, Enter chooses, Escape closes and returns focus, Tab closes. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value / onChange` | `string \| number \| null` | null | The chosen option's value. |
| `open / onOpenChange` | `boolean` | false | Whether the listbox is open. |
| `options` | `EcoOption[]` | — | { label, value, hint?, disabled? } — a disabled option stays listed with a hint saying why. |
| `placeholder` | `string` | "Choose an option" | Say what to choose (data-placeholder while empty). |
| `size` | `"md" \| "sm"` | "md" | sm (32px) for toolbars; md inside forms to line up with input. |
| `labelledby` | `string` | — | Id of an external label when there is no label prop. |
| `label / required / hint / error / invalid / first / disabled / id` | `—` | — | Field props. The error sets aria-invalid on the trigger. |
| `searchable` | `boolean` | false | A menu__find search box at the top of the open list. It takes focus when the list opens and is the combobox (aria-activedescendant, aria-autocomplete="list"); the trigger stays a button. The query resets when the list closes. |
| `searchPlaceholder` | `string` | "Search" | Placeholder of the search box. |
| `filter` | `boolean \| (option, query) => boolean` | true | true matches label and hint (case-insensitive); false leaves options as given, for server-side search; or your own test. The query arrives trimmed. |
| `clearable / clearLabel` | `boolean / string` | false / "Clear" | A × before the chevron while a value is chosen; it (and Delete or Backspace on the closed trigger) sets the model to null without opening the list. Only where no answer is valid. |
| `loading / loadingText` | `boolean / string` | false / "Loading options…" | The open list shows a status row instead of the options; the listbox is aria-busy. |
| `emptyText` | `string` | "No matches" / "No options" | Shown in menu__empty when nothing is listed: the first default while a query is typed, the second otherwise. |
| `onSearch` | `(query: string)` | — | The query as it is typed, and "" when the list closes. Pair with filter={false} and replace options. |
| `onClear` | `()` | — | The × or Delete/Backspace cleared the value. |
| `value / option` | `({ option }) => ReactNode / ({ option, selected }) => ReactNode` | — | Custom trigger text / option content. |
| `Keyboard` | `—` | — | ↑ ↓ Home End move, Enter/Space choose, Escape closes, typing jumps to a match. Searchable: typing on the trigger opens the list and searches; in the box, ↑ ↓ Home End move, Enter chooses, Escape closes and returns focus, Tab closes. |
