ant-design/components/time-picker/demo/disable-options.md

63 lines
1.3 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 5
title:
2016-07-26 11:03:44 +08:00
zh-CN: 禁止选项
2016-11-11 16:06:11 +08:00
en-US: Disabled Time
2016-03-31 09:40:55 +08:00
---
2016-07-26 11:03:44 +08:00
## zh-CN
2015-12-16 17:29:28 +08:00
2016-11-11 16:06:11 +08:00
可以使用 `disabledHours` `disabledMinutes` `disabledSeconds` 组合禁止用户选择某个时间,配合 `hideDisabledOptions` 可以直接把不可选择的项隐藏。
2015-12-16 17:29:28 +08:00
## en-US
2016-07-26 11:03:44 +08:00
2016-11-11 16:06:11 +08:00
Make part of time unselectable by `disabledHours` `disabledMinutes` `disabledSeconds`, and we even can hide those unselectable options by `hideDisabledOptions`.
2016-07-26 11:03:44 +08:00
2017-01-19 15:19:03 +08:00
````__react
2015-12-16 17:29:28 +08:00
import { TimePicker } from 'antd';
2016-11-11 16:06:11 +08:00
function range(start, end) {
2016-07-21 18:05:15 +08:00
const result = [];
2015-12-17 16:12:17 +08:00
for (let i = start; i < end; i++) {
result.push(i);
}
return result;
2015-12-16 17:29:28 +08:00
}
2015-12-17 16:12:17 +08:00
function disabledHours() {
2016-11-11 16:06:11 +08:00
const hours = range(0, 60);
2015-12-17 16:12:17 +08:00
hours.splice(20, 4);
return hours;
2015-12-16 17:29:28 +08:00
}
2015-12-17 16:12:17 +08:00
function disabledMinutes(h) {
if (h === 20) {
2016-11-11 16:06:11 +08:00
return range(0, 31);
2015-12-17 16:12:17 +08:00
} else if (h === 23) {
2016-11-11 16:06:11 +08:00
return range(30, 60);
2015-12-17 16:12:17 +08:00
}
return [];
2015-12-16 17:29:28 +08:00
}
ReactDOM.render(
2016-11-11 16:06:11 +08:00
<div>
<TimePicker
disabledHours={disabledHours}
disabledMinutes={disabledMinutes}
placeholder="Just Disabled"
/>
<TimePicker
disabledHours={disabledHours}
disabledMinutes={disabledMinutes}
hideDisabledOptions
placeholder="Hide Directly"
/>
2016-11-22 14:59:09 +08:00
</div>
, mountNode);
2015-12-16 17:29:28 +08:00
````
<style>
#components-time-picker-demo-disable-options .ant-time-picker {
width: 120px;
}
</style>