DateCalendar 是一个 React 组件,用于提供可自定义的单日期选择日历界面。该组件注重可访问性,遵循 ARIA 最佳实践,并支持键盘导航、屏幕阅读器和国际化标签。
import { DateCalendar } from '@wordpress/components';
function MyComponent() {
const [ selected, setSelected ] = useState< Date >( new Date() );
return <DateCalendar selected={ selected } onSelect={ setSelected } />;
}🔒 This component is locked as a private API. We do not yet recommend using this outside of the Gutenberg project.
DateCalendar is a React component that provides a customizable calendar interface for single date selection.
The component is built with accessibility in mind and follows ARIA best practices for calendar widgets. It provides keyboard navigation, screen reader support, and customizable labels for internationalization.
import { DateCalendar } from '@wordpress/components';
function MyComponent() {
const [ selected, setSelected ] = useState< Date >( new Date() );
return <DateCalendar selected={ selected } onSelect={ setSelected } />;
}
These props are shared between both single date and date range calendar modes.
requiredbooleanfalseWhether the selection is required. When true, there always needs to be a date selected.
selectedDate | undefined | nullThe selected date.
onSelect(selected: Date | undefined, triggerDate: Date, modifiers: Modifiers, e: React.MouseEvent | React.KeyboardEvent) => voidEvent handler when a day is selected.
defaultSelectedDateThe default selected date (for uncontrolled usage).
defaultMonthDateThe initial month to show in the calendar view (uncontrolled).
monthDateThe month displayed in the calendar view (controlled). Use together with onMonthChange to change the month programmatically.
numberOfMonthsnumber1The number of months displayed at once.
startMonthDateThe earliest month to start the month navigation.
endMonthDateThe latest month to end the month navigation.
autoFocusbooleanFocus the first selected day (if set) or today’s date (if not disabled). Use this prop when you need to focus the calendar after a user action (e.g. opening the dialog with the calendar).
disabledMatcher | Matcher[] | undefinedSpecify which days are disabled. Using true will disable all dates. See the Matcher Types section for more details.
disableNavigationbooleanDisable the navigation buttons.
labelsobjectUse custom labels for internationalization. All labels are optional and have sensible defaults:
{
labelNav?: () => string; // Navigation toolbar label
labelGrid?: (date: Date) => string; // Month grid label (default: "LLLL y")
labelGridcell?: (date: Date, modifiers?: Modifiers) => string; // Grid cell label
labelNext?: (month: Date | undefined) => string; // Next month button label
labelPrevious?: (month: Date | undefined) => string; // Previous month button label
labelDayButton?: (date: Date, modifiers?: Modifiers) => string; // Day button label
labelWeekday?: (date: Date) => string; // Weekday label
}
Important: For a correct localized experience, consumers should make sure the locale used for the translated labels and locale prop are consistent.
localeLocaleenUS from @date-fns/localeThe locale object used to localize dates. Pass a locale from @date-fns/locale to localize the calendar.
Important: For a correct localized experience, consumers should make sure the locale used for the translated labels and locale prop are consistent.
weekStartsOn0 | 1 | 2 | 3 | 4 | 5 | 6 | undefinedlocale propThe index of the first day of the week (0 – Sunday). Overrides the locale’s setting.
onMonthChange(month: Date) => voidEvent fired when the user navigates between months.
timeZonestringThe time zone (IANA or UTC offset) to use in the calendar. See Wikipedia for possible values.
When working with time zones, use the TZDate object exported by this package instead of the native Date object.
import { DateCalendar, TZDate } from '@wordpress/components';
export function WithTimeZone() {
const timeZone = 'America/New_York';
const [ selected, setSelected ] = useState< Date | undefined >(
new TZDate( 2024, 12, 10, timeZone ) // Use `TZDate` instead of `Date`
);
return (
<DateCalendar
timeZone={ timeZone }
selected={ selected }
onSelect={ setSelected }
/>
);
}
role'application' | 'dialog' | undefined'application'The role attribute to add to the container element.
The calendar component uses a flexible matching system to determine which days should be disabled or have specific modifiers. Here are the available matcher types:
const booleanMatcher: Matcher = true; // Will always match the day
const dateMatcher: Matcher = new Date(); // Will match today's date
const arrayMatcher: Matcher = [
new Date( 2019, 1, 2 ),
new Date( 2019, 1, 4 ),
]; // Will match the days in the array
const afterMatcher: DateAfter = { after: new Date( 2019, 1, 2 ) }; // Will match days after the 2nd of February 2019
const beforeMatcher: DateBefore = { before: new Date( 2019, 1, 2 ) }; // Will match days before the 2nd of February 2019
const intervalMatcher: DateInterval = {
after: new Date( 2019, 1, 2 ),
before: new Date( 2019, 1, 5 ),
}; // Will match the days between the 2nd and the 5th of February 2019 (exclusive)
const rangeMatcher: DateRange = {
from: new Date( 2019, 1, 2 ),
to: new Date( 2019, 1, 5 ),
}; // Will match the days between the 2nd and the 5th of February 2019 (inclusive)
const dayOfWeekMatcher: DayOfWeek = { dayOfWeek: 0 }; // Will match Sundays
const weekendMatcher: DayOfWeek = { dayOfWeek: [ 0, 6 ] }; // Will match weekends
const functionMatcher: Matcher = ( day: Date ) => {
return day.getMonth() === 2; // Will match when month is March
};