]> git.immae.eu Git - github/fretlink/time-picker.git/blob - src/Panel.jsx
fix react createClass and PropTypes warning
[github/fretlink/time-picker.git] / src / Panel.jsx
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';
7
8 function noop() {
9 }
10
11 function generateOptions(length, disabledOptions, hideDisabledOptions) {
12 const arr = [];
13 for (let value = 0; value < length; value++) {
14 if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
15 arr.push(value);
16 }
17 }
18 return arr;
19 }
20
21 class Panel extends Component {
22 static propTypes = {
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 disabledHours: PropTypes.func,
31 disabledMinutes: PropTypes.func,
32 disabledSeconds: PropTypes.func,
33 hideDisabledOptions: PropTypes.bool,
34 onChange: PropTypes.func,
35 onEsc: PropTypes.func,
36 allowEmpty: PropTypes.bool,
37 showHour: PropTypes.bool,
38 showMinute: PropTypes.bool,
39 showSecond: PropTypes.bool,
40 onClear: PropTypes.func,
41 use12Hours: PropTypes.bool,
42 addon: PropTypes.func,
43 };
44
45 static defaultProps = {
46 prefixCls: 'rc-time-picker-panel',
47 onChange: noop,
48 onClear: noop,
49 disabledHours: noop,
50 disabledMinutes: noop,
51 disabledSeconds: noop,
52 defaultOpenValue: moment(),
53 use12Hours: false,
54 addon: noop,
55 };
56
57 constructor(props) {
58 super(props);
59 this.state = {
60 value: props.value,
61 selectionRange: [],
62 };
63 }
64
65 componentWillReceiveProps(nextProps) {
66 const value = nextProps.value;
67 if (value) {
68 this.setState({
69 value,
70 });
71 }
72 }
73
74 onChange = (newValue) => {
75 this.setState({ value: newValue });
76 this.props.onChange(newValue);
77 }
78
79 onCurrentSelectPanelChange = (currentSelectPanel) => {
80 this.setState({ currentSelectPanel });
81 }
82
83 render() {
84 const {
85 prefixCls, className, placeholder, disabledHours, disabledMinutes,
86 disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
87 format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear,
88 } = this.props;
89 const {
90 value, currentSelectPanel,
91 } = this.state;
92 const disabledHourOptions = disabledHours();
93 const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null);
94 const disabledSecondOptions = disabledSeconds(value ? value.hour() : null,
95 value ? value.minute() : null);
96 const hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions);
97 const minuteOptions = generateOptions(60, disabledMinuteOptions, hideDisabledOptions);
98 const secondOptions = generateOptions(60, disabledSecondOptions, hideDisabledOptions);
99
100 return (
101 <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}>
102 <Header
103 clearText={clearText}
104 prefixCls={prefixCls}
105 defaultOpenValue={defaultOpenValue}
106 value={value}
107 currentSelectPanel={currentSelectPanel}
108 onEsc={onEsc}
109 format={format}
110 placeholder={placeholder}
111 hourOptions={hourOptions}
112 minuteOptions={minuteOptions}
113 secondOptions={secondOptions}
114 disabledHours={disabledHours}
115 disabledMinutes={disabledMinutes}
116 disabledSeconds={disabledSeconds}
117 onChange={this.onChange}
118 onClear={onClear}
119 allowEmpty={allowEmpty}
120 />
121 <Combobox
122 prefixCls={prefixCls}
123 value={value}
124 defaultOpenValue={defaultOpenValue}
125 format={format}
126 onChange={this.onChange}
127 showHour={showHour}
128 showMinute={showMinute}
129 showSecond={showSecond}
130 hourOptions={hourOptions}
131 minuteOptions={minuteOptions}
132 secondOptions={secondOptions}
133 disabledHours={disabledHours}
134 disabledMinutes={disabledMinutes}
135 disabledSeconds={disabledSeconds}
136 onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
137 use12Hours={use12Hours}
138 />
139 {addon(this)}
140 </div>
141 );
142 }
143 }
144
145 export default Panel;