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) + 12);
63 if (itemValue === 'AM') {
64 if (value.hour() >= 12) {
65 value.hour(value.hour() - 12);
70 value.second(+itemValue);
75 onEnterSelectPanel(range) {
76 this.props.onCurrentSelectPanelChange(range);
80 const { prefixCls, hourOptions, disabledHours, showHour, use12Hours } = this.props;
84 const disabledOptions = disabledHours();
88 hourOptionsAdj = [12].concat(hourOptions.filter(h => h < 12 && h > 0));
89 hourAdj = (hour % 12) || 12;
91 hourOptionsAdj = hourOptions;
98 options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))}
99 selectedIndex={hourOptionsAdj.indexOf(hourAdj)}
101 onSelect={this.onItemChange}
102 onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')}
107 getMinuteSelect(minute) {
108 const { prefixCls, minuteOptions, disabledMinutes, defaultOpenValue, showMinute } = this.props;
112 const value = this.props.value || defaultOpenValue;
113 const disabledOptions = disabledMinutes(value.hour());
117 prefixCls={prefixCls}
118 options={minuteOptions.map(option => formatOption(option, disabledOptions))}
119 selectedIndex={minuteOptions.indexOf(minute)}
121 onSelect={this.onItemChange}
122 onMouseEnter={this.onEnterSelectPanel.bind(this, 'minute')}
127 getSecondSelect(second) {
128 const { prefixCls, secondOptions, disabledSeconds, showSecond, defaultOpenValue } = this.props;
132 const value = this.props.value || defaultOpenValue;
133 const disabledOptions = disabledSeconds(value.hour(), value.minute());
137 prefixCls={prefixCls}
138 options={secondOptions.map(option => formatOption(option, disabledOptions))}
139 selectedIndex={secondOptions.indexOf(second)}
141 onSelect={this.onItemChange}
142 onMouseEnter={this.onEnterSelectPanel.bind(this, 'second')}
148 const { prefixCls, use12Hours } = this.props;
152 const AMPMOptions = [{ value: 'AM' }, { value: 'PM' }];
153 const selected = this.isAM() ? 0 : 1;
157 prefixCls={prefixCls}
158 options={AMPMOptions}
159 selectedIndex={selected}
161 onSelect={this.onItemChange}
162 onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')}
168 const { value } = this.props;
169 return value.hour() >= 0 && value.hour() < 12;
173 const { prefixCls, defaultOpenValue } = this.props;
174 const value = this.props.value || defaultOpenValue;
176 <div className={`${prefixCls}-combobox`}>
177 {this.getHourSelect(value.hour())}
178 {this.getMinuteSelect(value.minute())}
179 {this.getSecondSelect(value.second())}
180 {this.getAMPMSelect(value.hour())}
186 export default Combobox;