diff options
author | afc163 <afc163@gmail.com> | 2017-03-08 15:00:31 +0800 |
---|---|---|
committer | afc163 <afc163@gmail.com> | 2017-03-08 15:00:31 +0800 |
commit | 4e020febdf6201ab5f10a0e49be81c6ff32daaeb (patch) | |
tree | e5636fff22c61a93236e4a7dfe1e00e87d371061 /src | |
parent | 19b96283c09e678b8b7ea5c60569996a0bab5935 (diff) | |
parent | 369ffb9a3883cb01ff5d80cc2312a1a7f2fbe6a4 (diff) | |
download | time-picker-4e020febdf6201ab5f10a0e49be81c6ff32daaeb.tar.gz time-picker-4e020febdf6201ab5f10a0e49be81c6ff32daaeb.tar.zst time-picker-4e020febdf6201ab5f10a0e49be81c6ff32daaeb.zip |
Merge branch 'shaleynikov-master'
Diffstat (limited to 'src')
-rw-r--r-- | src/Combobox.jsx | 76 | ||||
-rw-r--r-- | src/Panel.jsx | 5 | ||||
-rw-r--r-- | src/Select.jsx | 2 | ||||
-rw-r--r-- | src/TimePicker.jsx | 18 |
4 files changed, 90 insertions, 11 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 | }, |
diff --git a/src/Panel.jsx b/src/Panel.jsx index fddea1c..df128ff 100644 --- a/src/Panel.jsx +++ b/src/Panel.jsx | |||
@@ -37,6 +37,7 @@ const Panel = React.createClass({ | |||
37 | showMinute: PropTypes.bool, | 37 | showMinute: PropTypes.bool, |
38 | showSecond: PropTypes.bool, | 38 | showSecond: PropTypes.bool, |
39 | onClear: PropTypes.func, | 39 | onClear: PropTypes.func, |
40 | use12Hours: PropTypes.bool, | ||
40 | addon: PropTypes.func, | 41 | addon: PropTypes.func, |
41 | }, | 42 | }, |
42 | 43 | ||
@@ -49,6 +50,7 @@ const Panel = React.createClass({ | |||
49 | disabledMinutes: noop, | 50 | disabledMinutes: noop, |
50 | disabledSeconds: noop, | 51 | disabledSeconds: noop, |
51 | defaultOpenValue: moment(), | 52 | defaultOpenValue: moment(), |
53 | use12Hours: false, | ||
52 | addon: noop, | 54 | addon: noop, |
53 | }; | 55 | }; |
54 | }, | 56 | }, |
@@ -90,7 +92,7 @@ const Panel = React.createClass({ | |||
90 | const { | 92 | const { |
91 | prefixCls, className, placeholder, disabledHours, disabledMinutes, | 93 | prefixCls, className, placeholder, disabledHours, disabledMinutes, |
92 | disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, | 94 | disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, |
93 | format, defaultOpenValue, clearText, onEsc, addon, | 95 | format, defaultOpenValue, clearText, onEsc, addon, use12Hours, |
94 | } = this.props; | 96 | } = this.props; |
95 | const { | 97 | const { |
96 | value, currentSelectPanel, | 98 | value, currentSelectPanel, |
@@ -140,6 +142,7 @@ const Panel = React.createClass({ | |||
140 | disabledMinutes={disabledMinutes} | 142 | disabledMinutes={disabledMinutes} |
141 | disabledSeconds={disabledSeconds} | 143 | disabledSeconds={disabledSeconds} |
142 | onCurrentSelectPanelChange={this.onCurrentSelectPanelChange} | 144 | onCurrentSelectPanelChange={this.onCurrentSelectPanelChange} |
145 | use12Hours={use12Hours} | ||
143 | /> | 146 | /> |
144 | {addon(this)} | 147 | {addon(this)} |
145 | </div> | 148 | </div> |
diff --git a/src/Select.jsx b/src/Select.jsx index 238a776..5733b1a 100644 --- a/src/Select.jsx +++ b/src/Select.jsx | |||
@@ -58,7 +58,7 @@ const Select = React.createClass({ | |||
58 | }); | 58 | }); |
59 | let onclick = null; | 59 | let onclick = null; |
60 | if (!item.disabled) { | 60 | if (!item.disabled) { |
61 | onclick = this.onSelect.bind(this, +item.value); | 61 | onclick = this.onSelect.bind(this, item.value); |
62 | } | 62 | } |
63 | return (<li | 63 | return (<li |
64 | className={cls} | 64 | className={cls} |
diff --git a/src/TimePicker.jsx b/src/TimePicker.jsx index e9d6d15..7065333 100644 --- a/src/TimePicker.jsx +++ b/src/TimePicker.jsx | |||
@@ -43,6 +43,7 @@ const Picker = React.createClass({ | |||
43 | addon: PropTypes.func, | 43 | addon: PropTypes.func, |
44 | name: PropTypes.string, | 44 | name: PropTypes.string, |
45 | autoComplete: PropTypes.string, | 45 | autoComplete: PropTypes.string, |
46 | use12Hours: PropTypes.bool, | ||
46 | }, | 47 | }, |
47 | 48 | ||
48 | getDefaultProps() { | 49 | getDefaultProps() { |
@@ -67,6 +68,7 @@ const Picker = React.createClass({ | |||
67 | onOpen: noop, | 68 | onOpen: noop, |
68 | onClose: noop, | 69 | onClose: noop, |
69 | addon: noop, | 70 | addon: noop, |
71 | use12Hours: false, | ||
70 | }; | 72 | }; |
71 | }, | 73 | }, |
72 | 74 | ||
@@ -126,10 +128,21 @@ const Picker = React.createClass({ | |||
126 | }, | 128 | }, |
127 | 129 | ||
128 | getFormat() { | 130 | getFormat() { |
129 | const { format, showHour, showMinute, showSecond } = this.props; | 131 | const { format, showHour, showMinute, showSecond, use12Hours } = this.props; |
130 | if (format) { | 132 | if (format) { |
131 | return format; | 133 | return format; |
132 | } | 134 | } |
135 | |||
136 | if (use12Hours) { | ||
137 | const fmtString = ([ | ||
138 | showHour ? 'h' : '', | ||
139 | showMinute ? 'mm' : '', | ||
140 | showSecond ? 'ss' : '', | ||
141 | ].filter(item => !!item).join(':')); | ||
142 | |||
143 | return fmtString.concat(' a'); | ||
144 | } | ||
145 | |||
133 | return [ | 146 | return [ |
134 | showHour ? 'HH' : '', | 147 | showHour ? 'HH' : '', |
135 | showMinute ? 'mm' : '', | 148 | showMinute ? 'mm' : '', |
@@ -142,7 +155,7 @@ const Picker = React.createClass({ | |||
142 | prefixCls, placeholder, disabledHours, | 155 | prefixCls, placeholder, disabledHours, |
143 | disabledMinutes, disabledSeconds, hideDisabledOptions, | 156 | disabledMinutes, disabledSeconds, hideDisabledOptions, |
144 | allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText, | 157 | allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText, |
145 | addon, | 158 | addon, use12Hours, |
146 | } = this.props; | 159 | } = this.props; |
147 | return ( | 160 | return ( |
148 | <Panel | 161 | <Panel |
@@ -164,6 +177,7 @@ const Picker = React.createClass({ | |||
164 | disabledMinutes={disabledMinutes} | 177 | disabledMinutes={disabledMinutes} |
165 | disabledSeconds={disabledSeconds} | 178 | disabledSeconds={disabledSeconds} |
166 | hideDisabledOptions={hideDisabledOptions} | 179 | hideDisabledOptions={hideDisabledOptions} |
180 | use12Hours={use12Hours} | ||
167 | addon={addon} | 181 | addon={addon} |
168 | /> | 182 | /> |
169 | ); | 183 | ); |