]>
Commit | Line | Data |
---|---|---|
3ab3a128 | 1 | import React, { Component } from 'react'; |
2 | import PropTypes from 'prop-types'; | |
4acbf95c M |
3 | import Header from './Header'; |
4 | import Combobox from './Combobox'; | |
4984ed85 | 5 | import moment from 'moment'; |
c6a1a235 | 6 | import classNames from 'classnames'; |
02de449a | 7 | |
8 | function noop() { | |
9 | } | |
10 | ||
5827568e | 11 | function generateOptions(length, disabledOptions, hideDisabledOptions, step = 1) { |
98275fd8 | 12 | const arr = []; |
5827568e | 13 | for (let value = 0; value < length; value += step) { |
0e62bf0b | 14 | if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) { |
518b852e M |
15 | arr.push(value); |
16 | } | |
98275fd8 | 17 | } |
18 | return arr; | |
02de449a | 19 | } |
20 | ||
3ab3a128 | 21 | class Panel extends Component { |
22 | static propTypes = { | |
4984ed85 | 23 | clearText: PropTypes.string, |
02de449a | 24 | prefixCls: PropTypes.string, |
c6a1a235 | 25 | className: PropTypes.string, |
4984ed85 | 26 | defaultOpenValue: PropTypes.object, |
9f9f39e4 | 27 | value: PropTypes.object, |
02de449a | 28 | placeholder: PropTypes.string, |
4984ed85 | 29 | format: PropTypes.string, |
71bd9bc1 M |
30 | disabledHours: PropTypes.func, |
31 | disabledMinutes: PropTypes.func, | |
32 | disabledSeconds: PropTypes.func, | |
518b852e | 33 | hideDisabledOptions: PropTypes.bool, |
02de449a | 34 | onChange: PropTypes.func, |
8133e8cf | 35 | onEsc: PropTypes.func, |
36 | allowEmpty: PropTypes.bool, | |
37 | showHour: PropTypes.bool, | |
37c36c09 | 38 | showMinute: PropTypes.bool, |
8133e8cf | 39 | showSecond: PropTypes.bool, |
02de449a | 40 | onClear: PropTypes.func, |
dd275f7d | 41 | use12Hours: PropTypes.bool, |
5827568e MP |
42 | hourStep: PropTypes.number, |
43 | minuteStep: PropTypes.number, | |
44 | secondStep: PropTypes.number, | |
b86fe1d1 | 45 | addon: PropTypes.func, |
3ab3a128 | 46 | }; |
02de449a | 47 | |
3ab3a128 | 48 | static defaultProps = { |
49 | prefixCls: 'rc-time-picker-panel', | |
50 | onChange: noop, | |
51 | onClear: noop, | |
52 | disabledHours: noop, | |
53 | disabledMinutes: noop, | |
54 | disabledSeconds: noop, | |
55 | defaultOpenValue: moment(), | |
56 | use12Hours: false, | |
57 | addon: noop, | |
58 | }; | |
02de449a | 59 | |
3ab3a128 | 60 | constructor(props) { |
61 | super(props); | |
62 | this.state = { | |
63 | value: props.value, | |
182e9fcc | 64 | selectionRange: [], |
02de449a | 65 | }; |
3ab3a128 | 66 | } |
02de449a | 67 | |
9f9f39e4 | 68 | componentWillReceiveProps(nextProps) { |
69 | const value = nextProps.value; | |
70 | if (value) { | |
71 | this.setState({ | |
72 | value, | |
73 | }); | |
74 | } | |
3ab3a128 | 75 | } |
9f9f39e4 | 76 | |
3ab3a128 | 77 | onChange = (newValue) => { |
02de449a | 78 | this.setState({ value: newValue }); |
79 | this.props.onChange(newValue); | |
3ab3a128 | 80 | } |
02de449a | 81 | |
3ab3a128 | 82 | onCurrentSelectPanelChange = (currentSelectPanel) => { |
182e9fcc | 83 | this.setState({ currentSelectPanel }); |
3ab3a128 | 84 | } |
b86fe1d1 | 85 | |
9b780804 | 86 | // https://github.com/ant-design/ant-design/issues/5829 |
87 | close() { | |
88 | this.props.onEsc(); | |
89 | } | |
90 | ||
02de449a | 91 | render() { |
4984ed85 | 92 | const { |
c6a1a235 | 93 | prefixCls, className, placeholder, disabledHours, disabledMinutes, |
37c36c09 | 94 | disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, |
3ab3a128 | 95 | format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear, |
5827568e | 96 | hourStep, minuteStep, secondStep, |
4984ed85 | 97 | } = this.props; |
98 | const { | |
99 | value, currentSelectPanel, | |
100 | } = this.state; | |
71bd9bc1 | 101 | const disabledHourOptions = disabledHours(); |
4984ed85 | 102 | const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null); |
103 | const disabledSecondOptions = disabledSeconds(value ? value.hour() : null, | |
104 | value ? value.minute() : null); | |
5827568e MP |
105 | const hourOptions = generateOptions( |
106 | 24, disabledHourOptions, hideDisabledOptions, hourStep | |
107 | ); | |
108 | const minuteOptions = generateOptions( | |
109 | 60, disabledMinuteOptions, hideDisabledOptions, minuteStep | |
110 | ); | |
111 | const secondOptions = generateOptions( | |
112 | 60, disabledSecondOptions, hideDisabledOptions, secondStep | |
113 | ); | |
518b852e | 114 | |
02de449a | 115 | return ( |
9c01af6e | 116 | <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}> |
02de449a | 117 | <Header |
4984ed85 | 118 | clearText={clearText} |
02de449a | 119 | prefixCls={prefixCls} |
4984ed85 | 120 | defaultOpenValue={defaultOpenValue} |
02de449a | 121 | value={value} |
4984ed85 | 122 | currentSelectPanel={currentSelectPanel} |
123 | onEsc={onEsc} | |
124 | format={format} | |
63541ed7 | 125 | placeholder={placeholder} |
02de449a | 126 | hourOptions={hourOptions} |
127 | minuteOptions={minuteOptions} | |
128 | secondOptions={secondOptions} | |
518b852e M |
129 | disabledHours={disabledHours} |
130 | disabledMinutes={disabledMinutes} | |
131 | disabledSeconds={disabledSeconds} | |
02de449a | 132 | onChange={this.onChange} |
3ab3a128 | 133 | onClear={onClear} |
8133e8cf | 134 | allowEmpty={allowEmpty} |
02de449a | 135 | /> |
136 | <Combobox | |
137 | prefixCls={prefixCls} | |
138 | value={value} | |
4984ed85 | 139 | defaultOpenValue={defaultOpenValue} |
140 | format={format} | |
02de449a | 141 | onChange={this.onChange} |
8133e8cf | 142 | showHour={showHour} |
37c36c09 | 143 | showMinute={showMinute} |
8133e8cf | 144 | showSecond={showSecond} |
02de449a | 145 | hourOptions={hourOptions} |
146 | minuteOptions={minuteOptions} | |
147 | secondOptions={secondOptions} | |
518b852e M |
148 | disabledHours={disabledHours} |
149 | disabledMinutes={disabledMinutes} | |
150 | disabledSeconds={disabledSeconds} | |
182e9fcc | 151 | onCurrentSelectPanelChange={this.onCurrentSelectPanelChange} |
dd275f7d | 152 | use12Hours={use12Hours} |
02de449a | 153 | /> |
b86fe1d1 | 154 | {addon(this)} |
02de449a | 155 | </div> |
156 | ); | |
3ab3a128 | 157 | } |
158 | } | |
02de449a | 159 | |
4acbf95c | 160 | export default Panel; |