TimePicker时间选择框

当用户需要输入一个时间,可以点击标准输入框,弹出时间面板进行选择。

代码演示

点击 TimePicker,然后可以在浮层中选择或者输入某一时间。

expand codeexpand code
import { TimePicker } from 'antd';
import type { Moment } from 'moment';
import moment from 'moment';
import React from 'react';

const onChange = (time: Moment, timeString: string) => {
  console.log(time, timeString);
};

const App: React.FC = () => (
  <TimePicker onChange={onChange} defaultOpenValue={moment('00:00:00', 'HH:mm:ss')} />
);

export default App;

三种大小的输入框,大的用在表单中,中的为默认。

expand codeexpand code
import { TimePicker } from 'antd';
import moment from 'moment';
import React from 'react';

const App: React.FC = () => (
  <>
    <TimePicker defaultValue={moment('12:08:23', 'HH:mm:ss')} size="large" />
    <TimePicker defaultValue={moment('12:08:23', 'HH:mm:ss')} />
    <TimePicker defaultValue={moment('12:08:23', 'HH:mm:ss')} size="small" />
  </>
);

export default App;

TimePicker 浮层中的列会随着 format 变化,当略去 format 中的某部分时,浮层中对应的列也会消失。

expand codeexpand code
import { TimePicker } from 'antd';
import moment from 'moment';
import React from 'react';

const format = 'HH:mm';

const App: React.FC = () => <TimePicker defaultValue={moment('12:08', format)} format={format} />;

export default App;

在 TimePicker 选择框底部显示自定义的内容。

expand codeexpand code
import { Button, TimePicker } from 'antd';
import React, { useState } from 'react';

const App: React.FC = () => {
  const [open, setOpen] = useState(false);

  return (
    <TimePicker
      open={open}
      onOpenChange={setOpen}
      renderExtraFooter={() => (
        <Button size="small" type="primary" onClick={() => setOpen(false)}>
          OK
        </Button>
      )}
    />
  );
};

export default App;

通过 TimePicker.RangePicker 使用时间范围选择器。

expand codeexpand code
import { TimePicker } from 'antd';
import React from 'react';

const App: React.FC = () => <TimePicker.RangePicker />;

export default App;

使用 status 为 TimePicker 添加状态,可选 error 或者 warning

expand codeexpand code
import { Space, TimePicker } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <Space direction="vertical">
    <TimePicker status="error" />
    <TimePicker status="warning" />
    <TimePicker.RangePicker status="error" />
    <TimePicker.RangePicker status="warning" />
  </Space>
);

export default App;
4.19.0

value 和 onChange 需要配合使用。

expand codeexpand code
import { TimePicker } from 'antd';
import type { Moment } from 'moment';
import React, { useState } from 'react';

const App: React.FC = () => {
  const [value, setValue] = useState<Moment | null>(null);

  const onChange = (time: Moment) => {
    setValue(time);
  };

  return <TimePicker value={value} onChange={onChange} />;
};

export default App;

禁用时间选择。

expand codeexpand code
import { TimePicker } from 'antd';
import moment from 'moment';
import React from 'react';

const App: React.FC = () => <TimePicker defaultValue={moment('12:08:23', 'HH:mm:ss')} disabled />;

export default App;

可以使用 hourStep minuteStep secondStep 按步长展示可选的时分秒。

expand codeexpand code
import { TimePicker } from 'antd';
import React from 'react';

const App: React.FC = () => <TimePicker minuteStep={15} secondStep={10} />;

export default App;

12 小时制的时间选择器,默认的 format 为 h:mm:ss a

expand codeexpand code
import { TimePicker } from 'antd';
import type { Moment } from 'moment';
import React from 'react';

const onChange = (time: Moment, timeString: string) => {
  console.log(time, timeString);
};

const App: React.FC = () => (
  <>
    <TimePicker use12Hours onChange={onChange} />
    <TimePicker use12Hours format="h:mm:ss A" onChange={onChange} style={{ width: 140 }} />
    <TimePicker use12Hours format="h:mm a" onChange={onChange} />
  </>
);

export default App;

无边框样式。

expand codeexpand code
import { TimePicker } from 'antd';
import React from 'react';

const { RangePicker } = TimePicker;

const App: React.FC = () => (
  <>
    <TimePicker bordered={false} />
    <RangePicker bordered={false} />
  </>
);

export default App;

API#


import moment from 'moment';
<TimePicker defaultValue={moment('13:30:56', 'HH:mm:ss')} />;
参数说明类型默认值版本
allowClear是否展示清除按钮booleantrue
autoFocus自动获取焦点booleanfalse
bordered是否有边框booleantrue
className选择器类名string-
clearIcon自定义的清除图标ReactNode-
clearText清除按钮的提示文案stringclear
defaultValue默认时间moment-
disabled禁用全部操作booleanfalse
disabledTime不可选择的时间DisabledTime-4.19.0
format展示的时间格式stringHH:mm:ss
getPopupContainer定义浮层的容器,默认为 body 上新建 divfunction(trigger)-
hideDisabledOptions隐藏禁止选择的选项booleanfalse
hourStep小时选项间隔number1
inputReadOnly设置输入框为只读(避免在移动设备上打开虚拟键盘)booleanfalse
minuteStep分钟选项间隔number1
open面板是否打开booleanfalse
placeholder没有值的时候显示的内容string | [string, string]请选择时间
placement选择框弹出的位置bottomLeft bottomRight topLeft topRightbottomLeft
popupClassName弹出层类名string-
popupStyle弹出层样式对象object-
renderExtraFooter选择框底部显示自定义的内容() => ReactNode-
secondStep秒选项间隔number1
showNow面板是否显示“此刻”按钮boolean-4.4.0
status设置校验状态'error' | 'warning'-4.19.0
suffixIcon自定义的选择框后缀图标ReactNode-
use12Hours使用 12 小时制,为 true 时 format 默认为 h:mm:ss abooleanfalse
value当前时间moment-
onChange时间发生变化的回调function(time: moment, timeString: string): void-
onOpenChange面板打开/关闭时的回调(open: boolean) => void-

DisabledTime#

type DisabledTime = (now: Moment) => {
  disabledHours?: () => number[];
  disabledMinutes?: (selectedHour: number) => number[];
  disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];
};

方法#

名称描述版本
blur()移除焦点
focus()获取焦点

RangePicker#

属性与 DatePicker 的 RangePicker 相同。还包含以下属性:

参数说明类型默认值版本
disabledTime不可选择的时间RangeDisabledTime-4.19.0
order始末时间是否自动排序booleantrue4.1.0

RangeDisabledTime#

type RangeDisabledTime = (
  now: Moment,
  type = 'start' | 'end',
) => {
  disabledHours?: () => number[];
  disabledMinutes?: (selectedHour: number) => number[];
  disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];
};

FAQ#

Switch开关Transfer穿梭框