1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _createClass from 'babel-runtime/helpers/createClass';
|
3 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
4 | import _inherits from 'babel-runtime/helpers/inherits';
|
5 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
6 | import React from 'react';
|
7 | import Animate from 'rc-animate';
|
8 | import TimePicker from './TimePicker';
|
9 | import DatePicker from './DatePicker';
|
10 | import ConfirmPanel from './calendar/ConfirmPanel';
|
11 | import ShortcutPanel from './calendar/ShortcutPanel';
|
12 | import AnimateWrapper from './calendar/AnimateWrapper';
|
13 | import Header from './calendar/Header';
|
14 | import { mergeDateTime } from './util';
|
15 | import defaultLocale from './locale/zh_CN';
|
16 | export var StateType = function StateType() {
|
17 | _classCallCheck(this, StateType);
|
18 |
|
19 | this.showTimePicker = false;
|
20 | this.startDate = undefined;
|
21 | this.endDate = undefined;
|
22 | this.disConfirmBtn = true;
|
23 | this.clientHight = 0;
|
24 | };
|
25 |
|
26 | var Calendar = function (_React$PureComponent) {
|
27 | _inherits(Calendar, _React$PureComponent);
|
28 |
|
29 | function Calendar(props) {
|
30 | _classCallCheck(this, Calendar);
|
31 |
|
32 | var _this = _possibleConstructorReturn(this, (Calendar.__proto__ || Object.getPrototypeOf(Calendar)).call(this, props));
|
33 |
|
34 | _this.selectDate = function (date) {
|
35 | var useDateTime = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
36 | var startDate = arguments[2];
|
37 | var endDate = arguments[3];
|
38 |
|
39 | if (!date) return {};
|
40 | var newState = {};
|
41 | var _this$props = _this.props,
|
42 | type = _this$props.type,
|
43 | pickTime = _this$props.pickTime,
|
44 | defaultTimeValue = _this$props.defaultTimeValue,
|
45 | _this$props$locale = _this$props.locale,
|
46 | locale = _this$props$locale === undefined ? {} : _this$props$locale;
|
47 |
|
48 | var newDate = pickTime && !useDateTime ? mergeDateTime(date, defaultTimeValue) : date;
|
49 | switch (type) {
|
50 | case 'one':
|
51 | newState = _extends({}, newState, { startDate: newDate, disConfirmBtn: false });
|
52 | if (pickTime) {
|
53 | newState = _extends({}, newState, { timePickerTitle: locale.selectTime, showTimePicker: true });
|
54 | }
|
55 | break;
|
56 | case 'range':
|
57 | if (!startDate || endDate) {
|
58 | newState = _extends({}, newState, { startDate: newDate, endDate: undefined, disConfirmBtn: true });
|
59 | if (pickTime) {
|
60 | newState = _extends({}, newState, { timePickerTitle: locale.selectStartTime, showTimePicker: true });
|
61 | }
|
62 | } else {
|
63 | newState = _extends({}, newState, { timePickerTitle: +newDate >= +startDate ? locale.selectEndTime : locale.selectStartTime, disConfirmBtn: false, endDate: pickTime && !useDateTime && +newDate >= +startDate ? new Date(+mergeDateTime(newDate, startDate) + 3600000) : newDate });
|
64 | }
|
65 | break;
|
66 | }
|
67 | return newState;
|
68 | };
|
69 | _this.onSelectedDate = function (date) {
|
70 | var _this$state = _this.state,
|
71 | startDate = _this$state.startDate,
|
72 | endDate = _this$state.endDate;
|
73 |
|
74 | _this.setState(_this.selectDate(date, false, startDate, endDate));
|
75 | };
|
76 | _this.onSelectHasDisableDate = function (date) {
|
77 | _this.onClear();
|
78 | if (_this.props.onSelectHasDisableDate) {
|
79 | _this.props.onSelectHasDisableDate(date);
|
80 | }
|
81 | };
|
82 | _this.onClose = function () {
|
83 | _this.setState(new StateType());
|
84 | };
|
85 | _this.onCancel = function () {
|
86 | _this.onClose();
|
87 | _this.props.onCancel && _this.props.onCancel();
|
88 | };
|
89 | _this.onConfirm = function () {
|
90 | _this.onClose();
|
91 | var onConfirm = _this.props.onConfirm;
|
92 | var _this$state2 = _this.state,
|
93 | startDate = _this$state2.startDate,
|
94 | endDate = _this$state2.endDate;
|
95 |
|
96 | if (startDate && endDate && +startDate > +endDate) {
|
97 | return onConfirm && onConfirm(endDate, startDate);
|
98 | }
|
99 | onConfirm && onConfirm(startDate, endDate);
|
100 | };
|
101 | _this.onTimeChange = function (date) {
|
102 | var _this$state3 = _this.state,
|
103 | startDate = _this$state3.startDate,
|
104 | endDate = _this$state3.endDate;
|
105 |
|
106 | if (endDate) {
|
107 | _this.setState({
|
108 | endDate: date
|
109 | });
|
110 | } else if (startDate) {
|
111 | _this.setState({
|
112 | startDate: date
|
113 | });
|
114 | }
|
115 | };
|
116 | _this.onClear = function () {
|
117 | _this.setState({
|
118 | startDate: undefined,
|
119 | endDate: undefined,
|
120 | showTimePicker: false
|
121 | });
|
122 | };
|
123 | _this.shortcutSelect = function (startDate, endDate) {
|
124 | var state = _this.selectDate(startDate, true);
|
125 | _this.setState(_extends({}, state, _this.selectDate(endDate, true, state.startDate), { showTimePicker: false }));
|
126 | };
|
127 | _this.setClientHight = function (height) {
|
128 | _this.setState({
|
129 | clientHight: height
|
130 | });
|
131 | };
|
132 | _this.state = new StateType();
|
133 | return _this;
|
134 | }
|
135 |
|
136 | _createClass(Calendar, [{
|
137 | key: 'render',
|
138 | value: function render() {
|
139 | var _props = this.props,
|
140 | type = _props.type,
|
141 | _props$locale = _props.locale,
|
142 | locale = _props$locale === undefined ? {} : _props$locale,
|
143 | prefixCls = _props.prefixCls,
|
144 | visible = _props.visible,
|
145 | pickTime = _props.pickTime,
|
146 | showShortcut = _props.showShortcut,
|
147 | renderHeader = _props.renderHeader,
|
148 | infinite = _props.infinite,
|
149 | infiniteOpt = _props.infiniteOpt,
|
150 | initalMonths = _props.initalMonths,
|
151 | defaultDate = _props.defaultDate,
|
152 | minDate = _props.minDate,
|
153 | maxDate = _props.maxDate,
|
154 | getDateExtra = _props.getDateExtra,
|
155 | rowSize = _props.rowSize,
|
156 | defaultTimeValue = _props.defaultTimeValue,
|
157 | renderShortcut = _props.renderShortcut,
|
158 | enterDirection = _props.enterDirection,
|
159 | timePickerPrefixCls = _props.timePickerPrefixCls,
|
160 | timePickerPickerPrefixCls = _props.timePickerPickerPrefixCls,
|
161 | style = _props.style;
|
162 | var _state = this.state,
|
163 | showTimePicker = _state.showTimePicker,
|
164 | timePickerTitle = _state.timePickerTitle,
|
165 | startDate = _state.startDate,
|
166 | endDate = _state.endDate,
|
167 | disConfirmBtn = _state.disConfirmBtn,
|
168 | clientHight = _state.clientHight;
|
169 |
|
170 | var headerProps = {
|
171 | locale: locale,
|
172 | showClear: !!startDate,
|
173 | onCancel: this.onCancel,
|
174 | onClear: this.onClear
|
175 | };
|
176 | return React.createElement(
|
177 | 'div',
|
178 | { className: '' + prefixCls, style: style },
|
179 | React.createElement(
|
180 | Animate,
|
181 | { showProp: 'visible', transitionName: 'fade' },
|
182 | React.createElement(AnimateWrapper, { className: 'mask', visible: !!visible })
|
183 | ),
|
184 | React.createElement(
|
185 | Animate,
|
186 | { showProp: 'visible', transitionName: enterDirection === 'horizontal' ? 'slideH' : 'slideV' },
|
187 | React.createElement(
|
188 | AnimateWrapper,
|
189 | { className: 'content', visible: !!visible },
|
190 | renderHeader ? renderHeader(headerProps) : React.createElement(Header, headerProps),
|
191 | React.createElement(DatePicker, { locale: locale, type: type, prefixCls: prefixCls, infinite: infinite, infiniteOpt: infiniteOpt, initalMonths: initalMonths, defaultDate: defaultDate, minDate: minDate, maxDate: maxDate, getDateExtra: getDateExtra, onCellClick: this.onSelectedDate, onSelectHasDisableDate: this.onSelectHasDisableDate, onLayout: this.setClientHight, startDate: startDate, endDate: endDate, rowSize: rowSize }),
|
192 | showTimePicker && React.createElement(TimePicker, { prefixCls: timePickerPrefixCls, pickerPrefixCls: timePickerPickerPrefixCls, locale: locale, title: timePickerTitle, defaultValue: defaultTimeValue, value: endDate ? endDate : startDate, onValueChange: this.onTimeChange, minDate: minDate, maxDate: maxDate, clientHeight: clientHight }),
|
193 | showShortcut && !showTimePicker && (renderShortcut ? renderShortcut(this.shortcutSelect) : React.createElement(ShortcutPanel, { locale: locale, onSelect: this.shortcutSelect })),
|
194 | startDate && React.createElement(ConfirmPanel, { type: type, locale: locale, startDateTime: startDate, endDateTime: endDate, onConfirm: this.onConfirm, disableBtn: disConfirmBtn, formatStr: pickTime ? locale.dateTimeFormat : locale.dateFormat })
|
195 | )
|
196 | )
|
197 | );
|
198 | }
|
199 | }]);
|
200 |
|
201 | return Calendar;
|
202 | }(React.PureComponent);
|
203 |
|
204 | export default Calendar;
|
205 |
|
206 | Calendar.DefaultHeader = Header;
|
207 | Calendar.DefaultShortcut = ShortcutPanel;
|
208 | Calendar.defaultProps = {
|
209 | visible: false,
|
210 | showHeader: true,
|
211 | locale: defaultLocale,
|
212 | pickTime: false,
|
213 | showShortcut: false,
|
214 | prefixCls: 'rmc-calendar',
|
215 | type: 'range',
|
216 | defaultTimeValue: new Date(2000, 0, 1, 8)
|
217 | }; |
\ | No newline at end of file |