SpaceMIX v0.1.0

UI kit - Forms

experimentalfor bothsince protocol 1

The @spacemix/ui form components - Field and how it wires labels, help and errors to controls, and every control (text area, checkbox, radio, switch, slider, number, search, password, tag and code fields, file drop, color swatches) plus the grouping parts - with their props.

Form controls and the parts a form is built from. Source: packages/ui/src/forms.tsx and packages/ui/src/field.ts. Input and RenameInput are on the kit's main page; the dropdown and date controls (Select, MultiSelect, Combobox, TimePicker, DatePicker, DateRangePicker) are in Pickers.

New value controls take value / defaultValue / onChange(value), and the callback gets the value, not the event; Input keeps the native event. Every component takes the native props of its root element, className joined last and ref (where ref goes to an inner element instead, the section says so). The conventions apply.

Stability: experimental, every export on this page: new in this release, and names and props may still be adjusted before they are promised.

Field

A labelled form control with an optional help line and error.

<Field label="Email" description="We send the invite here." error={emailError} required>
  <Input type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
</Field>
Prop Type Default What it does
label ReactNode none The label, a <label> pointing at the control.
description ReactNode none A help line under the control.
error ReactNode none An error under the control, announced politely when it appears. It marks the control invalid; the description stays.
invalid boolean none Marks the control invalid without a message.
required boolean false Marks the control required and shows a required marker on the label.
optional boolean none Shows "Optional" after the label, for forms where most fields are required.
disabled boolean false Disables the control and dims the label.
id string made up The control's id.
labelAside ReactNode none Content on the right of the label row, such as a "Forgot password?" Link.
orientation "vertical" | "horizontal" "vertical" "horizontal" puts the label on the left from md up, for settings rows.
children ReactNode | ((field: FieldState) => ReactNode) required The control. A function child gets the ids, for a control of your own.

How it wires the control. Field provides a FieldState to what is inside it. Every kit control reads it on its own (Input and the fields built on it, Textarea, Checkbox, CheckboxGroup, RadioGroup, Switch, Slider, NumberInput, TagInput, FileDrop, PinInput, ColorSwatchPicker, SegmentedControl and the pickers) and takes:

  • id, the one the label's htmlFor points at (groups such as RadioGroup use aria-labelledby with the label's id instead);
  • aria-describedby, the description's and the error's ids, added to any the control has itself;
  • aria-invalid, when there is an error or invalid;
  • required and disabled (a button-based picker gets aria-required).

What the control sets itself wins. For a control of your own, use a function child or useField:

<Field label="Color" description="Shown on the card.">
  {(f) => <input id={f.id} aria-describedby={f.describedBy} aria-invalid={f.invalid || undefined} required={f.required} disabled={f.disabled} />}
</Field>

FieldProps is the props type.

useField, FieldState

interface FieldState {
  id: string;            // the control's id; the label points at it
  labelId: string;       // the label's id, for controls that are groups (aria-labelledby)
  describedBy?: string;  // the description and error ids, joined, for aria-describedby
  invalid: boolean;
  required: boolean;
  disabled: boolean;
}

function useField(): FieldState | null;

The surrounding Field's ids and state, or null outside one. Call it in a control of your own to wire id, aria-describedby, aria-invalid, required and disabled the way kit controls do.

Label

A form label in the app's size and weight. Takes every <label> prop.

Prop Type Default What it does
required boolean none Shows a required marker (an asterisk; the control's own required is what screen readers read).
optional boolean none Shows "Optional" after the text.
disabled boolean none Dims the label to match a disabled control.

LabelProps is the props type.

Textarea

A multi-line text field that can grow with its text and count characters. ref and every <textarea> attribute go to the <textarea>.

Prop Type Default What it does
value string none The text (controlled).
defaultValue string "" The starting text when uncontrolled.
onChange (value: string) => void none Called with the text.
size Size "md" The text size and padding.
invalid boolean none aria-invalid and a danger border.
rows number 3 The height in rows, and the starting height with autoGrow.
autoGrow boolean none Grows with the text, from rows up to maxRows.
maxRows number 12 With autoGrow, the most rows before it scrolls.
showCount boolean none Shows the length under the field, against maxLength when set.
variant "outline" | "plain" "outline" "plain" has no border, background, focus ring or resize handle, for a textarea inside a box of your own (a composer, an inline editor).

TextareaProps is the props type.

Checkbox

A checkbox with an optional label and description, and an indeterminate state. It is a native checkbox underneath (Space toggles, it submits with a form); ref goes to the <input>, className to the row.

Prop Type Default What it does
checked boolean none Controlled state.
defaultChecked boolean false The starting state when uncontrolled.
onChange (checked: boolean) => void none Called with the new state.
indeterminate boolean false Shows a dash: some but not all of what it stands for are checked. A click then checks it.
label ReactNode none Text beside the box; the whole row toggles it.
description ReactNode none A muted line under the label.
size Size "md" The box and text size.
invalid boolean none aria-invalid and a danger border.

CheckboxProps is the props type.

ChoiceOption

interface ChoiceOption {
  value: string;
  label: ReactNode;
  description?: ReactNode;
  disabled?: boolean;
  icon?: ReactNode;  // shown by a RadioGroup with variant="card"
}

One choice in a CheckboxGroup or RadioGroup.

CheckboxGroup

A list of checkboxes that picks any number of values, in a role="group" labelled by its Field or an aria-label.

Prop Type Default What it does
options ChoiceOption[] required The choices.
value string[] none The checked values (controlled).
defaultValue string[] [] The starting values when uncontrolled.
onChange (value: string[]) => void none Called with the checked values.
name string none Submitted with a form, one entry per checked value.
orientation "vertical" | "horizontal" "vertical" A column or a wrapping row.
size Size "md" As on Checkbox.
disabled boolean none Turns every box off.
invalid boolean none Marks the group invalid.

CheckboxGroupProps is the props type.

RadioGroup, Radio

RadioGroup picks one value from a short list. It is native radios underneath, so Tab enters the group at the checked one and the arrow keys move and select; it is labelled by its Field or an aria-label. Pass options, or Radio children for custom rows.

Prop Type Default What it does
value string none The chosen value (controlled).
defaultValue string none The starting value when uncontrolled.
onChange (value: string) => void none Called with the chosen value.
options ChoiceOption[] none The choices; or pass Radio children instead.
name string made up The radios' shared name.
orientation "vertical" | "horizontal" "vertical" A column or a wrapping row.
size Size "md" The radio and text size.
disabled boolean none Turns every radio off.
invalid boolean none Marks the group invalid.
required boolean none One must be chosen before a form submits.
variant "default" | "card" "default" "card" shows each choice as a bordered card with its icon, label and description, the chosen one in the accent: for a few choices that need explaining (a plan, a layout).

Radio is one choice inside a RadioGroup; ref goes to the <input>, className to the row. It takes value (required), label, description, icon (shown in a "card" group) and disabled, plus the other <input> props.

RadioGroupProps and RadioProps are the props types. lucide-react also has a Radio icon; import it as RadioIcon.

Switch

An on/off switch for a setting that applies at once: a role="switch" button with an optional label and description. ref goes to the button.

Prop Type Default What it does
checked boolean none Controlled state.
defaultChecked boolean false The starting state when uncontrolled.
onChange (checked: boolean) => void none Called with the new state.
label ReactNode none Text beside the switch; clicking it flips the switch too. Without one, pass an aria-label.
description ReactNode none A muted line under the label.
switchPosition "start" | "end" "end" "end" puts the label first and the switch at the row's end, as settings rows do.
size Size "md" The switch and text size.
name string none Submitted with a form as "on" while checked.

SwitchProps is the props type.

Slider, SliderValue

type SliderValue = number | [number, number];

Picks a number, or a range with two thumbs, by dragging or with the keys: the arrows step, Page Up and Page Down step by a tenth of the range, Home and End jump to the ends. Each thumb is a role="slider".

Prop Type Default What it does
value T (a SliderValue) none The value (controlled).
defaultValue T min A number for one thumb, a [low, high] pair for two.
onChange (value: T) => void none Called while dragging and on each key press.
onChangeEnd (value: T) => void none Called once when a drag or key press is done, for saving.
min number 0 The lowest value.
max number 100 The highest value.
step number 1 The step.
disabled boolean none Turns it off.
formatValue (value: number) => string String Turns a value into text for screen readers and showValue, such as (n) => n + "%".
showValue boolean none Shows the value (or low – high) at the end of the row.
thumbLabels [string, string] ["Minimum", "Maximum"] Labels for the two thumbs of a range.
name string none Submitted with a form; a range submits two entries.

SliderProps is the props type.

NumberInput

A number field with − and + buttons, min, max and step, and the spin button keys (arrows, Page Up and Page Down by ten steps, Home and End). Typing is free; the value is clamped and snapped when the field loses focus or on Enter. Takes Input's props except value, defaultValue, onChange, type, min, max, step, leftIcon and rightIcon.

Prop Type Default What it does
value number | null none The number (controlled); null while the field is empty.
defaultValue number | null null The starting number when uncontrolled.
onChange (value: number | null) => void none Called with the number.
min number none The lowest value.
max number none The highest value.
step number 1 What the arrow keys and buttons move by.
hideStepper boolean none Hides the − and + buttons; the keys still work.
format (value: number) => string String The text shown while the field isn't being edited, such as (n) => n.toFixed(2).

NumberInputProps is the props type.

SearchField

A search field with a search icon, a clear button once there is text, and an optional shortcut hint; Escape clears it. Takes Input's props except value, defaultValue, onChange, type, leftIcon and rightIcon; placeholder defaults to "Search".

Prop Type Default What it does
value string none The text (controlled).
defaultValue string "" The starting text when uncontrolled.
onChange (value: string) => void none Called with the text.
onSearch (value: string) => void none Called on Enter with the text.
shortcut string | string[] none Keys shown at the end while the field is empty, such as ["Ctrl", "K"] or "/"; desktop only. Binding the shortcut is yours.
clearLabel string "Clear search" The clear button's accessible name.

SearchFieldProps is the props type.

PasswordInput

A password field with a button that shows and hides what was typed. Pass autoComplete ("current-password" or "new-password"). Takes Input's props except value, defaultValue, onChange, type and rightIcon.

Prop Type Default What it does
value string none The text (controlled).
defaultValue string "" The starting text when uncontrolled.
onChange (value: string) => void none Called with the text.
visible boolean none Whether the text is shown (controlled).
defaultVisible boolean false The starting state when uncontrolled.
onVisibleChange (visible: boolean) => void none Called when the button is pressed.

PasswordInputProps is the props type.

TagInput

A field that collects short values as chips (labels, emails, keywords): Enter or a comma adds one, pasted text is split on commas and new lines, Backspace in the empty field removes the last, and each chip has a remove button. ref goes to the text input.

Prop Type Default What it does
value string[] none The tags (controlled).
defaultValue string[] [] The starting tags when uncontrolled.
onChange (tags: string[]) => void none Called with the tags.
separators string[] ["Enter", ","] Keys that turn the text into a tag.
max number none The most tags; the field stops taking more.
allowDuplicates boolean false Keeps a tag that is already there.
validate (tag: string) => boolean none Return false to refuse a tag (a malformed email); the text stays in the field.
addOnBlur boolean true Adds what is typed when the field loses focus.
size Size "md" As on Input.
invalid boolean none aria-invalid and a danger border.

TagInputProps is the props type.

FileDrop, FileRejection

interface FileRejection { file: File; reason: "type" | "size" | "count" }

A zone that takes files by drag and drop or a click (Enter and Space too) that opens the file picker, checks them against accept, maxSize and maxFiles, and lists what was chosen. The files stay in the page; sending them is yours.

Prop Type Default What it does
value File[] none The chosen files (controlled).
defaultValue File[] [] The starting files when uncontrolled.
onChange (files: File[]) => void none Called with the chosen files.
accept string none As on a file input: "image/*", ".csv,.tsv", "application/pdf". Dropped files are checked against it too.
multiple boolean false Takes more than one file; without it a new file replaces the old one.
maxSize number none The largest file, in bytes.
maxFiles number none With multiple, the most files.
onReject (rejected: FileRejection[]) => void none Called with the files that were turned away and why.
disabled boolean none Turns it off.
invalid boolean none Marks it invalid.
title ReactNode "Drop files here or click to choose" ("a file" without multiple) The zone's main line.
description ReactNode none A line under the title, such as "PNG or JPG, up to 5 MB".
showList boolean true Lists the chosen files under the zone, each with a remove button.
size Size "md" "sm" is a one-line zone.
name string none The form field name.

FileDropProps is the props type. To store a chosen file on the server, see host.fs.

PinInput

A one-time code field, one cell per character: typing moves on, Backspace moves back, the arrow keys move between cells and a pasted code fills them all. It offers the browser's one-time-code autofill. Its accessible name defaults to "Verification code".

Prop Type Default What it does
value string none The code (controlled).
defaultValue string "" The starting code when uncontrolled.
onChange (value: string) => void none Called with the code.
onComplete (value: string) => void none Called once every cell is filled.
length number 6 How many characters.
type "numeric" | "alphanumeric" "numeric" Digits only, or letters and digits (upper-cased).
mask boolean none Hides the characters, as in a password.
size Size "md" The cell size.
disabled boolean none Turns it off.
invalid boolean none Marks it invalid.
autoFocus boolean none Focuses the first cell on mount.
name string none Submitted with a form, as one value.

PinInputProps is the props type.

ColorSwatchPicker, SwatchColor

type SwatchColor = "c1" | "c2" | "c3" | "c4" | "c5" | "c6" | "accent" | "success" | "warning" | "danger" | "info" | "muted";

Picks a color from the theme's palette (for a project, a label, a chart series) as a token name, so it follows light and dark; draw the choice with var(--color-<name>). It is a radiogroup: the arrow keys move and select.

Prop Type Default What it does
value SwatchColor none The chosen color (controlled).
defaultValue SwatchColor none The starting color when uncontrolled.
onChange (value: SwatchColor) => void none Called with the chosen color.
colors SwatchColor[] ["c1", "c2", "c3", "c4", "c5", "c6"] Which swatches, in order.
labels Partial<Record<SwatchColor, string>> the built-in names ("Primary", "Pink") Names read out for each swatch.
size Size "md" The swatch size.
disabled boolean none Turns it off.

ColorSwatchPickerProps is the props type.

Fieldset

A native <fieldset> with a legend and a help line, for controls that belong together (an address, a set of notification channels); disabled turns off everything inside. Takes legend and description (both ReactNode) plus every <fieldset> prop. FieldsetProps is the props type.

FormSection

A titled part of a longer form or settings page: a heading, a line of help and the fields under it (or beside it). Renders a <section>.

Prop Type Default What it does
title ReactNode required The heading.
description ReactNode none A line of help.
actions ReactNode none Buttons on the right of the title row.
layout "stacked" | "split" "stacked" "split" puts the title on the left from md up, as long settings pages do.

FormSectionProps is the props type.

FormActions

The row of buttons at the end of a form (Cancel, Save), aligned and spaced as the app's dialogs and settings are.

Prop Type Default What it does
align "start" | "end" | "between" "end" "between" pushes the first child (a Delete, a note) to the start.
sticky boolean none Sticks to the bottom of the scroll area with a top border, for long forms.

FormActionsProps is the props type.

This page as markdown · All pages as one file