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