import PropTypes from '../../../_util/vue-types'; import BaseMixin from '../../../_util/BaseMixin'; import { getMonthName } from '../util'; const CalendarHeader = { name: 'CalendarHeader', inheritAttrs: false, mixins: [BaseMixin], props: { value: PropTypes.object, locale: PropTypes.object, yearSelectOffset: PropTypes.number.def(10), yearSelectTotal: PropTypes.number.def(20), // onValueChange: PropTypes.func, // onTypeChange: PropTypes.func, Select: PropTypes.object, prefixCls: PropTypes.string, type: PropTypes.string, showTypeSwitch: PropTypes.looseBool, headerComponents: PropTypes.array, }, methods: { onYearChange(year) { const newValue = this.value.clone(); newValue.year(parseInt(year, 10)); this.__emit('valueChange', newValue); }, onMonthChange(month) { const newValue = this.value.clone(); newValue.month(parseInt(month, 10)); this.__emit('valueChange', newValue); }, yearSelectElement(year) { const { yearSelectOffset, yearSelectTotal, prefixCls, Select } = this; const start = year - yearSelectOffset; const end = start + yearSelectTotal; const options = []; for (let index = start; index < end; index++) { options.push({(() => index)()}); } return ( ); }, monthSelectElement(month) { const { value, Select, prefixCls } = this; const t = value.clone(); const options = []; for (let index = 0; index < 12; index++) { t.month(index); options.push({(() => getMonthName(t))()}); } return ( ); }, changeTypeToDate() { this.__emit('typeChange', 'date'); }, changeTypeToMonth() { this.__emit('typeChange', 'month'); }, }, render() { const { value, locale, prefixCls, type, showTypeSwitch, headerComponents } = this; const year = value.year(); const month = value.month(); const yearSelect = this.yearSelectElement(year); const monthSelect = type === 'month' ? null : this.monthSelectElement(month); const switchCls = `${prefixCls}-header-switcher`; const typeSwitcher = showTypeSwitch ? ( {type === 'date' ? ( {locale.month} ) : ( {locale.month} )} {type === 'month' ? ( {locale.year} ) : ( {locale.year} )} ) : null; return (
{typeSwitcher} {monthSelect} {yearSelect} {headerComponents}
); }, }; export default CalendarHeader;