-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
fix(datepicker): gray out filtered years in multi-year view #9563
Changes from 2 commits
943aa90
e59ceec
08c3691
2ab0832
d764085
7af5ba0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ describe('MatMultiYearView', () => { | |
|
||
// Test components. | ||
StandardMultiYearView, | ||
MultiYearViewWithDateFilter, | ||
], | ||
}); | ||
|
||
|
@@ -71,6 +72,27 @@ describe('MatMultiYearView', () => { | |
expect(cellEls[1].classList).toContain('mat-calendar-body-active'); | ||
}); | ||
}); | ||
|
||
describe('multi year view with date filter', () => { | ||
let fixture: ComponentFixture<MultiYearViewWithDateFilter>; | ||
let testComponent: MultiYearViewWithDateFilter; | ||
let multiYearViewNativeElement: Element; | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(MultiYearViewWithDateFilter); | ||
fixture.detectChanges(); | ||
|
||
let multiYearViewDebugElement = fixture.debugElement.query(By.directive(MatMultiYearView)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
multiYearViewNativeElement = multiYearViewDebugElement.nativeElement; | ||
testComponent = fixture.componentInstance; | ||
}); | ||
|
||
it('should disable years with no enabled days', () => { | ||
let cells = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
expect(cells[0].classList).not.toContain('mat-calendar-body-disabled'); | ||
expect(cells[1].classList).toContain('mat-calendar-body-disabled'); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
|
@@ -84,3 +106,16 @@ class StandardMultiYearView { | |
|
||
@ViewChild(MatYearView) yearView: MatYearView<Date>; | ||
} | ||
|
||
@Component({ | ||
template: `<mat-multi-year-view [activeDate]="activeDate" [dateFilter]="dateFilter"></mat-multi-year-view>` | ||
}) | ||
class MultiYearViewWithDateFilter { | ||
activeDate = new Date(2017, JAN, 1); | ||
dateFilter(date: Date) { | ||
if (date.getFullYear() == 2017) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about |
||
return false; | ||
} | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,9 @@ import { | |
Output, | ||
ViewEncapsulation | ||
} from '@angular/core'; | ||
import {DateAdapter} from '@angular/material/core'; | ||
import {MatCalendarCell} from './calendar-body'; | ||
import {createMissingDateImplError} from './datepicker-errors'; | ||
import { DateAdapter } from '@angular/material/core'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undo |
||
import { MatCalendarCell } from './calendar-body'; | ||
import { createMissingDateImplError } from './datepicker-errors'; | ||
|
||
|
||
export const yearsPerPage = 24; | ||
|
@@ -47,9 +47,9 @@ export class MatMultiYearView<D> implements AfterContentInit { | |
set activeDate(value: D) { | ||
let oldActiveDate = this._activeDate; | ||
this._activeDate = | ||
this._getValidDateOrNull(this._dateAdapter.deserialize(value)) || this._dateAdapter.today(); | ||
this._getValidDateOrNull(this._dateAdapter.deserialize(value)) || this._dateAdapter.today(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undo |
||
if (Math.floor(this._dateAdapter.getYear(oldActiveDate) / yearsPerPage) != | ||
Math.floor(this._dateAdapter.getYear(this._activeDate) / yearsPerPage)) { | ||
Math.floor(this._dateAdapter.getYear(this._activeDate) / yearsPerPage)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missed this one |
||
this._init(); | ||
} | ||
} | ||
|
@@ -79,8 +79,8 @@ export class MatMultiYearView<D> implements AfterContentInit { | |
/** The year of the selected date. Null if the selected date is null. */ | ||
_selectedYear: number | null; | ||
|
||
constructor(@Optional() public _dateAdapter: DateAdapter<D>, | ||
private _changeDetectorRef: ChangeDetectorRef) { | ||
constructor( @Optional() public _dateAdapter: DateAdapter<D>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undo |
||
private _changeDetectorRef: ChangeDetectorRef) { | ||
if (!this._dateAdapter) { | ||
throw createMissingDateImplError('DateAdapter'); | ||
} | ||
|
@@ -112,9 +112,9 @@ export class MatMultiYearView<D> implements AfterContentInit { | |
_yearSelected(year: number) { | ||
let month = this._dateAdapter.getMonth(this.activeDate); | ||
let daysInMonth = | ||
this._dateAdapter.getNumDaysInMonth(this._dateAdapter.createDate(year, month, 1)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undo |
||
this._dateAdapter.getNumDaysInMonth(this._dateAdapter.createDate(year, month, 1)); | ||
this.selectedChange.emit(this._dateAdapter.createDate(year, month, | ||
Math.min(this._dateAdapter.getDate(this.activeDate), daysInMonth))); | ||
Math.min(this._dateAdapter.getDate(this.activeDate), daysInMonth))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missed this one |
||
} | ||
|
||
_getActiveCell(): number { | ||
|
@@ -124,7 +124,26 @@ export class MatMultiYearView<D> implements AfterContentInit { | |
/** Creates an MatCalendarCell for the given year. */ | ||
private _createCellForYear(year: number) { | ||
let yearName = this._dateAdapter.getYearName(this._dateAdapter.createDate(year, 0, 1)); | ||
return new MatCalendarCell(year, yearName, yearName, true); | ||
return new MatCalendarCell(year, yearName, yearName, this._isYearEnabled(year)); | ||
} | ||
|
||
/** Whether the given year is enabled. */ | ||
private _isYearEnabled(year: number) { | ||
if (!this.dateFilter) { | ||
return true; | ||
} | ||
|
||
let firstOfYear = this._dateAdapter.createDate(year, 0, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const |
||
|
||
// If any date in the year is enabled count the year as enabled. | ||
for (let date = firstOfYear; this._dateAdapter.getYear(date) == year; | ||
date = this._dateAdapter.addCalendarDays(date, 1)) { | ||
if (this.dateFilter(date)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be simplified to
!date.getFullYear() && date.getMonth() % 2 == 1 && !date.getDate() % 2;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assume you meant:
!date.getFullYear() % 2 && date.getMonth() % 2 && !date.getDate() % 2;
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ups, yes. Bad copy/paste :D