UNPKG

@alifd/meet-react

Version:

Fusion Mobile React UI System Component

253 lines 11.9 kB
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 _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 _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 _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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } 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, { Component, createElement, createRef } from "react"; import dayjs from 'dayjs'; import View from '../../view'; import SelectDrawer from '../../select-drawer'; import { ItemContext } from '../../form'; import { Context as LocaleContext } from '../../locale'; import { isFunction, isValidArray } from '../../utils'; import { isBetween, isValidDate, toDate, toValidDate } from '../../utils/date'; import { getFormatType } from '../utils'; var BasePicker = /*#__PURE__*/function (_Component) { _inherits(BasePicker, _Component); function BasePicker(props) { var _this; _classCallCheck(this, BasePicker); _this = _callSuper(this, BasePicker, [props]); _this.ref = /*#__PURE__*/createRef(); _this.calendarRef = /*#__PURE__*/createRef(); _this.handleClear = function () { var _this$props = _this.props, onClear = _this$props.onClear, onChange = _this$props.onChange; if (!('value' in _this.props)) { _this.setState({ pickerValue: new Date(), value: null }); } if (onClear) { onClear(); } if (onChange) { onChange(null); } }; var v = toDate(props.value || props.defaultValue); var pv = new Date(); if (isValidDate(v)) { pv = v; } else if (isValidArray(_this.props.validRange) && !isBetween(new Date(), _this.props.validRange[0], _this.props.validRange[1], 'd')) { pv = _this.props.validRange[0]; } _this.state = { value: isValidDate(v) ? v : null, pickerValue: pv, yearPickerVisible: false }; _this.handleOk = _this.handleOk.bind(_assertThisInitialized(_this)); _this.handleCancel = _this.handleCancel.bind(_assertThisInitialized(_this)); _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this)); _this.handleVisibleChange = _this.handleVisibleChange.bind(_assertThisInitialized(_this)); _this.show = _this.show.bind(_assertThisInitialized(_this)); _this.hide = _this.hide.bind(_assertThisInitialized(_this)); _this.handleYearPickerVisibleChange = _this.handleYearPickerVisibleChange.bind(_assertThisInitialized(_this)); return _this; } _createClass(BasePicker, [{ key: "format", get: function get() { return this.props.format || this.getDefaultFormat(); } }, { key: "handleChange", value: function handleChange(pickerValue) { this.setState({ pickerValue: pickerValue }); } }, { key: "handleYearPickerVisibleChange", value: function handleYearPickerVisibleChange(show) { this.setState({ yearPickerVisible: show }); } }, { key: "handleOk", value: function handleOk() { var _a; var _this$props2 = this.props, onOk = _this$props2.onOk, onChange = _this$props2.onChange, validRange = _this$props2.validRange, disabledDate = _this$props2.disabledDate, format = _this$props2.format; var _this$state = this.state, value = _this$state.pickerValue, yearPickerVisible = _this$state.yearPickerVisible; if (yearPickerVisible && this.calendarRef.current) { (_a = this.calendarRef.current) === null || _a === void 0 ? void 0 : _a.toggleYearPicker(false); return false; } if (value) { if (Array.isArray(validRange) && !isBetween(value, validRange[0], validRange[1], getFormatType(format, true))) { return false; } if (isFunction(disabledDate) && disabledDate(value, getFormatType(format))) { return false; } } if (onChange) { onChange(value); } if (onOk) { onOk(value); } this.setState({ value: value }); return true; } }, { key: "handleVisibleChange", value: function handleVisibleChange(to) { var onVisibleChange = this.props.onVisibleChange; if (onVisibleChange) { onVisibleChange(to); } var yearPickerVisible = this.state.yearPickerVisible; if (to && yearPickerVisible) { this.setState({ yearPickerVisible: false }); } } }, { key: "handleCancel", value: function handleCancel(reason) { var onCancel = this.props.onCancel; this.setState({ pickerValue: this.state.value || new Date() }); if (isFunction(onCancel)) { onCancel(reason); } } }, { key: "componentDidUpdate", value: function componentDidUpdate() { 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 (newValue === this.state.value) { return; } var newValueDate = toDate(newValue); if (!isValidDate(newValueDate)) { return; } if (!this.state.value) { var newPickerValue = toValidDate(value); this.setState({ value: newValueDate, pickerValue: newPickerValue }); return; } if (newValueDate.getTime() === this.state.value.getTime()) { return; } this.setState({ value: newValueDate, pickerValue: toValidDate(value) }); } } }, { key: "show", value: function show() { var _a, _b; (_b = (_a = this.ref) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.show(); } }, { key: "hide", value: function hide() { var _a, _b; (_b = (_a = this.ref) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.hide(); } }, { key: "render", value: function render() { var _this2 = this; var _this$state2 = this.state, value = _this$state2.value, yearPickerVisible = _this$state2.yearPickerVisible; 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, others = __rest(_a, ["prefix", "iconType", "placeholder", "align", "size", "disabled", "readOnly", "hasClear"]); 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'); return /*#__PURE__*/React.createElement(LocaleContext.Consumer, null, function (localeContext) { var _a, _b; return /*#__PURE__*/React.createElement(SelectDrawer, _extends({}, others, { ref: _this2.ref, prefix: prefix, hasClear: hasClear, content: value ? dayjs(value).format(_this2.format) : '', align: align, size: size, iconType: iconType, placeholder: placeholder, disabled: disabled, readOnly: readOnly, okText: yearPickerVisible ? ((_b = (_a = localeContext === null || localeContext === void 0 ? void 0 : localeContext.locale) === null || _a === void 0 ? void 0 : _a.DatePicker) === null || _b === void 0 ? void 0 : _b.foldUp) || '收起' : undefined, onOk: _this2.handleOk, onCancel: _this2.handleCancel, onClear: _this2.handleClear, onVisibleChange: _this2.handleVisibleChange, disableScroll: true }), /*#__PURE__*/React.createElement(View, { className: "".concat(prefix, "datepicker-container") }, _this2.renderPicker())); }); }); } }]); return BasePicker; }(Component); export default BasePicker;