]> git.immae.eu Git - github/fretlink/time-picker.git/blame - src/Combobox.jsx
Fixes #75: Disabled Hours for 12 hour picker
[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 }
f30361fe 86 let 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;
f30361fe
VA
92
93 if (!this.isAM() && Array.isArray(disabledOptions)) {
94 disabledOptions = disabledOptions.map(h => h - 12);
95 }
95699887
AS
96 } else {
97 hourOptionsAdj = hourOptions;
dd2f6abd 98 hourAdj = hour;
95699887 99 }
71bd9bc1 100
02de449a 101 return (
102 <Select
103 prefixCls={prefixCls}
95699887
AS
104 options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))}
105 selectedIndex={hourOptionsAdj.indexOf(hourAdj)}
02de449a 106 type="hour"
107 onSelect={this.onItemChange}
182e9fcc 108 onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')}
02de449a 109 />
110 );
3ab3a128 111 }
02de449a 112
113 getMinuteSelect(minute) {
37c36c09 114 const { prefixCls, minuteOptions, disabledMinutes, defaultOpenValue, showMinute } = this.props;
115 if (!showMinute) {
116 return null;
117 }
4984ed85 118 const value = this.props.value || defaultOpenValue;
119 const disabledOptions = disabledMinutes(value.hour());
71bd9bc1 120
02de449a 121 return (
122 <Select
123 prefixCls={prefixCls}
71bd9bc1 124 options={minuteOptions.map(option => formatOption(option, disabledOptions))}
02de449a 125 selectedIndex={minuteOptions.indexOf(minute)}
126 type="minute"
127 onSelect={this.onItemChange}
182e9fcc 128 onMouseEnter={this.onEnterSelectPanel.bind(this, 'minute')}
02de449a 129 />
130 );
3ab3a128 131 }
02de449a 132
182e9fcc 133 getSecondSelect(second) {
4984ed85 134 const { prefixCls, secondOptions, disabledSeconds, showSecond, defaultOpenValue } = this.props;
02de449a 135 if (!showSecond) {
136 return null;
137 }
4984ed85 138 const value = this.props.value || defaultOpenValue;
139 const disabledOptions = disabledSeconds(value.hour(), value.minute());
71bd9bc1 140
02de449a 141 return (
142 <Select
143 prefixCls={prefixCls}
71bd9bc1 144 options={secondOptions.map(option => formatOption(option, disabledOptions))}
02de449a 145 selectedIndex={secondOptions.indexOf(second)}
146 type="second"
147 onSelect={this.onItemChange}
182e9fcc 148 onMouseEnter={this.onEnterSelectPanel.bind(this, 'second')}
02de449a 149 />
150 );
3ab3a128 151 }
02de449a 152
95699887 153 getAMPMSelect() {
eb3c19e2 154 const { prefixCls, use12Hours, format } = this.props;
dd275f7d 155 if (!use12Hours) {
95699887
AS
156 return null;
157 }
eb3c19e2
AS
158
159 const AMPMOptions = ['am', 'pm'] // If format has A char, then we should uppercase AM/PM
160 .map(c => format.match(/\sA/) ? c.toUpperCase() : c)
161 .map(c => ({ value: c }));
162
dd2f6abd 163 const selected = this.isAM() ? 0 : 1;
95699887
AS
164
165 return (
166 <Select
167 prefixCls={prefixCls}
168 options={AMPMOptions}
169 selectedIndex={selected}
170 type="ampm"
171 onSelect={this.onItemChange}
172 onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')}
173 />
174 );
3ab3a128 175 }
95699887 176
dd2f6abd 177 isAM() {
4f26f23b 178 const value = (this.props.value || this.props.defaultOpenValue);
dd2f6abd 179 return value.hour() >= 0 && value.hour() < 12;
3ab3a128 180 }
dd2f6abd 181
8133e8cf 182 render() {
4984ed85 183 const { prefixCls, defaultOpenValue } = this.props;
184 const value = this.props.value || defaultOpenValue;
02de449a 185 return (
186 <div className={`${prefixCls}-combobox`}>
4984ed85 187 {this.getHourSelect(value.hour())}
188 {this.getMinuteSelect(value.minute())}
189 {this.getSecondSelect(value.second())}
95699887 190 {this.getAMPMSelect(value.hour())}
02de449a 191 </div>
192 );
3ab3a128 193 }
194}
02de449a 195
196export default Combobox;