]> git.immae.eu Git - github/fretlink/time-picker.git/blame - src/Panel.jsx
fix react createClass and PropTypes warning
[github/fretlink/time-picker.git] / src / Panel.jsx
CommitLineData
3ab3a128 1import React, { Component } from 'react';
2import PropTypes from 'prop-types';
4acbf95c
M
3import Header from './Header';
4import Combobox from './Combobox';
4984ed85 5import moment from 'moment';
c6a1a235 6import classNames from 'classnames';
02de449a 7
8function noop() {
9}
10
518b852e 11function generateOptions(length, disabledOptions, hideDisabledOptions) {
98275fd8 12 const arr = [];
518b852e 13 for (let value = 0; value < length; value++) {
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 21class 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,
b86fe1d1 42 addon: PropTypes.func,
3ab3a128 43 };
02de449a 44
3ab3a128 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 };
02de449a 56
3ab3a128 57 constructor(props) {
58 super(props);
59 this.state = {
60 value: props.value,
182e9fcc 61 selectionRange: [],
02de449a 62 };
3ab3a128 63 }
02de449a 64
9f9f39e4 65 componentWillReceiveProps(nextProps) {
66 const value = nextProps.value;
67 if (value) {
68 this.setState({
69 value,
70 });
71 }
3ab3a128 72 }
9f9f39e4 73
3ab3a128 74 onChange = (newValue) => {
02de449a 75 this.setState({ value: newValue });
76 this.props.onChange(newValue);
3ab3a128 77 }
02de449a 78
3ab3a128 79 onCurrentSelectPanelChange = (currentSelectPanel) => {
182e9fcc 80 this.setState({ currentSelectPanel });
3ab3a128 81 }
b86fe1d1 82
02de449a 83 render() {
4984ed85 84 const {
c6a1a235 85 prefixCls, className, placeholder, disabledHours, disabledMinutes,
37c36c09 86 disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
3ab3a128 87 format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear,
4984ed85 88 } = this.props;
89 const {
90 value, currentSelectPanel,
91 } = this.state;
71bd9bc1 92 const disabledHourOptions = disabledHours();
4984ed85 93 const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null);
94 const disabledSecondOptions = disabledSeconds(value ? value.hour() : null,
95 value ? value.minute() : null);
71bd9bc1
M
96 const hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions);
97 const minuteOptions = generateOptions(60, disabledMinuteOptions, hideDisabledOptions);
98 const secondOptions = generateOptions(60, disabledSecondOptions, hideDisabledOptions);
518b852e 99
02de449a 100 return (
9c01af6e 101 <div className={classNames({ [`${prefixCls}-inner`]: true, [className]: !!className })}>
02de449a 102 <Header
4984ed85 103 clearText={clearText}
02de449a 104 prefixCls={prefixCls}
4984ed85 105 defaultOpenValue={defaultOpenValue}
02de449a 106 value={value}
4984ed85 107 currentSelectPanel={currentSelectPanel}
108 onEsc={onEsc}
109 format={format}
63541ed7 110 placeholder={placeholder}
02de449a 111 hourOptions={hourOptions}
112 minuteOptions={minuteOptions}
113 secondOptions={secondOptions}
518b852e
M
114 disabledHours={disabledHours}
115 disabledMinutes={disabledMinutes}
116 disabledSeconds={disabledSeconds}
02de449a 117 onChange={this.onChange}
3ab3a128 118 onClear={onClear}
8133e8cf 119 allowEmpty={allowEmpty}
02de449a 120 />
121 <Combobox
122 prefixCls={prefixCls}
123 value={value}
4984ed85 124 defaultOpenValue={defaultOpenValue}
125 format={format}
02de449a 126 onChange={this.onChange}
8133e8cf 127 showHour={showHour}
37c36c09 128 showMinute={showMinute}
8133e8cf 129 showSecond={showSecond}
02de449a 130 hourOptions={hourOptions}
131 minuteOptions={minuteOptions}
132 secondOptions={secondOptions}
518b852e
M
133 disabledHours={disabledHours}
134 disabledMinutes={disabledMinutes}
135 disabledSeconds={disabledSeconds}
182e9fcc 136 onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
dd275f7d 137 use12Hours={use12Hours}
02de449a 138 />
b86fe1d1 139 {addon(this)}
02de449a 140 </div>
141 );
3ab3a128 142 }
143}
02de449a 144
4acbf95c 145export default Panel;