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