TimePicker时间选择框
TimePicker 通过时、分、秒列选择一天中的时间。TimeRangePicker 选择开始与结束时间。值均为原生 Date 对象。
何时使用
- 当用户需要选择一天中的时间时使用:会议开始、营业时间、闹钟等。
- 步长与禁用时间让时间列只保留可选项;知道时间的人也可以直接输入。
- 相互关联的开始与结束请用
TimeRangePicker;同时需要日期时用带showTime的DatePicker。
代码演示
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
let start = $state<Date | null>(null);
const clock = (time: Date | null) => (time ? time.toTimeString().slice(0, 8) : 'nothing');
</script>
<div class="stack">
<div class="field">
<TimePicker aria-label="Start" bind:value={start} />
<span role="status">Value: {clock(start)}</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
</style>
format="HH:mm": two columns
14:05
format="HH": the hour alone
a list: the first writes, each reads (try 7.30)
a function writes; nothing typed is read
14 h 5 min
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
const moment = new Date(2026, 0, 1, 14, 5, 9);
</script>
<div class="stack">
<div class="field">
<span class="caption">format="HH:mm": two columns</span>
<TimePicker aria-label="Hours and minutes" format="HH:mm" value={moment} />
</div>
<div class="field">
<span class="caption">format="HH": the hour alone</span>
<TimePicker aria-label="Hour" format="HH" />
</div>
<div class="field">
<span class="caption">a list: the first writes, each reads (try 7.30)</span>
<TimePicker aria-label="Either way" format={['HH:mm', 'H.mm']} />
</div>
<div class="field">
<span class="caption">a function writes; nothing typed is read</span>
<TimePicker aria-label="In words" format={(time) => `${time.getHours()} h ${time.getMinutes()} min`} value={moment} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
use12Hours: h:mm:ss a
2:05:09 pm
format="h:mm A"
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
const moment = new Date(2026, 0, 1, 14, 5, 9);
</script>
<div class="stack">
<div class="field">
<span class="caption">use12Hours: h:mm:ss a</span>
<TimePicker aria-label="Twelve hours" use12Hours value={moment} />
</div>
<div class="field">
<span class="caption">format="h:mm A"</span>
<TimePicker aria-label="Twelve hours without seconds" format="h:mm A" />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
every 2 hours, 15 minutes and 30 seconds
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">every 2 hours, 15 minutes and 30 seconds</span>
<TimePicker aria-label="In steps" hourStep={2} minuteStep={15} secondStep={30} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
disabledTime: open from 9:00 to 18:30
hideDisabledOptions: they are not shown
<script lang="ts">
import { TimePicker, type DisabledTimes } from 'svelte-ant-design';
const range = (from: number, to: number) => Array.from({ length: to - from }, (_, index) => from + index);
// Open from 9 to 18; in the last hour only the first half.
const openingHours = (): DisabledTimes => ({
disabledHours: () => [...range(0, 9), ...range(19, 24)],
disabledMinutes: (hour) => (hour === 18 ? range(31, 60) : [])
});
</script>
<div class="stack">
<div class="field">
<span class="caption">disabledTime: open from 9:00 to 18:30</span>
<TimePicker aria-label="Visit" format="HH:mm" disabledTime={openingHours} />
</div>
<div class="field">
<span class="caption">hideDisabledOptions: they are not shown</span>
<TimePicker aria-label="Visit, short list" format="HH:mm" disabledTime={openingHours} hideDisabledOptions />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
the value waits for OK; onok says it was confirmed
needConfirm=false: every choice is taken Value: nothing
showNow=false, defaultOpenValue 09:00
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
let taken = $state<Date | null>(null);
let confirmed = $state('');
const nine = new Date(2026, 0, 1, 9, 0);
const clock = (time: Date | null) => (time ? time.toTimeString().slice(0, 8) : 'nothing');
</script>
<div class="stack">
<div class="field">
<span class="caption">the value waits for OK; onok says it was confirmed</span>
<TimePicker aria-label="Confirmed time" onok={(time) => (confirmed = clock(time))} />
{#if confirmed}<span role="status">Confirmed: {confirmed}</span>{/if}
</div>
<div class="field">
<span class="caption">needConfirm=false: every choice is taken</span>
<TimePicker aria-label="Taken at once" needConfirm={false} bind:value={taken} />
<span role="status">Value: {clock(taken)}</span>
</div>
<div class="field">
<span class="caption">showNow=false, defaultOpenValue 09:00</span>
<TimePicker aria-label="From nine" showNow={false} defaultOpenValue={nine} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
changeOnScroll with needConfirm=false: scroll a column Value: nothing
changeOnScroll: the scrolled time waits for OK
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
let taken = $state<Date | null>(null);
const clock = (time: Date | null) => (time ? time.toTimeString().slice(0, 8) : 'nothing');
</script>
<div class="stack">
<div class="field">
<span class="caption">changeOnScroll with needConfirm=false: scroll a column</span>
<TimePicker aria-label="Scrolled" changeOnScroll needConfirm={false} bind:value={taken} />
<span role="status">Value: {clock(taken)}</span>
</div>
<div class="field">
<span class="caption">changeOnScroll: the scrolled time waits for OK</span>
<TimePicker aria-label="Scrolled and confirmed" changeOnScroll />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { TimeRangePicker, type DateRange } from 'svelte-ant-design';
let shift = $state<DateRange | null>(null);
let last = $state('');
const clock = (time: Date | null) => (time ? time.toTimeString().slice(0, 5) : 'empty');
</script>
<div class="stack">
<div class="field">
<TimeRangePicker
aria-label="Shift"
format="HH:mm"
bind:value={shift}
oncalendarchange={(_, text, { range }) => (last = `${range}: ${text[range === 'start' ? 0 : 1]}`)}
/>
<span role="status">{shift ? `${clock(shift[0])} – ${clock(shift[1])}` : 'No shift chosen'}</span>
{#if last}<span role="status">Last end chosen, {last}</span>{/if}
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(320px, 100%);
}
</style>
order=false: a night shift ends before it starts
allowEmpty: the end may stay empty
disabled={[true, false]}: the start is fixed
09:00 18:00
disabledTime is told which end it is asked for
<script lang="ts">
import { TimeRangePicker, type DisabledTimes, type RangeEnd } from 'svelte-ant-design';
const opening: [Date, Date] = [new Date(2026, 0, 1, 9, 0), new Date(2026, 0, 1, 18, 0)];
// A start in the morning, an end in the afternoon.
const halves = (_: Date | null, end: RangeEnd): DisabledTimes => ({
disabledHours: () => Array.from({ length: 12 }, (_, hour) => (end === 'start' ? hour + 12 : hour))
});
let open = $state('');
</script>
<div class="stack">
<div class="field">
<span class="caption">order=false: a night shift ends before it starts</span>
<TimeRangePicker aria-label="Night shift" format="HH:mm" order={false} separator="to" id={{ start: 'night-from', end: 'night-to' }} />
</div>
<div class="field">
<span class="caption">allowEmpty: the end may stay empty</span>
<TimeRangePicker
aria-label="Opening"
format="HH:mm"
allowEmpty={[false, true]}
placeholder={['Opens', 'Closes (optional)']}
inputLabels={['Opening time', 'Closing time']}
onchange={(_, text) => (open = text.map((part) => part || 'open').join(' – '))}
/>
{#if open}<span role="status">{open}</span>{/if}
</div>
<div class="field">
<span class="caption">{'disabled={[true, false]}'}: the start is fixed</span>
<TimeRangePicker aria-label="Longer day" format="HH:mm" disabled={[true, false]} value={opening} />
</div>
<div class="field">
<span class="caption">disabledTime is told which end it is asked for</span>
<TimeRangePicker aria-label="Two halves" format="HH:mm" disabledTime={halves} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(320px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { TimeRangePicker } from 'svelte-ant-design';
const at = (hour: number, minute = 0) => new Date(2026, 0, 1, hour, minute);
const presets = [
{ label: 'Morning', value: [at(9), at(13)] as [Date, Date] },
{ label: 'Evening', value: [at(14), at(18)] as [Date, Date] }
];
</script>
<div class="stack">
<div class="field">
<TimeRangePicker aria-label="Meeting hours" format="HH:mm" {presets} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(320px, 100%);
}
</style>
disabled
14:30:00
inputReadOnly: chosen in the panel only
14:30:00
allowClear=false
14:30:00
allowClear with its own icon and name
14:30:00
placeholder
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
const moment = new Date(2026, 0, 1, 14, 30);
</script>
<div class="stack">
<div class="field">
<span class="caption">disabled</span>
<TimePicker aria-label="Disabled time" disabled value={moment} />
</div>
<div class="field">
<span class="caption">inputReadOnly: chosen in the panel only</span>
<TimePicker aria-label="Chosen only" inputReadOnly value={moment} />
</div>
<div class="field">
<span class="caption">allowClear=false</span>
<TimePicker aria-label="Always set" allowClear={false} value={moment} />
</div>
<div class="field">
<span class="caption">allowClear with its own icon and name</span>
<TimePicker aria-label="Reset" allowClear={{ label: 'Reset the time', clearIcon: '↺' }} value={moment} />
</div>
<div class="field">
<span class="caption">placeholder</span>
<TimePicker aria-label="Alarm" placeholder="Wake me at" />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
large
medium
small
<script lang="ts">
import { TimePicker, TimeRangePicker } from 'svelte-ant-design';
const sizes = ['large', 'medium', 'small'] as const;
</script>
<div class="stack">
{#each sizes as size (size)}
<div class="field">
<span class="caption">{size}</span>
<TimePicker aria-label="Time, {size}" {size} />
<TimeRangePicker aria-label="Range, {size}" {size} />
</div>
{/each}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(320px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
outlined
filled
borderless
underlined
<script lang="ts">
import { TimePicker, TimeRangePicker } from 'svelte-ant-design';
const variants = ['outlined', 'filled', 'borderless', 'underlined'] as const;
</script>
<div class="stack">
{#each variants as variant (variant)}
<div class="field">
<span class="caption">{variant}</span>
<TimePicker aria-label="Time, {variant}" {variant} />
<TimeRangePicker aria-label="Range, {variant}" {variant} />
</div>
{/each}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(320px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<TimePicker aria-label="Time with an error" status="error" aria-describedby="time-error" />
<span id="time-error">Choose a time during opening hours.</span>
</div>
<div class="field">
<TimePicker aria-label="Time with a warning" status="warning" aria-describedby="time-warning" />
<span id="time-warning">The office is busy at this hour.</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
</style>
prefix
From
suffix replaces the clock
UTC
suffix={null}: no icon
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">prefix</span>
<TimePicker aria-label="Departure" prefix="From" />
</div>
<div class="field">
<span class="caption">suffix replaces the clock</span>
<TimePicker aria-label="Arrival" suffix="UTC" />
</div>
<div class="field">
<span class="caption">{'suffix={null}'}: no icon</span>
<TimePicker aria-label="Plain" suffix={null} />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
cellRender: the full quarters are marked; extraFooter as a text
extraFooter as a snippet
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">cellRender: the full quarters are marked; extraFooter as a text</span>
<TimePicker aria-label="Quarter" format="HH:mm" extraFooter="Times of the local zone">
{#snippet cellRender({ column, value, text })}
<span class:quarter={column === 'minute' && value % 15 === 0}>{text}</span>
{/snippet}
</TimePicker>
</div>
<div class="field">
<span class="caption">extraFooter as a snippet</span>
<TimePicker aria-label="With a note">
{#snippet extraFooter()}<strong>Closed on Sundays</strong>{/snippet}
</TimePicker>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.quarter {
text-decoration: underline 2px var(--ads-color-primary);
text-underline-offset: 3px;
}
</style>
open held by the page Opened by the field 0 times
placement="topRight"
container: the panel is drawn inside the box
<script lang="ts">
import { Button, TimePicker } from 'svelte-ant-design';
let open = $state(false);
let opened = $state(0);
let box = $state<HTMLDivElement>();
</script>
<div class="stack">
<div class="field">
<span class="caption">open held by the page</span>
<TimePicker aria-label="Controlled panel" bind:open onopenchange={(next) => next && opened++} />
<Button onclick={() => (open = true)}>Open the panel</Button>
<span role="status">Opened by the field {opened} times</span>
</div>
<div class="field">
<span class="caption">placement="topRight"</span>
<TimePicker aria-label="Above the field" placement="topRight" />
</div>
<div class="field">
<span class="caption">container: the panel is drawn inside the box</span>
<div class="box" bind:this={box}>
<TimePicker aria-label="Inside the box" container={() => box ?? document.body} />
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.box {
position: relative;
justify-self: stretch;
padding: 12px;
border: 1px dashed var(--ads-color-border);
}
</style>
ru, with the halves of the day in words
9:05 утра
zh-cn
上午9:05
ru, a range
<script lang="ts">
import { TimePicker, TimeRangePicker } from 'svelte-ant-design';
import ru from 'svelte-ant-design/locales/ru';
import zhCn from 'svelte-ant-design/locales/zh-cn';
const moment = new Date(2026, 0, 1, 9, 5);
</script>
<div class="stack">
<div class="field">
<span class="caption">ru, with the halves of the day in words</span>
<TimePicker aria-label="Время" locale={ru} format="h:mm A" value={moment} />
</div>
<div class="field">
<span class="caption">zh-cn</span>
<TimePicker aria-label="时间" locale={zhCn} format="Ah:mm" value={moment} />
</div>
<div class="field">
<span class="caption">ru, a range</span>
<TimeRangePicker aria-label="Смена" locale={ru} format="HH:mm" />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(320px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
let log = $state<string[]>([]);
const note = (line: string) => (log = [line, ...log].slice(0, 4));
</script>
<div class="stack">
<div class="field">
<TimePicker
aria-label="Watched time"
onchange={(_, text) => note(`onchange: ${text || 'empty'}`)}
onclear={() => note('onclear')}
onfocus={() => note('onfocus')}
onblur={() => note('onblur')}
/>
<ul class="log" aria-live="polite">
{#each log as line, index (index)}<li>{line}</li>{/each}
</ul>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.log {
min-height: 5.5em;
margin: 0;
padding-inline-start: 18px;
}
</style>
Departure
<script lang="ts">
import { Button, Select, SpaceCompact, TimePicker } from 'svelte-ant-design';
let kind = $state('Departure');
</script>
<div class="stack">
<div class="field">
<SpaceCompact block>
<Select aria-label="Kind of time" bind:value={kind} options={['Departure', 'Arrival']} style="width: 140px" />
<TimePicker aria-label="Time of the trip" />
<Button type="primary">Search</Button>
</SpaceCompact>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(420px, 100%);
}
</style>
classNames and styles
<script lang="ts">
import { TimePicker } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">classNames and styles</span>
<TimePicker
aria-label="Styled time"
class="rounded"
style="max-width: 240px"
classNames={{ popup: 'soft', input: 'strong', cell: 'digit' }}
styles={{ root: { 'border-radius': '16px' }, suffix: { color: 'var(--ads-color-primary)' } }}
/>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 6px;
width: min(240px, 100%);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
:global(.rounded .strong) {
font-weight: 600;
}
:global(.soft .digit) {
font-variant-numeric: tabular-nums;
font-weight: 500;
}
</style>
API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 所选时间(带日期)。可用 bind:value 绑定。 | Date | null | null |
| format | 时间的书写方式;列表时第一个用于显示,任一个可用于解析。模板决定显示哪些列。use12Hours 时默认 h:mm:ss a。 | string | string[] | (date: Date) => string | 'HH:mm:ss' |
| use12Hours | 12 小时制,并显示上午/下午列。 | boolean | false |
| hourStep | 小时步长。 | number | 1 |
| minuteStep | 分钟步长。 | number | 1 |
| secondStep | 秒步长。 | number | 1 |
| disabledTime | 不可选的时、分、秒;参数为已选或待判断的时间,尚未选择时为 null。 | (date: Date | null) => DisabledTimes | — |
| hideDisabledOptions | 隐藏禁用的数值。 | boolean | false |
| defaultOpenValue | 选择时的初始时间和日期;缺省为今天 00:00:00。 | Date | — |
| needConfirm | 点击确定后才改变值。 | boolean | true |
| showNow | “此刻”按钮。 | boolean | true |
| changeOnScroll | 用户滚动后,列停下时顶部的数字即被选中。 | boolean | false |
| cellRender | 数字内容:{ column, value, text, selected, disabled, range }。 | Snippet<[cell: TimeCell]> | — |
| extraFooter | 时间列下方内容。 | string | Snippet | — |
| placeholder | 空字段的占位提示;默认取自语言包。 | string | — |
| disabled | 禁用字段。 | boolean | false |
| inputReadOnly | 字段不可输入;手机不弹出键盘。 | boolean | false |
| allowClear | 清除按钮;传对象可改图标、名称和 tabindex。 | boolean | { clearIcon?, label?, tabIndex? } | true |
| size | 默认 medium(紧凑布局可覆盖);middle、default 为别名。 | 'large' | 'medium' | 'middle' | 'small' | 'default' | — |
| variant | 字段外观。 | 'outlined' | 'filled' | 'borderless' | 'underlined' | 'outlined' |
| status | 标示错误或警告;error 同时设置 aria-invalid。 | 'error' | 'warning' | — |
| prefix | 文本之前的内容。 | string | Snippet | — |
| suffix | 替换时钟图标;null 隐藏。 | string | Snippet | null | — |
| open | 面板是否打开。可用 bind:open 绑定。 | boolean | false |
| placement | 面板的首选位置;靠近屏幕边缘时翻转。 | 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight' | 'bottomLeft' |
| container | 面板挂载位置(默认 <body> 末尾)。 | HTMLElement | (anchor) => HTMLElement | — |
| locale | 文字与上午/下午名称:import ru from 'svelte-ant-design/locales/ru'。 | DatePickerLocale | en |
| onchange | 用户改变了值;第二个参数为文本。 | (value, text: string) => void | — |
| onok | needConfirm 时通过确定或回车确认了时间。 | (value: Date) => void | — |
| onopenchange | 面板打开或关闭。 | (open: boolean) => void | — |
| onclear | 点击清除按钮清空了值。 | () => void | — |
| onfocus | 焦点从外部进入字段或面板。 | (event: FocusEvent) => void | — |
| onblur | 焦点离开字段与面板。 | (event: FocusEvent) => void | — |
| classNames | root、input、prefix、suffix、popup、body、cell、footer 的附加类名。 | TimePickerClassNames | — |
| styles | 同名部分的内联样式。 | TimePickerStyles | — |
| class / style | 字段的类名与样式。 | string | — |
| native attributes | 其他属性传给 <input>(id、name、aria-*、autofocus)。 | HTMLInputAttributes | — |
与 Ant Design 的差异:值为原生
Date 而非 dayjs;TimePicker.RangePicker 对应 TimeRangePicker;仅获得焦点不会打开面板;键盘操作遵循 ARIA 的组合框与列表框模式;范围选择带有“此刻”按钮。本版本暂不支持:毫秒、掩码 format、previewValue、panelRender、自定义底部按钮、单个 TimePicker 的 presets 以及替换日期库。TimeRangePicker
TimeRangePicker 支持 TimePicker 的属性;下列属性有所不同或为其独有。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 开始与结束;未选的一端为 null。可用 bind:value 绑定。 | [Date | null, Date | null] | null | null |
| placeholder | 两个输入框的占位提示;默认取自语言包。 | [string, string] | — |
| inputLabels | 两个输入框的无障碍名称;默认取自语言包。 | [string, string] | — |
| disabled | 整体或分别禁用。禁用端保留值并限制先后顺序。 | boolean | [boolean, boolean] | false |
| allowEmpty | 允许留空的一端。 | [boolean, boolean] | [false, false] |
| separator | 两个输入框之间的内容;默认箭头。 | string | Snippet | — |
| order | 按一天中的时间,开始不晚于结束:本次选择的两端会交换,之前保留的一端会重新选择。 | boolean | true |
| id | 两个输入框的 id。 | { start?: string; end?: string } | — |
| autofocus | 页面加载时聚焦开始输入框。 | boolean | false |
| disabledTime | 当前选择端不可选的时间。 | (date: Date | null, range: 'start' | 'end') => DisabledTimes | — |
| presets | 同时给出两端的预设项。 | { label: string | Snippet; value: [Date, Date] | (() => [Date, Date]) }[] | [] |
| onchange | 范围改变:两端完整后调用一次。 | (value, text: [string, string]) => void | — |
| oncalendarchange | 选择了某一端;range 指明是哪端。 | (value, text, info: { range }) => void | — |
| onok | needConfirm 时确认了范围。 | (value) => void | — |
| onfocus | 焦点从外部进入字段或面板。 | (event, info: { range }) => void | — |
| onblur | 焦点离开字段与面板。 | (event, info: { range }) => void | — |
| native attributes | 其他属性传给根元素(group):aria-label、data-*。 | HTMLAttributes<HTMLDivElement> | — |
方法
| 方法 | 说明 | 类型 |
|---|---|---|
| focus | 让字段获得焦点;范围选择时聚焦当前端。不会打开面板。 | (options?: FocusOptions) => void |
| blur | 让字段失焦;面板随之关闭。 | () => void |
设计令牌
| 令牌 | 说明 | 默认值 |
|---|---|---|
| --ads-time-picker-column-height | 时间列高度。 | 224px |
| --ads-picker-time-column-width | 时间列宽度。 | 56px |
| --ads-picker-time-item-height | 时间项高度。 | 28px |
| --ads-picker-time-selected-bg | 选中时间项背景。 | #e6f4ff |
| --ads-picker-cell-hover-bg | 悬停时间项背景。 | rgba(0, 0, 0, 0.04) |
| --ads-picker-popup-radius | 面板圆角半径。 | 8px |
| --ads-picker-popup-bg | 面板背景。 | #ffffff |
| --ads-picker-popup-shadow | 面板阴影。 | 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05) |
| --ads-picker-actions-height | 时间列下方按钮区的最小高度。 | 42px |
| --ads-picker-presets-width | 预设列宽度。 | 120px |
| --ads-picker-icon-color | 字段图标颜色。 | rgba(0, 0, 0, 0.45) |
键盘操作
- 在字段中按
↓打开面板并将焦点移入第一列。 - 列中
↑、↓改变数值,Home、End到首尾,←、→切换列。 Enter或空格选择数字;在已选数字上按Enter确认待定值。范围选择时焦点随后移到另一端输入框。Escape关闭面板、丢弃未确认的值并将焦点返回字段。Tab离开字段并关闭面板;范围选择时先从开始移到结束。面板内依次经过各列(各一个焦点)与底部。
无障碍
- 字段是关联对话框的
combobox;每一列是具名的listbox,其中的数字是选项,标明已选与禁用状态。 - 范围选择是包含两个具名输入框的
group;对话框说明正在选择哪一端。请用aria-label为该组命名。 - 默认浅色配色沿用 Ant Design,部分对比度未达 4.5:1,可引入
tokens-accessible.css。所选与禁用的数字在强制颜色模式下依然可辨。 - 已用真实屏幕阅读器(Orca 搭配 Firefox)检查:朗读字段、对话框名称、时间列及数字、确认后的值以及范围两端。Orca 自己不会朗读此类字段的值,因此每个输入框也把值作为描述提供,未检查的 NVDA、JAWS、VoiceOver 可能会读两遍。Orca 不会说明某个数字已禁用,按
Enter选择数字时只读出按键。未用 NVDA、JAWS、VoiceOver 或手机检查。