Date Input
逐段输入日期,顺序跟随用户语言的写法。
把 Date Input 用于人们记在心里的日期,比如生日或预订日期。日期的每一部分都是一个独立的小输入框,顺序按 locale 的写法排列。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field>
<ui-field-label>Start date</ui-field-label>
<ui-date-input></ui-date-input>
</ui-field>
`
}
export { mount }安装
npm install @ui-sdk/web @ui-sdk/core导入组件会定义它的元素。如果用到 field,也把它导入。在服务器上,导入不会做任何事。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"组合
用下面的组合构建 ui-date-input:
ui-date-input
├── ui-date-input-segment
│ └── input
└── ui-date-input-segment<ui-date-input> 渲染一个分组,日期的每一部分对应一个 <ui-date-input-segment>:英语里是月、日、年,德语里是日、月、年。你输入的每个分段里都有一个 <input>。它们之间的斜杠或点也是分段,只是纯文本。输入元素会替你渲染分段;只有想改动它们时才给它一个 <template>,参见自定义渲染。
用法
把输入框放进 Field,并给它一个标签:
<script type="module">
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
</script>
<ui-field>
<ui-field-label>Start date</ui-field-label>
<ui-date-input default-value="2026-11-02"></ui-date-input>
</ui-field>值是 "2026-11-02" 这样的 ISO 日期字符串,和原生日期输入框一样。在每个分段都填入内容之前,它一直是 null,因此你的应用只会看到完整日期。
输入数字,每个分段填满后移到下一个。像 4 这样不能再接一位数字的月份,会立即移到下一个。输入分隔符,比如 / 或 .,也会移到下一个。日会被限制在当月之内:从 1 月 31 日移到 2 月,显示的是 28 日。把完整日期粘贴到任意分段,可以是 2026-11-02,也可以是 locale 的写法。
字段
字段的标签为这个分组提供名称,点击标签会把焦点移到第一个分段。它的描述会和输入框一起被读出。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field>
<ui-field-label>Birthday</ui-field-label>
<ui-date-input name="birthday"></ui-date-input>
<ui-field-description>We send a card on the day.</ui-field-description>
</ui-field>
`
}
export { mount }日期与时间
把 granularity 设为 "hour"、"minute" 或 "second",就能加上时间。值会变成 "2026-11-02T14:30" 这样的本地日期和时间。只要时间,请用 Time Input。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field>
<ui-field-label>Departure</ui-field-label>
<ui-date-input granularity="minute" default-value="2026-11-02T14:30"></ui-date-input>
</ui-field>
`
}
export { mount }最小与最大
min 和 max 接受 ISO 日期。超出范围的日期无效:输入框会设置 data-invalid,它的表单也不会提交。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field>
<ui-field-label>Check-in</ui-field-label>
<ui-date-input min="2026-01-01" max="2026-12-31" default-value="2026-06-15"></ui-date-input>
<ui-field-description>Any day in 2026.</ui-field-description>
<ui-field-error></ui-field-error>
</ui-field>
`
}
export { mount }不可用的日期
unavailable property 接受与 Calendar 相同的匹配器:ISO 日期、{ from, to } 范围、{ weekday: [0, 6] } 这样的星期,或一个函数。不可用的日期无效,并以“This date isn't available.”作为消息。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field>
<ui-field-label>Delivery date</ui-field-label>
<ui-date-input default-value="2026-11-02"></ui-date-input>
<ui-field-description>We deliver Monday to Friday.</ui-field-description>
<ui-field-error></ui-field-error>
</ui-field>
`
root.querySelector("ui-date-input")!.unavailable = { weekday: [0, 6] }
}
export { mount }语言
输入框从 locale 读取顺序、分隔符和时钟,省略时读取页面的语言。数字始终是 0 到 9,日历始终是公历,所以这个值在任何地方都表示同一个日期。在服务器上,输入框会先以英语渲染,直到读到页面的语言;如果首次渲染很重要,请设置 locale。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="stack">
<ui-field>
<ui-field-label>Date (en-GB)</ui-field-label>
<ui-date-input locale="en-GB" default-value="2026-11-02"></ui-date-input>
</ui-field>
<ui-field>
<ui-field-label>Datum (de-DE)</ui-field-label>
<ui-date-input locale="de-DE" default-value="2026-11-02"></ui-date-input>
</ui-field>
<ui-field>
<ui-field-label>日付 (ja-JP)</ui-field-label>
<ui-date-input locale="ja-JP" default-value="2026-11-02"></ui-date-input>
</ui-field>
</div>
`
}
export { mount }禁用
在输入框上,或在它的 Field 或 fieldset 上设置 disabled,可以让它无法获得焦点,也不参与表单。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field>
<ui-field-label>Start date</ui-field-label>
<ui-date-input default-value="2026-11-02" disabled></ui-date-input>
</ui-field>
`
}
export { mount }只读
设置 read-only,得到一个可以聚焦、读取,并按原样提交的日期。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field>
<ui-field-label>Signed on</ui-field-label>
<ui-date-input default-value="2026-11-02" read-only></ui-date-input>
</ui-field>
`
}
export { mount }表单
这个元素与表单关联,以 name 提交 ISO 日期,没有隐藏输入框。空日期或只输入了一部分的日期会提交空字符串。设置 required 后,表单需要先有日期才能提交。在分段中按 Enter 会用已输入的内容提交表单。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
import "@ui-sdk/web/time-input"
import { log } from "./log"
function mount(root: HTMLElement) {
root.innerHTML = `
<form data-slot="stack">
<ui-field>
<ui-field-label>Date</ui-field-label>
<ui-date-input name="date" default-value="2026-11-02" required></ui-date-input>
<ui-field-error></ui-field-error>
</ui-field>
<ui-field>
<ui-field-label>Time</ui-field-label>
<ui-time-input name="time" default-value="09:30" required></ui-time-input>
<ui-field-error></ui-field-error>
</ui-field>
<div data-slot="row">
<button type="reset">Reset</button>
<button type="submit" data-variant="solid">Book</button>
</div>
</form>
`
const form = root.querySelector("form")!
form.addEventListener("submit", (event) => {
event.preventDefault()
const data = [...new FormData(form)].map(
([name, value]) => `${name}=${value}`
)
log(root, `Submitted ${data.join(", ")}`)
})
}
export { mount }只输入了一部分的日期也会阻止表单提交,并以“Enter a valid date.”作为消息。把 form 设为页面中另一个表单的 id,就能用那个表单提交和校验。表单重置会恢复 default-value,并报告 reset 原因。
受控
监听 valuechange 读取变更,并设置元素的 value property 来从你的应用更新它。事件会在得到每个完整日期时触发,在日期的任意部分被擦除后则以 null 触发。只输入了一半的日期本身就读作 null,所以把值设为 null 会保持已输入的内容。调用元素 api 上的 clear() 可以清空每个分段。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
import { log } from "./log"
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="stack">
<div data-slot="controls">
<button type="button">Clear</button>
</div>
<ui-field>
<ui-field-label>Due date</ui-field-label>
<ui-date-input default-value="2026-11-02"></ui-date-input>
</ui-field>
</div>
`
const input = root.querySelector("ui-date-input")!
input.addEventListener("valuechange", (event) => {
log(root, `Due date ${event.detail.value ?? "empty"}`)
})
root.querySelector("button")!.addEventListener("click", () => {
input.value = null
})
}
export { mount }控制器
用元素的 api 从别处设置、清空或聚焦输入框。它也会接受用 createDateInputController() 创建的控制器,作为它的 controller property。focus("year") 按单位聚焦某个分段。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="stack">
<div data-slot="controls">
<button type="button" data-action="christmas">Christmas</button>
<button type="button" data-action="year">Edit Year</button>
<button type="button" data-action="clear">Clear</button>
</div>
<ui-field>
<ui-field-label>Publish on</ui-field-label>
<ui-date-input></ui-date-input>
</ui-field>
</div>
`
const publish = root.querySelector("ui-date-input")!.api
const actions: Record<string, () => void> = {
christmas: () => publish.setValue("2026-12-25"),
year: () => publish.focus("year"),
clear: () => publish.clear(),
}
root
.querySelectorAll<HTMLButtonElement>("[data-action]")
.forEach((button) => {
button.addEventListener("click", () => actions[button.dataset.action!]())
})
}
export { mount }控制器涵盖了创建和共享控制器。
自定义渲染
在输入元素里放一个 <template>,其中包含每个分段都会复制的 <ui-date-input-segment>。输入元素按 locale 的顺序为每个分段渲染一份副本。你输入内容的分段会使用你副本里的 <input>,或新建一个;分段之间的文本会替换副本的内容。
import "@ui-sdk/web/date-input"
import "@ui-sdk/web/field"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field>
<ui-field-label>Start date</ui-field-label>
<ui-date-input default-value="2026-11-02">
<template>
<ui-date-input-segment><input /></ui-date-input-segment>
</template>
</ui-date-input>
</ui-field>
`
}
export { mount }每个部件的状态列在状态下。
样式
把根部件当作字段的外框来设置样式,分段则放在其中。每个分段都会把 data-type 设成它的单位,所以你可以加宽年份,或弱化分段之间的文本,后者带有 data-type="literal"。空的分段会设置 data-empty 并显示占位符。日期超出范围或不可用时,根部件会设置 data-invalid。
每个部件都有一个 data-slot,比如 date-input-segment,它的状态属性列在数据属性下。
数据属性
<ui-date-input>
[data-slot="date-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 date is before min, after max or unavailable. |
data-empty | Present while every segment is empty. |
<ui-date-input-segment>
[data-slot="date-input-segment"]
| 属性 | 含义 |
|---|---|
data-type | yearmonthdayhourminuteseconddayPeriodliteralThe 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 date is before min, after max or unavailable. |
data-empty | Present while the segment is empty. It shows its placeholder. |
API 参考
属性(props)使用 kebab 形式,比如 default-value、hour-cycle 和 read-only。value 同时也是一个 property,unavailable 和 controller 是 property。回调是 DOM 事件。
DateInput
value?string | null- The value: an ISO date like
"2026-11-02", with a time granularity a date and time like"2026-11-02T14:30", 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?DateInputGranularity- The smallest unit it shows.
"day"shows a date, and"hour","minute"or"second"add the time down to it. hourCycle?HourCycle- A 12 or 24 hour clock for the time. Defaults to the locale's.
min?string- The earliest value, as an ISO date or date and time. A value before it is invalid.
max?string- The latest value. A date alone takes any time that day. A value after it is invalid.
unavailable?DateMatchers- Dates that can't be chosen, as in Calendar: ISO dates,
{ from, to }ranges, weekdays or a function. A value on one is invalid. locale?string- The language whose order, 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 value 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
createDateInputController(). debug?boolean- Logs every state change with its reason to the console, in development builds.
DateInputSegment
segmentDateInputSegmentData- The segment it shows, from the root's
segments.
状态
api 读到什么,以及部件把什么反映为属性。
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.
每个分段的状态 DateInputSegmentState,是分段本身加上它旁边的输入框状态,并反映在 <ui-date-input-segment> 上:
keystring- Tells segments apart: the type, or
"literal-<n>"for the text between them. typeDateInputSegmentTypetextstring- What it shows: the number, padded, or AM and PM in the locale. Empty while it holds nothing.
valuenumber | null- The number it holds, or
nullwhile empty. AM is 0 and PM is 1. minnumber- The smallest number it takes.
maxnumber- The largest number it takes, like the days in the month.
emptyboolean- Whether it holds nothing yet.
editableboolean- Whether it's a segment you type in, rather than the text between segments.
labelstring- Its name in the locale, like "month", which names it to assistive technology.
disabledboolean- Whether it can't be used.
readOnlyboolean- Whether its value can't be changed.
invalidboolean- Whether the value is before
min, aftermaxor an unavailable date.
原因
details.reason 说明值为什么改变。
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.
控制器
用 createDateInputController() 创建一个,并作为 controller 传入。
每个写入方法都接受可选的 { reason, event },像输入一样运行变更回调,并在变更被取消时返回 false。
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-date-input> | ISO 日期;为空或只输入一部分时为 null |
api property | <ui-date-input> | 控制器 |
valuechange 事件 | <ui-date-input> | detail: { value, reason, event },在完整值形成或被擦除时 |
无障碍
根部件是一个分组,名称来自它的 Field 标签或你的 aria-label。每个分段都是一个 spinbutton,由它的单位命名,比如“month”,并带有它的值、范围和文本。空的分段读作“Empty”。分段之间的文本对屏幕阅读器隐藏,屏幕阅读器改为读取每个分段的名称。
分段是真正的输入框,所以手机显示数字键盘。元素定义完成后它们才会出现。
键盘
| 按键 | 动作 |
|---|---|
| 0 到 9 | 填充分段,然后移到下一个。 |
| ArrowUp / ArrowDown | 让分段步进一个值,越界时循环。空的分段从今天开始。 |
| PageUp / PageDown | 用更大的步进移动:年每次 5,月每次 3,日每次 7,分钟每次 15。 |
| Home / End | 跳到分段的第一个或最后一个值。 |
| ArrowLeft / ArrowRight | 移到前一个或后一个分段。 |
| Backspace | 删除最后一位数字,然后清空分段,再往回移动。 |
| Delete | 清空分段。 |
| A / P | 在 12 小时制的时间里选择 AM 或 PM。 |
| Enter | 用已输入的内容提交表单。 |
| Tab / Shift+Tab | 依次经过各个分段,然后离开输入框。 |
在从右到左的页面中,分段保持 locale 的顺序,ArrowLeft 和 ArrowRight 跟随屏幕上的顺序。
经过测试的行为
下面每一行都是一个自动化测试。
- 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。