# Entity picker

An async searchable single or multiple picker over remote records, with 200ms debounced search, cancellation and outline chips.

Live variants: https://design.arcadia.is/components/entity-picker

```tsx
import { EntityPicker } from "@arcadia/ui";
```

```tsx
import { useState } from "react";
import { Field, EntityPicker } from "@arcadia/ui";

function SinglePerson() {
  const [selected, setSelected] = useState<EntityOption[]>([PEOPLE[0]!]);
  return (
    <div {...sx("cmp-form", cmp.form)}>
      <Field label="Owner">
        <EntityPicker
          aria-label="Owner"
          value={selected}
          onChange={setSelected}
          initial={async () => PEOPLE.slice(0, 3)}
          search={searchPeople}
          placeholder="Choose a person"
          searchPlaceholder="Search people"
        />
      </Field>
    </div>
  );
}
```

Search and choose in the page.

```tsx
import { useState } from "react";
import { Field, EntityPicker } from "@arcadia/ui";

function InlinePerson() {
  const [selected, setSelected] = useState<EntityOption[]>([]);
  return (
    <div {...sx("cmp-form", cmp.form)}>
      <Field label="Reviewer">
        <EntityPicker inline aria-label="Reviewer" value={selected} onChange={setSelected} initial={async () => PEOPLE} search={searchPeople} />
      </Field>
    </div>
  );
}
```

## Accessibility

The trigger is named by `aria-label`, then by what it shows and, in `multiple` mode, how many are chosen. Inside a `Field`, the label, the description and error and the disabled state reach the trigger. The list is a `Menu`: in `single` mode its rows are radios with one checked, in `multiple` mode they are checkboxes. The search field takes focus on open; the Down arrow moves focus from it into the rows, Enter or Space picks or toggles the focused row (`single` closes the menu, `multiple` keeps it open), and Escape closes it and returns focus to the trigger. Each chip's remove control names its item, and removing a chip moves focus to the chip that takes its place, the one before it, or the trigger when none is left. An `inline` picker is a combobox whose listbox stands under it: the arrows move its active row, Enter picks or toggles it, and Escape empties the query, then leaves the field.

## API reference

### EntityPicker

- `mode` (`"single" | "multiple"`, default `"single"`)
- `value` (`readonly EntityOption[]`, required)
- `onChange` (`(next: EntityOption[]) => void`, required)
- `search` (`(query: string, signal: AbortSignal) => Promise<EntityOption[]>`, required)
- `initial` (`() => Promise<EntityOption[]>`)
- `max` (`number`)
- `placeholder` (`string`, default `"Select"`)
- `searchPlaceholder` (`string`, default `"Search"`)
- `emptyText` (`string`, default `"No matches"`)
- `errorText` (`string`, default `"Could not load results. Try again."`)
- `none` (`string`): Single mode: a row before the matches that clears the choice ("Nobody"). Picking it calls `onChange([])`.
- `inline` (`boolean`, default `false`): In the page rather than over it: a search field and its listbox rows, with the arrow keys and Enter to pick, and Escape to clear the query. A single pick empties the field and shows the recent list again.
- `aria-label` (`string`, required)
- `rootStyle` (`RootStyle`): A component's typed override prop: `Layout` and its documented knobs, each the references it takes or another kit knob of its kind; anything else is a type error at the call site (STYLEX.md).
