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') {
47 if (value.hour() > 12 || !value.hour()) {
48 value.hour(+itemValue + 12);
50 value.hour(+itemValue);
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();
101 hourOptionsAdj = hourOptions.filter(h => h <= 12 && h > 0);
103 hourOptionsAdj = hourOptions;
108 prefixCls={prefixCls}
109 options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))}
110 selectedIndex={hourOptionsAdj.indexOf(hourAdj)}
112 onSelect={this.onItemChange}
113 onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')}
118 getMinuteSelect(minute) {
119 const { prefixCls, minuteOptions, disabledMinutes, defaultOpenValue, showMinute } = this.props;
123 const value = this.props.value || defaultOpenValue;
124 const disabledOptions = disabledMinutes(value.hour());
128 prefixCls={prefixCls}
129 options={minuteOptions.map(option => formatOption(option, disabledOptions))}
130 selectedIndex={minuteOptions.indexOf(minute)}
132 onSelect={this.onItemChange}
133 onMouseEnter={this.onEnterSelectPanel.bind(this, 'minute')}
138 getSecondSelect(second) {
139 const { prefixCls, secondOptions, disabledSeconds, showSecond, defaultOpenValue } = this.props;
143 const value = this.props.value || defaultOpenValue;
144 const disabledOptions = disabledSeconds(value.hour(), value.minute());
148 prefixCls={prefixCls}
149 options={secondOptions.map(option => formatOption(option, disabledOptions))}
150 selectedIndex={secondOptions.indexOf(second)}
152 onSelect={this.onItemChange}
153 onMouseEnter={this.onEnterSelectPanel.bind(this, 'second')}
159 const { prefixCls, use12Hours, defaultOpenValue } = this.props;
163 const value = this.props.value || defaultOpenValue;
164 const AMPMOptions = [{ value: 'AM' }, { value: 'PM' }];
165 const selected = (!value.hour() || value.hour() > 12) ? 1 : 0;
169 prefixCls={prefixCls}
170 options={AMPMOptions}
171 selectedIndex={selected}
173 onSelect={this.onItemChange}
174 onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')}
180 const { prefixCls, defaultOpenValue } = this.props;
181 const value = this.props.value || defaultOpenValue;
183 <div className={`${prefixCls}-combobox`}>
184 {this.getHourSelect(value.hour())}
185 {this.getMinuteSelect(value.minute())}
186 {this.getSecondSelect(value.second())}
187 {this.getAMPMSelect(value.hour())}
193 export default Combobox;