]> git.immae.eu Git - github/fretlink/time-picker.git/blame - src/Combobox.jsx
fix react createClass and PropTypes warning
[github/fretlink/time-picker.git] / src / Combobox.jsx
CommitLineData
3ab3a128 1import React, { Component } from 'react';
2import PropTypes from 'prop-types';
02de449a 3import Select from './Select';
4
518b852e
M
5const formatOption = (option, disabledOptions) => {
6 let value = `${option}`;
02de449a 7 if (option < 10) {
518b852e 8 value = `0${option}`;
02de449a 9 }
518b852e
M
10
11 let disabled = false;
0e62bf0b 12 if (disabledOptions && disabledOptions.indexOf(option) >= 0) {
518b852e
M
13 disabled = true;
14 }
15
16 return {
17 value,
18 disabled,
19 };
02de449a 20};
21
3ab3a128 22class Combobox extends Component {
23 static propTypes = {
4984ed85 24 format: PropTypes.string,
25 defaultOpenValue: PropTypes.object,
02de449a 26 prefixCls: PropTypes.string,
27 value: PropTypes.object,
28 onChange: PropTypes.func,
29 showHour: PropTypes.bool,
37c36c09 30 showMinute: PropTypes.bool,
02de449a 31 showSecond: PropTypes.bool,
32 hourOptions: PropTypes.array,
33 minuteOptions: PropTypes.array,
34 secondOptions: PropTypes.array,
71bd9bc1
M
35 disabledHours: PropTypes.func,
36 disabledMinutes: PropTypes.func,
37 disabledSeconds: PropTypes.func,
182e9fcc 38 onCurrentSelectPanelChange: PropTypes.func,
dd275f7d 39 use12Hours: PropTypes.bool,
3ab3a128 40 };
02de449a 41
3ab3a128 42 onItemChange = (type, itemValue) => {
dd275f7d 43 const { onChange, defaultOpenValue, use12Hours } = this.props;
4984ed85 44 const value = (this.props.value || defaultOpenValue).clone();
95699887 45
11b97949 46 if (type === 'hour') {
dd275f7d 47 if (use12Hours) {
dd2f6abd
AS
48 if (this.isAM()) {
49 value.hour(+itemValue % 12);
95699887 50 } else {
dd2f6abd 51 value.hour((+itemValue % 12) + 12);
95699887
AS
52 }
53 } else {
54 value.hour(+itemValue);
55 }
11b97949 56 } else if (type === 'minute') {
95699887
AS
57 value.minute(+itemValue);
58 } else if (type === 'ampm') {
c1b40cab 59 const ampm = itemValue.toUpperCase();
dd275f7d 60 if (use12Hours) {
c1b40cab 61 if (ampm === 'PM' && value.hour() < 12) {
2a8cf5ae 62 value.hour((value.hour() % 12) + 12);
95699887
AS
63 }
64
c1b40cab 65 if (ampm === 'AM') {
2a8cf5ae 66 if (value.hour() >= 12) {
95699887
AS
67 value.hour(value.hour() - 12);
68 }
69 }
70 }
11b97949 71 } else {
95699887 72 value.second(+itemValue);
02de449a 73 }
02de449a 74 onChange(value);
3ab3a128 75 }
02de449a 76
3ab3a128 77 onEnterSelectPanel = (range) => {
182e9fcc 78 this.props.onCurrentSelectPanelChange(range);
3ab3a128 79 }
182e9fcc 80
02de449a 81 getHourSelect(hour) {
dd275f7d 82 const { prefixCls, hourOptions, disabledHours, showHour, use12Hours } = this.props;
02de449a 83 if (!showHour) {
84 return null;
85 }
71bd9bc1 86 const disabledOptions = disabledHours();
95699887 87 let hourOptionsAdj;
dd2f6abd 88 let hourAdj;
dd275f7d 89 if (use12Hours) {
dd2f6abd
AS
90 hourOptionsAdj = [12].concat(hourOptions.filter(h => h < 12 && h > 0));
91 hourAdj = (hour % 12) || 12;
95699887
AS
92 } else {
93 hourOptionsAdj = hourOptions;
dd2f6abd 94 hourAdj = hour;
95699887 95 }
71bd9bc1 96
02de449a 97 return (
98 <Select
99 prefixCls={prefixCls}
95699887
AS
100 options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))}
101 selectedIndex={hourOptionsAdj.indexOf(hourAdj)}
02de449a 102 type="hour"
103 onSelect={this.onItemChange}
182e9fcc 104 onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')}
02de449a 105 />
106 );
3ab3a128 107 }
02de449a 108
109 getMinuteSelect(minute) {
37c36c09 110 const { prefixCls, minuteOptions, disabledMinutes, defaultOpenValue, showMinute } = this.props;
111 if (!showMinute) {
112 return null;
113 }
4984ed85 114 const value = this.props.value || defaultOpenValue;
115 const disabledOptions = disabledMinutes(value.hour());
71bd9bc1 116
02de449a 117 return (
118 <Select
119 prefixCls={prefixCls}
71bd9bc1 120 options={minuteOptions.map(option => formatOption(option, disabledOptions))}
02de449a 121 selectedIndex={minuteOptions.indexOf(minute)}
122 type="minute"
123 onSelect={this.onItemChange}
182e9fcc 124 onMouseEnter={this.onEnterSelectPanel.bind(this, 'minute')}
02de449a 125 />
126 );
3ab3a128 127 }
02de449a 128
182e9fcc 129 getSecondSelect(second) {
4984ed85 130 const { prefixCls, secondOptions, disabledSeconds, showSecond, defaultOpenValue } = this.props;
02de449a 131 if (!showSecond) {
132 return null;
133 }
4984ed85 134 const value = this.props.value || defaultOpenValue;
135 const disabledOptions = disabledSeconds(value.hour(), value.minute());
71bd9bc1 136
02de449a 137 return (
138 <Select
139 prefixCls={prefixCls}
71bd9bc1 140 options={secondOptions.map(option => formatOption(option, disabledOptions))}
02de449a 141 selectedIndex={secondOptions.indexOf(second)}
142 type="second"
143 onSelect={this.onItemChange}
182e9fcc 144 onMouseEnter={this.onEnterSelectPanel.bind(this, 'second')}
02de449a 145 />
146 );
3ab3a128 147 }
02de449a 148
95699887 149 getAMPMSelect() {
eb3c19e2 150 const { prefixCls, use12Hours, format } = this.props;
dd275f7d 151 if (!use12Hours) {
95699887
AS
152 return null;
153 }
eb3c19e2
AS
154
155 const AMPMOptions = ['am', 'pm'] // If format has A char, then we should uppercase AM/PM
156 .map(c => format.match(/\sA/) ? c.toUpperCase() : c)
157 .map(c => ({ value: c }));
158
dd2f6abd 159 const selected = this.isAM() ? 0 : 1;
95699887
AS
160
161 return (
162 <Select
163 prefixCls={prefixCls}
164 options={AMPMOptions}
165 selectedIndex={selected}
166 type="ampm"
167 onSelect={this.onItemChange}
168 onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')}
169 />
170 );
3ab3a128 171 }
95699887 172
dd2f6abd 173 isAM() {
4f26f23b 174 const value = (this.props.value || this.props.defaultOpenValue);
dd2f6abd 175 return value.hour() >= 0 && value.hour() < 12;
3ab3a128 176 }
dd2f6abd 177
8133e8cf 178 render() {
4984ed85 179 const { prefixCls, defaultOpenValue } = this.props;
180 const value = this.props.value || defaultOpenValue;
02de449a 181 return (
182 <div className={`${prefixCls}-combobox`}>
4984ed85 183 {this.getHourSelect(value.hour())}
184 {this.getMinuteSelect(value.minute())}
185 {this.getSecondSelect(value.second())}
95699887 186 {this.getAMPMSelect(value.hour())}
02de449a 187 </div>
188 );
3ab3a128 189 }
190}
02de449a 191
192export default Combobox;