|
| 1 | +import { |
| 2 | + ChangeDetectionStrategy, |
| 3 | + ChangeDetectorRef, |
| 4 | + Component, |
| 5 | + Host, |
| 6 | + Inject, |
| 7 | + OnDestroy, |
| 8 | + ViewEncapsulation |
| 9 | +} from '@angular/core'; |
| 10 | +import {MatCalendar} from '@angular/material'; |
| 11 | +import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats} from '@angular/material/core'; |
| 12 | +import {Subject} from 'rxjs'; |
| 13 | +import {takeUntil} from 'rxjs/operators'; |
| 14 | + |
| 15 | +/** @title Datepicker with custom calendar header */ |
| 16 | +@Component({ |
| 17 | + selector: 'datepicker-custom-header-example', |
| 18 | + templateUrl: 'datepicker-custom-header-example.html', |
| 19 | + styleUrls: ['datepicker-custom-header-example.css'], |
| 20 | + encapsulation: ViewEncapsulation.None, |
| 21 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 22 | +}) |
| 23 | +export class DatepickerCustomHeaderExample { |
| 24 | + exampleHeader = ExampleHeader; |
| 25 | +} |
| 26 | + |
| 27 | +/** Custom header component for datepicker. */ |
| 28 | +@Component({ |
| 29 | + selector: 'example-header', |
| 30 | + template: ` |
| 31 | + <div class="example-header"> |
| 32 | + <button mat-icon-button class="example-double-arrow" (click)="previousClicked('year')"> |
| 33 | + <mat-icon>keyboard_arrow_left</mat-icon> |
| 34 | + <mat-icon>keyboard_arrow_left</mat-icon> |
| 35 | + </button> |
| 36 | + <button mat-icon-button (click)="previousClicked('month')"> |
| 37 | + <mat-icon>keyboard_arrow_left</mat-icon> |
| 38 | + </button> |
| 39 | + <span class="example-header-label">{{periodLabel}}</span> |
| 40 | + <button mat-icon-button (click)="nextClicked('month')"> |
| 41 | + <mat-icon>keyboard_arrow_right</mat-icon> |
| 42 | + </button> |
| 43 | + <button mat-icon-button class="example-double-arrow" (click)="nextClicked('year')"> |
| 44 | + <mat-icon>keyboard_arrow_right</mat-icon> |
| 45 | + <mat-icon>keyboard_arrow_right</mat-icon> |
| 46 | + </button> |
| 47 | + </div> |
| 48 | + `, |
| 49 | + encapsulation: ViewEncapsulation.None, |
| 50 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 51 | +}) |
| 52 | +export class ExampleHeader<D> implements OnDestroy { |
| 53 | + private destroyed = new Subject<void>(); |
| 54 | + |
| 55 | + constructor(@Host() private calendar: MatCalendar<D>, |
| 56 | + private dateAdapter: DateAdapter<D>, |
| 57 | + @Inject(MAT_DATE_FORMATS) private dateFormats: MatDateFormats, |
| 58 | + cdr: ChangeDetectorRef) { |
| 59 | + calendar.stateChanges |
| 60 | + .pipe(takeUntil(this.destroyed)) |
| 61 | + .subscribe(() => cdr.markForCheck()); |
| 62 | + } |
| 63 | + |
| 64 | + ngOnDestroy() { |
| 65 | + this.destroyed.next(); |
| 66 | + this.destroyed.complete(); |
| 67 | + } |
| 68 | + |
| 69 | + get periodLabel() { |
| 70 | + return this.dateAdapter |
| 71 | + .format(this.calendar.activeDate, this.dateFormats.display.monthYearLabel) |
| 72 | + .toLocaleUpperCase(); |
| 73 | + } |
| 74 | + |
| 75 | + previousClicked(mode: 'month' | 'year') { |
| 76 | + this.calendar.activeDate = mode == 'month' ? |
| 77 | + this.dateAdapter.addCalendarMonths(this.calendar.activeDate, -1) : |
| 78 | + this.dateAdapter.addCalendarYears(this.calendar.activeDate, -1); |
| 79 | + } |
| 80 | + |
| 81 | + nextClicked(mode: 'month' | 'year') { |
| 82 | + this.calendar.activeDate = mode == 'month' ? |
| 83 | + this.dateAdapter.addCalendarMonths(this.calendar.activeDate, 1) : |
| 84 | + this.dateAdapter.addCalendarYears(this.calendar.activeDate, 1); |
| 85 | + } |
| 86 | +} |
0 commit comments