1 import React, { Component } from 'react';
2 import PropTypes from 'prop-types';
3 import Select from './Select';
5 const formatOption = (option, disabledOptions) => {
6 let value = `${option}`;
12 if (disabledOptions && disabledOptions.indexOf(option) >= 0) {
22 class Combobox extends Component {
24 format: PropTypes.string,
25 defaultOpenValue: PropTypes.object,
26 prefixCls: PropTypes.string,
27 value: PropTypes.object,
28 onChange: PropTypes.func,
29 showHour: PropTypes.bool,
30 showMinute: PropTypes.bool,
31 showSecond: PropTypes.bool,
32 hourOptions: PropTypes.array,
33 minuteOptions: PropTypes.array,
34 secondOptions: PropTypes.array,
35 disabledHours: PropTypes.func,
36 disabledMinutes: PropTypes.func,
37 disabledSeconds: PropTypes.func,
38 onCurrentSelectPanelChange: PropTypes.func,
39 use12Hours: PropTypes.bool,
42 onItemChange = (type, itemValue) => {
43 const { onChange, defaultOpenValue, use12Hours } = this.props;
44 const value = (this.props.value || defaultOpenValue).clone();
46 if (type === 'hour') {
49 value.hour(+itemValue % 12);
51 value.hour((+itemValue % 12) + 12);
54 value.hour(+itemValue);
56 } else if (type === 'minute') {
57 value.minute(+itemValue);
58 } else if (type === 'ampm') {
59 const ampm = itemValue.toUpperCase();
61 if (ampm === 'PM' && value.hour() < 12) {
62 value.hour((value.hour() % 12) + 12);
66 if (value.hour() >= 12) {
67 value.hour(value.hour() - 12);
72 value.second(+itemValue);
77 onEnterSelectPanel = (range) => {
78 this.props.onCurrentSelectPanelChange(range);
82 const { prefixCls, hourOptions, disabledHours, showHour, use12Hours } = this.props;
86 let disabledOptions = disabledHours();
90 hourOptionsAdj = [12].concat(hourOptions.filter(h => h < 12 && h > 0));
91 hourAdj = (hour % 12) || 12;
93 if (!this.isAM() && Array.isArray(disabledOptions)) {
94 disabledOptions = disabledOptions.map(h => h - 12);
97 hourOptionsAdj = hourOptions;
103 prefixCls={prefixCls}
104 options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))}
105 selectedIndex={hourOptionsAdj.indexOf(hourAdj)}
107 onSelect={this.onItemChange}
108 onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')}
113 getMinuteSelect(minute) {
114 const { prefixCls, minuteOptions, disabledMinutes, defaultOpenValue, showMinute } = this.props;
118 const value = this.props.value || defaultOpenValue;
119 const disabledOptions = disabledMinutes(value.hour());
123 prefixCls={prefixCls}
124 options={minuteOptions.map(option => formatOption(option, disabledOptions))}
125 selectedIndex={minuteOptions.indexOf(minute)}
127 onSelect={this.onItemChange}
128 onMouseEnter={this.onEnterSelectPanel.bind(this, 'minute')}
133 getSecondSelect(second) {
134 const { prefixCls, secondOptions, disabledSeconds, showSecond, defaultOpenValue } = this.props;
138 const value = this.props.value || defaultOpenValue;
139 const disabledOptions = disabledSeconds(value.hour(), value.minute());
143 prefixCls={prefixCls}
144 options={secondOptions.map(option => formatOption(option, disabledOptions))}
145 selectedIndex={secondOptions.indexOf(second)}
147 onSelect={this.onItemChange}
148 onMouseEnter={this.onEnterSelectPanel.bind(this, 'second')}
154 const { prefixCls, use12Hours, format } = this.props;
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 }));
163 const selected = this.isAM() ? 0 : 1;
167 prefixCls={prefixCls}
168 options={AMPMOptions}
169 selectedIndex={selected}
171 onSelect={this.onItemChange}
172 onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')}
178 const value = (this.props.value || this.props.defaultOpenValue);
179 return value.hour() >= 0 && value.hour() < 12;
183 const { prefixCls, defaultOpenValue } = this.props;
184 const value = this.props.value || defaultOpenValue;
186 <div className={`${prefixCls}-combobox`}>
187 {this.getHourSelect(value.hour())}
188 {this.getMinuteSelect(value.minute())}
189 {this.getSecondSelect(value.second())}
190 {this.getAMPMSelect(value.hour())}
196 export default Combobox;