Time Input
逐段输入时间,时钟跟随用户语言的写法。
把 Time Input 用于一天中的某个时间,比如会议时间或开门时间。它和 Date Input 一样,为时、分各设一个分段,12 小时制下还有 AM 或 PM。
"use client"
import { Field, FieldLabel } from "@ui-sdk/react/field"
import { TimeInput } from "@ui-sdk/react/time-input"
function Example() {
return (
<Field>
<FieldLabel>Meeting time</FieldLabel>
<TimeInput defaultValue="09:30" />
</Field>
)
}
export { Example }安装
npm install @ui-sdk/react @ui-sdk/core组合
用下面的组合构建 TimeInput:
TimeInput
├── TimeInputSegment
└── TimeInputSegmentTimeInput 渲染一个分组,时间的每一部分对应一个 TimeInputSegment,分段之间的文本也在其中。
用法
把输入框放进 Field,并给它一个标签:
import { Field, FieldLabel } from "@ui-sdk/react/field"
import { TimeInput } from "@ui-sdk/react/time-input"
function Example() {
return (
<Field>
<FieldLabel>Meeting time</FieldLabel>
<TimeInput defaultValue="09:30" />
</Field>
)
}
export { Example }值是 "14:30" 这样的 24 小时制时间,和原生时间输入框一样,无论输入框显示哪种时钟。输入、步进和粘贴的行为与 Date Input 相同。
时钟与秒
输入框按 locale 的时钟显示时间。把 hourCycle 设为 12 或 24 可以选择一种,把 granularity 设为 "second" 可以加上秒,设为 "hour" 则只显示小时。
"use client"
import { Field, FieldLabel } from "@ui-sdk/react/field"
import { TimeInput } from "@ui-sdk/react/time-input"
function Example() {
return (
<div data-slot="stack">
<Field>
<FieldLabel>12-hour clock</FieldLabel>
<TimeInput hourCycle={12} defaultValue="18:45" />
</Field>
<Field>
<FieldLabel>24-hour clock</FieldLabel>
<TimeInput hourCycle={24} defaultValue="18:45" />
</Field>
<Field>
<FieldLabel>With seconds</FieldLabel>
<TimeInput
granularity="second"
hourCycle={24}
defaultValue="18:45:30"
/>
</Field>
</div>
)
}
export { Example }最小与最大
min 和 max 接受 "09:00" 这样的时间。超出范围的时间无效:输入框会设置 data-invalid,它的表单也不会提交。
表单
隐藏的输入框会以 name 提交时间。设置 required 后,表单需要先有时间才能提交;把 form 设为页面中另一个表单的 id,就能用那个表单提交。Date Input 的表单示例里就有一个时间。
受控与控制器
传入 value,并在 onValueChange 中更新它,或用 createTimeInputController() 驱动输入框,做法与 Date Input 相同。
样式
像 Date Input 那样设置样式。AM/PM 分段会设置 data-type="dayPeriod"。
每个部件都有一个 data-slot,比如 time-input-segment,它的状态属性列在数据属性下。
数据属性
TimeInput
[data-slot="time-input"]
| 属性 | 含义 |
|---|---|
data-disabled | Present when disabled, by its own prop or a <fieldset>. |
data-readonly | Present when read-only. |
data-required | Present when required. |
data-invalid | Present while the time is before min or after max. |
data-empty | Present while every segment is empty. |
TimeInputSegment
[data-slot="time-input-segment"]
| 属性 | 含义 |
|---|---|
data-type | hourminuteseconddayPeriodliteralThe segment's unit, or literal for the text between segments. |
data-disabled | Present when disabled, by its own prop or a <fieldset>. |
data-readonly | Present when read-only. |
data-required | Present when required. |
data-invalid | Present while the time is before min or after max. |
data-empty | Present while the segment is empty. It shows its placeholder. |
API 参考
属性和回调与下面相同,都作为 React props。
TimeInput
value?string | null- The value: a time like
"14:30", or"14:30:15"with seconds, ornull. Pass it to control the input. defaultValue?string | null- The value it starts with, when uncontrolled, and goes back to when its form resets.
onValueChange?((value: string | null, details: ChangeDetails<TimeInputValueChangeReason>) => void)- Runs when the value wants to change: once every segment holds something, and again with
nullwhen one is emptied.details.cancel()keeps the value. granularity?TimeInputGranularity- The smallest unit it shows.
hourCycle?HourCycle- A 12 or 24 hour clock. Defaults to the locale's.
min?string- The earliest time, like
"09:00". A time before it is invalid. max?string- The latest time, like
"17:30". A time after it is invalid. locale?string- The language whose separators and clock it shows. Defaults to the page's.
disabled?boolean- Whether it can't be used.
readOnly?boolean- Whether the value can be seen but not changed.
required?boolean- Whether a time must be entered before its form submits.
name?string- The name the value submits under.
form?string- The id of the form it belongs to, when it isn't inside it.
controller?object- Drives it from anywhere. Create one with
createTimeInputController(). debug?boolean- Logs every state change with its reason to the console, in development builds.
状态
valuestring | null- The value, or
nullwhile empty or partly filled. segmentsDateInputSegmentData[]- The segments in the order the locale shows them, with the text between them. Render one part for each.
disabledboolean- Whether it can't be used, by itself or by a
<fieldset>. readOnlyboolean- Whether the value can be seen but not changed.
invalidboolean- Whether the value is before
min, aftermaxor an unavailable date.
原因
input-changeEvent- Typing, pasting or dropping changed a text field's value; in a combobox it can also open or close it, or Enter keeps the typed text as the value.
step-keyKeyboardEvent- Arrow, Page Up, Page Down, Home or End stepped a slider or a number input.
pasteClipboardEvent- Files were pasted, like a screenshot, or text and tokens into a token input.
resetnone- A form reset.
restorenone- The browser restored state, like after going back, or a saved value was read back from storage.
imperativenone- A controller or
apicall.
控制器
getValue()() => string | null- The value, or
nullwhile empty or partly filled. setValue(value, options?)(value: string | null, options?: DateInputChangeOptions | undefined) => boolean- Sets the value. Returns false when it isn't a value the input can show, or when canceled.
clear(options?)(options?: DateInputChangeOptions | undefined) => boolean- Empties every segment.
focus(segment?)(segment?: string | undefined) => void- Focuses a segment, like
"month", or the first one.
事件
| 属性 | 所属 | 收到 |
|---|---|---|
onValueChange | TimeInput | (value, details):时间(被擦除后为 null),以及原因 |
无障碍
根部件是一个分组,名称来自它的 Field 标签或你的 aria-label。每个分段都是一个 spinbutton,由它的单位命名,比如“hour”或“AM/PM”,并带有它的值、范围和文本。按键与 Date Input 相同。
经过测试的行为
下面每一行都是一个自动化测试。
- Each unit is a spinbutton in the locale's order.
- Another locale orders the segments its own way.
- Typing fills each segment, moves on and reports the date once.
- A press on the box or between segments focuses the nearest segment.
- A digit that can't start a longer number moves on at once.
- A separator moves to the next segment.
- Typing with separators over a filled date or time keeps every segment.
- ArrowUp and ArrowDown step a segment and wrap around.
- PageUp and PageDown take bigger steps.
- Home and End go to a segment's first and last values.
- ArrowLeft and ArrowRight move between segments.
- Tab moves through the segments, then out.
- Backspace empties a segment, then moves back.
- A day past the end of a new month is moved back to its last day.
- Pasting a date fills every segment.
- A time takes hours, minutes and AM or PM.
- A 24-hour clock has no AM or PM.
- A date with a time shows its hour and minute too.
- A controller sets and clears the date.
- A canceled change keeps the date.
- The form submits the ISO date and time under their names.
- Enter submits the form with what was typed.
- Enter submits a form with no submit button, in a segment or the form's one text field.
- Resetting the form brings back the default date.
- A partly filled date can't be submitted.
- A required empty date can't be submitted.
- A date after max is invalid.
- An unavailable date is invalid.
- The field's label focuses the first segment.
- A native label for the input focuses the first segment wherever it's pressed. 仅在 Web components 运行。
- A read-only date takes focus but doesn't change.
- A disabled date can't be focused.
- Digits committed through an input method fill the segment.
运行于 React、Vue 和 web components。