1 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
2 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
3 | import _inherits from 'babel-runtime/helpers/inherits';
|
4 | import React from 'react';
|
5 | import PropTypes from 'prop-types';
|
6 | import classnames from 'classnames';
|
7 | import moment from 'moment';
|
8 | import { isAllowedDate, getTodayTime } from '../util/index';
|
9 |
|
10 | function noop() {}
|
11 |
|
12 | export function getNowByCurrentStateValue(value) {
|
13 | var ret = void 0;
|
14 | if (value) {
|
15 | ret = getTodayTime(value);
|
16 | } else {
|
17 | ret = moment();
|
18 | }
|
19 | return ret;
|
20 | }
|
21 |
|
22 | export var calendarMixinPropTypes = {
|
23 | value: PropTypes.object,
|
24 | defaultValue: PropTypes.object,
|
25 | onKeyDown: PropTypes.func
|
26 | };
|
27 |
|
28 | export var calendarMixinDefaultProps = {
|
29 | onKeyDown: noop
|
30 | };
|
31 |
|
32 | export var calendarMixinWrapper = function calendarMixinWrapper(ComposeComponent) {
|
33 | var _class, _temp2;
|
34 |
|
35 | return _temp2 = _class = function (_ComposeComponent) {
|
36 | _inherits(_class, _ComposeComponent);
|
37 |
|
38 | function _class() {
|
39 | var _temp, _this, _ret;
|
40 |
|
41 | _classCallCheck(this, _class);
|
42 |
|
43 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
44 | args[_key] = arguments[_key];
|
45 | }
|
46 |
|
47 | return _ret = (_temp = (_this = _possibleConstructorReturn(this, _ComposeComponent.call.apply(_ComposeComponent, [this].concat(args))), _this), _this.onSelect = function (value, cause) {
|
48 | if (value) {
|
49 | _this.setValue(value);
|
50 | }
|
51 | _this.setSelectedValue(value, cause);
|
52 | }, _this.renderRoot = function (newProps) {
|
53 | var _className;
|
54 |
|
55 | var props = _this.props;
|
56 | var prefixCls = props.prefixCls;
|
57 |
|
58 | var className = (_className = {}, _className[prefixCls] = 1, _className[prefixCls + '-hidden'] = !props.visible, _className[props.className] = !!props.className, _className[newProps.className] = !!newProps.className, _className);
|
59 |
|
60 | return React.createElement(
|
61 | 'div',
|
62 | {
|
63 | ref: _this.saveRoot,
|
64 | className: '' + classnames(className),
|
65 | style: _this.props.style,
|
66 | tabIndex: '0',
|
67 | onKeyDown: _this.onKeyDown,
|
68 | onBlur: _this.onBlur
|
69 | },
|
70 | newProps.children
|
71 | );
|
72 | }, _this.setSelectedValue = function (selectedValue, cause) {
|
73 |
|
74 | if (!('selectedValue' in _this.props)) {
|
75 | _this.setState({
|
76 | selectedValue: selectedValue
|
77 | });
|
78 | }
|
79 | if (_this.props.onSelect) {
|
80 | _this.props.onSelect(selectedValue, cause);
|
81 | }
|
82 |
|
83 | }, _this.setValue = function (value) {
|
84 | var originalValue = _this.state.value;
|
85 | if (!('value' in _this.props)) {
|
86 | _this.setState({
|
87 | value: value
|
88 | });
|
89 | }
|
90 | if (originalValue && value && !originalValue.isSame(value) || !originalValue && value || originalValue && !value) {
|
91 | _this.props.onChange(value);
|
92 | }
|
93 | }, _this.isAllowedDate = function (value) {
|
94 | var disabledDate = _this.props.disabledDate;
|
95 | var disabledTime = _this.props.disabledTime;
|
96 | return isAllowedDate(value, disabledDate, disabledTime);
|
97 | }, _temp), _possibleConstructorReturn(_this, _ret);
|
98 | }
|
99 |
|
100 | _class.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) {
|
101 |
|
102 | if (ComposeComponent.getDerivedStateFromProps) {
|
103 | return ComposeComponent.getDerivedStateFromProps(nextProps, prevState);
|
104 | }
|
105 |
|
106 | var value = nextProps.value,
|
107 | selectedValue = nextProps.selectedValue;
|
108 |
|
109 | var newState = {};
|
110 |
|
111 | if ('value' in nextProps) {
|
112 | newState.value = value || nextProps.defaultValue || getNowByCurrentStateValue(prevState.value);
|
113 | }
|
114 | if ('selectedValue' in nextProps) {
|
115 | newState.selectedValue = selectedValue;
|
116 | }
|
117 |
|
118 | return newState;
|
119 | };
|
120 |
|
121 | return _class;
|
122 | }(ComposeComponent), _class.displayName = 'CalendarMixinWrapper', _class.defaultProps = ComposeComponent.defaultProps, _temp2;
|
123 | }; |
\ | No newline at end of file |