Todae

Todae

useChipField

useChipField holds the chips and the input value for a Field.ChipInput, and getChipInputProps() spreads them onto it. Each commit goes through transform, or by default is trimmed with an empty value dropped, and a chip already in the list is dropped unless dedupe is false. The field itself never appends chips, so without the hook the parent's onCommit decides.

On this page
import { Field, useChipField } from '@tounsoo/todae';

export default function ChipInputHook() {
  const field = useChipField({ initialChips: ['react'] });
  return (
    <Field.Root aria-label="Tags">
      <Field.ChipInput {...field.getChipInputProps()} placeholder="Add a tag" />
    </Field.Root>
  );
}
react

Accessible composition

Todae handles: The hook renders nothing and sets no ARIA. It holds chips and inputValue and hands them to Field.ChipInput, which does the rest: its input takes its id, aria-describedby, aria-invalid and label from Field.Root, Enter commits by default (commitKeys), Backspace in an empty input removes the last chip, and each chip's remove button is named by chipRemoveLabel or Remove {value} and sends focus back to the input.

You supply: A name for the input: a Label for the field, or aria-label on Field.Root as in the example above. Feedback when a commit is dropped. With dedupe, a chip already in the list is dropped and the input still clears; when transform returns null, no chip is added and the input keeps its text, and Enter goes on to the form, as with any onCommit that returns false. Neither is announced, and neither are added or removed chips, so use useAnnouncer when people need to hear it.

Watch for: Tab in commitKeys: while the input has text, Tab commits instead of moving on. Backspace removes the last chip without a prompt; set removeOnBackspace={false} where that loses work.

When to use

When: Optional state helper for Field.ChipInput: chips, input value, dedupe, and transform on commit.

When not: You already own chip state → wire Field.ChipInput props directly. Picking from a list → Combobox recipe. Single value → Field.TextInput.

Props

useChipField props
NameTypeDescription
initialChipsreadonly string[]

Starting chips.

Default: []

initialInputValuestring

Starting input text.

Default: ""

dedupeboolean

Skip commits that already exist.

Default: true

transform(raw: string) => string | null

Normalize a commit. Null rejects. Default trims and rejects empty.