Skip to content

Commit 403ebbd

Browse files
julianobrasiljelbourn
authored andcommitted
fix(datepicker): gray out filtered years in multi-year view (#9563)
1 parent 637e1b6 commit 403ebbd

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

src/demo-app/datepicker/datepicker-demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<h2>Options</h2>
22
<p>
33
<mat-checkbox [(ngModel)]="touch">Use touch UI</mat-checkbox>
4-
<mat-checkbox [(ngModel)]="filterOdd">Filter odd months and dates</mat-checkbox>
4+
<mat-checkbox [(ngModel)]="filterOdd">Filter odd years, months and dates</mat-checkbox>
55
<mat-checkbox [(ngModel)]="yearView">Start in year view</mat-checkbox>
66
<mat-checkbox [(ngModel)]="datepickerDisabled">Disable datepicker</mat-checkbox>
77
<mat-checkbox [(ngModel)]="inputDisabled">Disable input</mat-checkbox>

src/demo-app/datepicker/datepicker-demo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export class DatepickerDemo {
3131
lastDateInput: Date | null;
3232
lastDateChange: Date | null;
3333

34-
dateFilter = (date: Date) => date.getMonth() % 2 == 1 && date.getDate() % 2 == 0;
34+
dateFilter =
35+
(date: Date) => !(date.getFullYear() % 2) && (date.getMonth() % 2) && !(date.getDate() % 2)
3536

3637
onDateInput = (e: MatDatepickerInputEvent<Date>) => this.lastDateInput = e.value;
3738
onDateChange = (e: MatDatepickerInputEvent<Date>) => this.lastDateChange = e.value;

src/lib/datepicker/multi-year-view.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('MatMultiYearView', () => {
1818

1919
// Test components.
2020
StandardMultiYearView,
21+
MultiYearViewWithDateFilter,
2122
],
2223
});
2324

@@ -71,6 +72,27 @@ describe('MatMultiYearView', () => {
7172
expect(cellEls[1].classList).toContain('mat-calendar-body-active');
7273
});
7374
});
75+
76+
describe('multi year view with date filter', () => {
77+
let fixture: ComponentFixture<MultiYearViewWithDateFilter>;
78+
let testComponent: MultiYearViewWithDateFilter;
79+
let multiYearViewNativeElement: Element;
80+
81+
beforeEach(() => {
82+
fixture = TestBed.createComponent(MultiYearViewWithDateFilter);
83+
fixture.detectChanges();
84+
85+
const multiYearViewDebugElement = fixture.debugElement.query(By.directive(MatMultiYearView));
86+
multiYearViewNativeElement = multiYearViewDebugElement.nativeElement;
87+
testComponent = fixture.componentInstance;
88+
});
89+
90+
it('should disablex years with no enabled days', () => {
91+
const cells = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
92+
expect(cells[0].classList).not.toContain('mat-calendar-body-disabled');
93+
expect(cells[1].classList).toContain('mat-calendar-body-disabled');
94+
});
95+
});
7496
});
7597

7698

@@ -84,3 +106,15 @@ class StandardMultiYearView {
84106

85107
@ViewChild(MatYearView) yearView: MatYearView<Date>;
86108
}
109+
110+
@Component({
111+
template: `
112+
<mat-multi-year-view [activeDate]="activeDate" [dateFilter]="dateFilter"></mat-multi-year-view>
113+
`
114+
})
115+
class MultiYearViewWithDateFilter {
116+
activeDate = new Date(2017, JAN, 1);
117+
dateFilter(date: Date) {
118+
return date.getFullYear() !== 2017;
119+
}
120+
}

src/lib/datepicker/multi-year-view.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,26 @@ export class MatMultiYearView<D> implements AfterContentInit {
124124
/** Creates an MatCalendarCell for the given year. */
125125
private _createCellForYear(year: number) {
126126
let yearName = this._dateAdapter.getYearName(this._dateAdapter.createDate(year, 0, 1));
127-
return new MatCalendarCell(year, yearName, yearName, true);
127+
return new MatCalendarCell(year, yearName, yearName, this._isYearEnabled(year));
128+
}
129+
130+
/** Whether the given year is enabled. */
131+
private _isYearEnabled(year: number) {
132+
if (!this.dateFilter) {
133+
return true;
134+
}
135+
136+
const firstOfYear = this._dateAdapter.createDate(year, 0, 1);
137+
138+
// If any date in the year is enabled count the year as enabled.
139+
for (let date = firstOfYear; this._dateAdapter.getYear(date) == year;
140+
date = this._dateAdapter.addCalendarDays(date, 1)) {
141+
if (this.dateFilter(date)) {
142+
return true;
143+
}
144+
}
145+
146+
return false;
128147
}
129148

130149
/**

src/lib/datepicker/year-view.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ describe('MatYearView', () => {
9797
fixture = TestBed.createComponent(YearViewWithDateFilter);
9898
fixture.detectChanges();
9999

100-
let yearViewDebugElement = fixture.debugElement.query(By.directive(MatYearView));
100+
const yearViewDebugElement = fixture.debugElement.query(By.directive(MatYearView));
101101
yearViewNativeElement = yearViewDebugElement.nativeElement;
102102
testComponent = fixture.componentInstance;
103103
});
104104

105-
it('should disabled months with no enabled days', () => {
106-
let cells = yearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
105+
it('should disable months with no enabled days', () => {
106+
const cells = yearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
107107
expect(cells[0].classList).not.toContain('mat-calendar-body-disabled');
108108
expect(cells[1].classList).toContain('mat-calendar-body-disabled');
109109
});

0 commit comments

Comments
 (0)