@txdfe/at
Version:
一个设计体系组件库
406 lines (342 loc) • 16.5 kB
JavaScript
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); }
var _excluded = ["prefix", "rtl", "dateCellRender", "monthCellRender", "yearCellRender", "className", "format", "locale", "showOtherMonth", "disabledDate"];
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; }
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import moment from 'moment';
import ConfigProvider from '../config-provider';
import nextLocale from '../locale/zh-cn';
import { obj, func } from '../util';
import RangePanelHeader from './head/range-panel-header';
import MonthPanelHeader from './head/month-panel-header';
import YearPanelHeader from './head/year-panel-header';
import DateTable from './table/date-table';
import MonthTable from './table/month-table';
import YearTable from './table/year-table';
import { checkMomentObj, formatDateValue, getVisibleMonth, isSameYearMonth, CALENDAR_MODES, CALENDAR_MODE_DATE, CALENDAR_MODE_MONTH, CALENDAR_MODE_YEAR, getLocaleData } from './utils';
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 === 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 (!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 = formatDateValue(props.startValue || props.defaultStartValue);
var endValue = formatDateValue(props.endValue || props.defaultEndValue);
var visibleMonth = getVisibleMonth(props.defaultVisibleMonth, startValue);
_this.state = {
startValue: startValue,
endValue: endValue,
mode: props.mode,
startVisibleMonth: visibleMonth,
activePanel: undefined
};
_this.today = moment();
return _this;
}
_createClass(RangeCalendar, [{
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
if ('startValue' in nextProps) {
var startValue = 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 = 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 = 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 CALENDAR_MODE_DATE:
{
table = [/*#__PURE__*/React.createElement("div", {
className: "".concat(prefix, "calendar-body-left"),
key: "left-panel"
}, /*#__PURE__*/React.createElement(DateTable, _extends({
format: format
}, tableProps, {
visibleMonth: startVisibleMonth,
onSelectDate: this.onSelectCell
}))), /*#__PURE__*/React.createElement("div", {
className: "".concat(prefix, "calendar-body-right"),
key: "right-panel"
}, /*#__PURE__*/React.createElement(DateTable, _extends({
format: format
}, tableProps, {
visibleMonth: endVisibleMonth,
onSelectDate: this.onSelectCell
})))];
header = /*#__PURE__*/React.createElement(RangePanelHeader, _extends({}, headerProps, {
goPrevYear: this.goPrevYear,
goPrevMonth: this.goPrevMonth,
goNextYear: this.goNextYear,
goNextMonth: this.goNextMonth
}));
break;
}
case CALENDAR_MODE_MONTH:
{
table = /*#__PURE__*/React.createElement(MonthTable, _extends({}, tableProps, {
visibleMonth: visibleMonth,
onSelectMonth: this.onSelectCell
}));
header = /*#__PURE__*/React.createElement(MonthPanelHeader, _extends({}, headerProps, {
visibleMonth: visibleMonth,
goPrevYear: this.goPrevYear,
goNextYear: this.goNextYear
}));
break;
}
case CALENDAR_MODE_YEAR:
{
table = /*#__PURE__*/React.createElement(YearTable, _extends({}, tableProps, {
rtl: rtl,
visibleMonth: visibleMonth,
onSelectYear: this.onSelectCell,
goPrevDecade: this.goPrevDecade,
goNextDecade: this.goNextDecade
}));
header = /*#__PURE__*/React.createElement(YearPanelHeader, _extends({}, headerProps, {
visibleMonth: visibleMonth,
goPrevDecade: this.goPrevDecade,
goNextDecade: this.goNextDecade
}));
break;
}
}
var classNames = classnames((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "calendar"), true), _defineProperty(_classnames, "".concat(prefix, "calendar-range"), true), _classnames), className);
return /*#__PURE__*/React.createElement("div", _extends({}, obj.pickOthers(RangeCalendar.propTypes, others), {
className: classNames
}), header, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefix, "calendar-body")
}, table));
}
}]);
return RangeCalendar;
}(React.Component);
_defineProperty(RangeCalendar, "propTypes", {
/**
* 样式前缀
*/
prefix: PropTypes.string,
rtl: PropTypes.bool,
/**
* 默认的开始日期
*/
defaultStartValue: checkMomentObj,
/**
* 默认的结束日期
*/
defaultEndValue: checkMomentObj,
/**
* 开始日期(moment 对象)
*/
startValue: checkMomentObj,
/**
* 结束日期(moment 对象)
*/
endValue: checkMomentObj,
// 面板模式
mode: PropTypes.oneOf(CALENDAR_MODES),
// 日期值的格式(用于日期title显示的格式)
format: PropTypes.string,
/**
* 是否显示非本月的日期
*/
showOtherMonth: PropTypes.bool,
/**
* 模板展示的月份(起始月份)
*/
defaultVisibleMonth: PropTypes.func,
/**
* 展现的月份变化时的回调
* @param {Object} value 显示的月份 (moment 对象)
* @param {String} reason 触发月份改变原因
*/
onVisibleMonthChange: PropTypes.func,
/**
* 不可选择的日期
* @param {Object} calendarDate 对应 Calendar 返回的自定义日期对象
* @param {String} view 当前视图类型,year: 年, month: 月, date: 日
* @returns {Boolean}
*/
disabledDate: PropTypes.func,
/**
* 选择日期单元格时的回调
* @param {Object} value 对应的日期值 (moment 对象)
*/
onSelect: PropTypes.func,
/**
* 自定义日期单元格渲染
*/
dateCellRender: PropTypes.func,
/**
* 自定义月份渲染函数
* @param {Object} calendarDate 对应 Calendar 返回的自定义日期对象
* @returns {ReactNode}
*/
monthCellRender: PropTypes.func,
yearCellRender: PropTypes.func,
// 兼容 0.x yearCellRender
locale: PropTypes.object,
className: PropTypes.string
});
_defineProperty(RangeCalendar, "defaultProps", {
prefix: 'next-',
rtl: false,
mode: CALENDAR_MODE_DATE,
format: 'YYYY-MM-DD',
dateCellRender: function dateCellRender(value) {
return value.date();
},
onSelect: func.noop,
onVisibleMonthChange: func.noop,
locale: nextLocale.Calendar,
showOtherMonth: false
});
export default ConfigProvider.config(RangeCalendar, {
componentName: 'Calendar'
});