TimePicker时间选择框

TimePicker 通过时、分、秒列选择一天中的时间。TimeRangePicker 选择开始与结束时间。值均为原生 Date 对象。

引入
import { TimePicker, TimeRangePicker } from 'svelte-ant-design';
链接
src/lib/TimePicker 问题反馈 组件 README

何时使用

  • 当用户需要选择一天中的时间时使用:会议开始、营业时间、闹钟等。
  • 步长与禁用时间让时间列只保留可选项;知道时间的人也可以直接输入。
  • 相互关联的开始与结束请用 TimeRangePicker;同时需要日期时用带 showTime 的 DatePicker。

代码演示

Value: nothing

基本用法

点击打开时间列;值为 Date,点击确定后生效。也可以直接输入时间。

format="HH:mm": two columns
format="HH": the hour alone
a list: the first writes, each reads (try 7.30)
a function writes; nothing typed is read

时间格式

format 控制字段文本的写入与解析,其模板决定显示哪些列。

use12Hours: h:mm:ss a
format="h:mm A"

12 小时制

use12Hours 显示 1 到 12 时,并增加上午/下午列。

every 2 hours, 15 minutes and 30 seconds

步长

hourStep、minuteStep、secondStep 设置各列数字的间隔。

disabledTime: open from 9:00 to 18:30
hideDisabledOptions: they are not shown

禁用时间

disabledTime 指定不可选的时、分、秒;hideDisabledOptions 将其隐藏。未选择时,列从第一个可选数字开始;向上滚动可看到其余数字。

the value waits for OK; onok says it was confirmed
needConfirm=false: every choice is taken
Value: nothing
showNow=false, defaultOpenValue 09:00

确定与此刻

默认点击确定后才改变值。needConfirm、showNow、defaultOpenValue 可调整选择方式。

changeOnScroll with needConfirm=false: scroll a column
Value: nothing
changeOnScroll: the scrolled time waits for OK

滚动选择

开启 changeOnScroll 后,滚动停下时位于顶部的数字即被选中。

No shift chosen

时间范围

TimeRangePicker 依次选择开始与结束时间,两端都选好后调用一次 onchange。

order=false: a night shift ends before it starts
allowEmpty: the end may stay empty
disabled={[true, false]}: the start is fixed
disabledTime is told which end it is asked for

范围两端

两端按一天中的时间比较。任一端可以留空或固定;关闭 order 后结束可早于开始。

范围预设

presets 显示在时间列旁,同时给出两端。

disabled
inputReadOnly: chosen in the panel only
allowClear=false
allowClear with its own icon and name
placeholder

禁用、只读与清除

inputReadOnly 禁止输入并避免弹出手机键盘。allowClear 隐藏或自定义清除按钮。

large
medium
small

尺寸

large、medium、small:40、32、24px,与其他输入控件一致。

outlined
filled
borderless
underlined

形态变体

outlined、filled、borderless 与 underlined。

Choose a time during opening hours.
The office is busy at this hour.

校验状态

status 标示错误或警告;请在字段旁用文字说明原因。

prefix
From
suffix replaces the clock
UTC
suffix={null}: no icon

图标

字段的 prefix 与 suffix。

cellRender: the full quarters are marked; extraFooter as a text
extraFooter as a snippet

自定义数字与页脚

cellRender 自定义数字内容,其名称不变;extraFooter 在时间列下方添加内容。

open held by the page
Opened by the field 0 times
placement="topRight"
container: the panel is drawn inside the box

面板控制

open 控制面板;placement、container 改变它的位置。

ru, with the halves of the day in words
zh-cn
ru, a range

语言

locale 提供界面文字与上午/下午名称。内置英语;俄语和中文随库提供,与 DatePicker 使用同一对象。

回调

onchange 与 onclear;onfocus、onblur 将字段与面板视为整体,各触发一次。

紧凑布局

在 SpaceCompact 中,字段与相邻控件合为一体。

classNames and styles

自定义类名与样式

classNames 与 styles 作用于字段和面板的各部分。

API

属性说明类型默认值
value所选时间(带日期)。可用 bind:value 绑定。Date | nullnull
format时间的书写方式;列表时第一个用于显示,任一个可用于解析。模板决定显示哪些列。use12Hours 时默认 h:mm:ss a。string | string[] | (date: Date) => string'HH:mm:ss'
use12Hours12 小时制,并显示上午/下午列。booleanfalse
hourStep小时步长。number1
minuteStep分钟步长。number1
secondStep秒步长。number1
disabledTime不可选的时、分、秒;参数为已选或待判断的时间,尚未选择时为 null。(date: Date | null) => DisabledTimes—
hideDisabledOptions隐藏禁用的数值。booleanfalse
defaultOpenValue选择时的初始时间和日期;缺省为今天 00:00:00。Date—
needConfirm点击确定后才改变值。booleantrue
showNow“此刻”按钮。booleantrue
changeOnScroll用户滚动后,列停下时顶部的数字即被选中。booleanfalse
cellRender数字内容:{ column, value, text, selected, disabled, range }。Snippet<[cell: TimeCell]>—
extraFooter时间列下方内容。string | Snippet—
placeholder空字段的占位提示;默认取自语言包。string—
disabled禁用字段。booleanfalse
inputReadOnly字段不可输入;手机不弹出键盘。booleanfalse
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 绑定。booleanfalse
placement面板的首选位置;靠近屏幕边缘时翻转。'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight''bottomLeft'
container面板挂载位置(默认 <body> 末尾)。HTMLElement | (anchor) => HTMLElement—
locale文字与上午/下午名称:import ru from 'svelte-ant-design/locales/ru'。DatePickerLocaleen
onchange用户改变了值;第二个参数为文本。(value, text: string) => void—
onokneedConfirm 时通过确定或回车确认了时间。(value: Date) => void—
onopenchange面板打开或关闭。(open: boolean) => void—
onclear点击清除按钮清空了值。() => void—
onfocus焦点从外部进入字段或面板。(event: FocusEvent) => void—
onblur焦点离开字段与面板。(event: FocusEvent) => void—
classNamesroot、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] | nullnull
placeholder两个输入框的占位提示;默认取自语言包。[string, string]—
inputLabels两个输入框的无障碍名称;默认取自语言包。[string, string]—
disabled整体或分别禁用。禁用端保留值并限制先后顺序。boolean | [boolean, boolean]false
allowEmpty允许留空的一端。[boolean, boolean][false, false]
separator两个输入框之间的内容;默认箭头。string | Snippet—
order按一天中的时间,开始不晚于结束:本次选择的两端会交换,之前保留的一端会重新选择。booleantrue
id两个输入框的 id。{ start?: string; end?: string }—
autofocus页面加载时聚焦开始输入框。booleanfalse
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—
onokneedConfirm 时确认了范围。(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 或手机检查。