Overview
Get Started
Components
- Accordion
- Alert Dialog
- Alert
- Autocomplete
- Avatar
- Badge
- Button Group
- Button
- Calendar
- Checkbox Group
- Checkbox
- Collapsible
- Combobox
- Command
- Dialog
- Field
- Form
- Input Group
- Input
- Kbd
- Label
- Menu
- Meter
- Navigation Menu
- Number Field
- Number InputNew
- OTP Field
- Pagination
- Popover
- Radio Group
- Scroll Area
- Select
- Sheet
- Sidebar
- Slider
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Tooltip
Field
A component that provides labeling and validation for form controls.
Visible on your profile
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
export function FieldDemo() {
return (
<Field className="max-w-xs">
<FieldLabel>Name</FieldLabel>
<Input placeholder="Enter your name" type="text" />
<FieldDescription>Visible on your profile</FieldDescription>
</Field>
);
}Installation
pnpm dlx @zbmui/cli@latest add field
Usage
import {
Field,
FieldDescription,
FieldError,
FieldLabel,
FieldValidity,
} from "@/components/ui/field";
import { Input } from "@/components/ui/input";<Field>
<FieldLabel>Name</FieldLabel>
<Input type="text" placeholder="Enter your name" />
<FieldDescription>Visible on your profile</FieldDescription>
<FieldError>Please enter a valid name</FieldError>
<FieldValidity>
{(validity) => (
{validity.error && <p>{validity.error}</p>}
)}
</FieldValidity>
</Field>Examples
Required field with indicator
Pass the showRequiredIndicator prop to the FieldLabel component to indicate that a field is required.
import { Field, FieldError, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
export function FieldDemo() {
return (
<Field className="max-w-xs">
<FieldLabel showRequiredIndicator>Name</FieldLabel>
<Input placeholder="Enter your name" type="text" required />
<FieldError>Please fill out this field.</FieldError>
</Field>
);
}Optional field with indicator
Pass the showOptionalIndicator prop to the FieldLabel component to indicate that a field is optional.
import { Field, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
export function FieldDemo() {
return (
<Field className="max-w-xs">
<FieldLabel showOptionalIndicator>Name</FieldLabel>
<Input placeholder="Enter your name" type="text" />
</Field>
);
}Disabled
Pass the disabled prop to the Field component to disable the entire field.
import { Field, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
export function FieldDemo() {
return (
<Field disabled className="max-w-xs">
<FieldLabel>Name</FieldLabel>
<Input placeholder="Enter your name" type="text" />
</Field>
);
}With error message
Enter an invalid email and press Enter to see the error.
import { Field, FieldError, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
export function FieldDemo() {
return (
<Field className="max-w-xs">
<FieldLabel>Email</FieldLabel>
<Input placeholder="Enter your email" type="email" />
<FieldError>Please enter a valid email address.</FieldError>
</Field>
);
}API Reference
Field Props
Component type props
Prop
Type
Default
Expand prop details
Whether to show the required indicator on the field label.
Type
booleanDefault
falseWhether to show the optional indicator on the field label.
Type
booleanDefault
false