From cadb45cb85d1c3c026f9434de869a6c2dc99d2b7 Mon Sep 17 00:00:00 2001 From: mengxiong10 <15623530290@163.com> Date: Thu, 2 Jun 2022 18:39:55 +0800 Subject: [PATCH] feat: add disabledCalendarChanger (#628) --- example/demo/Basic.vue | 10 +++++++ src/calendar/calendar-panel.js | 48 +++++++++++++++++++++++++++++--- src/calendar/icon-button.vue | 9 +++++- src/calendar/table-date.vue | 50 +++++++++++++++++++++++++++++++--- src/calendar/table-month.vue | 31 +++++++++++++++++++-- src/calendar/table-year.vue | 31 +++++++++++++++++++-- src/style/btn.scss | 5 ++++ 7 files changed, 171 insertions(+), 13 deletions(-) diff --git a/example/demo/Basic.vue b/example/demo/Basic.vue index 93257c5c..f4371ca7 100644 --- a/example/demo/Basic.vue +++ b/example/demo/Basic.vue @@ -6,6 +6,8 @@ v-model="value1" format="YYYY-MM-DD" type="date" + :disabled-date="disabledDate" + disabled-arrows="smart" placeholder="Select date" > @@ -45,5 +47,13 @@ export default { value6: null, }; }, + computed: { + // calendar() + }, + methods: { + disabledDate(date) { + return date > new Date(); + }, + }, }; diff --git a/src/calendar/calendar-panel.js b/src/calendar/calendar-panel.js index f9dec488..fac5b439 100644 --- a/src/calendar/calendar-panel.js +++ b/src/calendar/calendar-panel.js @@ -33,6 +33,10 @@ export default { defaultPanel: { type: String, }, + disabledCalendarChanger: { + type: [Function, String], + default: () => false, + }, disabledDate: { type: Function, default: () => false, @@ -111,6 +115,15 @@ export default { } this.innerCalendar = startOfMonth(calendarDate); }, + isDisabledCalendarChanger(date) { + if (this.disabledCalendarChanger === 'smart') { + return this.isDisabled(date); + } + if (typeof this.disabledCalendarChanger === 'function') { + return this.disabledCalendarChanger(date); + } + return false; + }, isDisabled(date) { return this.disabledDate(new Date(date), this.innerValue); }, @@ -185,19 +198,43 @@ export default { return classes.concat(this.getClasses(cellDate, this.innerValue, classes.join(' '))); }, getMonthClasses(month) { + const classes = []; if (this.type !== 'month') { - return this.calendarMonth === month ? 'active' : ''; + if (this.calendarMonth === month) { + classes.push('active'); + } + const minDate = new Date(this.innerCalendar); + minDate.setMonth(month, 1); + minDate.setHours(0, 0, 0, 0); + const maxDate = new Date(this.innerCalendar); + maxDate.setMonth(month + 1, 0); + maxDate.setHours(23, 59, 59, 999); + if (this.isDisabledCalendarChanger(minDate) && this.isDisabledCalendarChanger(maxDate)) { + classes.push('disabled'); + } + return classes; } - const classes = []; const cellDate = this.getMonthCellDate(month); classes.push(this.getStateClass(cellDate)); return classes.concat(this.getClasses(cellDate, this.innerValue, classes.join(' '))); }, getYearClasses(year) { + const classes = []; if (this.type !== 'year') { - return this.calendarYear === year ? 'active' : ''; + if (this.calendarYear === year) { + classes.push('active'); + } + const minDate = new Date(this.innerCalendar); + minDate.setFullYear(year, 0, 1); + minDate.setHours(0, 0, 0, 0); + const maxDate = new Date(this.innerCalendar); + maxDate.setFullYear(year, 11, 31); + maxDate.setHours(23, 59, 59, 999); + if (this.isDisabledCalendarChanger(minDate) && this.isDisabledCalendarChanger(maxDate)) { + classes.push('disabled'); + } + return classes; } - const classes = []; const cellDate = this.getYearCellDate(year); classes.push(this.getStateClass(cellDate)); return classes.concat(this.getClasses(cellDate, this.innerValue, classes.join(' '))); @@ -227,6 +264,7 @@ export default { if (panel === 'year') { return (