Field
A form field: the label, description and error message of a form control, and its validation state. It reads the browser's constraint validation and takes errors from outside, such as a server's or a schema's, and shows an error only once the field was touched. It holds no value: the value stays in the control or in your app.
- Size
- 1.8 kB
Example
import { Field } from "menhir/field";
export default function FieldExample() {
return (
<form
class="flex flex-col items-start gap-4"
onSubmit={(event) => event.preventDefault()}
>
<Field.Root name="email" class="flex flex-col gap-1">
<Field.Label class="text-sm font-medium">Email</Field.Label>
<Field.Control
type="email"
required
class="rounded-md border border-stone-300 px-3 py-1.5 data-invalid:border-red-600 dark:border-stone-700 dark:data-invalid:border-red-400"
/>
<Field.Description class="text-sm text-stone-500 dark:text-stone-400">
We send the receipt here.
</Field.Description>
<Field.Error class="text-sm text-red-600 dark:text-red-400" />
</Field.Root>
<button
type="submit"
class="rounded-md bg-stone-800 px-3 py-1.5 text-sm font-medium text-white dark:bg-stone-300 dark:text-stone-900"
>
Pay
</button>
</form>
);
}
Anatomy
<Field.Root>
<Field.Label />
<Field.Control />
<Field.Description />
<Field.Error />
</Field.Root>API reference
Root
The field: a <div> around the parts. It tracks whether the field was touched: once focus has left it, once its control has changed, or once a submission of its form was attempted; a form reset clears it. Every part gets data-touched once touched, data-invalid while the field shows an error, and data-disabled.
| Prop | Type | Default | Description |
|---|---|---|---|
name | string | — | The control's name, which the form submits it under. |
disabled | boolean | false | Whether the control is disabled. |
error | string | — | An error message from outside, such as a server's answer or a schema's issue, which makes the field invalid. It shows once the field was touched, over the browser's message, and does not block a submission. |
invalid | boolean | false | Whether the field is invalid from outside, with no message of its own: write the message as the Error's children. |
render | (props) => JSX.Element | — | Renders the field with your own component. Read once. |
Label
The control's label: a <label> whose for is the control's id, so it names the control before hydration.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the label with your own component. Read once. |
Control
The control: an <input> with the field's id, name and disabled, aria-describedby naming the Description and the rendered Error, and aria-invalid="true" while the field shows an error. It takes the input's own attributes, such as type, required or pattern, which the browser validates. It takes no id: the Root generates it.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the control as a <textarea>, a <select> or another component, which gets the same attributes: render={(props) => <textarea {...props} />}, render={Switch.Root}, or render={Select.Trigger} inside Select.Root, which keeps its own name, required and disabled. Children go through, such as a Switch's Thumb. Read once. |
Description
What the control is for or what it takes: a <p> the control names in aria-describedby, from the server HTML on. One per field; it takes no id.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the description with your own component. Read once. |
Error
The error message: a <p>, rendered only while the field shows an error, which the control names in aria-describedby while it is rendered. Without children it shows the error from outside, else the browser's message. It is not a live region: add role="alert" to have it announced as it appears.
| Prop | Type | Default | Description |
|---|---|---|---|
match | "valueMissing" | "typeMismatch" | "patternMismatch" | … | — | A flag of the browser's validity. The Error shows only while the control has it, so each state gets a message of your own, in your app's language. |
render | (props) => JSX.Element | — | Renders the error with your own component. Read once. |
Keyboard
| Keys | Action |
|---|---|
| Tab | Moves focus away from the control, which touches the field. |
Fieldset
A group of controls, such as radio buttons or checkboxes, is a native <fieldset> named by its <legend>, and needs no component: the browser gives it the group role and its name, and disabled on the fieldset disables every control inside it, Menhir's buttons and hidden inputs included, which :disabled matches. When the group has one description and one error, render the fieldset as a Field's Control: it gets aria-describedby naming the Description and the Error, aria-invalid while the error shows, and the Field's name and disabled. The Field reads the validity of every control in the group, such as radio buttons with required, and shows the error once focus has left the group, an option has changed or a submission was attempted. A rule the browser has no constraint for, such as at most two checkboxes, is an error from outside; for at least one checkbox, use Checkbox Group, whose required covers it. Use the <legend> in place of Field.Label, which cannot name a fieldset. A group of Fields, such as an address, is a plain <fieldset> around them, each Field with its own error.
import { createMemo, createSignal, For } from "solid-js";
import { Checkbox } from "menhir/checkbox";
import { Field } from "menhir/field";
const topics = ["Releases", "Security notices", "Events"];
export default function FieldsetGroup() {
const [chosen, setChosen] = createSignal<string[]>(["Releases"]);
const toggle = (topic: string, on: boolean) =>
setChosen((list) =>
on ? [...list, topic] : list.filter((t) => t !== topic),
);
// A rule the browser has no constraint for is an error from outside.
const error = createMemo(() =>
chosen().length === 0 ? "Choose at least one topic." : undefined,
);
return (
<Field.Root name="topics" error={error()}>
<Field.Control render={(props) => <fieldset {...props} />}>
<legend>Email me about</legend>
<Field.Description>You can change this later.</Field.Description>
<For each={topics}>
{(topic) => (
<label>
<Checkbox.Root
name="topics"
value={topic}
checked={chosen().includes(topic)}
onCheckedChange={(on) => toggle(topic, on)}
>
<Checkbox.Indicator />
</Checkbox.Root>
{topic}
</label>
)}
</For>
<Field.Error />
</Field.Control>
</Field.Root>
);
}