UNPKG

11.4 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = void 0;
7
8var _react = _interopRequireWildcard(require("react"));
9
10var _propTypes = _interopRequireDefault(require("prop-types"));
11
12var _Select = _interopRequireDefault(require("./Select"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
16function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
17
18function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
19
20function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
21
22function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
23
24function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
25
26function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
27
28function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
29
30function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
31
32function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
33
34function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
35
36var formatOption = function formatOption(option, disabledOptions) {
37 var value = "".concat(option);
38
39 if (option < 10) {
40 value = "0".concat(option);
41 }
42
43 var disabled = false;
44
45 if (disabledOptions && disabledOptions.indexOf(option) >= 0) {
46 disabled = true;
47 }
48
49 return {
50 value: value,
51 disabled: disabled
52 };
53};
54
55var Combobox =
56/*#__PURE__*/
57function (_Component) {
58 _inherits(Combobox, _Component);
59
60 function Combobox() {
61 var _getPrototypeOf2;
62
63 var _this;
64
65 _classCallCheck(this, Combobox);
66
67 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
68 args[_key] = arguments[_key];
69 }
70
71 _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Combobox)).call.apply(_getPrototypeOf2, [this].concat(args)));
72
73 _defineProperty(_assertThisInitialized(_this), "onItemChange", function (type, itemValue) {
74 var _this$props = _this.props,
75 onChange = _this$props.onChange,
76 defaultOpenValue = _this$props.defaultOpenValue,
77 use12Hours = _this$props.use12Hours,
78 propValue = _this$props.value,
79 isAM = _this$props.isAM,
80 onAmPmChange = _this$props.onAmPmChange;
81 var value = (propValue || defaultOpenValue).clone();
82
83 if (type === 'hour') {
84 if (use12Hours) {
85 if (isAM) {
86 value.hour(+itemValue % 12);
87 } else {
88 value.hour(+itemValue % 12 + 12);
89 }
90 } else {
91 value.hour(+itemValue);
92 }
93 } else if (type === 'minute') {
94 value.minute(+itemValue);
95 } else if (type === 'ampm') {
96 var ampm = itemValue.toUpperCase();
97
98 if (use12Hours) {
99 if (ampm === 'PM' && value.hour() < 12) {
100 value.hour(value.hour() % 12 + 12);
101 }
102
103 if (ampm === 'AM') {
104 if (value.hour() >= 12) {
105 value.hour(value.hour() - 12);
106 }
107 }
108 }
109
110 onAmPmChange(ampm);
111 } else {
112 value.second(+itemValue);
113 }
114
115 onChange(value);
116 });
117
118 _defineProperty(_assertThisInitialized(_this), "onEnterSelectPanel", function (range) {
119 var onCurrentSelectPanelChange = _this.props.onCurrentSelectPanelChange;
120 onCurrentSelectPanelChange(range);
121 });
122
123 return _this;
124 }
125
126 _createClass(Combobox, [{
127 key: "getHourSelect",
128 value: function getHourSelect(hour) {
129 var _this2 = this;
130
131 var _this$props2 = this.props,
132 prefixCls = _this$props2.prefixCls,
133 hourOptions = _this$props2.hourOptions,
134 disabledHours = _this$props2.disabledHours,
135 showHour = _this$props2.showHour,
136 use12Hours = _this$props2.use12Hours,
137 onEsc = _this$props2.onEsc;
138
139 if (!showHour) {
140 return null;
141 }
142
143 var disabledOptions = disabledHours();
144 var hourOptionsAdj;
145 var hourAdj;
146
147 if (use12Hours) {
148 hourOptionsAdj = [12].concat(hourOptions.filter(function (h) {
149 return h < 12 && h > 0;
150 }));
151 hourAdj = hour % 12 || 12;
152 } else {
153 hourOptionsAdj = hourOptions;
154 hourAdj = hour;
155 }
156
157 return _react["default"].createElement(_Select["default"], {
158 prefixCls: prefixCls,
159 options: hourOptionsAdj.map(function (option) {
160 return formatOption(option, disabledOptions);
161 }),
162 selectedIndex: hourOptionsAdj.indexOf(hourAdj),
163 type: "hour",
164 onSelect: this.onItemChange,
165 onMouseEnter: function onMouseEnter() {
166 return _this2.onEnterSelectPanel('hour');
167 },
168 onEsc: onEsc
169 });
170 }
171 }, {
172 key: "getMinuteSelect",
173 value: function getMinuteSelect(minute) {
174 var _this3 = this;
175
176 var _this$props3 = this.props,
177 prefixCls = _this$props3.prefixCls,
178 minuteOptions = _this$props3.minuteOptions,
179 disabledMinutes = _this$props3.disabledMinutes,
180 defaultOpenValue = _this$props3.defaultOpenValue,
181 showMinute = _this$props3.showMinute,
182 propValue = _this$props3.value,
183 onEsc = _this$props3.onEsc;
184
185 if (!showMinute) {
186 return null;
187 }
188
189 var value = propValue || defaultOpenValue;
190 var disabledOptions = disabledMinutes(value.hour());
191 return _react["default"].createElement(_Select["default"], {
192 prefixCls: prefixCls,
193 options: minuteOptions.map(function (option) {
194 return formatOption(option, disabledOptions);
195 }),
196 selectedIndex: minuteOptions.indexOf(minute),
197 type: "minute",
198 onSelect: this.onItemChange,
199 onMouseEnter: function onMouseEnter() {
200 return _this3.onEnterSelectPanel('minute');
201 },
202 onEsc: onEsc
203 });
204 }
205 }, {
206 key: "getSecondSelect",
207 value: function getSecondSelect(second) {
208 var _this4 = this;
209
210 var _this$props4 = this.props,
211 prefixCls = _this$props4.prefixCls,
212 secondOptions = _this$props4.secondOptions,
213 disabledSeconds = _this$props4.disabledSeconds,
214 showSecond = _this$props4.showSecond,
215 defaultOpenValue = _this$props4.defaultOpenValue,
216 propValue = _this$props4.value,
217 onEsc = _this$props4.onEsc;
218
219 if (!showSecond) {
220 return null;
221 }
222
223 var value = propValue || defaultOpenValue;
224 var disabledOptions = disabledSeconds(value.hour(), value.minute());
225 return _react["default"].createElement(_Select["default"], {
226 prefixCls: prefixCls,
227 options: secondOptions.map(function (option) {
228 return formatOption(option, disabledOptions);
229 }),
230 selectedIndex: secondOptions.indexOf(second),
231 type: "second",
232 onSelect: this.onItemChange,
233 onMouseEnter: function onMouseEnter() {
234 return _this4.onEnterSelectPanel('second');
235 },
236 onEsc: onEsc
237 });
238 }
239 }, {
240 key: "getAMPMSelect",
241 value: function getAMPMSelect() {
242 var _this5 = this;
243
244 var _this$props5 = this.props,
245 prefixCls = _this$props5.prefixCls,
246 use12Hours = _this$props5.use12Hours,
247 format = _this$props5.format,
248 isAM = _this$props5.isAM,
249 onEsc = _this$props5.onEsc;
250
251 if (!use12Hours) {
252 return null;
253 }
254
255 var AMPMOptions = ['am', 'pm'] // If format has A char, then we should uppercase AM/PM
256 .map(function (c) {
257 return format.match(/\sA/) ? c.toUpperCase() : c;
258 }).map(function (c) {
259 return {
260 value: c
261 };
262 });
263 var selected = isAM ? 0 : 1;
264 return _react["default"].createElement(_Select["default"], {
265 prefixCls: prefixCls,
266 options: AMPMOptions,
267 selectedIndex: selected,
268 type: "ampm",
269 onSelect: this.onItemChange,
270 onMouseEnter: function onMouseEnter() {
271 return _this5.onEnterSelectPanel('ampm');
272 },
273 onEsc: onEsc
274 });
275 }
276 }, {
277 key: "render",
278 value: function render() {
279 var _this$props6 = this.props,
280 prefixCls = _this$props6.prefixCls,
281 defaultOpenValue = _this$props6.defaultOpenValue,
282 propValue = _this$props6.value;
283 var value = propValue || defaultOpenValue;
284 return _react["default"].createElement("div", {
285 className: "".concat(prefixCls, "-combobox")
286 }, this.getHourSelect(value.hour()), this.getMinuteSelect(value.minute()), this.getSecondSelect(value.second()), this.getAMPMSelect(value.hour()));
287 }
288 }]);
289
290 return Combobox;
291}(_react.Component);
292
293_defineProperty(Combobox, "propTypes", {
294 format: _propTypes["default"].string,
295 defaultOpenValue: _propTypes["default"].object,
296 prefixCls: _propTypes["default"].string,
297 value: _propTypes["default"].object,
298 onChange: _propTypes["default"].func,
299 onAmPmChange: _propTypes["default"].func,
300 showHour: _propTypes["default"].bool,
301 showMinute: _propTypes["default"].bool,
302 showSecond: _propTypes["default"].bool,
303 hourOptions: _propTypes["default"].array,
304 minuteOptions: _propTypes["default"].array,
305 secondOptions: _propTypes["default"].array,
306 disabledHours: _propTypes["default"].func,
307 disabledMinutes: _propTypes["default"].func,
308 disabledSeconds: _propTypes["default"].func,
309 onCurrentSelectPanelChange: _propTypes["default"].func,
310 use12Hours: _propTypes["default"].bool,
311 onEsc: _propTypes["default"].func,
312 isAM: _propTypes["default"].bool
313});
314
315var _default = Combobox;
316exports["default"] = _default;
\No newline at end of file