Single Date Picker
const [selected, setSelected] = useState<Date>();
<DatePicker value={selected} onChange={setSelected} />
Range Date Picker
const [dateRange, setDateRange] = useState<{ start: Date; end: Date } | null>(null);
<DateRangePicker value={dateRange} onChange={setDateRange} />
Form
const formSchema = z.object({
startDate: z.date({
error: issue => (issue.input === undefined ? "Required" : "Invalid date"),
}),
eventPeriod: z.object(
{
start: z.date({
error: issue => (issue.input === undefined ? "Required" : "Invalid date"),
}),
end: z.date({
error: issue => (issue.input === undefined ? "Required" : "Invalid date"),
}),
},
{ message: "Both start and end date are required" }
),
});
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
startDate: undefined,
eventPeriod: undefined
},
});
function onSubmit(data: z.infer<typeof formSchema>) {
toast("You submitted the following values", {
description: (
<pre className="mt-2 w-[320px] rounded-md bg-neutral-950 p-4">
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
</pre>
),
});
}
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<DatePickerForm
control={form.control}
name="startDate"
label="Start Date"
placeholder="Pick a date"
isRequired
/>
<Button type="submit">Submit</Button>
</form>
</Form>
API Reference
DatePicker
| Props | Type | Default | Description |
|---|---|---|---|
| value? | Date | undefined | undefined | Current selected date. |
| onChange? | (date: Date | undefined) ⇒ void | - | Callback when date is changed. |
| placeholder? | string | "Select date" | Placeholder when no date is selected. |
| disabled? | boolean | false | Disable the input and button. |
DatePickerForm
| Props | Type | Default | Description |
|---|---|---|---|
| name | string | - | Field name (react-hook-form). |
| control | Control<T> | - | react-hook-form control object. |
| label? | string | - | Field label. |
| description? | string | - | Helper text under the field. |
| isRequired? | boolean | false | Mark the field as required. |
DateRangePicker
| Props | Type | Default | Description |
|---|---|---|---|
| value? | { start: Date; end: Date } | null | null | Currently selected range. |
| onChange? | (range: { start: Date; end: Date } | null) ⇒ void | - | Callback when range is changed. |
| placeholder? | string | "Select date range" | Placeholder when no range selected. |
| disabled? | boolean | false | Disable the input and button. |
DateRangePickerForm
| Props | Type | Default | Description |
|---|---|---|---|
| name | string | - | Field name (react-hook-form). |
| control | Control<T> | - | react-hook-form control object. |
| label? | string | - | Field label. |
| description? | string | - | Helper text under the field. |
| isRequired? | boolean | false | Mark the field as required. |