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 (Array.isArray(disabledOptions)) {
95 disabledOptions = disabledOptions.filter(h => h < 12).map(h => (h === 0 ? 12 : h));
97 disabledOptions = disabledOptions.map(h => (h === 12 ? 12 : h - 12));
101 hourOptionsAdj = hourOptions;
107 prefixCls={prefixCls}
108 options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))}
109 selectedIndex={hourOptionsAdj.indexOf(hourAdj)}
111 onSelect={this.onItemChange}
112 onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')}
117 getMinuteSelect(minute) {
118 const { prefixCls, minuteOptions, disabledMinutes, defaultOpenValue, showMinute } = this.props;
122 const value = this.props.value || defaultOpenValue;
123 const disabledOptions = disabledMinutes(value.hour());
127 prefixCls={prefixCls}
128 options={minuteOptions.map(option => formatOption(option, disabledOptions))}
129 selectedIndex={minuteOptions.indexOf(minute)}
131 onSelect={this.onItemChange}
132 onMouseEnter={this.onEnterSelectPanel.bind(this, 'minute')}
137 getSecondSelect(second) {
138 const { prefixCls, secondOptions, disabledSeconds, showSecond, defaultOpenValue } = this.props;
142 const value = this.props.value || defaultOpenValue;
143 const disabledOptions = disabledSeconds(value.hour(), value.minute());
147 prefixCls={prefixCls}
148 options={secondOptions.map(option => formatOption(option, disabledOptions))}
149 selectedIndex={secondOptions.indexOf(second)}
151 onSelect={this.onItemChange}
152 onMouseEnter={this.onEnterSelectPanel.bind(this, 'second')}
158 const { prefixCls, use12Hours, format } = this.props;
163 const AMPMOptions = ['am', 'pm'] // If format has A char, then we should uppercase AM/PM
164 .map(c => format.match(/\sA/) ? c.toUpperCase() : c)
165 .map(c => ({ value: c }));
167 const selected = this.isAM() ? 0 : 1;
171 prefixCls={prefixCls}
172 options={AMPMOptions}
173 selectedIndex={selected}
175 onSelect={this.onItemChange}
176 onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')}
182 const value = (this.props.value || this.props.defaultOpenValue);
183 return value.hour() >= 0 && value.hour() < 12;
187 const { prefixCls, defaultOpenValue } = this.props;
188 const value = this.props.value || defaultOpenValue;
190 <div className={`${prefixCls}-combobox`}>
191 {this.getHourSelect(value.hour())}
192 {this.getMinuteSelect(value.minute())}
193 {this.getSecondSelect(value.second())}
194 {this.getAMPMSelect(value.hour())}
200 export default Combobox;