diff options
author | Antony Shaleynikov <shaleynikov@gmail.com> | 2017-03-01 23:58:06 +0300 |
---|---|---|
committer | Antony Shaleynikov <shaleynikov@gmail.com> | 2017-03-01 23:58:06 +0300 |
commit | 95699887ac75de5dff6bd25278289e31e4745482 (patch) | |
tree | 1d3ba6855fa867e90e380be4312578777736ff73 /src/Combobox.jsx | |
parent | 19b96283c09e678b8b7ea5c60569996a0bab5935 (diff) | |
download | time-picker-95699887ac75de5dff6bd25278289e31e4745482.tar.gz time-picker-95699887ac75de5dff6bd25278289e31e4745482.tar.zst time-picker-95699887ac75de5dff6bd25278289e31e4745482.zip |
Added 12hours display support
Diffstat (limited to 'src/Combobox.jsx')
-rw-r--r-- | src/Combobox.jsx | 78 |
1 files changed, 71 insertions, 7 deletions
diff --git a/src/Combobox.jsx b/src/Combobox.jsx index 013617c..e31b7fd 100644 --- a/src/Combobox.jsx +++ b/src/Combobox.jsx | |||
@@ -35,17 +35,42 @@ 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 | show12Hours: PropTypes.bool, | ||
38 | }, | 39 | }, |
39 | 40 | ||
40 | onItemChange(type, itemValue) { | 41 | onItemChange(type, itemValue) { |
41 | const { onChange, defaultOpenValue } = this.props; | 42 | const { onChange, defaultOpenValue, show12Hours } = 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 (show12Hours) { |
47 | if (value.hour() > 12 || !value.hour()) { | ||
48 | value.hour(+itemValue + 12); | ||
49 | } else { | ||
50 | value.hour(+itemValue); | ||
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 | if (show12Hours) { | ||
59 | if (itemValue === 'PM' && value.hour() <= 12) { | ||
60 | value.hour(value.hour() + 12); | ||
61 | } | ||
62 | |||
63 | if (itemValue === 'AM') { | ||
64 | if (!value.hour()) { | ||
65 | value.hour(12); | ||
66 | } else | ||
67 | if (value.hour() > 12) { | ||
68 | value.hour(value.hour() - 12); | ||
69 | } | ||
70 | } | ||
71 | } | ||
47 | } else { | 72 | } else { |
48 | value.second(itemValue); | 73 | value.second(+itemValue); |
49 | } | 74 | } |
50 | onChange(value); | 75 | onChange(value); |
51 | }, | 76 | }, |
@@ -55,17 +80,34 @@ const Combobox = React.createClass({ | |||
55 | }, | 80 | }, |
56 | 81 | ||
57 | getHourSelect(hour) { | 82 | getHourSelect(hour) { |
58 | const { prefixCls, hourOptions, disabledHours, showHour } = this.props; | 83 | const { prefixCls, hourOptions, disabledHours, showHour, show12Hours } = this.props; |
59 | if (!showHour) { | 84 | if (!showHour) { |
60 | return null; | 85 | return null; |
61 | } | 86 | } |
62 | const disabledOptions = disabledHours(); | 87 | const disabledOptions = disabledHours(); |
88 | let hourAdj; | ||
89 | if (show12Hours) { | ||
90 | if (hour > 12) { | ||
91 | hourAdj = hour - 12; | ||
92 | } else { | ||
93 | hourAdj = hour || 12; | ||
94 | } | ||
95 | } else { | ||
96 | hourAdj = hour; | ||
97 | } | ||
98 | |||
99 | let hourOptionsAdj; | ||
100 | if (show12Hours) { | ||
101 | hourOptionsAdj = hourOptions.filter(h => h <= 12 && h > 0); | ||
102 | } else { | ||
103 | hourOptionsAdj = hourOptions; | ||
104 | } | ||
63 | 105 | ||
64 | return ( | 106 | return ( |
65 | <Select | 107 | <Select |
66 | prefixCls={prefixCls} | 108 | prefixCls={prefixCls} |
67 | options={hourOptions.map(option => formatOption(option, disabledOptions))} | 109 | options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))} |
68 | selectedIndex={hourOptions.indexOf(hour)} | 110 | selectedIndex={hourOptionsAdj.indexOf(hourAdj)} |
69 | type="hour" | 111 | type="hour" |
70 | onSelect={this.onItemChange} | 112 | onSelect={this.onItemChange} |
71 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')} | 113 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')} |
@@ -113,6 +155,27 @@ const Combobox = React.createClass({ | |||
113 | ); | 155 | ); |
114 | }, | 156 | }, |
115 | 157 | ||
158 | getAMPMSelect() { | ||
159 | const { prefixCls, show12Hours, defaultOpenValue } = this.props; | ||
160 | if (!show12Hours) { | ||
161 | return null; | ||
162 | } | ||
163 | const value = this.props.value || defaultOpenValue; | ||
164 | const AMPMOptions = [{ value: 'AM' }, { value: 'PM' }]; | ||
165 | const selected = (!value.hour() || value.hour() > 12) ? 1 : 0; | ||
166 | |||
167 | return ( | ||
168 | <Select | ||
169 | prefixCls={prefixCls} | ||
170 | options={AMPMOptions} | ||
171 | selectedIndex={selected} | ||
172 | type="ampm" | ||
173 | onSelect={this.onItemChange} | ||
174 | onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')} | ||
175 | /> | ||
176 | ); | ||
177 | }, | ||
178 | |||
116 | render() { | 179 | render() { |
117 | const { prefixCls, defaultOpenValue } = this.props; | 180 | const { prefixCls, defaultOpenValue } = this.props; |
118 | const value = this.props.value || defaultOpenValue; | 181 | const value = this.props.value || defaultOpenValue; |
@@ -121,6 +184,7 @@ const Combobox = React.createClass({ | |||
121 | {this.getHourSelect(value.hour())} | 184 | {this.getHourSelect(value.hour())} |
122 | {this.getMinuteSelect(value.minute())} | 185 | {this.getMinuteSelect(value.minute())} |
123 | {this.getSecondSelect(value.second())} | 186 | {this.getSecondSelect(value.second())} |
187 | {this.getAMPMSelect(value.hour())} | ||
124 | </div> | 188 | </div> |
125 | ); | 189 | ); |
126 | }, | 190 | }, |