UNPKG

@txdfe/at

Version:

一个设计体系组件库

430 lines (349 loc) 17.6 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _classnames2 = _interopRequireDefault(require("classnames")); var _moment = _interopRequireDefault(require("moment")); var _configProvider = _interopRequireDefault(require("../config-provider")); var _zhCn = _interopRequireDefault(require("../locale/zh-cn")); var _util = require("../util"); var _rangePanelHeader = _interopRequireDefault(require("./head/range-panel-header")); var _monthPanelHeader = _interopRequireDefault(require("./head/month-panel-header")); var _yearPanelHeader = _interopRequireDefault(require("./head/year-panel-header")); var _dateTable = _interopRequireDefault(require("./table/date-table")); var _monthTable = _interopRequireDefault(require("./table/month-table")); var _yearTable = _interopRequireDefault(require("./table/year-table")); var _utils = require("./utils"); var _excluded = ["prefix", "rtl", "dateCellRender", "monthCellRender", "yearCellRender", "className", "format", "locale", "showOtherMonth", "disabledDate"]; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _extends() { _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; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } 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, 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 _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 || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } 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() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var RangeCalendar = /*#__PURE__*/function (_React$Component) { _inherits(RangeCalendar, _React$Component); var _super = _createSuper(RangeCalendar); function RangeCalendar(props, context) { var _this; _classCallCheck(this, RangeCalendar); _this = _super.call(this, props, context); _defineProperty(_assertThisInitialized(_this), "onSelectCell", function (date, nextMode) { // this.changeVisibleMonth(date, 'cellClick'); if (_this.state.mode === _utils.CALENDAR_MODE_DATE) { _this.props.onSelect(date); } _this.changeMode(nextMode); }); _defineProperty(_assertThisInitialized(_this), "changeMode", function (mode, activePanel) { if (typeof mode === 'string' && mode !== _this.state.mode) { _this.setState({ mode: mode }); } if (activePanel && activePanel !== _this.state.activePanel) { _this.setState({ activePanel: activePanel }); } }); _defineProperty(_assertThisInitialized(_this), "changeVisibleMonth", function (date, reason) { if (!(0, _utils.isSameYearMonth)(date, _this.state.startVisibleMonth)) { _this.setState({ startVisibleMonth: date }); _this.props.onVisibleMonthChange(date, reason); } }); _defineProperty(_assertThisInitialized(_this), "changeVisibleMonthByOffset", function (offset, type) { var offsetDate = _this.state.startVisibleMonth.clone().add(offset, type); _this.changeVisibleMonth(offsetDate, 'buttonClick'); }); _defineProperty(_assertThisInitialized(_this), "goPrevDecade", function () { _this.changeVisibleMonthByOffset(-10, 'years'); }); _defineProperty(_assertThisInitialized(_this), "goNextDecade", function () { _this.changeVisibleMonthByOffset(10, 'years'); }); _defineProperty(_assertThisInitialized(_this), "goPrevYear", function () { _this.changeVisibleMonthByOffset(-1, 'years'); }); _defineProperty(_assertThisInitialized(_this), "goNextYear", function () { _this.changeVisibleMonthByOffset(1, 'years'); }); _defineProperty(_assertThisInitialized(_this), "goPrevMonth", function () { _this.changeVisibleMonthByOffset(-1, 'months'); }); _defineProperty(_assertThisInitialized(_this), "goNextMonth", function () { _this.changeVisibleMonthByOffset(1, 'months'); }); var startValue = (0, _utils.formatDateValue)(props.startValue || props.defaultStartValue); var endValue = (0, _utils.formatDateValue)(props.endValue || props.defaultEndValue); var visibleMonth = (0, _utils.getVisibleMonth)(props.defaultVisibleMonth, startValue); _this.state = { startValue: startValue, endValue: endValue, mode: props.mode, startVisibleMonth: visibleMonth, activePanel: undefined }; _this.today = (0, _moment["default"])(); return _this; } _createClass(RangeCalendar, [{ key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { if ('startValue' in nextProps) { var startValue = (0, _utils.formatDateValue)(nextProps.startValue); this.setState({ startValue: startValue }); if (startValue && !startValue.isSame(this.state.startValue, 'day')) { this.setState({ startVisibleMonth: startValue }); } } if ('endValue' in nextProps) { var endValue = (0, _utils.formatDateValue)(nextProps.endValue); this.setState({ endValue: endValue }); } if ('mode' in nextProps) { this.setState({ mode: nextProps.mode }); } } }, { key: "render", value: function render() { var _classnames; var _this$props = this.props, prefix = _this$props.prefix, rtl = _this$props.rtl, dateCellRender = _this$props.dateCellRender, monthCellRender = _this$props.monthCellRender, yearCellRender = _this$props.yearCellRender, className = _this$props.className, format = _this$props.format, locale = _this$props.locale, showOtherMonth = _this$props.showOtherMonth, disabledDate = _this$props.disabledDate, others = _objectWithoutProperties(_this$props, _excluded); var _this$state = this.state, startValue = _this$state.startValue, endValue = _this$state.endValue, mode = _this$state.mode, startVisibleMonth = _this$state.startVisibleMonth, activePanel = _this$state.activePanel; // reset moment locale if (locale.momentLocale) { startValue && startValue.locale(locale.momentLocale); endValue && endValue.locale(locale.momentLocale); startVisibleMonth.locale(locale.momentLocale); } if (rtl) { others.dir = 'rtl'; } var localeData = (0, _utils.getLocaleData)(locale.format || {}, startVisibleMonth.localeData()); var endVisibleMonth = startVisibleMonth.clone().add(1, 'months'); var headerProps = { prefix: prefix, rtl: rtl, mode: mode, locale: locale, momentLocale: localeData, startVisibleMonth: startVisibleMonth, endVisibleMonth: endVisibleMonth, changeMode: this.changeMode }; var tableProps = { prefix: prefix, value: startValue, startValue: startValue, endValue: endValue, mode: mode, locale: locale, momentLocale: localeData, showOtherMonth: showOtherMonth, today: this.today, disabledDate: disabledDate, dateCellRender: dateCellRender, monthCellRender: monthCellRender, yearCellRender: yearCellRender, changeMode: this.changeMode, changeVisibleMonth: this.changeVisibleMonth }; var visibleMonths = { start: startVisibleMonth, end: endVisibleMonth }; var visibleMonth = visibleMonths[activePanel]; var header; var table; switch (mode) { case _utils.CALENDAR_MODE_DATE: { table = [/*#__PURE__*/_react["default"].createElement("div", { className: "".concat(prefix, "calendar-body-left"), key: "left-panel" }, /*#__PURE__*/_react["default"].createElement(_dateTable["default"], _extends({ format: format }, tableProps, { visibleMonth: startVisibleMonth, onSelectDate: this.onSelectCell }))), /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(prefix, "calendar-body-right"), key: "right-panel" }, /*#__PURE__*/_react["default"].createElement(_dateTable["default"], _extends({ format: format }, tableProps, { visibleMonth: endVisibleMonth, onSelectDate: this.onSelectCell })))]; header = /*#__PURE__*/_react["default"].createElement(_rangePanelHeader["default"], _extends({}, headerProps, { goPrevYear: this.goPrevYear, goPrevMonth: this.goPrevMonth, goNextYear: this.goNextYear, goNextMonth: this.goNextMonth })); break; } case _utils.CALENDAR_MODE_MONTH: { table = /*#__PURE__*/_react["default"].createElement(_monthTable["default"], _extends({}, tableProps, { visibleMonth: visibleMonth, onSelectMonth: this.onSelectCell })); header = /*#__PURE__*/_react["default"].createElement(_monthPanelHeader["default"], _extends({}, headerProps, { visibleMonth: visibleMonth, goPrevYear: this.goPrevYear, goNextYear: this.goNextYear })); break; } case _utils.CALENDAR_MODE_YEAR: { table = /*#__PURE__*/_react["default"].createElement(_yearTable["default"], _extends({}, tableProps, { rtl: rtl, visibleMonth: visibleMonth, onSelectYear: this.onSelectCell, goPrevDecade: this.goPrevDecade, goNextDecade: this.goNextDecade })); header = /*#__PURE__*/_react["default"].createElement(_yearPanelHeader["default"], _extends({}, headerProps, { visibleMonth: visibleMonth, goPrevDecade: this.goPrevDecade, goNextDecade: this.goNextDecade })); break; } } var classNames = (0, _classnames2["default"])((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "calendar"), true), _defineProperty(_classnames, "".concat(prefix, "calendar-range"), true), _classnames), className); return /*#__PURE__*/_react["default"].createElement("div", _extends({}, _util.obj.pickOthers(RangeCalendar.propTypes, others), { className: classNames }), header, /*#__PURE__*/_react["default"].createElement("div", { className: "".concat(prefix, "calendar-body") }, table)); } }]); return RangeCalendar; }(_react["default"].Component); _defineProperty(RangeCalendar, "propTypes", { /** * 样式前缀 */ prefix: _propTypes["default"].string, rtl: _propTypes["default"].bool, /** * 默认的开始日期 */ defaultStartValue: _utils.checkMomentObj, /** * 默认的结束日期 */ defaultEndValue: _utils.checkMomentObj, /** * 开始日期(moment 对象) */ startValue: _utils.checkMomentObj, /** * 结束日期(moment 对象) */ endValue: _utils.checkMomentObj, // 面板模式 mode: _propTypes["default"].oneOf(_utils.CALENDAR_MODES), // 日期值的格式(用于日期title显示的格式) format: _propTypes["default"].string, /** * 是否显示非本月的日期 */ showOtherMonth: _propTypes["default"].bool, /** * 模板展示的月份(起始月份) */ defaultVisibleMonth: _propTypes["default"].func, /** * 展现的月份变化时的回调 * @param {Object} value 显示的月份 (moment 对象) * @param {String} reason 触发月份改变原因 */ onVisibleMonthChange: _propTypes["default"].func, /** * 不可选择的日期 * @param {Object} calendarDate 对应 Calendar 返回的自定义日期对象 * @param {String} view 当前视图类型,year: 年, month: 月, date: 日 * @returns {Boolean} */ disabledDate: _propTypes["default"].func, /** * 选择日期单元格时的回调 * @param {Object} value 对应的日期值 (moment 对象) */ onSelect: _propTypes["default"].func, /** * 自定义日期单元格渲染 */ dateCellRender: _propTypes["default"].func, /** * 自定义月份渲染函数 * @param {Object} calendarDate 对应 Calendar 返回的自定义日期对象 * @returns {ReactNode} */ monthCellRender: _propTypes["default"].func, yearCellRender: _propTypes["default"].func, // 兼容 0.x yearCellRender locale: _propTypes["default"].object, className: _propTypes["default"].string }); _defineProperty(RangeCalendar, "defaultProps", { prefix: 'next-', rtl: false, mode: _utils.CALENDAR_MODE_DATE, format: 'YYYY-MM-DD', dateCellRender: function dateCellRender(value) { return value.date(); }, onSelect: _util.func.noop, onVisibleMonthChange: _util.func.noop, locale: _zhCn["default"].Calendar, showOtherMonth: false }); var _default = _configProvider["default"].config(RangeCalendar, { componentName: 'Calendar' }); exports["default"] = _default;