UNPKG

11.2 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
138 if (!showHour) {
139 return null;
140 }
141
142 var disabledOptions = disabledHours();
143 var hourOptionsAdj;
144 var hourAdj;
145
146 if (use12Hours) {
147 hourOptionsAdj = [12].concat(hourOptions.filter(function (h) {
148 return h < 12 && h > 0;
149 }));
150 hourAdj = hour % 12 || 12;
151 } else {
152 hourOptionsAdj = hourOptions;
153 hourAdj = hour;
154 }
155
156 return _react["default"].createElement(_Select["default"], {
157 prefixCls: prefixCls,
158 options: hourOptionsAdj.map(function (option) {
159 return formatOption(option, disabledOptions);
160 }),
161 selectedIndex: hourOptionsAdj.indexOf(hourAdj),
162 type: "hour",
163 onSelect: this.onItemChange,
164 onMouseEnter: function onMouseEnter() {
165 return _this2.onEnterSelectPanel('hour');
166 }
167 });
168 }
169 }, {
170 key: "getMinuteSelect",
171 value: function getMinuteSelect(minute) {
172 var _this3 = this;
173
174 var _this$props3 = this.props,
175 prefixCls = _this$props3.prefixCls,
176 minuteOptions = _this$props3.minuteOptions,
177 disabledMinutes = _this$props3.disabledMinutes,
178 defaultOpenValue = _this$props3.defaultOpenValue,
179 showMinute = _this$props3.showMinute,
180 propValue = _this$props3.value;
181
182 if (!showMinute) {
183 return null;
184 }
185
186 var value = propValue || defaultOpenValue;
187 var disabledOptions = disabledMinutes(value.hour());
188 return _react["default"].createElement(_Select["default"], {
189 prefixCls: prefixCls,
190 options: minuteOptions.map(function (option) {
191 return formatOption(option, disabledOptions);
192 }),
193 selectedIndex: minuteOptions.indexOf(minute),
194 type: "minute",
195 onSelect: this.onItemChange,
196 onMouseEnter: function onMouseEnter() {
197 return _this3.onEnterSelectPanel('minute');
198 }
199 });
200 }
201 }, {
202 key: "getSecondSelect",
203 value: function getSecondSelect(second) {
204 var _this4 = this;
205
206 var _this$props4 = this.props,
207 prefixCls = _this$props4.prefixCls,
208 secondOptions = _this$props4.secondOptions,
209 disabledSeconds = _this$props4.disabledSeconds,
210 showSecond = _this$props4.showSecond,
211 defaultOpenValue = _this$props4.defaultOpenValue,
212 propValue = _this$props4.value;
213
214 if (!showSecond) {
215 return null;
216 }
217
218 var value = propValue || defaultOpenValue;
219 var disabledOptions = disabledSeconds(value.hour(), value.minute());
220 return _react["default"].createElement(_Select["default"], {
221 prefixCls: prefixCls,
222 options: secondOptions.map(function (option) {
223 return formatOption(option, disabledOptions);
224 }),
225 selectedIndex: secondOptions.indexOf(second),
226 type: "second",
227 onSelect: this.onItemChange,
228 onMouseEnter: function onMouseEnter() {
229 return _this4.onEnterSelectPanel('second');
230 }
231 });
232 }
233 }, {
234 key: "getAMPMSelect",
235 value: function getAMPMSelect() {
236 var _this5 = this;
237
238 var _this$props5 = this.props,
239 prefixCls = _this$props5.prefixCls,
240 use12Hours = _this$props5.use12Hours,
241 format = _this$props5.format,
242 isAM = _this$props5.isAM;
243
244 if (!use12Hours) {
245 return null;
246 }
247
248 var AMPMOptions = ['am', 'pm'] // If format has A char, then we should uppercase AM/PM
249 .map(function (c) {
250 return format.match(/\sA/) ? c.toUpperCase() : c;
251 }).map(function (c) {
252 return {
253 value: c
254 };
255 });
256 var selected = isAM ? 0 : 1;
257 return _react["default"].createElement(_Select["default"], {
258 prefixCls: prefixCls,
259 options: AMPMOptions,
260 selectedIndex: selected,
261 type: "ampm",
262 onSelect: this.onItemChange,
263 onMouseEnter: function onMouseEnter() {
264 return _this5.onEnterSelectPanel('ampm');
265 }
266 });
267 }
268 }, {
269 key: "render",
270 value: function render() {
271 var _this$props6 = this.props,
272 prefixCls = _this$props6.prefixCls,
273 defaultOpenValue = _this$props6.defaultOpenValue,
274 propValue = _this$props6.value;
275 var value = propValue || defaultOpenValue;
276 return _react["default"].createElement("div", {
277 className: "".concat(prefixCls, "-combobox")
278 }, this.getHourSelect(value.hour()), this.getMinuteSelect(value.minute()), this.getSecondSelect(value.second()), this.getAMPMSelect(value.hour()));
279 }
280 }]);
281
282 return Combobox;
283}(_react.Component);
284
285_defineProperty(Combobox, "propTypes", {
286 format: _propTypes["default"].string,
287 defaultOpenValue: _propTypes["default"].object,
288 prefixCls: _propTypes["default"].string,
289 value: _propTypes["default"].object,
290 onChange: _propTypes["default"].func,
291 onAmPmChange: _propTypes["default"].func,
292 showHour: _propTypes["default"].bool,
293 showMinute: _propTypes["default"].bool,
294 showSecond: _propTypes["default"].bool,
295 hourOptions: _propTypes["default"].array,
296 minuteOptions: _propTypes["default"].array,
297 secondOptions: _propTypes["default"].array,
298 disabledHours: _propTypes["default"].func,
299 disabledMinutes: _propTypes["default"].func,
300 disabledSeconds: _propTypes["default"].func,
301 onCurrentSelectPanelChange: _propTypes["default"].func,
302 use12Hours: _propTypes["default"].bool,
303 isAM: _propTypes["default"].bool
304});
305
306var _default = Combobox;
307exports["default"] = _default;
\No newline at end of file