Components
Date Field
A field for a date, typed one part at a time: month, day and year, in the order the user's language writes them. Built on react-aria-components.
Introduction
Date Field is for a date you know by heart: a date of birth, a start date, a due date. The user types it part by part, so it is faster and harder to get wrong than a plain text box, and it writes the date the way their language does: month first, or day first, with the right separators.
- Each part is its own segment. Typing fills it and moves on, and the arrow keys step it up and down. A day that does not exist, such as February 31, is marked invalid.
minValue,maxValueandisDateUnavailablemark a date that is not allowed as it is typed.- The value is plain text such as
"2026-03-15", always year, month, day, and it works in a form. - For a date picked from a grid of days, use
Calendar. The two take the same text.
A date has no time and no time zone. The segments are drawn in the browser, because their order and names depend on the user's language, so a frame of the same size holds their place until then.
Installation
centerUI has no CLI or registry yet: components are copied into your project. Date Field also needs @internationalized/date, which react-aria uses for dates.
# bun
bun add react-aria-components @internationalized/date tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components @internationalized/date tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components @internationalized/date tailwind-variants tailwind-merge clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/date-field-styles.tsShow code
import { tv } from "tailwind-variants";
// The frame holds the calendar icon and the segments (month, day, year). The
// focus ring wraps all of it, and the segment that has focus is highlighted.
export const dateFieldStyles = tv({
slots: {
frame: [
"flex w-full items-center gap-2 rounded-md text-fg transition-colors",
"focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 focus-within:ring-offset-bg",
"group-data-invalid/date-field:border-danger",
"group-data-disabled/date-field:opacity-50",
"[&_svg]:shrink-0",
],
input: "flex min-w-0 flex-1 items-center",
segment: [
"rounded-[4px] px-0.5 tabular-nums caret-transparent outline-none",
"data-placeholder:text-muted-fg",
"data-focused:bg-primary data-focused:text-primary-fg data-focused:data-placeholder:text-primary-fg",
"data-[type=literal]:px-0",
"data-disabled:cursor-not-allowed",
],
icon: "text-muted-fg",
},
variants: {
variant: {
outline: { frame: "border bg-transparent" },
secondary: { frame: "bg-secondary text-secondary-fg" },
ghost: { frame: "bg-transparent focus-within:bg-muted hover:bg-muted" },
},
size: {
sm: { frame: "h-8 px-2.5 text-sm", icon: "size-4" },
md: { frame: "h-9 px-3 text-sm", icon: "size-4" },
lg: { frame: "h-11 px-3.5 text-base", icon: "size-5" },
},
},
defaultVariants: { variant: "outline", size: "md" },
});2. Add the Date Field file
src/components/ui/date-field/date-field.tsxShow code
"use client";
import { type DateValue, parseDate } from "@internationalized/date";
import { type ReactNode, useMemo, useRef, useSyncExternalStore } from "react";
import {
DateField as AriaDateField,
type DateFieldProps as AriaDateFieldProps,
DateInput,
DateSegment,
FieldError,
Label,
Text,
type ValidationResult,
} from "react-aria-components";
import { CalendarIcon } from "@/components/icons";
import { cn } from "@/lib/cn";
import { dateFieldStyles } from "@/types/date-field-styles";
export interface DateFieldProps
extends Omit<
AriaDateFieldProps<DateValue>,
| "children"
| "className"
| "value"
| "defaultValue"
| "onChange"
| "minValue"
| "maxValue"
| "placeholderValue"
| "granularity"
| "isDateUnavailable"
| "hourCycle"
| "shouldForceLeadingZeros"
> {
label?: ReactNode;
/** Help text under the field. */
description?: ReactNode;
/** Shown when the field is invalid. A function receives the validation result. */
errorMessage?: string | ((validation: ValidationResult) => string);
/** The date as text, `"2026-03-15"`, or `null` while it is not complete. For a controlled field. */
value?: string | null;
/** The date to start with, as text such as `"2026-03-15"`. For an uncontrolled field. */
defaultValue?: string | null;
onChange?: (value: string | null) => void;
/** The earliest date that is valid, such as `"2026-01-01"`. */
minValue?: string;
/** The latest date that is valid, such as `"2026-12-31"`. */
maxValue?: string;
/** Decides for each date if it is not allowed, for rules the range cannot say. Called with `"2026-03-15"`. */
isDateUnavailable?: (date: string) => boolean;
/**
* The date whose year and calendar the empty field starts from, when the user
* types the first part. Defaults to today.
*/
placeholderValue?: string;
/** Show the month and the day with two digits, such as 03/05 instead of 3/5. */
showLeadingZeros?: boolean;
/** Replaces the calendar icon. `null` shows none. */
icon?: ReactNode;
variant?: "outline" | "secondary" | "ghost";
size?: "sm" | "md" | "lg";
className?: string;
}
function toDate(text: string | null | undefined) {
if (!text) return null;
try {
return parseDate(text);
} catch {
return null;
}
}
/**
* A field for a date, typed one part at a time: month, day and year, in the
* order the user's language writes them. Each part is its own segment: type
* digits, or use the arrow keys, and focus moves to the next one by itself.
* Built on react-aria's DateField, so keyboard support, screen reader
* support, the calendar of the user's language and form submission (`name`)
* come from there.
*
* The value is plain text, `"2026-03-15"` (year, month, day, always), which is
* what a form and a database want. It is `null` until every part is filled. A
* date has no time and no time zone.
*
* @example
* <DateField label="Date of birth" defaultValue="1990-06-24" onChange={setBirthday} />
*/
export function DateField({
label,
description,
errorMessage,
value,
defaultValue,
onChange,
minValue,
maxValue,
isDateUnavailable,
placeholderValue,
showLeadingZeros,
icon,
variant,
size = "md",
className,
...props
}: DateFieldProps) {
const styles = dateFieldStyles({ variant, size });
// False on the server and while the page hydrates, true after.
const isClient = useSyncExternalStore(
() => () => {},
() => true,
() => false,
);
const frameRef = useRef<HTMLDivElement>(null);
const isControlled = value !== undefined;
const controlled = useMemo(() => toDate(value), [value]);
const initial = useMemo(() => toDate(defaultValue), [defaultValue]);
const min = useMemo(() => toDate(minValue) ?? undefined, [minValue]);
const max = useMemo(() => toDate(maxValue) ?? undefined, [maxValue]);
const placeholder = useMemo(
() => toDate(placeholderValue) ?? undefined,
[placeholderValue],
);
return (
<AriaDateField
{...props}
{...(isControlled ? { value: controlled } : { defaultValue: initial })}
onChange={(next) => onChange?.(next ? next.toString() : null)}
minValue={min}
maxValue={max}
placeholderValue={placeholder}
isDateUnavailable={
isDateUnavailable
? (date) => isDateUnavailable(date.toString())
: undefined
}
granularity="day"
shouldForceLeadingZeros={showLeadingZeros}
// Show a problem while the user types, not only on submit.
validationBehavior="aria"
className={cn("group/date-field flex w-full flex-col gap-1.5", className)}
>
{label ? (
<Label className="text-sm font-medium group-data-required/date-field:after:ml-0.5 group-data-required/date-field:after:text-danger group-data-required/date-field:after:content-['*']">
{label}
</Label>
) : null}
{/* biome-ignore lint/a11y/noStaticElementInteractions: a mouse shortcut, the segments are still reachable by keyboard */}
<div
ref={frameRef}
className={styles.frame()}
// Clicking the empty space of the frame focuses the first segment.
onMouseDown={(event) => {
if ((event.target as HTMLElement).closest('[role="spinbutton"]'))
return;
event.preventDefault();
frameRef.current
?.querySelector<HTMLElement>('[role="spinbutton"]')
?.focus();
}}
>
{icon === null ? null : (
<span aria-hidden="true" className={styles.icon()}>
{icon ?? <CalendarIcon />}
</span>
)}
{isClient ? (
<DateInput className={styles.input()}>
{(segment) => (
<DateSegment segment={segment} className={styles.segment()} />
)}
</DateInput>
) : (
// The segments are in the order and the language of the user, which
// the server does not know, so they are drawn in the browser only
// and the frame keeps its place until then.
<div aria-hidden="true" className={styles.input()} />
)}
</div>
{description ? (
<Text slot="description" className="text-sm text-muted-fg">
{description}
</Text>
) : null}
<FieldError className="text-sm text-danger">{errorMessage}</FieldError>
</AriaDateField>
);
}It imports CalendarIcon from @/components/icons. Add that icon, or swap it for your own.
3. Use it
import { DateField } from "@/components/ui/date-field/date-field";Anatomy
A framed row of segments with a calendar in front.
<DateField
label="Date of birth"
description="As on your passport."
defaultValue="1990-06-24"
minValue="1900-01-01"
maxValue="2026-12-31"
/>| Part | Rendered as | Notes |
|---|---|---|
| Label | label | Names the field. Clicking it focuses the first segment. |
| Icon | svg | A calendar. Replace it with the icon prop, or remove it with icon={null}. |
| Segments | spinbutton | Month, day and year, in the order of the user's language, each one its own control, with separators between them. The one with focus is highlighted. |
| Description / error | text under the field | Help text, replaced by the error message when the field is invalid. |
Without a label, give the field an aria-label so a screen reader can name it.
Value
The date is text with the year first, whatever order the field shows.
// The value is text: year, month, day, whatever order the field shows.
"2026-03-15" // March 15, 2026, or 15/03/2026 in another language
null // not complete yetThe value is null until the month, the day and the year are all filled, so an unfinished date is never reported as a wrong one.
Keyboard
Every segment is a control of its own. These keys work while one has focus.
| Key | What it does |
|---|---|
| Type digits | Fill the segment that has focus. Focus moves to the next one when the segment is full. |
| Arrow Up, Arrow Down | Add or subtract one from the segment. Page Up and Page Down step by more. |
| Arrow Left, Arrow Right | Move to the previous or the next segment. |
| Backspace | Remove the last digit of the segment. When it is empty, the segment shows its placeholder again. |
Examples
Click a segment and type, or use the arrow keys.
Usage
Label
Every field needs a name. Use label, or aria-label when the label is written elsewhere.
Description
Help text under the field.
Default value
Start with a date, written as text.
Leading zeros
By default the field writes the month and the day the way the user's language does. showLeadingZeros makes them two digits.
Minimum and maximum
A date outside the range is marked invalid as it is typed, with a message in the user's language.
Your own rule
isDateUnavailable is called with the date as text, and a date it turns down is marked invalid. Here nothing is delivered on the 13th.
Where an empty field starts
When the user types the first part of an empty field, the rest comes from today. placeholderValue changes that, such as to start a date of birth from an earlier year.
Icon
A calendar by default. Use icon for another one, or icon={null} for none.
Controlled
Keep the date in your own state with value and onChange. The value is null while the date is not complete, for example after it is cleared.
value: "2026-03-15"
In a form
With a name, the date is submitted as one field, in the form 1990-06-24.
Read only
The date can be seen and focused, but not changed.
Sizes
Variants
Disabled
Validation
minValue, maxValue and isDateUnavailable cover the simple rules. For a rule of your own, set isInvalid with an errorMessage. The field gets a red border and the message shows under it.
Component API
Date Field takes the props below, plus most of what the react-aria DateField accepts.
| Prop | Type | Default |
|---|---|---|
| label | ReactNode | - |
| description | ReactNode | - |
| errorMessage | string | (validation) => string | - |
| value | string | null | - |
| defaultValue | string | null | - |
| onChange | (value: string | null) => void | - |
| minValue | string | - |
| maxValue | string | - |
| isDateUnavailable | (date: string) => boolean | - |
| placeholderValue | string | today |
| showLeadingZeros | boolean | the language's |
| icon | ReactNode | null | a calendar |
| name | string | - |
| variant | "outline" | "secondary" | "ghost" | "outline" |
| size | "sm" | "md" | "lg" | "md" |
| autoFocus | boolean | false |
| isDisabled | boolean | false |
| isReadOnly | boolean | false |
| isInvalid | boolean | false |
| isRequired | boolean | false |
| aria-label | string | - (when there is no label) |