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 onKeyDown: PropTypes.func,
51 static defaultProps = {
52 prefixCls: 'rc-time-picker-panel',
56 disabledMinutes: noop,
57 disabledSeconds: noop,
58 defaultOpenValue: moment(),
73 componentWillReceiveProps(nextProps) {
74 const value = nextProps.value;
82 onChange = (newValue) => {
83 this.setState({ value: newValue });
84 this.props.onChange(newValue);
87 onCurrentSelectPanelChange = (currentSelectPanel) => {
88 this.setState({ currentSelectPanel });
91 // https://github.com/ant-design/ant-design/issues/5829
96 disabledHours = () => {
97 const { use12Hours, disabledHours } = this.props;
98 let disabledOptions = disabledHours();
99 if (use12Hours && Array.isArray(disabledOptions)) {
101 disabledOptions = disabledOptions.filter(h => h < 12).map(h => (h === 0 ? 12 : h));
103 disabledOptions = disabledOptions.map(h => (h === 12 ? 12 : h - 12));
106 return disabledOptions;
110 const value = (this.state.value || this.props.defaultOpenValue);
111 return value.hour() >= 0 && value.hour() < 12;
116 prefixCls, className, placeholder, disabledMinutes,
117 disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
118 format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear,
119 focusOnOpen, onKeyDown, hourStep, minuteStep, secondStep, inputReadOnly,
122 value, currentSelectPanel,
124 const disabledHourOptions = this.disabledHours();
125 const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null);
126 const disabledSecondOptions = disabledSeconds(value ? value.hour() : null,
127 value ? value.minute() : null);
128 const hourOptions = generateOptions(
129 24, disabledHourOptions, hideDisabledOptions, hourStep
131 const minuteOptions = generateOptions(
132 60, disabledMinuteOptions, hideDisabledOptions, minuteStep
134 const secondOptions = generateOptions(
135 60, disabledSecondOptions, hideDisabledOptions, secondStep
139 <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}>
141 clearText={clearText}
142 prefixCls={prefixCls}
143 defaultOpenValue={defaultOpenValue}
145 currentSelectPanel={currentSelectPanel}
148 placeholder={placeholder}
149 hourOptions={hourOptions}
150 minuteOptions={minuteOptions}
151 secondOptions={secondOptions}
152 disabledHours={this.disabledHours}
153 disabledMinutes={disabledMinutes}
154 disabledSeconds={disabledSeconds}
155 onChange={this.onChange}
157 allowEmpty={allowEmpty}
158 focusOnOpen={focusOnOpen}
159 onKeyDown={onKeyDown}
160 inputReadOnly={inputReadOnly}
163 prefixCls={prefixCls}
165 defaultOpenValue={defaultOpenValue}
167 onChange={this.onChange}
169 showMinute={showMinute}
170 showSecond={showSecond}
171 hourOptions={hourOptions}
172 minuteOptions={minuteOptions}
173 secondOptions={secondOptions}
174 disabledHours={this.disabledHours}
175 disabledMinutes={disabledMinutes}
176 disabledSeconds={disabledSeconds}
177 onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
178 use12Hours={use12Hours}
187 export default Panel;