TimePicker时间选择框
当用户需要输入一个时间,可以点击标准输入框,弹出时间面板进行选择。
代码演示
import { TimePicker } from 'antd';
import moment from 'moment';
function onChange(time, timeString) {
console.log(time, timeString);
}
ReactDOM.render(
<TimePicker onChange={onChange} defaultOpenValue={moment('00:00:00', 'HH:mm:ss')} />,
mountNode
);
import { TimePicker } from 'antd';
import moment from 'moment';
ReactDOM.render(
<div>
<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" />
</div>
, mountNode);
import { TimePicker } from 'antd';
import moment from 'moment';
const format = 'HH:mm';
ReactDOM.render(
<TimePicker defaultValue={moment('12:08', format)} format={format} />
, mountNode);
import { TimePicker, Button } from 'antd';
class TimePickerAddonDemo extends React.Component {
state = { open: false };
handleOpenChange = (open) => {
this.setState({ open });
}
handleClose = () => this.setState({ open: false })
render() {
return (
<TimePicker
open={this.state.open}
onOpenChange={this.handleOpenChange}
addon={() => (
<Button size="small" type="primary" onClick={this.handleClose}>
Ok
</Button>
)}
/>
);
}
}
ReactDOM.render(<TimePickerAddonDemo />, mountNode);
import { TimePicker } from 'antd';
class Demo extends React.Component {
state = {
value: null,
};
onChange = (time) => {
console.log(time);
this.setState({ value: time });
}
render() {
return <TimePicker value={this.state.value} onChange={this.onChange} />;
}
}
ReactDOM.render(<Demo />, mountNode);
import { TimePicker } from 'antd';
import moment from 'moment';
ReactDOM.render(
<TimePicker defaultValue={moment('12:08:23', 'HH:mm:ss')} disabled />
, mountNode);
import { TimePicker } from 'antd';
function range(start, end) {
const result = [];
for (let i = start; i < end; i++) {
result.push(i);
}
return result;
}
function disabledHours() {
const hours = range(0, 60);
hours.splice(20, 4);
return hours;
}
function disabledMinutes(h) {
if (h === 20) {
return range(0, 31);
} else if (h === 23) {
return range(30, 60);
}
return [];
}
ReactDOM.render(
<div>
<TimePicker
disabledHours={disabledHours}
disabledMinutes={disabledMinutes}
placeholder="Just Disabled"
/>
<TimePicker
disabledHours={disabledHours}
disabledMinutes={disabledMinutes}
hideDisabledOptions
placeholder="Hide Directly"
/>
</div>
, mountNode);
import { TimePicker } from 'antd';
function onChange(time, timeString) {
console.log(time, timeString);
}
ReactDOM.render(
<div>
<TimePicker use12Hours onChange={onChange} />
<TimePicker use12Hours format="h:mm:ss A" onChange={onChange} />
<TimePicker use12Hours format="h:mm a" onChange={onChange} />
</div>
, mountNode);
API#
import moment from 'moment';
<TimePicker defaultValue={moment('13:30:56', 'HH:mm:ss')} />
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
defaultValue | 默认时间 | moment | 无 |
value | 当前时间 | moment | 无 |
defaultOpenValue | 无选中值时,面板打开时高亮的值 | moment | moment() |
open | 面板是否打开 | boolean | false |
onOpenChange | 面板打开/关闭时的回调 | (open: boolean): void | 无 |
placeholder | 没有值的时候显示的内容 | string | "请选择时间" |
onChange | 时间发生变化的回调 | function(time: moment, timeString: string): void | 无 |
format | 展示的时间格式 | string | "HH:mm:ss" |
disabled | 禁用全部操作 | boolean | false |
disabledHours | 禁止选择部分小时选项 | function() | 无 |
disabledMinutes | 禁止选择部分分钟选项 | function(selectedHour) | 无 |
disabledSeconds | 禁止选择部分秒选项 | function(selectedHour, selectedMinute) | 无 |
hideDisabledOptions | 隐藏禁止选择的选项 | boolean | false |
getPopupContainer | 定义浮层的容器,默认为 body 上新建 div | function(trigger) | 无 |
addon | 选择框底部显示自定义的内容 | function | 无 |
use12Hours | 使用 12 小时制,为 true 时 format 默认为 h:mm:ss a | boolean | false |
className | 选择器类名 | string | '' |
popupClassName | 弹出层类名 | string | '' |