import PropTypes from '../_util/vue-types'; import BaseMixin from '../_util/BaseMixin'; import { getOptionProps, hasProp, initDefaultProps, getListeners } from '../_util/props-util'; import * as moment from 'moment'; import FullCalendar from '../vc-calendar/src/FullCalendar'; import Header from './Header'; import LocaleReceiver from '../locale-provider/LocaleReceiver'; import interopDefault from '../_util/interopDefault'; import { ConfigConsumerProps } from '../config-provider'; import enUS from './locale/en_US'; import Base from '../base'; import { checkValidate, stringToMoment, momentToString, TimeType } from '../_util/moment-util'; function noop() { return null; } function zerofixed(v) { if (v < 10) { return `0${v}`; } return `${v}`; } function isMomentArray(value) { return Array.isArray(value) && !!value.find(val => moment.isMoment(val)); } export const CalendarMode = PropTypes.oneOf(['month', 'year']); export const CalendarProps = () => ({ prefixCls: PropTypes.string, value: TimeType, defaultValue: TimeType, mode: CalendarMode, fullscreen: PropTypes.bool, // dateCellRender: PropTypes.func, // monthCellRender: PropTypes.func, // dateFullCellRender: PropTypes.func, // monthFullCellRender: PropTypes.func, locale: PropTypes.object, // onPanelChange?: (date?: moment.Moment, mode?: CalendarMode) => void; // onSelect?: (date?: moment.Moment) => void; disabledDate: PropTypes.func, validRange: PropTypes.custom(isMomentArray), headerRender: PropTypes.func, valueFormat: PropTypes.string, }); const Calendar = { name: 'ACalendar', mixins: [BaseMixin], props: initDefaultProps(CalendarProps(), { locale: {}, fullscreen: true, }), model: { prop: 'value', event: 'change', }, inject: { configProvider: { default: () => ConfigConsumerProps }, }, data() { const { value, defaultValue, valueFormat } = this; const sValue = value || defaultValue || interopDefault(moment)(); checkValidate('Calendar', defaultValue, 'defaultValue', valueFormat); checkValidate('Calendar', value, 'value', valueFormat); this._sPrefixCls = undefined; return { sValue: stringToMoment(sValue, valueFormat), sMode: this.mode || 'month', }; }, watch: { value(val) { checkValidate('Calendar', val, 'value', this.valueFormat); this.setState({ sValue: stringToMoment(val, this.valueFormat), }); }, mode(val) { this.setState({ sMode: val, }); }, }, methods: { onHeaderValueChange(value) { this.setValue(value, 'changePanel'); }, onHeaderTypeChange(mode) { this.sMode = mode; this.onPanelChange(this.sValue, mode); }, onPanelChange(value, mode) { const val = this.valueFormat ? momentToString(value, this.valueFormat) : value; this.$emit('panelChange', val, mode); if (value !== this.sValue) { this.$emit('change', val); } }, onSelect(value) { this.setValue(value, 'select'); }, setValue(value, way) { const prevValue = this.value ? stringToMoment(this.value, this.valueFormat) : this.sValue; const { sMode: mode, valueFormat } = this; if (!hasProp(this, 'value')) { this.setState({ sValue: value }); } if (way === 'select') { if (prevValue && prevValue.month() !== value.month()) { this.onPanelChange(value, mode); } this.$emit('select', valueFormat ? momentToString(value, valueFormat) : value); } else if (way === 'changePanel') { this.onPanelChange(value, mode); } }, getDateRange(validRange, disabledDate) { return current => { if (!current) { return false; } const [startDate, endDate] = validRange; const inRange = !current.isBetween(startDate, endDate, 'days', '[]'); if (disabledDate) { return disabledDate(current) || inRange; } return inRange; }; }, getDefaultLocale() { const result = { ...enUS, ...this.$props.locale, }; result.lang = { ...result.lang, ...(this.$props.locale || {}).lang, }; return result; }, monthCellRender2(value) { const { _sPrefixCls, $scopedSlots } = this; const monthCellRender = this.monthCellRender || $scopedSlots.monthCellRender || noop; return (