-
Notifications
You must be signed in to change notification settings - Fork 6.8k
docs(datepicker): add docs for custom calendar header #10543
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
<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 class="demo-calendar-header"> | ||
<button mat-icon-button class="demo-double-arrow" (click)="previousClicked('year')"> | ||
<mat-icon>keyboard_arrow_left</mat-icon> | ||
<mat-icon>keyboard_arrow_left</mat-icon> | ||
</button> | ||
<button mat-icon-button (click)="previousClicked('month')"> | ||
<mat-icon>keyboard_arrow_left</mat-icon> | ||
</button> | ||
<span class="demo-calendar-header-label">{{periodLabel}}</span> | ||
<button mat-icon-button (click)="nextClicked('month')"> | ||
<mat-icon>keyboard_arrow_right</mat-icon> | ||
</button> | ||
<button mat-icon-button class="demo-double-arrow" (click)="nextClicked('year')"> | ||
<mat-icon>keyboard_arrow_right</mat-icon> | ||
<mat-icon>keyboard_arrow_right</mat-icon> | ||
</button> | ||
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
.custom-header { | ||
padding: 1em 1.5em; | ||
.demo-calendar-header { | ||
display: flex; | ||
align-items: center; | ||
padding: 0.5em; | ||
} | ||
|
||
.custom-header-label { | ||
.demo-calendar-header-label { | ||
flex: 1; | ||
height: 1em; | ||
font-weight: bold; | ||
text-align: center; | ||
} | ||
|
||
.demo-double-arrow .mat-icon { | ||
margin: -22%; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/material-examples/datepicker-custom-header/datepicker-custom-header-example.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.example-header { | ||
display: flex; | ||
align-items: center; | ||
padding: 0.5em; | ||
} | ||
|
||
.example-header-label { | ||
flex: 1; | ||
height: 1em; | ||
font-weight: bold; | ||
text-align: center; | ||
} | ||
|
||
.example-double-arrow .mat-icon { | ||
margin: -22%; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/material-examples/datepicker-custom-header/datepicker-custom-header-example.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<mat-form-field> | ||
<mat-label>Custom calendar header</mat-label> | ||
<input matInput [matDatepicker]="picker"> | ||
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> | ||
<mat-datepicker #picker [calendarHeaderComponent]="exampleHeader"></mat-datepicker> | ||
</mat-form-field> |
86 changes: 86 additions & 0 deletions
86
src/material-examples/datepicker-custom-header/datepicker-custom-header-example.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
Host, | ||
Inject, | ||
OnDestroy, | ||
ViewEncapsulation | ||
} from '@angular/core'; | ||
import {MatCalendar} from '@angular/material'; | ||
import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats} from '@angular/material/core'; | ||
import {Subject} from 'rxjs'; | ||
import {takeUntil} from 'rxjs/operators'; | ||
|
||
/** @title Datepicker with custom calendar header */ | ||
@Component({ | ||
selector: 'datepicker-custom-header-example', | ||
templateUrl: 'datepicker-custom-header-example.html', | ||
styleUrls: ['datepicker-custom-header-example.css'], | ||
encapsulation: ViewEncapsulation.None, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class DatepickerCustomHeaderExample { | ||
exampleHeader = ExampleHeader; | ||
} | ||
|
||
/** Custom header component for datepicker. */ | ||
@Component({ | ||
selector: 'example-header', | ||
template: ` | ||
<div class="example-header"> | ||
<button mat-icon-button class="example-double-arrow" (click)="previousClicked('year')"> | ||
<mat-icon>keyboard_arrow_left</mat-icon> | ||
<mat-icon>keyboard_arrow_left</mat-icon> | ||
</button> | ||
<button mat-icon-button (click)="previousClicked('month')"> | ||
<mat-icon>keyboard_arrow_left</mat-icon> | ||
</button> | ||
<span class="example-header-label">{{periodLabel}}</span> | ||
<button mat-icon-button (click)="nextClicked('month')"> | ||
<mat-icon>keyboard_arrow_right</mat-icon> | ||
</button> | ||
<button mat-icon-button class="example-double-arrow" (click)="nextClicked('year')"> | ||
<mat-icon>keyboard_arrow_right</mat-icon> | ||
<mat-icon>keyboard_arrow_right</mat-icon> | ||
</button> | ||
</div> | ||
`, | ||
encapsulation: ViewEncapsulation.None, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class ExampleHeader<D> implements OnDestroy { | ||
private destroyed = new Subject<void>(); | ||
|
||
constructor(@Host() private calendar: MatCalendar<D>, | ||
private dateAdapter: DateAdapter<D>, | ||
@Inject(MAT_DATE_FORMATS) private dateFormats: MatDateFormats, | ||
cdr: ChangeDetectorRef) { | ||
calendar.stateChanges | ||
.pipe(takeUntil(this.destroyed)) | ||
.subscribe(() => cdr.markForCheck()); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroyed.next(); | ||
this.destroyed.complete(); | ||
} | ||
|
||
get periodLabel() { | ||
return this.dateAdapter | ||
.format(this.calendar.activeDate, this.dateFormats.display.monthYearLabel) | ||
.toLocaleUpperCase(); | ||
} | ||
|
||
previousClicked(mode: 'month' | 'year') { | ||
this.calendar.activeDate = mode == 'month' ? | ||
this.dateAdapter.addCalendarMonths(this.calendar.activeDate, -1) : | ||
this.dateAdapter.addCalendarYears(this.calendar.activeDate, -1); | ||
} | ||
|
||
nextClicked(mode: 'month' | 'year') { | ||
this.calendar.activeDate = mode == 'month' ? | ||
this.dateAdapter.addCalendarMonths(this.calendar.activeDate, 1) : | ||
this.dateAdapter.addCalendarYears(this.calendar.activeDate, 1); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe not the right time to ask this question, but why an API that takes a component as an input? We don't have any APIs like that elsewhere in the library, instead using templates and/or content projection.
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.
Content projection wasn't feasible because it would have had to be passed through several layers
<mat-datepicker>
--><mat-datepicker-content>
--><mat-calendar>
. A template may have been workable, but there's a fair chunk of logic that goes with the header so a component seemed cleaner. At some point I want to break the datepicker into a bunch of composable pieces, we may be able to change how this works at that point