diff options
Diffstat (limited to 'src/Combobox.jsx')
-rw-r--r-- | src/Combobox.jsx | 76 |
1 files changed, 69 insertions, 7 deletions
diff --git a/src/Combobox.jsx b/src/Combobox.jsx index 013617c..36b61cc 100644 --- a/src/Combobox.jsx +++ b/src/Combobox.jsx | |||
@@ -35,17 +35,40 @@ const Combobox = React.createClass({ | |||
35 | disabledMinutes: PropTypes.func, | 35 | disabledMinutes: PropTypes.func, |
36 | disabledSeconds: PropTypes.func, | 36 | disabledSeconds: PropTypes.func, |
37 | onCurrentSelectPanelChange: PropTypes.func, | 37 | onCurrentSelectPanelChange: PropTypes.func, |
38 | use12Hours: PropTypes.bool, | ||
38 | }, | 39 | }, |
39 | 40 | ||
40 | onItemChange(type, itemValue) { | 41 | onItemChange(type, itemValue) { |
41 | const { onChange, defaultOpenValue } = this.props; | 42 | const { onChange, defaultOpenValue, use12Hours } = this.props; |
42 | const value = (this.props.value || defaultOpenValue).clone(); | 43 | const value = (this.props.value || defaultOpenValue).clone(); |
44 | |||
43 | if (type === 'hour') { | 45 | if (type === 'hour') { |
44 | value.hour(itemValue); | 46 | if (use12Hours) { |
47 | if (this.isAM()) { | ||
48 | value.hour(+itemValue % 12); | ||
49 | } else { | ||
50 | value.hour((+itemValue % 12) + 12); | ||
51 | } | ||
52 | } else { | ||
53 | value.hour(+itemValue); | ||
54 | } | ||
45 | } else if (type === 'minute') { | 55 | } else if (type === 'minute') { |
46 | value.minute(itemValue); | 56 | value.minute(+itemValue); |
57 | } else if (type === 'ampm') { | ||
58 | const ampm = itemValue.toUpperCase(); | ||
59 | if (use12Hours) { | ||
60 | if (ampm === 'PM' && value.hour() < 12) { | ||
61 | value.hour((value.hour() % 12) + 12); | ||
62 | } | ||
63 | |||
64 | if (ampm === 'AM') { | ||
65 | if (value.hour() >= 12) { | ||
66 | value.hour(value.hour() - 12); | ||
67 | } | ||
68 | } | ||
69 | } | ||
47 | } else { | 70 | } else { |
48 | value.second(itemValue); | 71 | value.second(+itemValue); |
49 | } | 72 | } |
50 | onChange(value); | 73 | onChange(value); |
51 | }, | 74 | }, |
@@ -55,17 +78,26 @@ const Combobox = React.createClass({ | |||
55 | }, | 78 | }, |
56 | 79 | ||
57 | getHourSelect(hour) { | 80 | getHourSelect(hour) { |
58 | const { prefixCls, hourOptions, disabledHours, showHour } = this.props; | 81 | const { prefixCls, hourOptions, disabledHours, showHour, use12Hours } = this.props; |
59 | if (!showHour) { | 82 | if (!showHour) { |
60 | return null; | 83 | return null; |
61 | } | 84 | } |
62 | const disabledOptions = disabledHours(); | 85 | const disabledOptions = disabledHours(); |
86 | let hourOptionsAdj; | ||
87 | let hourAdj; | ||
88 | if (use12Hours) { | ||
89 | hourOptionsAdj = [12].concat(hourOptions.filter(h => h < 12 && h > 0)); | ||
90 | hourAdj = (hour % 12) || 12; | ||
91 | } else { | ||
92 | hourOptionsAdj = hourOptions; | ||
93 | hourAdj = hour; | ||
94 | } | ||
63 | 95 | ||
64 | return ( | 96 | return ( |
65 | <Select | 97 | <Select |
66 | prefixCls={prefixCls} | 98 | prefixCls={prefixCls} |
67 | options={hourOptions.map(option => formatOption(option, disabledOptions))} | 99 | options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))} |
68 | selectedIndex={hourOptions.indexOf(hour)} | 100 | selectedIndex={hourOptionsAdj.indexOf(hourAdj)} |
69 | type="hour" | 101 | type="hour" |
70 | onSelect={this.onItemChange} | 102 | onSelect={this.onItemChange} |
71 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')} | 103 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')} |
@@ -113,6 +145,35 @@ const Combobox = React.createClass({ | |||
113 | ); | 145 | ); |
114 | }, | 146 | }, |
115 | 147 | ||
148 | getAMPMSelect() { | ||
149 | const { prefixCls, use12Hours, format } = this.props; | ||
150 | if (!use12Hours) { | ||
151 | return null; | ||
152 | } | ||
153 | |||
154 | const AMPMOptions = ['am', 'pm'] // If format has A char, then we should uppercase AM/PM | ||
155 | .map(c => format.match(/\sA/) ? c.toUpperCase() : c) | ||
156 | .map(c => ({ value: c })); | ||
157 | |||
158 | const selected = this.isAM() ? 0 : 1; | ||
159 | |||
160 | return ( | ||
161 | <Select | ||
162 | prefixCls={prefixCls} | ||
163 | options={AMPMOptions} | ||
164 | selectedIndex={selected} | ||
165 | type="ampm" | ||
166 | onSelect={this.onItemChange} | ||
167 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')} | ||
168 | /> | ||
169 | ); | ||
170 | }, | ||
171 | |||
172 | isAM() { | ||
173 | const { value } = this.props; | ||
174 | return value.hour() >= 0 && value.hour() < 12; | ||
175 | }, | ||
176 | |||
116 | render() { | 177 | render() { |
117 | const { prefixCls, defaultOpenValue } = this.props; | 178 | const { prefixCls, defaultOpenValue } = this.props; |
118 | const value = this.props.value || defaultOpenValue; | 179 | const value = this.props.value || defaultOpenValue; |
@@ -121,6 +182,7 @@ const Combobox = React.createClass({ | |||
121 | {this.getHourSelect(value.hour())} | 182 | {this.getHourSelect(value.hour())} |
122 | {this.getMinuteSelect(value.minute())} | 183 | {this.getMinuteSelect(value.minute())} |
123 | {this.getSecondSelect(value.second())} | 184 | {this.getSecondSelect(value.second())} |
185 | {this.getAMPMSelect(value.hour())} | ||
124 | </div> | 186 | </div> |
125 | ); | 187 | ); |
126 | }, | 188 | }, |