]>
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, |
4e70ea0d | 30 | inputReadOnly: PropTypes.bool, |
71bd9bc1 M |
31 | disabledHours: PropTypes.func, |
32 | disabledMinutes: PropTypes.func, | |
33 | disabledSeconds: PropTypes.func, | |
518b852e | 34 | hideDisabledOptions: PropTypes.bool, |
02de449a | 35 | onChange: PropTypes.func, |
8133e8cf | 36 | onEsc: PropTypes.func, |
37 | allowEmpty: PropTypes.bool, | |
38 | showHour: PropTypes.bool, | |
37c36c09 | 39 | showMinute: PropTypes.bool, |
8133e8cf | 40 | showSecond: PropTypes.bool, |
02de449a | 41 | onClear: PropTypes.func, |
dd275f7d | 42 | use12Hours: PropTypes.bool, |
5827568e MP |
43 | hourStep: PropTypes.number, |
44 | minuteStep: PropTypes.number, | |
45 | secondStep: PropTypes.number, | |
b86fe1d1 | 46 | addon: PropTypes.func, |
d18ecfb6 | 47 | focusOnOpen: PropTypes.bool, |
3931819e | 48 | closeOnEsc: PropTypes.bool, |
0e4fd162 | 49 | onKeyDown: PropTypes.func, |
3ab3a128 | 50 | }; |
02de449a | 51 | |
3ab3a128 | 52 | static defaultProps = { |
53 | prefixCls: 'rc-time-picker-panel', | |
54 | onChange: noop, | |
55 | onClear: noop, | |
56 | disabledHours: noop, | |
57 | disabledMinutes: noop, | |
58 | disabledSeconds: noop, | |
59 | defaultOpenValue: moment(), | |
60 | use12Hours: false, | |
61 | addon: noop, | |
0e4fd162 | 62 | onKeyDown: noop, |
4e70ea0d | 63 | inputReadOnly: false, |
3931819e | 64 | closeOnEsc: true, |
3ab3a128 | 65 | }; |
02de449a | 66 | |
3ab3a128 | 67 | constructor(props) { |
68 | super(props); | |
69 | this.state = { | |
70 | value: props.value, | |
182e9fcc | 71 | selectionRange: [], |
02de449a | 72 | }; |
3ab3a128 | 73 | } |
02de449a | 74 | |
9f9f39e4 | 75 | componentWillReceiveProps(nextProps) { |
76 | const value = nextProps.value; | |
77 | if (value) { | |
78 | this.setState({ | |
79 | value, | |
80 | }); | |
81 | } | |
3ab3a128 | 82 | } |
9f9f39e4 | 83 | |
3ab3a128 | 84 | onChange = (newValue) => { |
02de449a | 85 | this.setState({ value: newValue }); |
86 | this.props.onChange(newValue); | |
3ab3a128 | 87 | } |
02de449a | 88 | |
3ab3a128 | 89 | onCurrentSelectPanelChange = (currentSelectPanel) => { |
182e9fcc | 90 | this.setState({ currentSelectPanel }); |
3ab3a128 | 91 | } |
b86fe1d1 | 92 | |
9b780804 | 93 | // https://github.com/ant-design/ant-design/issues/5829 |
94 | close() { | |
95 | this.props.onEsc(); | |
96 | } | |
97 | ||
7211e9ba WZ |
98 | disabledHours = () => { |
99 | const { use12Hours, disabledHours } = this.props; | |
100 | let disabledOptions = disabledHours(); | |
101 | if (use12Hours && Array.isArray(disabledOptions)) { | |
102 | if (this.isAM()) { | |
103 | disabledOptions = disabledOptions.filter(h => h < 12).map(h => (h === 0 ? 12 : h)); | |
104 | } else { | |
105 | disabledOptions = disabledOptions.map(h => (h === 12 ? 12 : h - 12)); | |
106 | } | |
107 | } | |
108 | return disabledOptions; | |
109 | } | |
110 | ||
111 | isAM() { | |
112 | const value = (this.state.value || this.props.defaultOpenValue); | |
113 | return value.hour() >= 0 && value.hour() < 12; | |
114 | } | |
115 | ||
02de449a | 116 | render() { |
4984ed85 | 117 | const { |
7211e9ba | 118 | prefixCls, className, placeholder, disabledMinutes, |
37c36c09 | 119 | disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, |
f0ac29c6 | 120 | format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear, |
4e70ea0d | 121 | focusOnOpen, onKeyDown, hourStep, minuteStep, secondStep, inputReadOnly, |
3931819e | 122 | closeOnEsc, |
4984ed85 | 123 | } = this.props; |
124 | const { | |
125 | value, currentSelectPanel, | |
126 | } = this.state; | |
7211e9ba | 127 | const disabledHourOptions = this.disabledHours(); |
4984ed85 | 128 | const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null); |
129 | const disabledSecondOptions = disabledSeconds(value ? value.hour() : null, | |
130 | value ? value.minute() : null); | |
5827568e MP |
131 | const hourOptions = generateOptions( |
132 | 24, disabledHourOptions, hideDisabledOptions, hourStep | |
133 | ); | |
134 | const minuteOptions = generateOptions( | |
135 | 60, disabledMinuteOptions, hideDisabledOptions, minuteStep | |
136 | ); | |
137 | const secondOptions = generateOptions( | |
138 | 60, disabledSecondOptions, hideDisabledOptions, secondStep | |
139 | ); | |
518b852e | 140 | |
02de449a | 141 | return ( |
9c01af6e | 142 | <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}> |
02de449a | 143 | <Header |
4984ed85 | 144 | clearText={clearText} |
02de449a | 145 | prefixCls={prefixCls} |
4984ed85 | 146 | defaultOpenValue={defaultOpenValue} |
02de449a | 147 | value={value} |
4984ed85 | 148 | currentSelectPanel={currentSelectPanel} |
3931819e | 149 | onEsc={closeOnEsc ? onEsc : undefined} |
4984ed85 | 150 | format={format} |
63541ed7 | 151 | placeholder={placeholder} |
02de449a | 152 | hourOptions={hourOptions} |
153 | minuteOptions={minuteOptions} | |
154 | secondOptions={secondOptions} | |
7211e9ba | 155 | disabledHours={this.disabledHours} |
518b852e M |
156 | disabledMinutes={disabledMinutes} |
157 | disabledSeconds={disabledSeconds} | |
02de449a | 158 | onChange={this.onChange} |
3ab3a128 | 159 | onClear={onClear} |
8133e8cf | 160 | allowEmpty={allowEmpty} |
d18ecfb6 | 161 | focusOnOpen={focusOnOpen} |
0e4fd162 | 162 | onKeyDown={onKeyDown} |
4e70ea0d | 163 | inputReadOnly={inputReadOnly} |
02de449a | 164 | /> |
165 | <Combobox | |
166 | prefixCls={prefixCls} | |
167 | value={value} | |
4984ed85 | 168 | defaultOpenValue={defaultOpenValue} |
169 | format={format} | |
02de449a | 170 | onChange={this.onChange} |
8133e8cf | 171 | showHour={showHour} |
37c36c09 | 172 | showMinute={showMinute} |
8133e8cf | 173 | showSecond={showSecond} |
02de449a | 174 | hourOptions={hourOptions} |
175 | minuteOptions={minuteOptions} | |
176 | secondOptions={secondOptions} | |
7211e9ba | 177 | disabledHours={this.disabledHours} |
518b852e M |
178 | disabledMinutes={disabledMinutes} |
179 | disabledSeconds={disabledSeconds} | |
182e9fcc | 180 | onCurrentSelectPanelChange={this.onCurrentSelectPanelChange} |
dd275f7d | 181 | use12Hours={use12Hours} |
7211e9ba | 182 | isAM={this.isAM()} |
02de449a | 183 | /> |
b86fe1d1 | 184 | {addon(this)} |
02de449a | 185 | </div> |
186 | ); | |
3ab3a128 | 187 | } |
188 | } | |
02de449a | 189 | |
4acbf95c | 190 | export default Panel; |