UNPKG

21.2 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
9var _rcCalendar = require("./rc-calendar");
10
11var _rcCalendar2 = _interopRequireDefault(_rcCalendar);
12
13var _react = require("react");
14
15var _react2 = _interopRequireDefault(_react);
16
17var _reactDom = require("react-dom");
18
19var _reactDom2 = _interopRequireDefault(_reactDom);
20
21var _tinperBeeCore = require("tinper-bee-core");
22
23var _Picker = require("./rc-calendar/Picker");
24
25var _Picker2 = _interopRequireDefault(_Picker);
26
27var _beeFormControl = require("bee-form-control");
28
29var _beeFormControl2 = _interopRequireDefault(_beeFormControl);
30
31var _Panel = require("rc-time-picker/lib/Panel");
32
33var _Panel2 = _interopRequireDefault(_Panel);
34
35var _moment = require("moment");
36
37var _moment2 = _interopRequireDefault(_moment);
38
39var _beeIcon = require("bee-icon");
40
41var _beeIcon2 = _interopRequireDefault(_beeIcon);
42
43var _classnames = require("classnames");
44
45var _classnames2 = _interopRequireDefault(_classnames);
46
47var _beeInputGroup = require("bee-input-group");
48
49var _beeInputGroup2 = _interopRequireDefault(_beeInputGroup);
50
51var _zh_CN = require("./locale/zh_CN");
52
53var _zh_CN2 = _interopRequireDefault(_zh_CN);
54
55var _omit = require("omit.js");
56
57var _omit2 = _interopRequireDefault(_omit);
58
59function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
60
61function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
62
63function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
64
65function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
66
67function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
68
69function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); } /**
70 * Created by chief on 17/4/6.
71 */
72
73function noop() {}
74var timerDatePicker = true;
75
76var DatePicker = function (_Component) {
77 _inherits(DatePicker, _Component);
78
79 function DatePicker(props, context) {
80 _classCallCheck(this, DatePicker);
81
82 var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));
83
84 _initialiseProps.call(_this);
85
86 _this.state = {
87 type: "month",
88 value: _this.initValue(props),
89 open: props.open || false,
90 inputValue: _this.initValue(props),
91 showClose: false
92 };
93 _this.fileChange = true;
94
95 return _this;
96 }
97
98 DatePicker.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
99 if ("value" in nextProps) {
100 this.setState({
101 value: this.initValue(nextProps),
102 inputValue: nextProps.value && this.getValue(nextProps.value) || ''
103 });
104 }
105 if ("open" in nextProps) {
106 this.setState({
107 open: nextProps.open
108 });
109 }
110 if ("renderIcon" in nextProps) {
111 this.setState({
112 renderIcon: nextProps.renderIcon
113 });
114 }
115 };
116 //日期面板中输入框的失焦事件
117
118 //fix:更改系统时区后,日期框需要触发 onChange 事件
119
120
121 DatePicker.prototype.render = function render() {
122 var _this2 = this;
123
124 var state = this.state;
125 var props = this.props;
126
127 var showClose = props.showClose,
128 defaultPanelShown = props.defaultPanelShown,
129 onBlur = props.onBlur,
130 showHour = props.showHour,
131 showMinute = props.showMinute,
132 showSecond = props.showSecond,
133 autoTriggerChange = props.autoTriggerChange,
134 inputShowValue = props.inputShowValue,
135 tabIndex = props.tabIndex,
136 others = _objectWithoutProperties(props, ["showClose", "defaultPanelShown", "onBlur", "showHour", "showMinute", "showSecond", "autoTriggerChange", "inputShowValue", "tabIndex"]);
137
138 var value = state.value;
139 var pickerChangeHandler = {};
140 var calendarHandler = {};
141 var autofocus = !this.state.open && this.props.autofocus ? { autofocus: 'autofocus' } : null;
142
143 if (props.showTime) {
144 calendarHandler = {
145 // fix https://github.com/ant-design/ant-design/issues/1902
146 onSelect: this.handleChange
147 };
148 } else {
149 pickerChangeHandler = {
150 onChange: this.handleChange
151 };
152 }
153
154 var splitNumber = '3';
155 if (!showHour) splitNumber -= 1;
156 if (!showMinute) splitNumber -= 1;
157 if (!showSecond) splitNumber -= 1;
158
159 var calendarProps = {};
160 if (autoTriggerChange) {
161 calendarProps.value = value;
162 calendarProps.onChange = this.handleCalendarChange;
163 } else {
164 calendarProps.onChange = noop;
165 }
166
167 var calendar = _react2["default"].createElement(_rcCalendar2["default"], _extends({
168 timePicker: props.showTime ? _react2["default"].createElement(_Panel2["default"], {
169 className: 'time-split-' + splitNumber,
170 showHour: showHour, showMinute: showMinute, showSecond: showSecond,
171 defaultValue: (0, _moment2["default"])((0, _moment2["default"])().format("HH:mm:ss"), "HH:mm:ss") }) : null
172 }, (0, _omit2["default"])(props, ['value']), calendarProps, {
173 onSelect: this.handleSelect,
174 onInputBlur: this.onDateInputBlur
175 }));
176
177 var keyboardInputProps = {};
178 if (props.keyboardInput) {
179 keyboardInputProps.readOnly = false;
180 keyboardInputProps.onChange = this.inputChange;
181 keyboardInputProps.value = inputShowValue || (state.inputValue && state.inputValue.format && state.inputValue.isValid() && this.props.validatorFunc(state.inputValue) ? state.inputValue.format(props.format) : state.inputValue);
182 } else {
183 keyboardInputProps.readOnly = true;
184 keyboardInputProps.value = inputShowValue || value && this.getValue(value) || "";
185 }
186 var classes = (0, _classnames2["default"])(props.className, "datepicker-container");
187 return _react2["default"].createElement(
188 "div",
189 _extends({ className: classes, onMouseEnter: this.onDateHover
190 }, (0, _omit2["default"])(others, ['onDateInputBlur', 'getCalendarContainer', 'showToday', 'renderFooter', 'keyboardInput', 'showDateInput', 'showTime', 'closeIcon', 'renderIcon', 'focusOnOpen', 'defultSelect', 'onOpenChange', 'locale', 'showMonthInput', 'onKeyDown', 'renderError', 'format', 'placeholder', 'disabledTime', 'onChange', 'disabledDate', 'iconClick', 'outInputKeydown'])),
191 _react2["default"].createElement(
192 _Picker2["default"],
193 _extends({
194 animation: "slide-up"
195 }, props, pickerChangeHandler, {
196 onOpenChange: this.onOpenChange,
197 calendar: calendar,
198 mode: 'year',
199 open: 'defaultPanelShown' in props ? defaultPanelShown : this.state.open,
200 value: state.value
201 }),
202 function () {
203 return _react2["default"].createElement(
204 _beeInputGroup2["default"],
205 { simple: true, className: "datepicker-input-group",
206 onMouseEnter: _this2.onMouseEnter,
207 onMouseLeave: _this2.onMouseLeave
208 },
209 _react2["default"].createElement(_beeFormControl2["default"], _extends({
210 tabIndex: tabIndex,
211 ref: function ref(_ref) {
212 return _this2.outInput = _ref;
213 },
214 disabled: props.disabled,
215 placeholder: _this2.props.placeholder,
216 onClick: function onClick(event) {
217 _this2.onClick(event);
218 },
219 focusSelect: props.defaultSelected,
220 onFocus: function onFocus(v, e) {
221 _this2.outInputFocus(e);
222 },
223 onKeyDown: _this2.outInputKeydown
224 // value={(value && value.format(props.format)) || ""}
225 }, keyboardInputProps, autofocus)),
226 showClose && _this2.state.value && _this2.state.showClose && !props.disabled ? _react2["default"].createElement(
227 _beeInputGroup2["default"].Button,
228 { shape: "border",
229 onClick: _this2.clear },
230 props.closeIcon()
231 ) : _react2["default"].createElement(
232 _beeInputGroup2["default"].Button,
233 { shape: "border",
234 onClick: function onClick(e) {
235 props.keyboardInput ? _this2.iconClick(e) : '';
236 } },
237 props.renderIcon()
238 )
239 );
240 }
241 )
242 );
243 };
244
245 DatePicker.prototype.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
246 if (prevState.open && !this.state.open) {
247 _reactDom2["default"].findDOMNode(this.outInput).focus(); // 按esc时候焦点回到input输入框
248 }
249 };
250
251 return DatePicker;
252}(_react.Component);
253
254var _initialiseProps = function _initialiseProps() {
255 var _this3 = this;
256
257 this.initValue = function (props) {
258 var value = props.value || props.defaultValue;
259 if (value) {
260 if (typeof value == 'string') {
261 if ((0, _moment2["default"])(value, _this3.props.format).isValid()) {
262 value = (0, _moment2["default"])(value, _this3.props.format);
263 } else {
264 console.error('value is not in the correct format');
265 value = '';
266 }
267 } else if (value.format && value.isValid()) {
268 value = value;
269 } else {
270 console.error('value is not in the correct format');
271 value = '';
272 }
273 }
274 return value;
275 };
276
277 this.getValue = function (value) {
278 var format = _this3.props.format;
279
280 if (value.format) {
281 if (typeof format == 'string') {
282 return value.format(format);
283 } else {
284 return value.format(format[0]);
285 }
286 }
287 return value;
288 };
289
290 this.onChange = function (value) {
291 _this3.setState({ value: value });
292 };
293
294 this.inputFocus = function () {
295 var _props = _this3.props,
296 format = _props.format,
297 validatorFunc = _props.validatorFunc,
298 disabledDate = _props.disabledDate;
299
300 var input = document.querySelector('.rc-calendar-input');
301 if (input) {
302 if (input.value) {
303 input.select();
304 } else {
305 input.focus();
306 }
307 input.onkeydown = function (e) {
308 // 日历的input
309 if (e.keyCode == _tinperBeeCore.KeyCode.DELETE) {
310 input.value = '';
311 _this3.fireChange('', '');
312 } else if (e.keyCode == _tinperBeeCore.KeyCode.ESC || e.keyCode == _tinperBeeCore.KeyCode.TAB) {
313 if (e.keyCode == _tinperBeeCore.KeyCode.TAB) {
314 console.debug('[bee-datepicker] [DatePicker] e.keyCode == KeyCode.TAB');
315 }
316 _this3.setState({
317 open: false
318 });
319 e.target._dataTransfer = {
320 owner: _reactDom2["default"].findDOMNode(_this3.outInput),
321 _target: e.target,
322 open: false
323 };
324
325 console.debug(' [bee-datepicker] [DatePicker] ReactDOM.findDOMNode(this.outInput)', _reactDom2["default"].findDOMNode(_this3.outInput));
326 // input.blur();
327
328 // 按esc时候焦点回到input输入框
329 _reactDom2["default"].findDOMNode(_this3.outInput).focus();
330 _reactDom2["default"].findDOMNode(_this3.outInput).select();
331 // e.stopPropagation();
332
333 var v = _this3.state.value;
334 _this3.props.onOpenChange(false, v, v && _this3.getValue(v) || '');
335 } else if (e.keyCode == _tinperBeeCore.KeyCode.ENTER) {
336 var parsed = (0, _moment2["default"])(input.value, format, true);
337 var isDisabled = disabledDate && disabledDate(parsed);
338 if (parsed.isValid() && validatorFunc(input.value) && !isDisabled) {
339 _this3.setState({
340 open: false
341 });
342 var _v = _this3.state.value;
343 _this3.props.onOpenChange(false, _v, _v && _this3.getValue(_v) || '');
344 _reactDom2["default"].findDOMNode(_this3.outInput).focus();
345 }
346 if (!input.value) {
347 _this3.setState({
348 open: false
349 });
350 }
351 } else if (e.keyCode >= 37 && e.keyCode <= 40) {
352 // 向下
353 // 自定义_dataTransfer
354 e.target._dataTransfer = {
355 owner: _reactDom2["default"].findDOMNode(_this3.outInput),
356 _target: e.target,
357 open: _this3.state.open
358 };
359 }
360 _this3.props.onKeyDown && _this3.props.onKeyDown(e);
361 };
362 }
363 };
364
365 this.onOpenChange = function (open) {
366 var props = _this3.props;
367 var self = _this3;
368 _this3.setState({
369 open: open
370 }, function () {
371 if (open) {
372 setTimeout(function () {
373 self.inputFocus();
374 }, 0);
375 }
376 });
377 var value = self.state.value;
378 props.onOpenChange(open, value, value && _this3.getValue(value) || '');
379 if (open) {
380 setTimeout(function () {
381 self.inputFocus();
382 }, 200);
383 }
384 };
385
386 this.handleCalendarChange = function (value) {
387 var props = _this3.props;
388 _this3.setState({ value: value, inputValue: value && _this3.getValue(value) || '' });
389 _this3.fireChange(value, value && _this3.getValue(value) || '');
390 };
391
392 this.handleChange = function (value) {
393 var props = _this3.props;
394 _this3.setState({
395 value: value && _extends(value, { _type: 'date' }) || value,
396 inputValue: value && _this3.getValue(value) || ''
397 });
398 if (timerDatePicker) {
399 clearTimeout(_this3.timerout);
400 _this3.fireChange(value, value && _this3.getValue(value) || '');
401 timerDatePicker = false;
402 _this3.timerout = window.setTimeout(function () {
403 timerDatePicker = true;
404 }, 300);
405 }
406 };
407
408 this.onClick = function (e) {
409 var props = _this3.props;
410 if (props.keyboardInput) e.stopPropagation();
411 var value = _this3.state.value;
412 if (props.keyboardInput) {
413 props.onClick && props.onClick(e.nativeEvent, value || null, _this3.state.inputValue);
414 } else {
415 props.onClick && props.onClick(e.nativeEvent, value || null, value && _this3.getValue(value) || '');
416 }
417 };
418
419 this.inputChange = function (value, e) {
420 if (_this3.props.keyboardInput) e.stopPropagation();
421 _this3.setState({
422 inputValue: value
423 });
424 if ((0, _moment2["default"])(value, _this3.props.format, true).isValid() && _this3.props.validatorFunc(value)) {
425 _this3.setState({
426 value: (0, _moment2["default"])(value, _this3.props.format)
427 });
428 value = (0, _moment2["default"])(value, _this3.props.format);
429 _this3.fireChange(value, value && _this3.getValue(value) || '');
430 } else {
431 _this3.fireChange(null, value);
432 }
433 };
434
435 this.outInputFocus = function (e) {
436 if (_this3.props.hasOwnProperty('open')) e.stopPropagation();
437 _this3.props.outInputFocus && _this3.props.outInputFocus(e);
438 };
439
440 this.iconClick = function (e) {
441 _this3.props.iconClick && _this3.props.iconClick(e);
442 };
443
444 this.outInputKeydown = function (e) {
445 // 外部(非弹窗日历)核心input
446 if (e.keyCode == _tinperBeeCore.KeyCode.DELETE) {
447 _this3.setState({
448 inputValue: ''
449 });
450 _this3.fireChange('', '');
451 } else if (e.keyCode == _tinperBeeCore.KeyCode.ESC) {
452 console.debug('c%==========================[bee-datepicker] [DatePicker] [outInputKeydown()] e.keyCode == KeyCode.ESC', 'color:blue');
453 _this3.setState({
454 open: false
455 });
456 e.target._dataTransfer = {
457 open: false,
458 owner: e.target,
459 _target: e.target,
460 ownerIsTarget: true
461 };
462 var value = _this3.state.inputValue;
463 if ((0, _moment2["default"])(value, _this3.props.format).isValid() && _this3.props.validatorFunc(value)) {
464 _this3.setState({
465 value: (0, _moment2["default"])(value, _this3.props.format)
466 });
467 value = (0, _moment2["default"])(value, _this3.props.format);
468 _this3.fireChange(value, value && _this3.getValue(value) || '');
469 } else {
470 _this3.fireChange(null, value);
471 }
472 } else {
473 console.debug('==========================[bee-datepicker] [DatePicker] [outInputKeydown()] e.keyCode == ' + e.keyCode);
474 }
475 if (_this3.props.outInputKeydown) {
476 console.debug('======================[bee-datepicker] [DatePicker] [outInputKeydown()] exist this.props.outInputKeydown and the props is ,' + _this3.props);
477 _this3.props.outInputKeydown(e);
478 } else {
479 console.debug('======================[bee-datepicker] [DatePicker] [outInputKeydown()] don\'t exist this.props.outInputKeydown and the props is ,' + _this3.props);
480 }
481 };
482
483 this.onMouseLeave = function (e) {
484 _this3.setState({
485 showClose: false
486 });
487 };
488
489 this.onMouseEnter = function (e) {
490 _this3.setState({
491 showClose: true
492 });
493 };
494
495 this.clear = function (e) {
496 e.stopPropagation();
497 _this3.setState({
498 inputValue: '',
499 value: ''
500 });
501 _this3.fireChange('', '');
502 };
503
504 this.handleSelect = function (value) {
505 _this3.setState({
506 value: value
507 });
508 _this3.props.onSelect && _this3.props.onSelect(value, value && _this3.getValue(value) || '');
509 // ReactDOM.findDOMNode(this.outInput).focus()
510 };
511
512 this.onDateInputBlur = function (e) {
513 var input = document.querySelector('.rc-calendar-input');
514 var value = void 0;
515 if (input) {
516 value = input.value ? input.value : '';
517 }
518 _this3.props.onDateInputBlur && _this3.props.onDateInputBlur(e, value);
519 };
520
521 this.onDateHover = function () {
522 var _props2 = _this3.props,
523 format = _props2.format,
524 inputShowValue = _props2.inputShowValue;
525 var value = _this3.state.value,
526 newValue = value && _this3.getValue(value);
527
528 var inputValue = _this3.outInput.state.value;
529 inputValue = format ? inputValue : inputValue && _this3.getValue((0, _moment2["default"])(inputValue));
530 if (newValue && !inputShowValue && inputValue !== newValue) {
531 _this3.fireChange(value, newValue || '');
532 }
533 };
534
535 this.fireChange = function (value, stringValue) {
536 _this3.fileChange && _this3.props.onChange(value, stringValue);
537 _this3.fileChange = false;
538 _this3.fileChangeTimer = window.setTimeout(function () {
539 _this3.fileChange = true;
540 }, 10);
541 };
542};
543
544DatePicker.defaultProps = {
545 closeIcon: function closeIcon() {
546 return _react2["default"].createElement(_beeIcon2["default"], { type: "uf-close-c" });
547 },
548 renderIcon: function renderIcon() {
549 return _react2["default"].createElement(_beeIcon2["default"], { type: "uf-calendar" });
550 },
551 focusOnOpen: true,
552 defultSelect: false,
553 onOpenChange: function onOpenChange() {},
554 onChange: function onChange() {},
555 locale: _zh_CN2["default"],
556 showMonthInput: false,
557 onKeyDown: function onKeyDown() {},
558 renderError: function renderError() {},
559 showClose: true,
560 format: "YYYY-MM-DD",
561 showSecond: true,
562 showHour: true,
563 showMinute: true,
564 autoTriggerChange: true,
565 validatorFunc: function validatorFunc() {
566 return true;
567 }
568};
569
570exports["default"] = DatePicker;
571module.exports = exports["default"];
\No newline at end of file