@alifd/meet-react
Version:
Fusion Mobile React UI System Component
305 lines • 15.3 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
import { __rest } from "tslib";
import React, { createElement, Component } from "react";
import classNames from 'classnames';
import dayjs from 'dayjs';
import View from '../../view';
import SelectDrawer from '../../select-drawer';
import { ItemContext } from '../../form';
import { formatDate, isValidDate, toDate } from '../../utils/date';
import { isFunction } from '../../utils';
import { DEFAULT_FORMAT } from '../../time-picker/utils';
import { withTime } from '../utils';
var DEFAULT_START_TIME = new Date('1970-01-01');
var DEFAULT_END_TIME = new Date('2100-12-31');
var BaseRangePicker = /*#__PURE__*/function (_Component) {
_inherits(BaseRangePicker, _Component);
function BaseRangePicker(props) {
var _this;
_classCallCheck(this, BaseRangePicker);
_this = _callSuper(this, BaseRangePicker, [props]);
_this.getPickerValidRange = function () {
var step = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var _this$props$validRang = _this.props.validRange,
validRange = _this$props$validRang === void 0 ? [] : _this$props$validRang;
if (step === 0) {
return [toDate(validRange[0] || DEFAULT_START_TIME), toDate(validRange[1] || DEFAULT_END_TIME)];
} else {
return [toDate(_this.state.pickerValue[0] || validRange[0] || DEFAULT_START_TIME), toDate(validRange[1] || DEFAULT_END_TIME)];
}
};
_this.handleChange = function (value) {
var step = _this.state.step;
if (step === 0) {
_this.handleFirstStepChange(value);
} else {
_this.handleSecondStepChange(value);
}
};
_this.handleFirstStepChange = function (value) {
var pickerValue = _this.state.pickerValue;
_this.setState({
pickerValue: [value, pickerValue[1]]
});
};
_this.handleSecondStepChange = function (value) {
var pickerValue = _this.state.pickerValue;
_this.setState({
pickerValue: [pickerValue[0], value]
});
};
_this.handleOk = function () {
var _this$props = _this.props,
onOk = _this$props.onOk,
onChange = _this$props.onChange;
var pickerValue = _this.state.pickerValue;
var step = _this.state.step;
var value = pickerValue;
var _ref = pickerValue || [],
_ref2 = _slicedToArray(_ref, 2),
startDate = _ref2[0],
endDate = _ref2[1];
if (startDate && endDate && dayjs(startDate).isAfter(endDate)) {
value = [startDate, startDate];
}
if (step === 1 && !('value' in _this.props)) {
_this.setState({
value: value
});
}
if (step === 0) {
_this.scrollTo(1);
return false;
}
_this.scrollTo(0);
if (!('value' in _this.props)) {
_this.setState({
value: value
});
}
if (isFunction(onOk)) {
onOk(value);
}
if (isFunction(onChange)) {
onChange(value);
}
return true;
};
_this.handleCancel = function (reason) {
var onCancel = _this.props.onCancel;
var step = _this.state.step;
if (reason === 'cancel-button' && step === 1) {
_this.scrollTo(0);
return false;
}
_this.scrollTo(0, 250);
if (isFunction(onCancel)) {
onCancel(reason);
}
_this.setState({
pickerValue: _this.getPickerValue(_this.props)
});
return true;
};
_this.handleClear = function () {
var _this$props2 = _this.props,
onClear = _this$props2.onClear,
onChange = _this$props2.onChange;
if (!('value' in _this.props)) {
_this.setState({
value: null
});
}
if (onClear) {
onClear();
}
if (onChange) {
onChange(null);
}
};
_this.state = {
step: 0,
value: props.value || props.defaultValue || null,
pickerValue: _this.getPickerValue(props)
};
return _this;
}
_createClass(BaseRangePicker, [{
key: "rangeType",
get: function get() {
return 'second';
}
}, {
key: "timeFormat",
get: function get() {
var showTime = this.props.showTime;
return _typeof(showTime) === 'object' && showTime.format ? showTime.format : DEFAULT_FORMAT;
}
}, {
key: "format",
get: function get() {
var showTime = this.props.showTime;
var format = this.props.format || 'YYYY-MM-DD';
return showTime && !withTime(format) ? "".concat(format, " ").concat(this.timeFormat) : format;
}
}, {
key: "getPickerValue",
value: function getPickerValue(props) {
var _a;
var value = props.value,
defaultValue = props.defaultValue;
var today = dayjs().startOf(this.rangeType).toDate();
if ((_a = this.state) === null || _a === void 0 ? void 0 : _a.value) {
var _this$state$value = _slicedToArray(this.state.value, 2),
start = _this$state$value[0],
end = _this$state$value[1];
return [start || today, end || today];
}
if (value) {
var _value = _slicedToArray(value, 2),
_start = _value[0],
_end = _value[1];
return [_start || today, _end || today];
}
if (defaultValue) {
var _defaultValue = _slicedToArray(defaultValue, 2),
_start2 = _defaultValue[0],
_end2 = _defaultValue[1];
return [_start2 || today, _end2 || today];
}
return [today, today];
}
}, {
key: "scrollTo",
value: function scrollTo(step, wait) {
var _this2 = this;
if (wait) {
setTimeout(function () {
_this2.setState({
step: step
});
}, wait);
} else {
this.setState({
step: step
});
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
var _a, _b;
if ('value' in this.props) {
var _this$props3 = this.props,
value = _this$props3.value,
defaultValue = _this$props3.defaultValue;
var newValue = value || defaultValue;
if (!newValue && this.state.value) {
this.setState({
value: null
});
return;
}
if (!Array.isArray(newValue) || newValue === this.state.value) {
return;
}
var d1 = toDate(newValue[0]);
var d2 = toDate(newValue[1]);
if (!isValidDate(d1) || !isValidDate(d2)) {
return;
}
var newValueDate = [d1, d2];
if (!this.state.value) {
this.setState({
value: newValueDate,
pickerValue: newValueDate
});
return;
}
if (d1.getTime() === ((_a = toDate(this.state.value[0])) === null || _a === void 0 ? void 0 : _a.getTime()) && d2.getTime() === ((_b = toDate(this.state.value[1])) === null || _b === void 0 ? void 0 : _b.getTime())) {
return;
}
this.setState({
value: newValueDate,
pickerValue: newValueDate
});
}
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var _this$state = this.state,
value = _this$state.value,
step = _this$state.step,
pickerValue = _this$state.pickerValue;
var _a = this.props,
_a$prefix = _a.prefix,
prefix = _a$prefix === void 0 ? 'mt-' : _a$prefix,
iconType = _a.iconType,
placeholder = _a.placeholder,
alignProp = _a.align,
sizeProp = _a.size,
disabled = _a.disabled,
readOnly = _a.readOnly,
hasClear = _a.hasClear,
locale = _a.locale,
others = __rest(_a, ["prefix", "iconType", "placeholder", "align", "size", "disabled", "readOnly", "hasClear", "locale"]);
var clsPrefix = "".concat(prefix, "datepicker-");
var showNext = step === 0;
var showPrev = step === 1;
return /*#__PURE__*/React.createElement(ItemContext.Consumer, null, function (context) {
var isInsideForm = context.isInsideForm,
contentAlign = context.contentAlign,
contextSize = context.size;
var align = alignProp || (isInsideForm ? contentAlign : 'left');
var size = sizeProp || (isInsideForm ? contextSize : 'medium');
var useRangeLocale = locale ? locale.ok && locale.cancel : null;
return /*#__PURE__*/React.createElement(SelectDrawer, _extends({}, others, {
locale: useRangeLocale ? locale : undefined,
prefix: prefix,
content: value ? "".concat(formatDate(dayjs(value[0]), _this3.format), " - ").concat(formatDate(dayjs(value[1]), _this3.format)) : '',
align: align,
size: size,
hasClear: hasClear,
iconType: iconType,
placeholder: placeholder,
disabled: disabled,
readOnly: readOnly,
onOk: _this3.handleOk,
onCancel: _this3.handleCancel,
onClear: _this3.handleClear,
drawerTitle: showNext && locale ? locale.selectStartTime : locale.selectEndTime,
okText: showNext && locale ? locale.next : null,
cancelText: showPrev && locale ? locale.prev : null
}), /*#__PURE__*/React.createElement(View, {
className: "".concat(clsPrefix, "container")
}, /*#__PURE__*/React.createElement(View, {
className: classNames("".concat(clsPrefix, "range"), _defineProperty({}, "".concat(clsPrefix, "range--next"), showPrev))
}, _this3.renderFirstChild(pickerValue[0], _this3.getPickerValidRange(0), _this3.handleFirstStepChange), _this3.renderSecondChild(pickerValue[1], _this3.getPickerValidRange(1), _this3.handleSecondStepChange))));
});
}
}]);
return BaseRangePicker;
}(Component);
export default BaseRangePicker;