2016-03-31 14:17:09 +08:00
|
|
|
|
---
|
|
|
|
|
category: Components
|
2022-11-09 12:28:04 +08:00
|
|
|
|
group: 数据录入
|
2016-09-21 11:28:38 +08:00
|
|
|
|
title: TimePicker
|
2024-03-22 14:22:42 +08:00
|
|
|
|
subtitle: 时间选择框
|
|
|
|
|
description: 输入或选择时间的控件。
|
2022-11-30 20:14:41 +08:00
|
|
|
|
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*kGmGSLk_1fwAAAAAAAAAAAAADrJ8AQ/original
|
2023-02-09 22:17:31 +08:00
|
|
|
|
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*1hDmQJIDFJQAAAAAAAAAAAAADrJ8AQ/original
|
2022-11-09 12:28:04 +08:00
|
|
|
|
demo:
|
|
|
|
|
cols: 2
|
2016-03-31 14:17:09 +08:00
|
|
|
|
---
|
2015-11-16 23:11:41 +08:00
|
|
|
|
|
2016-07-26 11:03:44 +08:00
|
|
|
|
## 何时使用
|
2017-10-25 10:25:44 +08:00
|
|
|
|
|
2015-11-16 23:11:41 +08:00
|
|
|
|
当用户需要输入一个时间,可以点击标准输入框,弹出时间面板进行选择。
|
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
|
## 代码演示
|
|
|
|
|
|
2022-11-17 17:31:26 +08:00
|
|
|
|
<!-- prettier-ignore -->
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/basic.tsx">基本</code>
|
|
|
|
|
<code src="./demo/value.tsx">受控组件</code>
|
|
|
|
|
<code src="./demo/size.tsx">三种大小</code>
|
2024-05-19 15:58:05 +08:00
|
|
|
|
<code src="./demo/need-confirm.tsx" version="5.14.0">选择确认</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/disabled.tsx">禁用</code>
|
|
|
|
|
<code src="./demo/hide-column.tsx">选择时分</code>
|
|
|
|
|
<code src="./demo/interval-options.tsx">步长选项</code>
|
|
|
|
|
<code src="./demo/addon.tsx">附加内容</code>
|
|
|
|
|
<code src="./demo/12hours.tsx">12 小时制</code>
|
2024-01-29 15:34:48 +08:00
|
|
|
|
<code src="./demo/change-on-scroll.tsx" version="5.14.0">滚动即改变</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/colored-popup.tsx" debug>色付きポップアップ</code>
|
|
|
|
|
<code src="./demo/range-picker.tsx">范围选择器</code>
|
2024-02-05 09:39:35 +08:00
|
|
|
|
<code src="./demo/variant.tsx" version="5.13.0">多种形态</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/status.tsx">自定义状态</code>
|
2024-10-23 15:00:35 +08:00
|
|
|
|
<code src="./demo/suffix.tsx">前后缀</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/render-panel.tsx" debug>_InternalPanelDoNotUseOrYouWillBeFired</code>
|
|
|
|
|
|
2016-07-26 11:03:44 +08:00
|
|
|
|
## API
|
2017-10-25 10:25:44 +08:00
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
|
---
|
2015-11-16 23:11:41 +08:00
|
|
|
|
|
2023-08-08 18:27:48 +08:00
|
|
|
|
通用属性参考:[通用属性](/docs/react/common-props)
|
|
|
|
|
|
2016-09-09 13:55:21 +08:00
|
|
|
|
```jsx
|
2022-02-14 14:40:40 +08:00
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import customParseFormat from 'dayjs/plugin/customParseFormat'
|
|
|
|
|
|
|
|
|
|
dayjs.extend(customParseFormat)
|
|
|
|
|
<TimePicker defaultValue={dayjs('13:30:56', 'HH:mm:ss')} />;
|
2015-11-16 23:11:41 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-07-11 14:14:33 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| --- | --- | --- | --- | --- |
|
2023-07-27 14:20:18 +08:00
|
|
|
|
| allowClear | 自定义清除按钮 | boolean \| { clearIcon?: ReactNode } | true | 5.8.0: 支持对象类型 |
|
2019-07-11 14:14:33 +08:00
|
|
|
|
| autoFocus | 自动获取焦点 | boolean | false | |
|
2023-06-28 10:34:25 +08:00
|
|
|
|
| cellRender | 自定义单元格的内容 | (current: number, info: { originNode: React.ReactNode, today: dayjs, range?: 'start' \| 'end', subType: 'hour' \| 'minute' \| 'second' \| 'meridiem' }) => React.ReactNode | - | 5.4.0 |
|
2024-01-29 15:34:48 +08:00
|
|
|
|
| changeOnScroll | 在滚动时改变选择值 | boolean | false | 5.14.0 |
|
2020-07-01 20:18:48 +08:00
|
|
|
|
| className | 选择器类名 | string | - | |
|
2022-02-14 14:40:40 +08:00
|
|
|
|
| defaultValue | 默认时间 | [dayjs](http://day.js.org/) | - | |
|
2019-07-11 14:14:33 +08:00
|
|
|
|
| disabled | 禁用全部操作 | boolean | false | |
|
2022-12-22 14:12:26 +08:00
|
|
|
|
| disabledTime | 不可选择的时间 | [DisabledTime](#disabledtime) | - | 4.19.0 |
|
2020-07-01 20:18:48 +08:00
|
|
|
|
| format | 展示的时间格式 | string | `HH:mm:ss` | |
|
2020-06-17 23:18:14 +08:00
|
|
|
|
| getPopupContainer | 定义浮层的容器,默认为 body 上新建 div | function(trigger) | - | |
|
2019-07-11 14:14:33 +08:00
|
|
|
|
| hideDisabledOptions | 隐藏禁止选择的选项 | boolean | false | |
|
|
|
|
|
| hourStep | 小时选项间隔 | number | 1 | |
|
2019-11-20 14:55:34 +08:00
|
|
|
|
| inputReadOnly | 设置输入框为只读(避免在移动设备上打开虚拟键盘) | boolean | false | |
|
2019-07-11 14:14:33 +08:00
|
|
|
|
| minuteStep | 分钟选项间隔 | number | 1 | |
|
2024-01-29 15:34:48 +08:00
|
|
|
|
| needConfirm | 是否需要确认按钮,为 `false` 时失去焦点即代表选择 | boolean | - | 5.14.0 |
|
2019-07-11 14:14:33 +08:00
|
|
|
|
| open | 面板是否打开 | boolean | false | |
|
2020-07-01 20:18:48 +08:00
|
|
|
|
| placeholder | 没有值的时候显示的内容 | string \| \[string, string] | `请选择时间` | |
|
2022-01-20 16:54:47 +08:00
|
|
|
|
| placement | 选择框弹出的位置 | `bottomLeft` `bottomRight` `topLeft` `topRight` | bottomLeft | |
|
2020-04-14 16:49:58 +08:00
|
|
|
|
| popupClassName | 弹出层类名 | string | - | |
|
2019-11-20 14:55:34 +08:00
|
|
|
|
| popupStyle | 弹出层样式对象 | object | - | |
|
2024-10-23 15:00:35 +08:00
|
|
|
|
| prefix | 自定义前缀 | ReactNode | - | 5.22.0 |
|
2020-10-21 10:33:43 +08:00
|
|
|
|
| renderExtraFooter | 选择框底部显示自定义的内容 | () => ReactNode | - | |
|
2019-07-11 14:14:33 +08:00
|
|
|
|
| secondStep | 秒选项间隔 | number | 1 | |
|
2020-10-21 10:33:43 +08:00
|
|
|
|
| showNow | 面板是否显示“此刻”按钮 | boolean | - | 4.4.0 |
|
2023-03-13 17:01:12 +08:00
|
|
|
|
| size | 输入框大小,`large` 高度为 40px,`small` 为 24px,默认是 32px | `large` \| `middle` \| `small` | - | |
|
2022-02-16 21:14:51 +08:00
|
|
|
|
| status | 设置校验状态 | 'error' \| 'warning' | - | 4.19.0 |
|
2019-11-20 14:55:34 +08:00
|
|
|
|
| suffixIcon | 自定义的选择框后缀图标 | ReactNode | - | |
|
2019-07-11 14:14:33 +08:00
|
|
|
|
| use12Hours | 使用 12 小时制,为 true 时 `format` 默认为 `h:mm:ss a` | boolean | false | |
|
2022-02-14 14:40:40 +08:00
|
|
|
|
| value | 当前时间 | [dayjs](http://day.js.org/) | - | |
|
2024-02-05 09:39:35 +08:00
|
|
|
|
| variant | 形态变体 | `outlined` \| `borderless` \| `filled` | `outlined` | 5.13.0 |
|
2024-03-14 21:06:08 +08:00
|
|
|
|
| onCalendarChange | 待选日期发生变化的回调。`info` 参数自 4.4.0 添加 | function(dates: \[dayjs, dayjs], dateStrings: \[string, string], info: { range:`start`\|`end` }) | - | |
|
2022-02-14 14:40:40 +08:00
|
|
|
|
| onChange | 时间发生变化的回调 | function(time: dayjs, timeString: string): void | - | |
|
2020-07-01 20:18:48 +08:00
|
|
|
|
| onOpenChange | 面板打开/关闭时的回调 | (open: boolean) => void | - | |
|
2015-11-16 23:11:41 +08:00
|
|
|
|
|
2021-12-31 11:28:02 +08:00
|
|
|
|
#### DisabledTime
|
|
|
|
|
|
|
|
|
|
```typescript
|
2022-06-13 14:03:43 +08:00
|
|
|
|
type DisabledTime = (now: Dayjs) => {
|
2021-12-31 11:28:02 +08:00
|
|
|
|
disabledHours?: () => number[];
|
|
|
|
|
disabledMinutes?: (selectedHour: number) => number[];
|
|
|
|
|
disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];
|
2024-01-29 15:34:48 +08:00
|
|
|
|
disabledMilliseconds?: (
|
|
|
|
|
selectedHour: number,
|
|
|
|
|
selectedMinute: number,
|
|
|
|
|
selectedSecond: number,
|
|
|
|
|
) => number[];
|
2021-12-31 11:28:02 +08:00
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2024-01-29 15:34:48 +08:00
|
|
|
|
注意:`disabledMilliseconds` 为 `5.14.0` 新增。
|
|
|
|
|
|
2017-12-01 19:28:41 +08:00
|
|
|
|
## 方法
|
|
|
|
|
|
2022-02-14 14:40:40 +08:00
|
|
|
|
| 名称 | 描述 | 版本 |
|
|
|
|
|
| ------- | -------- | ---- |
|
|
|
|
|
| blur() | 移除焦点 | |
|
|
|
|
|
| focus() | 获取焦点 | |
|
2017-12-01 19:28:41 +08:00
|
|
|
|
|
2021-12-31 11:28:02 +08:00
|
|
|
|
## RangePicker
|
2020-03-06 20:19:24 +08:00
|
|
|
|
|
2022-12-22 14:12:26 +08:00
|
|
|
|
属性与 DatePicker 的 [RangePicker](/components/date-picker-cn#rangepicker) 相同。还包含以下属性:
|
2020-03-06 20:19:24 +08:00
|
|
|
|
|
2021-12-31 11:28:02 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| ------------ | -------------------- | --------------------------------------- | ------ | ------ |
|
2022-12-22 14:12:26 +08:00
|
|
|
|
| disabledTime | 不可选择的时间 | [RangeDisabledTime](#rangedisabledtime) | - | 4.19.0 |
|
2021-12-31 11:28:02 +08:00
|
|
|
|
| order | 始末时间是否自动排序 | boolean | true | 4.1.0 |
|
|
|
|
|
|
|
|
|
|
### RangeDisabledTime
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
type RangeDisabledTime = (
|
2022-06-13 14:03:43 +08:00
|
|
|
|
now: Dayjs,
|
2021-12-31 11:28:02 +08:00
|
|
|
|
type = 'start' | 'end',
|
|
|
|
|
) => {
|
|
|
|
|
disabledHours?: () => number[];
|
|
|
|
|
disabledMinutes?: (selectedHour: number) => number[];
|
|
|
|
|
disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];
|
|
|
|
|
};
|
|
|
|
|
```
|
2020-03-06 20:19:24 +08:00
|
|
|
|
|
2023-08-30 23:02:35 +08:00
|
|
|
|
## 主题变量(Design Token)
|
2023-04-11 10:25:24 +08:00
|
|
|
|
|
|
|
|
|
<ComponentTokenTable component="DatePicker"></ComponentTokenTable>
|
|
|
|
|
|
2020-01-02 16:05:59 +08:00
|
|
|
|
## FAQ
|
|
|
|
|
|
2022-12-22 14:12:26 +08:00
|
|
|
|
- [如何在 TimePicker 中使用自定义日期库(如 Moment.js )](/docs/react/use-custom-date-library#timepicker)
|