Documentation / Forms
DatePicker
Two date fields: DatePicker for a day, DateRangePicker for a period. The date is typed on the keyboard, in the order of the reader’s country (dd/mm/yyyy in fr-FR, mm/dd/yyyy in en-US, whatever the separator, and YYYY-MM-DD everywhere), or picked in a Calendar opened from the field’s button or with Alt + ↓. The calendar opens in a Popover, the focus on the chosen day; Escape closes it.
The text is read when the field is left or on Enter. A text that is no day the field takes — not a date, before minDate, after maxDate, one of disabledDates — stays on screen to be corrected, is reported as null, and the field says why under itself. error is read as on every field of the library. name (startName / endName for a period) submits the day as YYYY-MM-DD, whatever the language. The presets of DateRangePicker are those of ChartPeriodPicker, with the same labels.
"use client" components. The ref is the text field (the first one, for a period): a form library sends the focus there after a submit that failed.
Examples
Basic usage
Type a date or open the calendar; onChange receives the day, at local midnight.
import { useState } from "react";
import { DatePicker } from "@synertech/ui";
export function Basic() {
const [date, setDate] = useState<Date | null>(null);
return (
<div className="max-w-xs">
<DatePicker
label="Date d’échéance"
helperText="La facture est due ce jour-là."
value={date}
onChange={setDate}
/>
</div>
);
}Bounds and excluded days
minDate, maxDate and disabledDates hold for the calendar and for typing alike.
import { DatePicker } from "@synertech/ui";
export function Bounds() {
const today = new Date();
const inTwoMonths = new Date(today.getFullYear(), today.getMonth() + 2, today.getDate());
return (
<div className="max-w-xs">
<DatePicker
label="Livraison"
helperText="Un jour ouvré dans les deux mois."
minDate={today}
maxDate={inTwoMonths}
disabledDates={(day) => day.getDay() === 0 || day.getDay() === 6}
/>
</div>
);
}Date of birth
captionLayout="dropdown" adds the month and year menus, bounded by fromYear and toYear.
import { DatePicker } from "@synertech/ui";
export function BirthDate() {
const year = new Date().getFullYear();
return (
<div className="max-w-xs">
<DatePicker
label="Date de naissance"
captionLayout="dropdown"
fromYear={year - 100}
toYear={year}
maxDate={new Date()}
/>
</div>
);
}Validation on submit
error takes the application’s message; an unreadable text has its own first.
import { useState, type FormEvent } from "react";
import { Button, DatePicker } from "@synertech/ui";
export function Validation() {
const [date, setDate] = useState<Date | null>(null);
const [error, setError] = useState<string>();
const submit = (event: FormEvent) => {
event.preventDefault();
setError(date ? undefined : "Choisissez la date de la réunion.");
};
return (
<form onSubmit={submit} noValidate className="flex max-w-xs flex-col gap-4">
<DatePicker
label="Réunion"
name="meeting"
required
value={date}
onChange={(next) => {
setDate(next);
if (next) setError(undefined);
}}
error={error}
/>
<div>
<Button type="submit" label="Planifier" />
</div>
</form>
);
}Period and presets
presets offers named windows beside the calendar, held within minDate and maxDate.
import { useState } from "react";
import { DateRangePicker, type CalendarDateRange } from "@synertech/ui";
export function Range() {
const [range, setRange] = useState<CalendarDateRange>({ from: null, to: null });
const today = new Date();
return (
<div className="max-w-md">
<DateRangePicker
label="Période"
value={range}
onChange={setRange}
presets={["7d", "30d", "mtd", "qtd", "ytd"]}
maxDate={today}
numberOfMonths={2}
helperText="Les deux bornes sont incluses."
/>
</div>
);
}The application’s windows
A preset can be a period of your own, with its caption.
import { DateRangePicker } from "@synertech/ui";
export function CustomPresets() {
const year = new Date().getFullYear();
return (
<div className="max-w-md">
<DateRangePicker
label="Séjour"
startName="arrivee"
endName="depart"
presets={[
{ label: "Vacances de printemps", from: new Date(year, 3, 4), to: new Date(year, 3, 19) },
{ label: "Été", from: new Date(year, 6, 4), to: new Date(year, 7, 31) },
]}
/>
</div>
);
}API reference
Components
A date field: typed in the order of the reader's locale, or picked in a Calendar opened from the button at its end (or Alt + ↓ in the field). The text is read when the field is left or on Enter; a text that is no day the field can take — not a date, before minDate, after maxDate, one of disabledDates — is kept, reported as null, and the field says why. error is read as on every field of the library. The ref is the text field.
import { DatePicker } from "@synertech/ui";Props — DatePickerProps
| Prop | Type | Default | Description |
|---|---|---|---|
captionLayout |
"label" | "dropdown" |
— | Month and year menus in the calendar: for a day far away, a birth date. |
defaultValue |
Date | null |
— | The day the field starts with, when it holds the value itself. |
description |
string |
— | Written under the label, above the field. |
disabledDates |
Date[] | ((date: Date) => boolean) |
— | Days that cannot be chosen or typed, as a list or a predicate. |
error |
boolean | string |
— |
|
fromYear |
number |
— | First year of the year menu. |
helperText |
string |
— | Written under the field; a message passed as |
hideLabel |
boolean |
false |
Keeps the label for screen readers only: it is still the field's name. |
inline |
boolean |
false |
The label beside the field rather than above it. |
label |
string |
— | The field's label, above it (beside it with |
locale |
string |
— | BCP 47 tag the field writes and reads the day in, and the calendar its months. Defaults to the |
maxDate |
Date |
— | Latest day that can be chosen or typed (inclusive). |
minDate |
Date |
— | Earliest day that can be chosen or typed (inclusive). |
name |
string |
— | Submits the day under this name as |
onChange |
(date: Date | null) => void |
— | Called with the day chosen in the calendar, or typed and left (Tab, Enter); with |
openLabel |
string |
— | Accessible name of the calendar button. Defaults to the |
placeholder |
string |
— | What the empty field shows. Defaults to the order of the locale, |
required |
boolean |
false |
Draws the asterisk after the label and sets |
size |
DateFieldSize |
"md" |
|
today |
Date |
— | The day the calendar marks as today, and the century a two-digit year is read in. Defaults to the clock: pass it when the page renders on the server. |
toYear |
number |
— | Last year of the year menu. |
value |
Date | null |
— | The chosen day, when the application holds it. |
weekStartsOn |
CalendarWeekStart |
— | First day of the week of the calendar — 0 = Sunday … 6 = Saturday. |
+ 299 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).
A window of two dates: each typed in its own field, in the order of the reader's locale, or both picked in a Calendar in range mode opened from the button at the end (or Alt + ↓ in either field), beside optional presets. Each field is read when it is left or on Enter, as DatePicker reads its own; error is read as on every field of the library. The ref is the start field.
import { DateRangePicker } from "@synertech/ui";Props — DateRangePickerProps
| Prop | Type | Default | Description |
|---|---|---|---|
captionLayout |
"label" | "dropdown" |
— | Month and year menus in the calendar. |
className |
string |
— | Classes of the root of the field. |
defaultValue |
CalendarDateRange | null |
— | The window the field starts with, when it holds the value itself. |
description |
string |
— | Written under the label, above the fields. |
disabled |
boolean |
false |
|
disabledDates |
Date[] | ((date: Date) => boolean) |
— | Days that cannot be chosen or typed, as a list or a predicate. |
endName |
string |
— | Submits the end under this name as |
error |
boolean | string |
— |
|
fromYear |
number |
— | First year of the year menu. |
helperText |
string |
— | Written under the fields; a message passed as |
hideLabel |
boolean |
false |
Keeps the label for screen readers only: it is still the fields' name. |
id |
string |
— | The id of the start field, which the label points at. Generated when left out. |
inline |
boolean |
false |
The label beside the fields rather than above them. |
label |
string |
— | The field's label, above it (beside it with |
labels |
UILabelsOf<"dateRangePicker"> |
— | Strings of this instance, keyed like the |
locale |
string |
— | BCP 47 tag the fields write and read the days in, and the calendar its months. Defaults to the |
maxDate |
Date |
— | Latest day that can be chosen or typed (inclusive). |
minDate |
Date |
— | Earliest day that can be chosen or typed (inclusive). |
numberOfMonths |
number |
1 |
Number of months the calendar shows side by side. |
onBlur |
() => void |
— | Called when a field of the two is left. |
onChange |
(range: CalendarDateRange) => void |
— | Called with the window once both ends are picked in the calendar, a preset is chosen, or a field is typed in and left. An end that is empty, or holds a text that is no day the field can take, is |
placeholder |
string |
— | What the empty fields show. Defaults to the order of the locale. |
presets |
readonly DateRangePreset[] |
[] |
Windows offered beside the calendar: the keys of |
readOnly |
boolean |
false |
|
required |
boolean |
false |
Draws the asterisk after the label and sets |
size |
DateFieldSize |
"md" |
|
startName |
string |
— | Submits the start under this name as |
today |
Date |
— | The day the presets are measured from, the calendar marks as today, and the century a two-digit year is read in. Defaults to the clock. |
toYear |
number |
— | Last year of the year menu. |
value |
CalendarDateRange | null |
— | The chosen window, when the application holds it. Both ends are days, at local midnight. |
weekStartsOn |
CalendarWeekStart |
— | First day of the week of the calendar — 0 = Sunday … 6 = Saturday. |
+ 2 inherited native attributes from RefAttributes, Attributes (className, id, aria-*, event handlers…).
Types
DateRangePresetOption
InterfaceA window of the application's own, with its caption.
interface DateRangePresetOption| Field | Type | Default | Description |
|---|---|---|---|
from• |
Date |
— | |
label• |
string |
— | |
to• |
Date |
— |
DateRangePreset
Typetype DateRangePreset = DateRangePresetKey | DateRangePresetOptionA window offered by name, resolved against today: the presets of ChartPeriodPicker.
type DateRangePresetKey = Exclude<ChartPeriodPreset, "custom">