diff --git a/example/demo/Basic.vue b/example/demo/Basic.vue index 93257c5..f4371ca 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 f9dec48..fac5b43 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 (