Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calendar enhancements to restrict year, month and date navigation beyond minDate and maxDate. #12870

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 92 additions & 2 deletions src/app/components/calendar/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,18 @@ export type CalendarTypeView = 'date' | 'month' | 'year';
<div class="p-datepicker-group-container">
<div class="p-datepicker-group" *ngFor="let month of months; let i = index">
<div class="p-datepicker-header">
<button (keydown)="onContainerButtonKeydown($event)" class="p-datepicker-prev p-link" (click)="onPrevButtonClick($event)" *ngIf="i === 0" type="button" pRipple>
<button
(keydown)="onContainerButtonKeydown($event)"
class="p-datepicker-prev p-link"
(click)="onPrevButtonClick($event)"
*ngIf="i === 0"
type="button"
pRipple
[disabled]="isNavigationDisabled('prev', month)"
[ngClass]="{
'p-disabled': isNavigationDisabled('prev', month)
}"
>
<span class="p-datepicker-prev-icon pi pi-chevron-left"></span>
</button>
<div class="p-datepicker-title">
Expand All @@ -134,6 +145,10 @@ export type CalendarTypeView = 'date' | 'month' | 'year';
[style.display]="numberOfMonths === 1 ? 'inline-flex' : i === numberOfMonths - 1 ? 'inline-flex' : 'none'"
type="button"
pRipple
[disabled]="isNavigationDisabled('next', month)"
[ngClass]="{
'p-disabled': isNavigationDisabled('next', month)
}"
>
<span class="p-datepicker-next-icon pi pi-chevron-right"></span>
</button>
Expand Down Expand Up @@ -184,7 +199,14 @@ export type CalendarTypeView = 'date' | 'month' | 'year';
</span>
</div>
<div class="p-yearpicker" *ngIf="currentView === 'year'">
<span *ngFor="let y of yearPickerValues()" (click)="onYearSelect($event, y)" (keydown)="onYearCellKeydown($event, y)" class="p-yearpicker-year" [ngClass]="{ 'p-highlight': isYearSelected(y) }" pRipple>
<span
*ngFor="let y of yearPickerValues()"
(click)="onYearSelect($event, y)"
(keydown)="onYearCellKeydown($event, y)"
class="p-yearpicker-year"
[ngClass]="{ 'p-highlight': isYearSelected(y), 'p-disabled': isYearDisabled(y) }"
pRipple
>
{{ y }}
</span>
</div>
Expand Down Expand Up @@ -1364,6 +1386,74 @@ export class Calendar implements OnInit, OnDestroy, ControlValueAccessor {
return true;
}

isYearDisabled(year): boolean {
let validMin = true;
let validMax = true;

if (this.minDate) {
if (this.minDate.getFullYear() > year) {
validMin = false;
}
}
if (this.maxDate) {
if (this.maxDate.getFullYear() < year) {
validMax = false;
}
}
return !validMin || !validMax;
}

isNavigationDisabled(type: string, month): boolean {
if(this.currentView === 'date') {
if (this.minDate && type === 'prev') {
if (this.minDate.getFullYear() > this.months[0].year) {
return true;
} else if(this.minDate.getFullYear() === this.months[0].year){
if (this.minDate.getMonth() >= this.months[0].month) {
return true;
}
}
}
if (this.maxDate && type === 'next') {
if (this.maxDate.getFullYear() < this.months[0].year) {
return true;
} else if(this.maxDate.getFullYear() === this.months[0].year){
if (this.maxDate.getMonth() <= this.months[0].month) {
return true;
}
}
}
return false;
} else if(this.currentView === 'year') {
let yearpicker: any[] = this.yearPickerValues();
if (this.minDate && type === 'prev') {
if(yearpicker[0] > this.minDate.getFullYear()) {
return false;
}
}
if (this.maxDate && type === 'next') {
if(yearpicker[yearpicker.length - 1] < this.maxDate.getFullYear()) {
return false;
}
}
return true;
} else if(this.currentView === 'month') {
let year = this.getYear(month);
if (this.minDate && type === 'prev') {
if(year > this.minDate.getFullYear()) {
return false;
}
}
if (this.maxDate && type === 'next') {
if(year < this.maxDate.getFullYear()) {
return false;
}
}
return true;
}
return false;
}

isYearSelected(year) {
if (this.isComparable()) {
let value = this.isRangeSelection() ? this.value[0] : this.value;
Expand Down