]>
Commit | Line | Data |
---|---|---|
4984ed85 | 1 | import React, { PropTypes } from 'react'; |
02de449a | 2 | import Select from './Select'; |
3 | ||
518b852e M |
4 | const formatOption = (option, disabledOptions) => { |
5 | let value = `${option}`; | |
02de449a | 6 | if (option < 10) { |
518b852e | 7 | value = `0${option}`; |
02de449a | 8 | } |
518b852e M |
9 | |
10 | let disabled = false; | |
0e62bf0b | 11 | if (disabledOptions && disabledOptions.indexOf(option) >= 0) { |
518b852e M |
12 | disabled = true; |
13 | } | |
14 | ||
15 | return { | |
16 | value, | |
17 | disabled, | |
18 | }; | |
02de449a | 19 | }; |
20 | ||
21 | const Combobox = React.createClass({ | |
22 | propTypes: { | |
4984ed85 | 23 | format: PropTypes.string, |
24 | defaultOpenValue: PropTypes.object, | |
02de449a | 25 | prefixCls: PropTypes.string, |
26 | value: PropTypes.object, | |
27 | onChange: PropTypes.func, | |
28 | showHour: PropTypes.bool, | |
37c36c09 | 29 | showMinute: PropTypes.bool, |
02de449a | 30 | showSecond: PropTypes.bool, |
31 | hourOptions: PropTypes.array, | |
32 | minuteOptions: PropTypes.array, | |
33 | secondOptions: PropTypes.array, | |
71bd9bc1 M |
34 | disabledHours: PropTypes.func, |
35 | disabledMinutes: PropTypes.func, | |
36 | disabledSeconds: PropTypes.func, | |
182e9fcc | 37 | onCurrentSelectPanelChange: PropTypes.func, |
02de449a | 38 | }, |
39 | ||
40 | onItemChange(type, itemValue) { | |
4984ed85 | 41 | const { onChange, defaultOpenValue } = this.props; |
42 | const value = (this.props.value || defaultOpenValue).clone(); | |
11b97949 | 43 | if (type === 'hour') { |
4984ed85 | 44 | value.hour(itemValue); |
11b97949 | 45 | } else if (type === 'minute') { |
4984ed85 | 46 | value.minute(itemValue); |
11b97949 | 47 | } else { |
4984ed85 | 48 | value.second(itemValue); |
02de449a | 49 | } |
02de449a | 50 | onChange(value); |
51 | }, | |
52 | ||
182e9fcc | 53 | onEnterSelectPanel(range) { |
54 | this.props.onCurrentSelectPanelChange(range); | |
55 | }, | |
56 | ||
02de449a | 57 | getHourSelect(hour) { |
518b852e | 58 | const { prefixCls, hourOptions, disabledHours, showHour } = this.props; |
02de449a | 59 | if (!showHour) { |
60 | return null; | |
61 | } | |
71bd9bc1 M |
62 | const disabledOptions = disabledHours(); |
63 | ||
02de449a | 64 | return ( |
65 | <Select | |
66 | prefixCls={prefixCls} | |
71bd9bc1 | 67 | options={hourOptions.map(option => formatOption(option, disabledOptions))} |
02de449a | 68 | selectedIndex={hourOptions.indexOf(hour)} |
69 | type="hour" | |
70 | onSelect={this.onItemChange} | |
182e9fcc | 71 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')} |
02de449a | 72 | /> |
73 | ); | |
74 | }, | |
75 | ||
76 | getMinuteSelect(minute) { | |
37c36c09 | 77 | const { prefixCls, minuteOptions, disabledMinutes, defaultOpenValue, showMinute } = this.props; |
78 | if (!showMinute) { | |
79 | return null; | |
80 | } | |
4984ed85 | 81 | const value = this.props.value || defaultOpenValue; |
82 | const disabledOptions = disabledMinutes(value.hour()); | |
71bd9bc1 | 83 | |
02de449a | 84 | return ( |
85 | <Select | |
86 | prefixCls={prefixCls} | |
71bd9bc1 | 87 | options={minuteOptions.map(option => formatOption(option, disabledOptions))} |
02de449a | 88 | selectedIndex={minuteOptions.indexOf(minute)} |
89 | type="minute" | |
90 | onSelect={this.onItemChange} | |
182e9fcc | 91 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'minute')} |
02de449a | 92 | /> |
93 | ); | |
94 | }, | |
95 | ||
182e9fcc | 96 | getSecondSelect(second) { |
4984ed85 | 97 | const { prefixCls, secondOptions, disabledSeconds, showSecond, defaultOpenValue } = this.props; |
02de449a | 98 | if (!showSecond) { |
99 | return null; | |
100 | } | |
4984ed85 | 101 | const value = this.props.value || defaultOpenValue; |
102 | const disabledOptions = disabledSeconds(value.hour(), value.minute()); | |
71bd9bc1 | 103 | |
02de449a | 104 | return ( |
105 | <Select | |
106 | prefixCls={prefixCls} | |
71bd9bc1 | 107 | options={secondOptions.map(option => formatOption(option, disabledOptions))} |
02de449a | 108 | selectedIndex={secondOptions.indexOf(second)} |
109 | type="second" | |
110 | onSelect={this.onItemChange} | |
182e9fcc | 111 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'second')} |
02de449a | 112 | /> |
113 | ); | |
114 | }, | |
115 | ||
8133e8cf | 116 | render() { |
4984ed85 | 117 | const { prefixCls, defaultOpenValue } = this.props; |
118 | const value = this.props.value || defaultOpenValue; | |
02de449a | 119 | return ( |
120 | <div className={`${prefixCls}-combobox`}> | |
4984ed85 | 121 | {this.getHourSelect(value.hour())} |
122 | {this.getMinuteSelect(value.minute())} | |
123 | {this.getSecondSelect(value.second())} | |
02de449a | 124 | </div> |
125 | ); | |
126 | }, | |
127 | }); | |
128 | ||
129 | export default Combobox; |