DateRangeCalendar 是一个 React 组件,用于提供可自定义的日期范围选择日历界面。该组件注重无障碍访问,遵循 ARIA 最佳实践,并支持键盘导航、屏幕阅读器和国际化标签。
import { DateRangeCalendar } from '@wordpress/components';
type DateRange = {
from: Date | undefined;
to?: Date | undefined;
};
function MyComponent() {
const [ selected, setSelected ] = useState< DateRange >( {
from: new Date( date.getFullYear(), date.getMonth(), 1 ),
to: new Date(),
} );
return <DateRangeCalendar selected={ selected } onSelect={ setSelected } />;
}🔒 This component is locked as a private API. We do not yet recommend using this outside of the Gutenberg project.
DateRangeCalendar is a React component that provides a customizable calendar interface for date range 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 { DateRangeCalendar } from '@wordpress/components';
type DateRange = {
from: Date | undefined;
to?: Date | undefined;
};
function MyComponent() {
const [ selected, setSelected ] = useState< DateRange >( {
from: new Date( date.getFullYear(), date.getMonth(), 1 ),
to: new Date(),
} );
return <DateRangeCalendar 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.
selectedDateRange | undefined | nullThe selected date range. A DateRange object has the following shape:
{
from: Date | undefined;
to?: Date | undefined;
}
onSelect(selected: DateRange | undefined, triggerDate: Date, modifiers: Modifiers, e: React.MouseEvent | React.KeyboardEvent) => voidEvent handler when the selection changes. The selected parameter will contain the new date range.
defaultSelectedDateRangeThe default selected range (for uncontrolled usage).
excludeDisabledbooleanWhen true, the range will reset when including a disabled day. This is useful to prevent users from selecting ranges that include unavailable dates.
minnumberThe minimum number of days to include in the range. If a user tries to select a range shorter than this, the selection will be adjusted to meet the minimum requirement.
maxnumberThe maximum number of days to include in the range. If a user tries to select a range longer than this, the selection will be adjusted to meet the maximum requirement.
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 { DateRangeCalendar, TZDate } from '@wordpress/components';
export function WithTimeZone() {
const timeZone = 'America/New_York';
const [ selected, setSelected ] = useState< Date | undefined >( {
from: new TZDate( 2024, 12, 10, timeZone ), // Use `TZDate` instead of `Date`
to: new TZDate( 2024, 12, 8, timeZone ), // Use `TZDate` instead of `Date`
} );
return (
<DateRangeCalendar
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
};