UNPKG

16.6 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 _rcTrigger = _interopRequireDefault(require("rc-trigger"));
13
14var _moment = _interopRequireDefault(require("moment"));
15
16var _classnames = _interopRequireDefault(require("classnames"));
17
18var _Panel = _interopRequireDefault(require("./Panel"));
19
20var _placements = _interopRequireDefault(require("./placements"));
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
23
24function _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; } }
25
26function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
27
28function _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); } }
29
30function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
31
32function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
33
34function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
35
36function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
37
38function _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); }
39
40function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
41
42function _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; }
43
44function noop() {}
45
46function refFn(field, component) {
47 this[field] = component;
48}
49
50var Picker =
51/*#__PURE__*/
52function (_Component) {
53 _inherits(Picker, _Component);
54
55 function Picker(props) {
56 var _this;
57
58 _classCallCheck(this, Picker);
59
60 _this = _possibleConstructorReturn(this, _getPrototypeOf(Picker).call(this, props));
61
62 _defineProperty(_assertThisInitialized(_this), "onPanelChange", function (value) {
63 _this.setValue(value);
64 });
65
66 _defineProperty(_assertThisInitialized(_this), "onAmPmChange", function (ampm) {
67 var onAmPmChange = _this.props.onAmPmChange;
68 onAmPmChange(ampm);
69 });
70
71 _defineProperty(_assertThisInitialized(_this), "onClear", function (event) {
72 event.stopPropagation();
73
74 _this.setValue(null);
75
76 _this.setOpen(false);
77 });
78
79 _defineProperty(_assertThisInitialized(_this), "onVisibleChange", function (open) {
80 _this.setOpen(open);
81 });
82
83 _defineProperty(_assertThisInitialized(_this), "onEsc", function () {
84 _this.setOpen(false);
85
86 _this.focus();
87 });
88
89 _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
90 if (e.keyCode === 40) {
91 _this.setOpen(true);
92 }
93 });
94
95 _this.saveInputRef = refFn.bind(_assertThisInitialized(_this), 'picker');
96 _this.savePanelRef = refFn.bind(_assertThisInitialized(_this), 'panelInstance');
97
98 var defaultOpen = props.defaultOpen,
99 defaultValue = props.defaultValue,
100 _props$open = props.open,
101 _open = _props$open === void 0 ? defaultOpen : _props$open,
102 _props$value = props.value,
103 _value = _props$value === void 0 ? defaultValue : _props$value;
104
105 _this.state = {
106 open: _open,
107 value: _value
108 };
109 return _this;
110 }
111
112 _createClass(Picker, [{
113 key: "componentWillReceiveProps",
114 value: function componentWillReceiveProps(nextProps) {
115 var value = nextProps.value,
116 open = nextProps.open;
117
118 if ('value' in nextProps) {
119 this.setState({
120 value: value
121 });
122 }
123
124 if (open !== undefined) {
125 this.setState({
126 open: open
127 });
128 }
129 }
130 }, {
131 key: "setValue",
132 value: function setValue(value) {
133 var onChange = this.props.onChange;
134
135 if (!('value' in this.props)) {
136 this.setState({
137 value: value
138 });
139 }
140
141 onChange(value);
142 }
143 }, {
144 key: "getFormat",
145 value: function getFormat() {
146 var _this$props = this.props,
147 format = _this$props.format,
148 showHour = _this$props.showHour,
149 showMinute = _this$props.showMinute,
150 showSecond = _this$props.showSecond,
151 use12Hours = _this$props.use12Hours;
152
153 if (format) {
154 return format;
155 }
156
157 if (use12Hours) {
158 var fmtString = [showHour ? 'h' : '', showMinute ? 'mm' : '', showSecond ? 'ss' : ''].filter(function (item) {
159 return !!item;
160 }).join(':');
161 return fmtString.concat(' a');
162 }
163
164 return [showHour ? 'HH' : '', showMinute ? 'mm' : '', showSecond ? 'ss' : ''].filter(function (item) {
165 return !!item;
166 }).join(':');
167 }
168 }, {
169 key: "getPanelElement",
170 value: function getPanelElement() {
171 var _this$props2 = this.props,
172 prefixCls = _this$props2.prefixCls,
173 placeholder = _this$props2.placeholder,
174 disabledHours = _this$props2.disabledHours,
175 disabledMinutes = _this$props2.disabledMinutes,
176 disabledSeconds = _this$props2.disabledSeconds,
177 hideDisabledOptions = _this$props2.hideDisabledOptions,
178 inputReadOnly = _this$props2.inputReadOnly,
179 showHour = _this$props2.showHour,
180 showMinute = _this$props2.showMinute,
181 showSecond = _this$props2.showSecond,
182 defaultOpenValue = _this$props2.defaultOpenValue,
183 clearText = _this$props2.clearText,
184 addon = _this$props2.addon,
185 use12Hours = _this$props2.use12Hours,
186 focusOnOpen = _this$props2.focusOnOpen,
187 onKeyDown = _this$props2.onKeyDown,
188 hourStep = _this$props2.hourStep,
189 minuteStep = _this$props2.minuteStep,
190 secondStep = _this$props2.secondStep,
191 clearIcon = _this$props2.clearIcon;
192 var value = this.state.value;
193 return _react["default"].createElement(_Panel["default"], {
194 clearText: clearText,
195 prefixCls: "".concat(prefixCls, "-panel"),
196 ref: this.savePanelRef,
197 value: value,
198 inputReadOnly: inputReadOnly,
199 onChange: this.onPanelChange,
200 onAmPmChange: this.onAmPmChange,
201 defaultOpenValue: defaultOpenValue,
202 showHour: showHour,
203 showMinute: showMinute,
204 showSecond: showSecond,
205 onEsc: this.onEsc,
206 format: this.getFormat(),
207 placeholder: placeholder,
208 disabledHours: disabledHours,
209 disabledMinutes: disabledMinutes,
210 disabledSeconds: disabledSeconds,
211 hideDisabledOptions: hideDisabledOptions,
212 use12Hours: use12Hours,
213 hourStep: hourStep,
214 minuteStep: minuteStep,
215 secondStep: secondStep,
216 addon: addon,
217 focusOnOpen: focusOnOpen,
218 onKeyDown: onKeyDown,
219 clearIcon: clearIcon
220 });
221 }
222 }, {
223 key: "getPopupClassName",
224 value: function getPopupClassName() {
225 var _this$props3 = this.props,
226 showHour = _this$props3.showHour,
227 showMinute = _this$props3.showMinute,
228 showSecond = _this$props3.showSecond,
229 use12Hours = _this$props3.use12Hours,
230 prefixCls = _this$props3.prefixCls,
231 popupClassName = _this$props3.popupClassName;
232 var selectColumnCount = 0;
233
234 if (showHour) {
235 selectColumnCount += 1;
236 }
237
238 if (showMinute) {
239 selectColumnCount += 1;
240 }
241
242 if (showSecond) {
243 selectColumnCount += 1;
244 }
245
246 if (use12Hours) {
247 selectColumnCount += 1;
248 } // Keep it for old compatibility
249
250
251 return (0, _classnames["default"])(popupClassName, _defineProperty({}, "".concat(prefixCls, "-panel-narrow"), (!showHour || !showMinute || !showSecond) && !use12Hours), "".concat(prefixCls, "-panel-column-").concat(selectColumnCount));
252 }
253 }, {
254 key: "setOpen",
255 value: function setOpen(open) {
256 var _this$props4 = this.props,
257 onOpen = _this$props4.onOpen,
258 onClose = _this$props4.onClose;
259 var currentOpen = this.state.open;
260
261 if (currentOpen !== open) {
262 if (!('open' in this.props)) {
263 this.setState({
264 open: open
265 });
266 }
267
268 if (open) {
269 onOpen({
270 open: open
271 });
272 } else {
273 onClose({
274 open: open
275 });
276 }
277 }
278 }
279 }, {
280 key: "focus",
281 value: function focus() {
282 this.picker.focus();
283 }
284 }, {
285 key: "blur",
286 value: function blur() {
287 this.picker.blur();
288 }
289 }, {
290 key: "renderClearButton",
291 value: function renderClearButton() {
292 var _this2 = this;
293
294 var value = this.state.value;
295 var _this$props5 = this.props,
296 prefixCls = _this$props5.prefixCls,
297 allowEmpty = _this$props5.allowEmpty,
298 clearIcon = _this$props5.clearIcon,
299 clearText = _this$props5.clearText,
300 disabled = _this$props5.disabled;
301
302 if (!allowEmpty || !value || disabled) {
303 return null;
304 }
305
306 if (_react["default"].isValidElement(clearIcon)) {
307 var _ref = clearIcon.props || {},
308 _onClick = _ref.onClick;
309
310 return _react["default"].cloneElement(clearIcon, {
311 onClick: function onClick() {
312 if (_onClick) _onClick.apply(void 0, arguments);
313
314 _this2.onClear.apply(_this2, arguments);
315 }
316 });
317 }
318
319 return _react["default"].createElement("a", {
320 role: "button",
321 className: "".concat(prefixCls, "-clear"),
322 title: clearText,
323 onClick: this.onClear,
324 tabIndex: 0
325 }, clearIcon || _react["default"].createElement("i", {
326 className: "".concat(prefixCls, "-clear-icon")
327 }));
328 }
329 }, {
330 key: "render",
331 value: function render() {
332 var _this$props6 = this.props,
333 prefixCls = _this$props6.prefixCls,
334 placeholder = _this$props6.placeholder,
335 placement = _this$props6.placement,
336 align = _this$props6.align,
337 id = _this$props6.id,
338 disabled = _this$props6.disabled,
339 transitionName = _this$props6.transitionName,
340 style = _this$props6.style,
341 className = _this$props6.className,
342 getPopupContainer = _this$props6.getPopupContainer,
343 name = _this$props6.name,
344 autoComplete = _this$props6.autoComplete,
345 onFocus = _this$props6.onFocus,
346 onBlur = _this$props6.onBlur,
347 autoFocus = _this$props6.autoFocus,
348 inputReadOnly = _this$props6.inputReadOnly,
349 inputIcon = _this$props6.inputIcon,
350 popupStyle = _this$props6.popupStyle;
351 var _this$state = this.state,
352 open = _this$state.open,
353 value = _this$state.value;
354 var popupClassName = this.getPopupClassName();
355 return _react["default"].createElement(_rcTrigger["default"], {
356 prefixCls: "".concat(prefixCls, "-panel"),
357 popupClassName: popupClassName,
358 popupStyle: popupStyle,
359 popup: this.getPanelElement(),
360 popupAlign: align,
361 builtinPlacements: _placements["default"],
362 popupPlacement: placement,
363 action: disabled ? [] : ['click'],
364 destroyPopupOnHide: true,
365 getPopupContainer: getPopupContainer,
366 popupTransitionName: transitionName,
367 popupVisible: open,
368 onPopupVisibleChange: this.onVisibleChange
369 }, _react["default"].createElement("span", {
370 className: (0, _classnames["default"])(prefixCls, className),
371 style: style
372 }, _react["default"].createElement("input", {
373 className: "".concat(prefixCls, "-input"),
374 ref: this.saveInputRef,
375 type: "text",
376 placeholder: placeholder,
377 name: name,
378 onKeyDown: this.onKeyDown,
379 disabled: disabled,
380 value: value && value.format(this.getFormat()) || '',
381 autoComplete: autoComplete,
382 onFocus: onFocus,
383 onBlur: onBlur,
384 autoFocus: autoFocus,
385 onChange: noop,
386 readOnly: !!inputReadOnly,
387 id: id
388 }), inputIcon || _react["default"].createElement("span", {
389 className: "".concat(prefixCls, "-icon")
390 }), this.renderClearButton()));
391 }
392 }]);
393
394 return Picker;
395}(_react.Component);
396
397exports["default"] = Picker;
398
399_defineProperty(Picker, "propTypes", {
400 prefixCls: _propTypes["default"].string,
401 clearText: _propTypes["default"].string,
402 value: _propTypes["default"].object,
403 defaultOpenValue: _propTypes["default"].object,
404 inputReadOnly: _propTypes["default"].bool,
405 disabled: _propTypes["default"].bool,
406 allowEmpty: _propTypes["default"].bool,
407 defaultValue: _propTypes["default"].object,
408 open: _propTypes["default"].bool,
409 defaultOpen: _propTypes["default"].bool,
410 align: _propTypes["default"].object,
411 placement: _propTypes["default"].any,
412 transitionName: _propTypes["default"].string,
413 getPopupContainer: _propTypes["default"].func,
414 placeholder: _propTypes["default"].string,
415 format: _propTypes["default"].string,
416 showHour: _propTypes["default"].bool,
417 showMinute: _propTypes["default"].bool,
418 showSecond: _propTypes["default"].bool,
419 style: _propTypes["default"].object,
420 className: _propTypes["default"].string,
421 popupClassName: _propTypes["default"].string,
422 popupStyle: _propTypes["default"].object,
423 disabledHours: _propTypes["default"].func,
424 disabledMinutes: _propTypes["default"].func,
425 disabledSeconds: _propTypes["default"].func,
426 hideDisabledOptions: _propTypes["default"].bool,
427 onChange: _propTypes["default"].func,
428 onAmPmChange: _propTypes["default"].func,
429 onOpen: _propTypes["default"].func,
430 onClose: _propTypes["default"].func,
431 onFocus: _propTypes["default"].func,
432 onBlur: _propTypes["default"].func,
433 addon: _propTypes["default"].func,
434 name: _propTypes["default"].string,
435 autoComplete: _propTypes["default"].string,
436 use12Hours: _propTypes["default"].bool,
437 hourStep: _propTypes["default"].number,
438 minuteStep: _propTypes["default"].number,
439 secondStep: _propTypes["default"].number,
440 focusOnOpen: _propTypes["default"].bool,
441 onKeyDown: _propTypes["default"].func,
442 autoFocus: _propTypes["default"].bool,
443 id: _propTypes["default"].string,
444 inputIcon: _propTypes["default"].node,
445 clearIcon: _propTypes["default"].node
446});
447
448_defineProperty(Picker, "defaultProps", {
449 clearText: 'clear',
450 prefixCls: 'rc-time-picker',
451 defaultOpen: false,
452 inputReadOnly: false,
453 style: {},
454 className: '',
455 popupClassName: '',
456 popupStyle: {},
457 id: '',
458 align: {},
459 defaultOpenValue: (0, _moment["default"])(),
460 allowEmpty: true,
461 showHour: true,
462 showMinute: true,
463 showSecond: true,
464 disabledHours: noop,
465 disabledMinutes: noop,
466 disabledSeconds: noop,
467 hideDisabledOptions: false,
468 placement: 'bottomLeft',
469 onChange: noop,
470 onAmPmChange: noop,
471 onOpen: noop,
472 onClose: noop,
473 onFocus: noop,
474 onBlur: noop,
475 addon: noop,
476 use12Hours: false,
477 focusOnOpen: false,
478 onKeyDown: noop
479});
\No newline at end of file