Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(datepicker): Add Custom Header to DatePicker#9639
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
6251e1e8d3014081c89fa737a563998be7ca14dbd4585407f6668ae6f9628a8050904ba2db2837f071ec5c9f6623467c0a014777a179c9afb6938f20faefc4aa8f8372b4d4d2b23810746b2c9da45f3eef8c4c5bb6b50ccca7a0db2058afbf6f19e8702317d528361ee36dc71a7975d202b560457bd12ca88fecb4c66467258929a81147f1586d5daa9d4c589bba08f1ff7e455196ee74cee00cda4993870ff16f533739ec2874956d0a4d2a33b70cc7f21bf715e4135f4b0fc3057f9dd81db6094c825ab6fc003796cbf2abf1c621fdb1c3822dd56ba4e8391ee0f93aff641dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <div class="custom-header"> | ||
| <button mat-icon-button (click)="previousClicked('year')"><<</button> | ||
| <button mat-icon-button (click)="previousClicked('month')"><</button> | ||
| <span class="custom-header-label">{{periodLabel}}</span> | ||
| <button mat-icon-button (click)="nextClicked('month')">></button> | ||
| <button mat-icon-button (click)="nextClicked('year')">>></button> | ||
| </div> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| .custom-header { | ||
| padding: 1em 1.5em; | ||
| display: flex; | ||
| align-items: center; | ||
| } | ||
| .custom-header-label { | ||
| flex: 1; | ||
| text-align: center; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <div class="mat-calendar-header"> | ||
| <div class="mat-calendar-controls"> | ||
| <button mat-button class="mat-calendar-period-button" | ||
| (click)="currentPeriodClicked()" [attr.aria-label]="periodButtonLabel"> | ||
| {{periodButtonText}} | ||
| <div class="mat-calendar-arrow" [class.mat-calendar-invert]="calendar.currentView != 'month'"></div> | ||
| </button> | ||
| <div class="mat-calendar-spacer"></div> | ||
| <button mat-icon-button class="mat-calendar-previous-button" | ||
| [disabled]="!previousEnabled()" (click)="previousClicked()" | ||
| [attr.aria-label]="prevButtonLabel"> | ||
| </button> | ||
| <button mat-icon-button class="mat-calendar-next-button" | ||
| [disabled]="!nextEnabled()" (click)="nextClicked()" | ||
| [attr.aria-label]="nextButtonLabel"> | ||
| </button> | ||
| </div> | ||
| </div> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import {Direction, Directionality} from '@angular/cdk/bidi'; | ||
| import {MatDatepickerModule} from './datepicker-module'; | ||
| import {async, ComponentFixture, TestBed} from '@angular/core/testing'; | ||
| import {MatDatepickerIntl} from './datepicker-intl'; | ||
| import {DEC, FEB, JAN, MatNativeDateModule} from '@angular/material/core'; | ||
| import {Component} from '@angular/core'; | ||
| import {MatCalendar} from './calendar'; | ||
| import {By} from '@angular/platform-browser'; | ||
| import {yearsPerPage} from './multi-year-view'; | ||
| describe('MatCalendarHeader', () => { | ||
| let dir: { value: Direction }; | ||
| beforeEach(async(() => { | ||
| TestBed.configureTestingModule({ | ||
| imports: [ | ||
| MatNativeDateModule, | ||
| MatDatepickerModule, | ||
| ], | ||
| declarations: [ | ||
| // Test components. | ||
| StandardCalendar, | ||
| ], | ||
| providers: [ | ||
| MatDatepickerIntl, | ||
| {provide: Directionality, useFactory: () => dir = {value: 'ltr'}} | ||
| ], | ||
| }); | ||
| TestBed.compileComponents(); | ||
| })); | ||
| describe('standard calendar', () => { | ||
| let fixture: ComponentFixture<StandardCalendar>; | ||
| let testComponent: StandardCalendar; | ||
| let calendarElement: HTMLElement; | ||
| let periodButton: HTMLElement; | ||
| let prevButton: HTMLElement; | ||
| let nextButton: HTMLElement; | ||
| let calendarInstance: MatCalendar<Date>; | ||
| beforeEach(() => { | ||
| fixture = TestBed.createComponent(StandardCalendar); | ||
| fixture.detectChanges(); | ||
| let calendarDebugElement = fixture.debugElement.query(By.directive(MatCalendar)); | ||
| calendarElement = calendarDebugElement.nativeElement; | ||
| periodButton = calendarElement.querySelector('.mat-calendar-period-button') as HTMLElement; | ||
| prevButton = calendarElement.querySelector('.mat-calendar-previous-button') as HTMLElement; | ||
| nextButton = calendarElement.querySelector('.mat-calendar-next-button') as HTMLElement; | ||
| calendarInstance = calendarDebugElement.componentInstance; | ||
| testComponent = fixture.componentInstance; | ||
| }); | ||
| it('should be in month view with specified month active', () => { | ||
| expect(calendarInstance.currentView).toBe('month'); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); | ||
| }); | ||
| it('should toggle view when period clicked', () => { | ||
| expect(calendarInstance.currentView).toBe('month'); | ||
| periodButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.currentView).toBe('multi-year'); | ||
| periodButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.currentView).toBe('month'); | ||
| }); | ||
| it('should go to next and previous month', () => { | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); | ||
| nextButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2017, FEB, 28)); | ||
| prevButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 28)); | ||
| }); | ||
| it('should go to previous and next year', () => { | ||
| periodButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.currentView).toBe('multi-year'); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); | ||
| (calendarElement.querySelector('.mat-calendar-body-active') as HTMLElement).click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.currentView).toBe('year'); | ||
| nextButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2018, JAN, 31)); | ||
| prevButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); | ||
| }); | ||
| it('should go to previous and next multi-year range', () => { | ||
| periodButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.currentView).toBe('multi-year'); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); | ||
| nextButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2017 + yearsPerPage, JAN, 31)); | ||
| prevButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); | ||
| }); | ||
| it('should go back to month view after selecting year and month', () => { | ||
| periodButton.click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.currentView).toBe('multi-year'); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); | ||
| let yearCells = calendarElement.querySelectorAll('.mat-calendar-body-cell'); | ||
| (yearCells[0] as HTMLElement).click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.currentView).toBe('year'); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2016, JAN, 31)); | ||
| let monthCells = calendarElement.querySelectorAll('.mat-calendar-body-cell'); | ||
| (monthCells[monthCells.length - 1] as HTMLElement).click(); | ||
| fixture.detectChanges(); | ||
| expect(calendarInstance.currentView).toBe('month'); | ||
| expect(calendarInstance.activeDate).toEqual(new Date(2016, DEC, 31)); | ||
| expect(testComponent.selected).toBeFalsy('no date should be selected yet'); | ||
| }); | ||
| }); | ||
| }); | ||
| @Component({ | ||
| template: ` | ||
| <mat-calendar | ||
| [startAt]="startDate" | ||
| [(selected)]="selected" | ||
| (yearSelected)="selectedYear=$event" | ||
| (monthSelected)="selectedMonth=$event"> | ||
| </mat-calendar>` | ||
| }) | ||
| class StandardCalendar { | ||
| selected: Date; | ||
| selectedYear: Date; | ||
| selectedMonth: Date; | ||
| startDate = new Date(2017, JAN, 31); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
nit: normal indentation = 2 spaces, continuation lines can be indented 4 or align the attributes
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.
done in a7975d2