1 import React, { PropTypes } from 'react';
2 import Select from './Select';
4 const formatOption = (option, disabledOptions) => {
5 let value = `${option}`;
11 if (disabledOptions && disabledOptions.indexOf(option) >= 0) {
21 const Combobox = React.createClass({
23 format: PropTypes.string,
24 defaultOpenValue: PropTypes.object,
25 prefixCls: PropTypes.string,
26 value: PropTypes.object,
27 onChange: PropTypes.func,
28 showHour: PropTypes.bool,
29 showMinute: PropTypes.bool,
30 showSecond: PropTypes.bool,
31 hourOptions: PropTypes.array,
32 minuteOptions: PropTypes.array,
33 secondOptions: PropTypes.array,
34 disabledHours: PropTypes.func,
35 disabledMinutes: PropTypes.func,
36 disabledSeconds: PropTypes.func,
37 onCurrentSelectPanelChange: PropTypes.func,
38 use12Hours: PropTypes.bool,
41 onItemChange(type, itemValue) {
42 const { onChange, defaultOpenValue, use12Hours } = this.props;
43 const value = (this.props.value || defaultOpenValue).clone();
45 if (type === 'hour') {
48 value.hour(+itemValue % 12);
50 value.hour((+itemValue % 12) + 12);
53 value.hour(+itemValue);
55 } else if (type === 'minute') {
56 value.minute(+itemValue);
57 } else if (type === 'ampm') {
59 if (itemValue === 'PM' && value.hour() <= 12) {
60 value.hour(value.hour() + 12);
63 if (itemValue === 'AM') {
67 if (value.hour() > 12) {
68 value.hour(value.hour() - 12);
73 value.second(+itemValue);
78 onEnterSelectPanel(range) {
79 this.props.onCurrentSelectPanelChange(range);
83 const { prefixCls, hourOptions, disabledHours, showHour, use12Hours } = this.props;
87 const disabledOptions = disabledHours();
91 hourOptionsAdj = [12].concat(hourOptions.filter(h => h < 12 && h > 0));
92 hourAdj = (hour % 12) || 12;
94 hourOptionsAdj = hourOptions;
100 prefixCls={prefixCls}
101 options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))}
102 selectedIndex={hourOptionsAdj.indexOf(hourAdj)}
104 onSelect={this.onItemChange}
105 onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')}
110 getMinuteSelect(minute) {
111 const { prefixCls, minuteOptions, disabledMinutes, defaultOpenValue, showMinute } = this.props;
115 const value = this.props.value || defaultOpenValue;
116 const disabledOptions = disabledMinutes(value.hour());
120 prefixCls={prefixCls}
121 options={minuteOptions.map(option => formatOption(option, disabledOptions))}
122 selectedIndex={minuteOptions.indexOf(minute)}
124 onSelect={this.onItemChange}
125 onMouseEnter={this.onEnterSelectPanel.bind(this, 'minute')}
130 getSecondSelect(second) {
131 const { prefixCls, secondOptions, disabledSeconds, showSecond, defaultOpenValue } = this.props;
135 const value = this.props.value || defaultOpenValue;
136 const disabledOptions = disabledSeconds(value.hour(), value.minute());
140 prefixCls={prefixCls}
141 options={secondOptions.map(option => formatOption(option, disabledOptions))}
142 selectedIndex={secondOptions.indexOf(second)}
144 onSelect={this.onItemChange}
145 onMouseEnter={this.onEnterSelectPanel.bind(this, 'second')}
151 const { prefixCls, use12Hours } = this.props;
155 const AMPMOptions = [{ value: 'AM' }, { value: 'PM' }];
156 const selected = this.isAM() ? 0 : 1;
160 prefixCls={prefixCls}
161 options={AMPMOptions}
162 selectedIndex={selected}
164 onSelect={this.onItemChange}
165 onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')}
171 const { value } = this.props;
172 return value.hour() >= 0 && value.hour() < 12;
176 const { prefixCls, defaultOpenValue } = this.props;
177 const value = this.props.value || defaultOpenValue;
179 <div className={`${prefixCls}-combobox`}>
180 {this.getHourSelect(value.hour())}
181 {this.getMinuteSelect(value.minute())}
182 {this.getSecondSelect(value.second())}
183 {this.getAMPMSelect(value.hour())}
189 export default Combobox;