Overview
Get Started
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
import { Field as FieldPrimitive } from "@base-ui/react/field";
import { cn } from "@/lib/cn";
function Field({ className, ...props }: React.ComponentProps<typeof FieldPrimitive.Root>) {
return (
<FieldPrimitive.Root
data-slot="field"
className={cn("flex w-full flex-col items-start gap-2", className)}
{...props}
/>
);
}
function FieldLabel({
children,
className,
showRequiredIndicator,
showOptionalIndicator,
...props
}: React.ComponentProps<typeof FieldPrimitive.Label> & {
showRequiredIndicator?: boolean;
showOptionalIndicator?: boolean;
}) {
return (
<FieldPrimitive.Label
data-slot="field-label"
className={cn("inline-flex items-center gap-2 font-medium text-sm", className)}
{...props}
>
{children}
{showRequiredIndicator && <span className="font-normal text-danger">*</span>}
{showOptionalIndicator && (
<span className="font-normal text-muted-foreground">(optional)</span>
)}
</FieldPrimitive.Label>
);
}
function FieldDescription({
className,
...props
}: React.ComponentProps<typeof FieldPrimitive.Description>) {
return (
<FieldPrimitive.Description
data-slot="field-description"
className={cn("text-muted-foreground text-xs", className)}
{...props}
/>
);
}
function FieldError({ className, ...props }: React.ComponentProps<typeof FieldPrimitive.Error>) {
return (
<FieldPrimitive.Error
data-slot="field-error"
className={cn("text-danger text-xs", className)}
{...props}
/>
);
}
function FieldControl(props: React.ComponentProps<typeof FieldPrimitive.Control>) {
return <FieldPrimitive.Control data-slot="field-control" {...props} />;
}
function FieldValidity(props: React.ComponentProps<typeof FieldPrimitive.Validity>) {
return <FieldPrimitive.Validity data-slot="field-validity" {...props} />;
}
export { Field, FieldLabel, FieldDescription, FieldError, FieldControl, FieldValidity };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