Skip to content
TopClass UI

Field

The label, the hint and the error around any form control, tied to it by ids in the server HTML before any script runs.

split - size not measured yet

Field

split - size not measured yet

The label, the hint and the error around any form control, tied to it by ids in the server HTML before any script runs.

Install

The code

field.tsx
import { Input } from 'topclassui/input'
import { Field } from 'topclassui/field'

/** The label points at the input, and the input is named by it before any script runs. */
export default function Demo() {
  return (
    <div className="grid max-w-sm">
      <Field name="email" label="Email">
        {(control) => <Input {...control} type="email" autoComplete="email" fullWidth />}
      </Field>
    </div>
  )
}

There is no Label component: the label is this prop, and the child spreads the control object onto the control.

Import from the package

topclassui/field
import { Field } from 'topclassui/field'

Install once:npm i topclassui@beta zod

Examples

  • In a server file use Field; in a client file FieldLive renders the same HTML and shows the error the nearest FormAction returned for its name.
  • The child is a function, (control) => <Input {...control} />. The control object is id, name, required and the three ARIA attributes, and each of the eleven controls takes it as it is.
  • The ids come from id or useId(), never from name: two Fields may share a name. Pass id when a page links to the field.
  • error is one message or the array Zod's fieldErrors gives; with one, the control is invalid unless invalid says otherwise.
  • inline puts the control first and the label beside it, for a Checkbox or a Switch.
basic
hint-error
Letters and numbers only
That name is taken
Shown on your profile
Too short
Add at least one sentence
Optional
required
We never share it
inline
About once a month
Turn it on to get the summary
group
You can change it later
controls
Input hint
Input error
Textarea hint
Textarea error
Checkbox hint
Checkbox error
Switch hint
Switch error
RadioGroup hint
RadioGroup error
SegmentedControl hint
SegmentedControl error
Slider hint
Slider error
NumberField hint
NumberField error
OTPField hint
OTPField error
Select hint
Select error
Combobox hint
Combobox error
form
We never share it
live
Shown to other players
Pick the size you ordered
long-content
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
In Arabic, right to left
لن نشاركه مع أحد
هذا البريد مستخدم
اقرأها قبل المتابعة

Props

The props of Field
PropTypeDefaultLimitsDescription
children*(control: FieldControl) => ReactNode--Called with the control object, which the control spreads: (control) => <Input {...control} />. Client-only for FieldLive.
label*ReactNode--The visible name of the control.
name*string--The control's name, and the key of its errors in a FormAction's answer.
errorFieldError--One message or several, read after the hint; with one, the control is invalid unless invalid says otherwise.
hintReactNode--Text under the control that describes it: the first id of the control's aria-describedby.
idstring--The control's id; without it useId() makes one. Pass it when a page links to the field.
inlinebooleanfalse-The control first and the label beside it, the hint and the error under the label: for a Checkbox or a Switch.
invalidboolean--Sets aria-invalid on the control. Without it, true when error holds a message and false when it holds none.
requiredbooleanfalse-Passes required to the control, which tells a screen reader, and shows an asterisk that is only seen.

Keyboard and accessibility

The keyboard of Field
KeysWhat it does
TabMoves the focus to the control; Field itself takes no focus.
  • The label names the control through <label for> and aria-labelledby; aria-describedby reads the hint and then the error, only those rendered.
  • The asterisk is only seen: required reaches the control, which says so itself, as required or aria-required.
  • The error is no live region. It arrives with the page, and after a FormAction the focus goes to the first invalid control.

Right to left

The label, the hint and the error start at the right; the asterisk follows the label, and an inline box sits at the right.

Server and client

Renders on the server; the interactive part is a client component that ships only when it is used.

Files: index.tsx, client.tsx.

On the chip: split - size not measured yet.