ant-design-vue/components/vc-calendar/src/full-calendar/CalendarHeader.jsx

136 lines
3.8 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import PropTypes from '../../../_util/vue-types';
import BaseMixin from '../../../_util/BaseMixin';
import { getMonthName } from '../util';
2018-03-10 14:10:13 +00:00
const CalendarHeader = {
name: 'CalendarHeader',
2020-07-21 10:36:27 +00:00
inheritAttrs: false,
2018-03-10 14:10:13 +00:00
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,
2018-03-12 14:13:59 +00:00
Select: PropTypes.object,
2018-03-10 14:10:13 +00:00
prefixCls: PropTypes.string,
type: PropTypes.string,
showTypeSwitch: PropTypes.looseBool,
2018-03-10 14:10:13 +00:00
headerComponents: PropTypes.array,
},
methods: {
2019-01-12 03:33:27 +00:00
onYearChange(year) {
const newValue = this.value.clone();
newValue.year(parseInt(year, 10));
this.__emit('valueChange', newValue);
2018-03-10 14:10:13 +00:00
},
2019-01-12 03:33:27 +00:00
onMonthChange(month) {
const newValue = this.value.clone();
newValue.month(parseInt(month, 10));
this.__emit('valueChange', newValue);
2018-03-10 14:10:13 +00:00
},
2019-01-12 03:33:27 +00:00
yearSelectElement(year) {
const { yearSelectOffset, yearSelectTotal, prefixCls, Select } = this;
const start = year - yearSelectOffset;
const end = start + yearSelectTotal;
2018-03-10 14:10:13 +00:00
2019-01-12 03:33:27 +00:00
const options = [];
2018-03-10 14:10:13 +00:00
for (let index = start; index < end; index++) {
2020-07-23 07:15:38 +00:00
options.push(<Select.Option key={`${index}`}>{(() => index)()}</Select.Option>);
2018-03-10 14:10:13 +00:00
}
return (
<Select
class={`${prefixCls}-header-year-select`}
onChange={this.onYearChange}
dropdownStyle={{ zIndex: 2000 }}
dropdownMenuStyle={{ maxHeight: '250px', overflow: 'auto', fontSize: '12px' }}
2019-01-12 03:33:27 +00:00
optionLabelProp="children"
2018-03-10 14:10:13 +00:00
value={String(year)}
showSearch={false}
>
2019-01-12 03:33:27 +00:00
{options}
2018-03-10 14:10:13 +00:00
</Select>
2019-01-12 03:33:27 +00:00
);
2018-03-10 14:10:13 +00:00
},
2019-01-12 03:33:27 +00:00
monthSelectElement(month) {
const { value, Select, prefixCls } = this;
const t = value.clone();
const options = [];
2018-03-10 14:10:13 +00:00
for (let index = 0; index < 12; index++) {
2019-01-12 03:33:27 +00:00
t.month(index);
2020-07-23 07:15:38 +00:00
options.push(<Select.Option key={`${index}`}>{(() => getMonthName(t))()}</Select.Option>);
2018-03-10 14:10:13 +00:00
}
return (
<Select
class={`${prefixCls}-header-month-select`}
dropdownStyle={{ zIndex: 2000 }}
2019-01-12 03:33:27 +00:00
dropdownMenuStyle={{
maxHeight: '250px',
overflow: 'auto',
overflowX: 'hidden',
fontSize: '12px',
}}
optionLabelProp="children"
2018-03-10 14:10:13 +00:00
value={String(month)}
showSearch={false}
onChange={this.onMonthChange}
>
2019-01-12 03:33:27 +00:00
{options}
2018-03-10 14:10:13 +00:00
</Select>
2019-01-12 03:33:27 +00:00
);
2018-03-10 14:10:13 +00:00
},
2019-01-12 03:33:27 +00:00
changeTypeToDate() {
this.__emit('typeChange', 'date');
2018-03-10 14:10:13 +00:00
},
2019-01-12 03:33:27 +00:00
changeTypeToMonth() {
this.__emit('typeChange', 'month');
2018-03-10 14:10:13 +00:00
},
},
2019-01-12 03:33:27 +00:00
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`;
2018-03-10 14:10:13 +00:00
const typeSwitcher = showTypeSwitch ? (
<span class={switchCls}>
2019-01-12 03:33:27 +00:00
{type === 'date' ? (
<span class={`${switchCls}-focus`}>{locale.month}</span>
) : (
<span onClick={this.changeTypeToDate} class={`${switchCls}-normal`}>
2018-03-10 14:10:13 +00:00
{locale.month}
</span>
2019-01-12 03:33:27 +00:00
)}
{type === 'month' ? (
<span class={`${switchCls}-focus`}>{locale.year}</span>
) : (
<span onClick={this.changeTypeToMonth} class={`${switchCls}-normal`}>
2018-03-10 14:10:13 +00:00
{locale.year}
</span>
2019-01-12 03:33:27 +00:00
)}
2018-03-10 14:10:13 +00:00
</span>
2019-01-12 03:33:27 +00:00
) : null;
2018-03-10 14:10:13 +00:00
return (
<div class={`${prefixCls}-header`}>
2019-01-12 03:33:27 +00:00
{typeSwitcher}
{monthSelect}
{yearSelect}
{headerComponents}
2018-03-10 14:10:13 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-03-10 14:10:13 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-03-19 02:16:27 +00:00
2019-01-12 03:33:27 +00:00
export default CalendarHeader;