TimePicker 是一个 React 组件,用于渲染时间和日期选择表单输入,可独立使用或作为 DateTimePicker 的一部分。它还提供了 TimePicker.TimeInput 子组件,用于独立渲染时间输入字段。
import { useState } from 'react';
import { TimePicker } from '@wordpress/components';
const MyTimePicker = () => {
const [ time, setTime ] = useState( new Date() );
return (
<TimePicker
currentTime={ time }
onChange={ ( newTime ) => setTime( newTime ) }
is12Hour
/>
);
};TimePicker is a React component that renders form inputs for time and date selection. It can be used independently or as part of the DateTimePicker component.
Note: TimePicker also exposes a compound sub-component, TimePicker.TimeInput, which can be used independently to render a time input field without the full picker UI.
Render a TimePicker.
import { useState } from 'react';
import { TimePicker } from '@wordpress/components';
const MyTimePicker = () => {
const [ time, setTime ] = useState( new Date() );
return (
<TimePicker
currentTime={ time }
onChange={ ( newTime ) => setTime( newTime ) }
is12Hour
/>
);
};
The component accepts the following props:
currentTime: Date | string | number | nullThe current time at initialization. Optionally pass in a null value to specify no time is currently selected.
onChange: ( time: string ) => voidThe function called when a new time has been selected. It is passed the time as an ISO-formatted string.
is12Hour: booleanWhether we use a 12-hour clock. With a 12-hour clock, an AM/PM widget is displayed.
dateOrder: 'dmy' | 'mdy' | 'ymd'The order of day, month, and year. This prop overrides the time format determined by is12Hour prop.
'dmy' (or 'mdy' when is12Hour is true)hideLabelFromVision: booleanWhether to visually hide field labels while keeping them accessible to screen readers.
A standalone time input component. Values are passed as an object in 24-hour format ({ hours: number, minutes: number }).
import { useState } from 'react';
import { TimePicker } from '@wordpress/components';
const MyTimeInput = () => {
const [ time, setTime ] = useState( { hours: 13, minutes: 30 } );
return (
<TimePicker.TimeInput
value={ time }
onChange={ setTime }
label="Time"
/>
);
};
value: { hours: number, minutes: number }The time input value in 24-hour format.
defaultValue: { hours: number, minutes: number }An optional default value for the control when used in uncontrolled mode. If left undefined, the current time will be used.
onChange: ( time: { hours: number, minutes: number } ) => voidCalled when the time changes. Receives the new value as an object with hours and minutes.
is12Hour: booleanWhether to use a 12-hour clock. With a 12-hour clock, an AM/PM widget is displayed.
label: stringThe label for the time input.