Time Input
逐段输入时间,时钟跟随用户语言的写法。
把 Time Input 用于一天中的某个时间,比如会议时间或开门时间。它和 Date Input 一样,为时、分各设一个分段,12 小时制下还有 AM 或 PM。
import "@ui-sdk/web/field"
import "@ui-sdk/web/time-input"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field>
<ui-field-label>Meeting time</ui-field-label>
<ui-time-input default-value="09:30"></ui-time-input>
</ui-field>
`
}
export { mount }安装
npm install @ui-sdk/web @ui-sdk/core导入组件会定义它的元素。如果用到 field,也把它导入。在服务器上,导入不会做任何事。
import "@ui-sdk/web/time-input"
import "@ui-sdk/web/field"组合
用下面的组合构建 ui-time-input:
ui-time-input
├── ui-time-input-segment
│ └── input
└── ui-time-input-segment<ui-time-input> 渲染一个分组,时间的每一部分对应一个 <ui-time-input-segment>,分段之间的文本也在其中。
用法
把输入框放进 Field,并给它一个标签:
<script type="module">
import "@ui-sdk/web/time-input"
import "@ui-sdk/web/field"
</script>
<ui-field>
<ui-field-label>Meeting time</ui-field-label>
<ui-time-input default-value="09:30"></ui-time-input>
</ui-field>值是 "14:30" 这样的 24 小时制时间,和原生时间输入框一样,无论输入框显示哪种时钟。输入、步进和粘贴的行为与 Date Input 相同。
时钟与秒
输入框按 locale 的时钟显示时间。把 hour-cycle 设为 12 或 24 可以选择一种,把 granularity 设为 "second" 可以加上秒,设为 "hour" 则只显示小时。
import "@ui-sdk/web/field"
import "@ui-sdk/web/time-input"
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="stack">
<ui-field>
<ui-field-label>12-hour clock</ui-field-label>
<ui-time-input hour-cycle="12" default-value="18:45"></ui-time-input>
</ui-field>
<ui-field>
<ui-field-label>24-hour clock</ui-field-label>
<ui-time-input hour-cycle="24" default-value="18:45"></ui-time-input>
</ui-field>
<ui-field>
<ui-field-label>With seconds</ui-field-label>
<ui-time-input granularity="second" hour-cycle="24" default-value="18:45:30"></ui-time-input>
</ui-field>
</div>
`
}
export { mount }最小与最大
min 和 max 接受 "09:00" 这样的时间。超出范围的时间无效:输入框会设置 data-invalid,它的表单也不会提交。
表单
这个元素与表单关联,以 name 提交时间,没有隐藏输入框。设置 required 后,表单需要先有时间才能提交;把 form 设为页面中另一个表单的 id,就能用那个表单提交。Date Input 的表单示例里就有一个时间。
受控与控制器
监听 valuechange 并设置 value property,或用元素的 api 驱动输入框,做法与 Date Input 相同。
样式
像 Date Input 那样设置样式。AM/PM 分段会设置 data-type="dayPeriod"。
每个部件都有一个 data-slot,比如 time-input-segment,它的状态属性列在数据属性下。
数据属性
<ui-time-input>
[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. |
<ui-time-input-segment>
[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 参考
属性(props)使用 kebab 形式,比如 default-value、hour-cycle 和 read-only。value 同时也是一个 property,controller 也是 property。回调是 DOM 事件。
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.
valuechange event- 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.
事件
事件在各自的元素上触发,不会冒泡,值在 event.detail.value 中,reason 和 event 在旁边。在 valuechange 上调用 preventDefault() 会取消变更。
| 成员 | 所属 | 说明 |
|---|---|---|
value property | <ui-time-input> | 时间;为空或只输入一部分时为 null |
api property | <ui-time-input> | 控制器 |
valuechange 事件 | <ui-time-input> | detail: { value, reason, event },在完整值形成或被擦除时 |
无障碍
根部件是一个分组,名称来自它的 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。