To add the DatePicker
component to your app:
1 2import { DatePicker } from '@forge/react';
A date picker allows the user to select a particular date.
Name | Type | Required | Description |
---|---|---|---|
appearance | "default" | "subtle" | "none" | No | Set the appearance of the picker. subtle will remove the borders, background, and icon. |
autoFocus | boolean | No | Set the picker to autofocus on mount. |
defaultIsOpen | boolean | No | The default for isOpen. Will be false if not provided. |
defaultValue | string | No | The default for value. This should be an ISO date. |
disabled | Array<string> | No | An array of ISO dates that should be disabled on the calendar. This does not affect what users can type into the picker. |
maxDate | string | No | The latest enabled date. Dates after this are disabled on the calendar. This does not affect what users can type into the picker. |
minDate | string | No | The earliest enabled date. Dates before this are disabled on the calendar. This does not affect what users can type into the picker. |
id | string | No | Sets the ID of the field. |
isDisabled | boolean | No | Set if the picker is disabled. |
isOpen | boolean | No | Set if the picker is open. |
name | string | No | The name of the field. |
nextMonthLabel | string | No | The aria-label attribute associated with the next-month arrow. Defaults to "Next month". |
onBlur | (e: BlurEvent) => void | No | Called when the field is blurred. |
onChange | (value: string) => void | No | Called when the value changes. The only argument is an ISO time or empty string. |
onFocus | (e: FocusEvent) => void | No | Called when the field is focused. |
previousMonthLabel | string | No | The aria-label attribute associated with the previous-month arrow. Defaults to "Previous month". |
spacing | "default" | "compact" | No | The spacing for the select control. |
value | string | No | The ISO time used as the input value. |
isInvalid | boolean | No | Set if the picker has an invalid value. |
dateFormat | string | No | Format the date with a string that is accepted by date-fn's format function. Note that this property only changes the displayed value, and that it does not affect the placeholder value nor the required format for manually-typed inputs. |
placeholder | string | No | Placeholder text displayed in input. |
locale | string | No | Locale used to format the date and calendar. See DateTimeFormat. Note that this affects the accepted format for the text input. |
weekStartDay | 0 | 1 |2 | 3 | 4 | 5 | 6 | No | Start day of the week for the calendar.0 Sunday (default value)1 Monday2 Tuesday3 Wednesday4 Thursday5 Friday6 Saturday |
By default, selecting the date field opens the calendar view. The current date text is bold, underlined, and highlighted blue.
1 2const DatePickerDefaultExample = () => ( <> <Label labelFor="default-date-picker-example">Choose date</Label> <DatePicker id="default-date-picker-example" /> </> );
Always use a label component for each field and associate the label to the field properly. Use the HelperMessage
component for any optional field-related message.
For required fields, always add RequiredAsterisk
component next to the label.
Use ErrorMessage
or ValidationMessage
components to display validation-related messages.
1 2import React from "react"; import { Form, FormSection, FormFooter, Label, DatePicker, Button, ErrorMessage, ValidMessage, RequiredAsterisk } from "@forge/react"; export const DatePickerFormExample = () => { const { register, handleSubmit, formState, getFieldId } = useForm(); return ( <Form onSubmit={handleSubmit((values) => console.log("form submitted", values))} > <FormSection> <Label labelFor={getFieldId("dateValue")}> Start day <RequiredAsterisk /> </Label> <DatePicker {...register("dateValue", { required: true })} /> {formState.dirtyFields.dateValue && !formState.errors.dateValue && ( <ValidMessage>You have entered a valid date</ValidMessage> )} {formState.errors.dateValue && ( <ErrorMessage>This field is required</ErrorMessage> )} </FormSection> <FormFooter> <Button type="submit">Submit</Button> </FormFooter> </Form> ); };
If a certain date is not a valid selection, you may disable it in the calendar shown to users. This does not restrict the dates that a user may type, so validation is necessary.
Use disabled
to restrict selection of individual dates.
1 2import { DatePicker, Label } from "@forge/react"; const disabledDates = [ '2022-12-07', '2022-12-08', '2022-12-09', '2022-12-16', '2022-12-17', '2022-12-18', ]; const DatePickerDisabledExample = () => ( <> <Label labelFor="datepicker-disabled">Disabled Dates</Label> <DatePicker defaultValue="2022-12-15" disabled={disabledDates} id="datepicker-disabled" /> </> );
Use minDate
to set a minimum valid date and maxDate
to set a maximum valid date. These can be used to define a valid date range.
1 2import { DatePicker, Label } from "@forge/react"; const DatePickerDisableRangeExample = () => ( <> <Label labelFor="datepicker-disabled-range">Disabled Date Range</Label> <DatePicker defaultValue="2020-12-15" minDate="2022-12-10" maxDate="2022-12-20" id="datepicker-disabled-range" /> </> );
DatePicker
supports internationalization through two props:
locale
affects language, format and, parsing.weekStartDay
determines the first day of the week shown on the calendar.Use locale
to tailor UI copy to local audiences.
1 2import { DatePicker, Label } from "@forge/react"; const DatePickerLocaleExample = () => ( <> <Label labelFor="datepicker-locale-en">English example</Label> <DatePicker locale="en-US" id="datepicker-locale-en" /> <Label labelFor="datepicker-locale-jp">Japanese example</Label> <DatePicker locale="ja-JP" id="datepicker-locale-jp" /> </> ); export default DatePickerLocaleExample;
Use weekStartDay
to adjust which day of the week is shown first in the calendar. A value of 0
corresponds to Sunday (default), 1
to Monday, and so on.
1 2import { DatePicker, Label } from "@forge/react"; const DatePickerWeekStartDayExample = () => ( <> <Label labelFor="datepicker-sunday">Week starting on Sunday</Label> <DatePicker weekStartDay={0} id="datepicker-sunday" /> <Label labelFor="datepicker-monday">Week starting on Monday</Label> <DatePicker weekStartDay={1} id="datepicker-monday" /> </> );
You can customize the date format using the dateFormat
prop. Formats are given as strings and use the syntax specified at Modern JavaScript date utility library.
Where possible, use locale for date formatting, instead of a custom format. Date formats should be informed by the user’s locale and the use case.
1 2import { DatePicker, Label } from "@forge/react"; const DatePickerFormattingExample = () => ( <> <Label labelFor="datepicker-format">Custom Date Format</Label> <DatePicker dateFormat="YYYY-MM-DD" placeholder="2021-06-10" id="datepicker-format" /> </> );
When using the DatePicker
component, we recommend keeping the following accessibility considerations in mind:
Rate this page: