UNPKG

17.9 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 _react = require("react");
10
11var _react2 = _interopRequireDefault(_react);
12
13var _RangeCalendar = require("./rc-calendar/RangeCalendar");
14
15var _RangeCalendar2 = _interopRequireDefault(_RangeCalendar);
16
17var _Panel = require("rc-time-picker/lib/Panel");
18
19var _Panel2 = _interopRequireDefault(_Panel);
20
21var _beeFormControl = require("bee-form-control");
22
23var _beeFormControl2 = _interopRequireDefault(_beeFormControl);
24
25var _Picker = require("./rc-calendar/Picker");
26
27var _Picker2 = _interopRequireDefault(_Picker);
28
29var _beeInputGroup = require("bee-input-group");
30
31var _beeInputGroup2 = _interopRequireDefault(_beeInputGroup);
32
33var _beeIcon = require("bee-icon");
34
35var _beeIcon2 = _interopRequireDefault(_beeIcon);
36
37var _classnames = require("classnames");
38
39var _classnames2 = _interopRequireDefault(_classnames);
40
41var _tinperBeeCore = require("tinper-bee-core");
42
43var _util = require("./rc-calendar/util");
44
45var _zh_CN = require("./locale/zh_CN");
46
47var _zh_CN2 = _interopRequireDefault(_zh_CN);
48
49var _omit = require("omit.js");
50
51var _omit2 = _interopRequireDefault(_omit);
52
53var _moment = require("moment");
54
55var _moment2 = _interopRequireDefault(_moment);
56
57require("moment/locale/zh-cn");
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 _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure undefined"); }
64
65function _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; }
66
67function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
68
69function _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; }
70
71function _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); } /**
72 * Created by chief on 17/4/6.
73 */
74
75
76// function formatDate(value,format) {
77// if (!value) {
78// return '';
79// }
80
81// if (Array.isArray(format)) {
82// format = format[0];
83// }
84
85// return value.formatDate(format);
86// }
87
88var fullFormat = "YYYY-MM-DD";
89
90var cn = typeof window !== 'undefined' ? location.search.indexOf("cn") !== -1 : true;
91
92var now = (0, _moment2["default"])();
93
94function isValidRange(v) {
95 return v && v[0] && v[1];
96}
97
98if (cn) {
99 now.locale("zh-cn").utcOffset(8);
100} else {
101 now.locale("en-gb").utcOffset(0);
102}
103
104var RangePicker = function (_Component) {
105 _inherits(RangePicker, _Component);
106
107 function RangePicker(props, context) {
108 _classCallCheck(this, RangePicker);
109
110 var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));
111
112 _initialiseProps.call(_this);
113
114 _this.state = {
115 hoverValue: [],
116 value: _this.initValue(props),
117 open: props.open || false
118 };
119 return _this;
120 }
121
122 RangePicker.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
123 if ("value" in nextProps) {
124 this.setState({
125 value: this.initValue(nextProps)
126 });
127 }
128 if ("open" in nextProps) {
129 this.setState({
130 open: nextProps.open
131 });
132 }
133 this.setState({
134 renderIcon: nextProps.renderIcon
135 });
136 };
137 //判断value是否为空
138
139 //日期面板中输入框的失焦事件
140
141 //阻止组件内部事件冒泡到组件外部容器
142
143
144 RangePicker.prototype.render = function render() {
145 var _this2 = this;
146
147 var props = this.props;
148
149 var showClose = props.showClose,
150 onChange = props.onChange,
151 showHour = props.showHour,
152 showMinute = props.showMinute,
153 showSecond = props.showSecond,
154 tabIndex = props.tabIndex,
155 others = _objectWithoutProperties(props, ["showClose", "onChange", "showHour", "showMinute", "showSecond", "tabIndex"]);
156
157 var _state = this.state,
158 value = _state.value,
159 open = _state.open;
160
161 var formatStr = props.format || 'YYYY-MM-DD';
162 var timePickerElement = _react2["default"].createElement(_Panel2["default"], {
163 showHour: showHour, showMinute: showMinute, showSecond: showSecond,
164 defaultValue: (0, _moment2["default"])((0, _moment2["default"])().format("HH:mm:ss"), "HH:mm:ss") });
165 var calendar = _react2["default"].createElement(_RangeCalendar2["default"], {
166 hoverValue: this.state.hoverValue,
167 onHoverChange: this.onHoverChange,
168 showWeekNumber: false,
169 format: formatStr,
170 dateInputPlaceholder: props.dateInputPlaceholder || ['start', 'end'],
171 locale: props.locale || _zh_CN2["default"],
172 onChange: this.onChange,
173 disabledDate: props.disabledDate,
174 showClear: props.showClear,
175 showOk: props.showOk,
176 showToday: props.showToday,
177 renderFooter: props.renderFooter,
178 timePicker: props.showTime ? timePickerElement : null,
179 renderError: props.renderError,
180 onStartInputBlur: this.onStartInputBlur,
181 onEndInputBlur: this.onEndInputBlur,
182 onClear: this.clear,
183 onOk: this.onOk,
184 validatorFunc: props.validatorFunc
185 });
186 return _react2["default"].createElement(
187 "div",
188 _extends({
189 onClick: this.stopPropagation, onMouseOver: this.stopPropagation
190 }, (0, _omit2["default"])(others, ['closeIcon', 'renderIcon', 'showClear', 'showToday', 'locale', 'placeholder', 'showOk', 'dateInputPlaceholder', 'onPanelChange', 'onStartInputBlur', 'onEndInputBlur', 'renderFooter', 'showTime', 'disabledDate', 'disabledTime'])),
191 _react2["default"].createElement(
192 _Picker2["default"],
193 _extends({}, props, {
194 value: value,
195 animation: 'animation' in props ? props.animation : "slide-up",
196 calendar: calendar,
197 disabled: props.disabled,
198 dropdownClassName: props.dropdownClassName,
199 onOpenChange: this.onOpenChange,
200 open: open
201 }),
202 function (_ref) {
203 _objectDestructuringEmpty(_ref);
204
205 return _react2["default"].createElement(
206 "div",
207 { className: (0, _classnames2["default"])('calendar-picker', 'u-input-group', 'simple', props.className),
208 onMouseEnter: _this2.onMouseEnter,
209 onMouseLeave: _this2.onMouseLeave
210 },
211 _react2["default"].createElement(_beeFormControl2["default"], {
212 placeholder: _this2.props.placeholder ? _this2.props.placeholder : 'start ~ end',
213 value: isValidRange(value) && (_this2.props.inputShowValue && _this2.props.inputShowValue[0] && _this2.props.inputShowValue[1] ? _this2.props.inputShowValue[0] + " ~ " + _this2.props.inputShowValue[1] : (0, _util.formatDate)(value[0], formatStr) + " ~ " + (0, _util.formatDate)(value[1], formatStr)) || '',
214 disabled: props.disabled,
215 onFocus: function onFocus(v, e) {
216 _this2.outInputFocus(e);
217 },
218 tabIndex: tabIndex
219 }),
220 showClose && !_this2.valueIsEmpty(value) && _this2.state.showClose && !props.disabled ? _react2["default"].createElement(
221 _beeInputGroup2["default"].Button,
222 { shape: "border",
223 onClick: _this2.clear },
224 props.closeIcon()
225 ) : _react2["default"].createElement(
226 _beeInputGroup2["default"].Button,
227 { shape: "border" },
228 props.renderIcon()
229 )
230 );
231 }
232 )
233 );
234 };
235
236 return RangePicker;
237}(_react.Component);
238
239var _initialiseProps = function _initialiseProps() {
240 var _this3 = this;
241
242 this.initValue = function (props) {
243 var valueProp = props.value || props.defaultValue || [];
244 var values = [];
245 for (var i = 0; i < 2; i++) {
246 var value = valueProp[i] || '';
247 if (value) {
248 if (typeof value == 'string') {
249 if ((0, _moment2["default"])(value).isValid()) {
250 values.push((0, _moment2["default"])(value));
251 } else {
252 console.error('value is not in the correct format');
253 values.push('');
254 }
255 } else if (value.format && value.isValid()) {
256 values.push(value);
257 } else {
258 console.error('value is not in the correct format');
259 values = [];
260 }
261 } else {
262 values.push('');
263 }
264 }
265 return values;
266 };
267
268 this.clearHoverValue = function () {
269 return _this3.setState({ hoverValue: [] });
270 };
271
272 this.onChange = function (value) {
273 var props = _this3.props;
274 var formatStr = props.format || 'YYYY-MM-DD';
275 if (value.length < 2) {
276 return;
277 }
278 _this3.setState({
279 value: value
280 });
281
282 //传入value和dateString
283 if (props.onChange && isValidRange(value) || value.length == 0) {
284 if (value.length > 0) {
285 props.onChange(value, "[\"" + (0, _util.formatDate)(value[0], formatStr) + "\" , \"" + (0, _util.formatDate)(value[1], formatStr) + "\"]", ["" + (0, _util.formatDate)(value[0], formatStr), "" + (0, _util.formatDate)(value[1], formatStr)]);
286 } else {
287 props.onChange(null);
288 }
289 } else {
290 props.onPanelChange && props.onPanelChange(value);
291 }
292 };
293
294 this.onHoverChange = function (hoverValue) {
295 _this3.setState({ hoverValue: hoverValue });
296 _this3.props.onHoverChange && _this3.props.onHoverChange(hoverValue);
297 };
298
299 this.remove = function (e) {
300 _this3.setState({ value: '' });
301 };
302
303 this.handleCalendarChange = function (value) {};
304
305 this.onMouseLeave = function (e) {
306 _this3.setState({
307 showClose: false
308 });
309 };
310
311 this.onMouseEnter = function (e) {
312 if (!_this3.valueIsEmpty(_this3.state.value)) {
313 _this3.setState({
314 showClose: true
315 });
316 }
317 };
318
319 this.valueIsEmpty = function (value) {
320 if (value) {
321 if (value.length == 0) {
322 return true;
323 } else {
324 //value.length>0
325 var flag = true;
326 if (value[0] || value[1]) flag = false;
327 return flag;
328 }
329 } else {
330 return true;
331 }
332 };
333
334 this.clear = function (e) {
335 e && e.stopPropagation && e.stopPropagation();
336 _this3.setState({
337 value: []
338 });
339 _this3.props.onChange && _this3.props.onChange([], []);
340 _this3.props.onFormControlClear && _this3.props.onFormControlClear();
341 };
342
343 this.onOpenChange = function (open) {
344 var props = _this3.props;
345 var self = _this3;
346 // let {value} = this.state;
347 // if(!isValidRange(value)){
348 // debugger
349 // }
350 if (open === false) {
351 _this3.clearHoverValue();
352 }
353
354 _this3.setState({
355 open: open
356 }, function () {
357 if (open) {
358 setTimeout(function () {
359 self.inputFocus();
360 }, 0);
361 }
362 });
363 props.onOpenChange && props.onOpenChange(open);
364 if (open) {
365 setTimeout(function () {
366 self.inputFocus();
367 }, 200);
368 }
369 };
370
371 this.outInputFocus = function (e) {
372 if (_this3.props.hasOwnProperty('open')) e.stopPropagation();
373 _this3.props.outInputFocus && _this3.props.outInputFocus(e);
374 };
375
376 this.inputFocus = function () {
377 var inputs = document.querySelectorAll('.rc-calendar-input');
378 if (!inputs) {
379 return;
380 }
381 if (inputs[0].value) {
382 inputs[0].select();
383 } else {
384 inputs[0].focus();
385 }
386 inputs[0].onkeydown = _this3.keydownLeft;
387 inputs[1].onkeydown = _this3.keydownRight;
388 };
389
390 this.keydownLeft = function (e) {
391 var inputs = document.querySelectorAll('.rc-calendar-input');
392 if (e.keyCode == _tinperBeeCore.KeyCode.ESC) {
393 _this3.setState({
394 open: false
395 });
396 _this3.props.onOpenChange(false, v, v && _this3.getValue(v) || '');
397 }
398 if (e.keyCode == _tinperBeeCore.KeyCode.RIGHT || e.keyCode == _tinperBeeCore.KeyCode.LEFT) {
399 inputs[1].focus();
400 }
401 };
402
403 this.keydownRight = function (e) {
404 var inputs = document.querySelectorAll('.rc-calendar-input');
405 if (e.keyCode == _tinperBeeCore.KeyCode.ESC) {
406 _this3.setState({
407 open: false
408 });
409 _this3.props.onOpenChange(false, v, v && _this3.getValue(v) || '');
410 }
411 if (e.keyCode == _tinperBeeCore.KeyCode.LEFT || e.keyCode == _tinperBeeCore.KeyCode.RIGHT) {
412 inputs[0].focus();
413 }
414 };
415
416 this.onStartInputBlur = function (e) {
417 var inputs = document.querySelectorAll('.rc-calendar-input');
418 var startValue = void 0,
419 endValue = void 0;
420 if (inputs) {
421 startValue = inputs[0].value ? inputs[0].value : '';
422 endValue = inputs[1].value ? inputs[1].value : '';
423 }
424 _this3.props.onStartInputBlur && _this3.props.onStartInputBlur(e, startValue, "[\"" + startValue + "\" , \"" + endValue + "\"]");
425 };
426
427 this.onEndInputBlur = function (e) {
428 var inputs = document.querySelectorAll('.rc-calendar-input');
429 var startValue = void 0,
430 endValue = void 0;
431 if (inputs) {
432 startValue = inputs[0].value ? inputs[0].value : '';
433 endValue = inputs[1].value ? inputs[1].value : '';
434 }
435 _this3.props.onEndInputBlur && _this3.props.onEndInputBlur(e, endValue, "[\"" + startValue + "\" , \"" + endValue + "\"]");
436 };
437
438 this.stopPropagation = function (e) {
439 e.stopPropagation();
440 };
441
442 this.onOk = function (value) {
443 _this3.props.onOk && _this3.props.onOk(value);
444 };
445};
446
447RangePicker.defaultProps = {
448 closeIcon: function closeIcon() {
449 return _react2["default"].createElement(_beeIcon2["default"], { type: "uf-close-c" });
450 },
451 renderIcon: function renderIcon() {
452 return _react2["default"].createElement(_beeIcon2["default"], { type: "uf-calendar" });
453 },
454 locale: _zh_CN2["default"],
455 showClear: true,
456 showToday: true,
457 showOk: true,
458 showClose: true,
459 showSecond: true,
460 showHour: true,
461 showMinute: true,
462 validatorFunc: function validatorFunc() {
463 return true;
464 }
465};
466
467exports["default"] = RangePicker;
468module.exports = exports["default"];
\No newline at end of file