1 import React, { Component } from 'react';
2 import PropTypes from 'prop-types';
3 import Header from './Header';
4 import Combobox from './Combobox';
5 import moment from 'moment';
6 import classNames from 'classnames';
11 function generateOptions(length, disabledOptions, hideDisabledOptions, step = 1) {
13 for (let value = 0; value < length; value += step) {
14 if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
21 class Panel extends Component {
23 clearText: PropTypes.string,
24 prefixCls: PropTypes.string,
25 className: PropTypes.string,
26 defaultOpenValue: PropTypes.object,
27 value: PropTypes.object,
28 placeholder: PropTypes.string,
29 format: PropTypes.string,
30 inputReadOnly: PropTypes.bool,
31 disabledHours: PropTypes.func,
32 disabledMinutes: PropTypes.func,
33 disabledSeconds: PropTypes.func,
34 hideDisabledOptions: PropTypes.bool,
35 onChange: PropTypes.func,
36 onEsc: PropTypes.func,
37 allowEmpty: PropTypes.bool,
38 showHour: PropTypes.bool,
39 showMinute: PropTypes.bool,
40 showSecond: PropTypes.bool,
41 onClear: PropTypes.func,
42 use12Hours: PropTypes.bool,
43 hourStep: PropTypes.number,
44 minuteStep: PropTypes.number,
45 secondStep: PropTypes.number,
46 addon: PropTypes.func,
47 focusOnOpen: PropTypes.bool,
48 closeOnEsc: PropTypes.bool,
49 onKeyDown: PropTypes.func,
52 static defaultProps = {
53 prefixCls: 'rc-time-picker-panel',
57 disabledMinutes: noop,
58 disabledSeconds: noop,
59 defaultOpenValue: moment(),
75 componentWillReceiveProps(nextProps) {
76 const value = nextProps.value;
84 onChange = (newValue) => {
85 this.setState({ value: newValue });
86 this.props.onChange(newValue);
89 onCurrentSelectPanelChange = (currentSelectPanel) => {
90 this.setState({ currentSelectPanel });
93 // https://github.com/ant-design/ant-design/issues/5829
98 disabledHours = () => {
99 const { use12Hours, disabledHours } = this.props;
100 let disabledOptions = disabledHours();
101 if (use12Hours && Array.isArray(disabledOptions)) {
103 disabledOptions = disabledOptions.filter(h => h < 12).map(h => (h === 0 ? 12 : h));
105 disabledOptions = disabledOptions.map(h => (h === 12 ? 12 : h - 12));
108 return disabledOptions;
112 const value = (this.state.value || this.props.defaultOpenValue);
113 return value.hour() >= 0 && value.hour() < 12;
118 prefixCls, className, placeholder, disabledMinutes,
119 disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
120 format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear,
121 focusOnOpen, onKeyDown, hourStep, minuteStep, secondStep, inputReadOnly,
125 value, currentSelectPanel,
127 const disabledHourOptions = this.disabledHours();
128 const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null);
129 const disabledSecondOptions = disabledSeconds(value ? value.hour() : null,
130 value ? value.minute() : null);
131 const hourOptions = generateOptions(
132 24, disabledHourOptions, hideDisabledOptions, hourStep
134 const minuteOptions = generateOptions(
135 60, disabledMinuteOptions, hideDisabledOptions, minuteStep
137 const secondOptions = generateOptions(
138 60, disabledSecondOptions, hideDisabledOptions, secondStep
142 <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}>
144 clearText={clearText}
145 prefixCls={prefixCls}
146 defaultOpenValue={defaultOpenValue}
148 currentSelectPanel={currentSelectPanel}
149 onEsc={closeOnEsc ? onEsc : undefined}
151 placeholder={placeholder}
152 hourOptions={hourOptions}
153 minuteOptions={minuteOptions}
154 secondOptions={secondOptions}
155 disabledHours={this.disabledHours}
156 disabledMinutes={disabledMinutes}
157 disabledSeconds={disabledSeconds}
158 onChange={this.onChange}
160 allowEmpty={allowEmpty}
161 focusOnOpen={focusOnOpen}
162 onKeyDown={onKeyDown}
163 inputReadOnly={inputReadOnly}
166 prefixCls={prefixCls}
168 defaultOpenValue={defaultOpenValue}
170 onChange={this.onChange}
172 showMinute={showMinute}
173 showSecond={showSecond}
174 hourOptions={hourOptions}
175 minuteOptions={minuteOptions}
176 secondOptions={secondOptions}
177 disabledHours={this.disabledHours}
178 disabledMinutes={disabledMinutes}
179 disabledSeconds={disabledSeconds}
180 onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
181 use12Hours={use12Hours}
190 export default Panel;