Field.Picker
Field.Picker is a button inside a Field that opens a Popover around a panel you pass as its children. The panel can be a Calendar.Root, an OptionList.Root, a ColorPicker.Root or one of your own. It holds no value of its own: give the panel the same value as the input beside it, such as a Field.DateInput, which shows and sends it. A pick in a Calendar or OptionList inside closes the popover.
On this page
import { Calendar, Field, Label } from '@tounsoo/todae';
import { useId, useState } from 'react';
// Without onValueChange the date input is read-only: it shows and sends the date, and a press on it opens the calendar.
export default function FieldPickerPickOnly({ locale }: { locale: string }) {
const id = useId();
const [checkIn, setCheckIn] = useState<Temporal.PlainDate | null>(null);
return (
<Field.Group>
<Label.Root htmlFor={id}>Check-in</Label.Root>
<Field.Root id={id} name="check-in">
<Field.DateInput value={checkIn} locale={locale} placeholder="Pick a date" />
<Field.Picker>
<Calendar.Root aria-label="Check-in" value={checkIn} onValueChange={setCheckIn} locale={locale}>
<Calendar.Header />
<Calendar.Grid />
</Calendar.Root>
</Field.Picker>
</Field.Root>
</Field.Group>
);
}
Here the Field.DateInput has no onValueChange, so it is read-only: it shows the date and sends it with a form, and the date can only be picked. A press on the read-only input opens the picker, as a press elsewhere in the field does. With onValueChange, people can type the date too, as on Field.DateInput, and a press elsewhere in the field focuses the input instead.
Panels
Children are mounted only while the popover is open. A Calendar.Root picks a date, or a range with mode="range", as on Field.DateInput and Field.DateRangeInput. An OptionList.Root picks one option, such as a time from timeOptions, as on Field.TimeInput. A ColorPicker.Root picks a color. Any other panel works too.
A pick in a Calendar, or in an OptionList with one value, makes the value and closes the popover, with the reason 'select'. A pick in an OptionList with selectionMode="multiple", and a drag or key in a ColorPicker, leave it open. Each of them tells the field that a person changed its value, so a server error that Form.Root shows on the field is dismissed.
With closeOnPick={false}, no pick closes the popover, for a panel that takes more than one pick. Children can then be a function that gets { close }, to close the popover once the value is whole. The Date and time picker recipe puts a Calendar beside an OptionList of times this way, and closes once both a day and a time are set. A function child also suits a panel of your own, such as a list of buttons, that has no pick to close on.
Swatch
With swatch, a color such as #3366ff, the button is a swatch one field line tall, filled with that color, in place of a glyph. Put a ColorPicker.Root inside and give both the same color. A Field.TextInput beside it shows the color as text and sends it with a form; normalizeHex reads a typed hex color.
import { ColorPicker, Field, Label, normalizeHex } from '@tounsoo/todae';
import { useId, useState } from 'react';
// The color is the value. The text shows it, or what is being typed until it leaves the field.
export default function FieldPickerColor() {
const id = useId();
const [color, setColor] = useState('#3366ff');
const [typed, setTyped] = useState<string | null>(null);
return (
<Field.Group>
<Label.Root htmlFor={id}>Accent</Label.Root>
<Field.Root id={id} name="accent">
<Field.Picker swatch={color}>
<ColorPicker.Root
aria-label="Accent"
value={color}
onValueChange={(next) => {
setColor(next);
setTyped(null);
}}
>
<ColorPicker.Area />
<ColorPicker.Hue />
</ColorPicker.Root>
</Field.Picker>
<Field.TextInput
value={typed ?? color}
onValueChange={(next) => {
setTyped(next);
const hex = normalizeHex(next);
if (hex) setColor(hex);
}}
onBlur={() => setTyped(null)}
spellCheck={false}
/>
</Field.Root>
</Field.Group>
);
}
Labels and glyphs
label names the button, when another control is beside it, and the popover. By default it comes from the typed input beside it: the datePicker, timePicker, dateTimePicker or dateRangePicker string in StringsProvider, such as "Choose date", beside a Field.DateInput, Field.TimeInput, Field.DateTimeInput or Field.DateRangeInput. With no typed input, it is the colorPicker string with swatch, "Color picker", and the picker string without, "Open picker". glyph is what the button shows: by default the calendar glyph from GlyphsProvider beside a date input, a date-and-time input or a range input, the clock glyph beside a time input, and the chevron glyph otherwise.
Open and close
The popover opens and closes through open with onOpenChange, or defaultOpen. onOpenChange gets the reason it closed: 'select' for a pick that closes it or a call to close, 'escape', 'outside', 'trigger', or 'disabled' when the field becomes disabled. A disabled field shows no popover, and one that is open when the field is disabled closes and stays closed when it is enabled again. Open, focus goes to the panel's own place to start, a Calendar's day or an OptionList, else to the popover's first focusable element. Escape closes it, and focus goes back to where it was before it opened. A press anywhere in the field is not a press outside.
The popover opens under the whole field. Where the panel is taller than the room above and below the field, as a calendar with times under it can be on a phone, it fits the room on one side and scrolls, in browsers with CSS position-area. It keeps that side and its scroll while it is open, and is placed again when the window resizes, the page scrolls or the panel changes size.
Accessible composition
Todae handles: Beside a typed input or another control, such as a Field.TextInput, that control takes the field's id and aria-describedby, and the button keeps disabled and aria-invalid and is named label unless it has its own aria-label. On its own, the button is the field's control and takes id, aria-label, aria-labelledby and aria-describedby from Field.Root. The button gets aria-haspopup="dialog" and aria-expanded, and aria-controls while the popover is open. The popover is a role="dialog" named label. The button sends nothing with a form.
You supply: A name for the field, through a Label whose htmlFor is the Root id or aria-label / aria-labelledby on the Root. A name for the panel, such as aria-label on a Calendar.Root, an OptionList.Root or a ColorPicker.Root. The panel's value, the same as the input's.
Watch for: A server render can't know a control sits beside the button, so until the page hydrates, the button and that control both carry the field's id. Put the typed input before the picker, so the server names the button from it too. A swatch's name is never the color, so a screen reader does not hear the value from the button: show the color as text, in a Field.TextInput or a Field.Affix. On its own, the button shows only its glyph and sends nothing, so pair it with an input that shows and sends the value.
When to use
When: A panel to pick a field's value from, beside the input that holds it: a calendar beside a date, a list of times beside a time, a color picker beside a hex color.
When not: A fixed set of options with no typing → Field.Select. A calendar or a color picker shown on the page → Calendar or ColorPicker. A menu of actions → Menu.
Props
| Name | Type | Description |
|---|---|---|
label | string | The button's name beside another control, and the popover's name: the datePicker, timePicker, dateTimePicker or dateRangePicker string from StringsProvider beside a Field.DateInput, Field.TimeInput, Field.DateTimeInput or Field.DateRangeInput, else colorPicker with swatch and picker without. Default: from the typed input beside it |
glyph | ReactNode | What the button shows, from GlyphsProvider by default. Default: a calendar beside a date input, a clock beside a time input, else a chevron |
swatch | string | A CSS color, such as a ColorPicker's value, that makes the button a swatch filled with it, one line of field text tall, in place of a glyph. |
closeOnPick | boolean | Whether a pick in a Calendar or OptionList inside closes the popover. Set false for a panel that takes more than one pick, such as a Calendar beside an OptionList of times, and call close once the value is whole. Default: true |
open | boolean | Controlled open state of the popover. Pass with onOpenChange. |
defaultOpen | boolean | Initial open state when uncontrolled. |
onOpenChange | (open: boolean, reason: FieldPickerCloseReason) => void | Fires when the popover opens or closes, with why it closed: 'select' for a pick or close, 'escape', 'outside', 'trigger', or 'disabled' when the field becomes disabled. |
children | ReactNode| ((controls: { close: () => void }) => ReactNode) | What the popover holds, mounted only while it is open. A function gets close, for a panel of your own. |