$npx @dinachi/cli@latest add field
We'll never share your email.
invaliddisabledFieldDescriptionemail-field.tsx
import { Field, FieldControl, FieldDescription, FieldLabel } from "@/components/ui/field"
export function EmailField() {
return (
<Field className="w-full max-w-sm">
<FieldLabel>Email</FieldLabel>
<FieldControl type="email" placeholder="Enter your email" />
<FieldDescription>We'll never share your email.</FieldDescription>
</Field>
)
}Usage
import {
Field,
FieldControl,
FieldDescription,
FieldError,
FieldLabel,
} from "@/components/ui/field"
export function EmailField() {
return (
<Field name="email">
<FieldLabel>Email</FieldLabel>
<FieldControl type="email" required />
<FieldDescription>We only use it to sign you in.</FieldDescription>
<FieldError />
</Field>
)
}Examples
We'll never share your email.
Label, control and description.
import { Field, FieldLabel, FieldControl, FieldDescription } from '@/components/ui/field';
export function Example() {
return (
<Field className="w-full max-w-sm">
<FieldLabel>Email</FieldLabel>
<FieldControl type="email" placeholder="Enter your email" />
<FieldDescription>We'll never share your email.</FieldDescription>
</Field>
);
}Must be at least 3 characters.
Validate as you type after the first blur.
import { Field, FieldLabel, FieldControl, FieldDescription, FieldError } from '@/components/ui/field';
import { useState } from 'react';
export function Example() {
const [value, setValue] = useState('');
const [touched, setTouched] = useState(false);
const isValid = value.length >= 3;
const showError = touched && !isValid;
return (
<Field invalid={showError} className="w-full max-w-sm">
<FieldLabel>Username</FieldLabel>
<FieldControl
value={value}
onChange={(e) => setValue(e.target.value)}
onBlur={() => setTouched(true)}
placeholder="Enter username"
/>
<FieldDescription>Must be at least 3 characters.</FieldDescription>
{showError && <FieldError>Username must be at least 3 characters</FieldError>}
</Field>
);
}An invalid field with its message.
import { Field, FieldLabel, FieldControl, FieldError } from '@/components/ui/field';
export function Example() {
return (
<Field invalid className="w-full max-w-sm">
<FieldLabel>Password</FieldLabel>
<FieldControl type="password" placeholder="Enter password" />
<FieldError>Password is required</FieldError>
</Field>
);
}Your legal name as it appears on documents.
We'll use this for account recovery.
Required controls with a visible marker.
import { Field, FieldLabel, FieldControl, FieldDescription } from '@/components/ui/field';
export function Example() {
return (
<div className="space-y-4 w-full max-w-sm">
<Field className="w-full">
<FieldLabel>
Full Name <span className="text-destructive">*</span>
</FieldLabel>
<FieldControl placeholder="Enter your full name" required />
<FieldDescription>Your legal name as it appears on documents.</FieldDescription>
</Field>
<Field className="w-full">
<FieldLabel>
Phone Number <span className="text-destructive">*</span>
</FieldLabel>
<FieldControl type="tel" placeholder="+1 (555) 000-0000" required />
<FieldDescription>We'll use this for account recovery.</FieldDescription>
</Field>
</div>
);
}This field cannot be modified.
Disable the whole field from its root.
import { Field, FieldLabel, FieldControl, FieldDescription } from '@/components/ui/field';
export function Example() {
return (
<Field disabled className="w-full max-w-sm">
<FieldLabel>Account ID</FieldLabel>
<FieldControl value="ACC-12345-ABCDE" readOnly />
<FieldDescription>This field cannot be modified.</FieldDescription>
</Field>
);
}Notes
Required fields
Put required on FieldControl. It shows nothing visually; mark the label too.
Server errors
Inside a Form, pass errors keyed by each Field's name. Each error clears once its control changes.
API
namestring
Names the field on submit and keys its server errors.validate(value, formValues) => string | string[] | null
Returns an error message, or nothing when valid. May be async.validationMode"onSubmit" | "onBlur" | "onChange" = "onSubmit"
When validate runs. Wins over the Form's setting.validationDebounceTimenumber = 0
Milliseconds between validate calls in onChange mode.invalid
Marks the field invalid, for example from a form library.agent
booleandisabled
Disables the control. Overrides the control's own disabled.agent
boolean = falsechildren
Also the name the agent reads for the control.agent
ReactNodetypestring = "text"
Any native input type.valuestring
Controlled value.defaultValuestring
Starting value when uncontrolled.onValueChange(value, details) => void
Runs with the new string on each change.required
Must have a value to be valid.agent
boolean = falserenderReactElement
Swap the element, keep the wiring.Takes the props of the element it renders.
matchboolean | keyof ValidityState
Shows only for one validity failure, such as valueMissing. true always shows.children
The message. Defaults to the browser's or validate's message. The agent reads it as the control's error.agent
ReactNodechildren(state) => ReactNode
Receives the validity state and the current value.Accessibility
FieldLabellabels the control; clicking it focuses the input.FieldDescriptionandFieldErrordescribe the control via aria-describedby.- Invalid controls get aria-invalid and
data-invalid. - While invalid, the agent reads
FieldError's text as the control'serrors.