DatePicker 是一个 React 组件,用于渲染日期选择日历,可独立使用或作为 DateTimePicker 的一部分。它支持自定义日期初始化、事件处理和验证。
import { useState } from 'react';
import { DatePicker } from '@wordpress/components';
const MyDatePicker = () => {
const [ date, setDate ] = useState( new Date() );
return (
<DatePicker
currentDate={ date }
onChange={ ( newDate ) => setDate( newDate ) }
/>
);
}; DatePicker is a React component that renders a calendar for date selection. It can be used independently or as part of the DateTimePicker component.
Render a DatePicker.
import { useState } from 'react';
import { DatePicker } from '@wordpress/components';
const MyDatePicker = () => {
const [ date, setDate ] = useState( new Date() );
return (
<DatePicker
currentDate={ date }
onChange={ ( newDate ) => setDate( newDate ) }
/>
);
};
The component accepts the following props:
currentDate: Date | string | number | nullThe current date at initialization. Optionally pass in a null value to specify no date is currently selected.
onChange: ( date: string ) => voidThe function called when a new date has been selected. It is passed the currentDate as an argument.
events: { date: Date }[]List of events to show in the date picker. Each event will appear as a dot on the day of the event.
isInvalidDate: ( date: Date ) => booleanA callback function which receives a Date object representing a day as an argument, and should return a Boolean to signify if the day is valid or not.
onMonthPreviewed: ( date: string ) => voidA callback invoked when selecting the previous/next month in the date picker. The callback receives the new month date in the ISO format as an argument.
startOfWeek: 0 | 1 | 2 | 3 | 4 | 5 | 6The day that the week should start on. 0 for Sunday, 1 for Monday, etc.