@@ -64,68 +64,47 @@ export class MatCalendarHeader<D> {
64
64
calendar = inject < MatCalendar < D > > ( MatCalendar ) ;
65
65
private _dateAdapter = inject < DateAdapter < D > > ( DateAdapter , { optional : true } ) ! ;
66
66
private _dateFormats = inject < MatDateFormats > ( MAT_DATE_FORMATS , { optional : true } ) ! ;
67
+ private _periodButtonText : string ;
68
+ private _periodButtonDescription : string ;
69
+ private _periodButtonLabel : string ;
70
+ private _prevButtonLabel : string ;
71
+ private _nextButtonLabel : string ;
67
72
68
73
constructor ( ...args : unknown [ ] ) ;
69
74
70
75
constructor ( ) {
71
76
inject ( _CdkPrivateStyleLoader ) . load ( _VisuallyHiddenLoader ) ;
72
77
const changeDetectorRef = inject ( ChangeDetectorRef ) ;
73
- this . calendar . stateChanges . subscribe ( ( ) => changeDetectorRef . markForCheck ( ) ) ;
78
+ this . _updateLabels ( ) ;
79
+ this . calendar . stateChanges . subscribe ( ( ) => {
80
+ this . _updateLabels ( ) ;
81
+ changeDetectorRef . markForCheck ( ) ;
82
+ } ) ;
74
83
}
75
84
76
85
/** The display text for the current calendar view. */
77
86
get periodButtonText ( ) : string {
78
- if ( this . calendar . currentView == 'month' ) {
79
- return this . _dateAdapter
80
- . format ( this . calendar . activeDate , this . _dateFormats . display . monthYearLabel )
81
- . toLocaleUpperCase ( ) ;
82
- }
83
- if ( this . calendar . currentView == 'year' ) {
84
- return this . _dateAdapter . getYearName ( this . calendar . activeDate ) ;
85
- }
86
-
87
- return this . _intl . formatYearRange ( ...this . _formatMinAndMaxYearLabels ( ) ) ;
87
+ return this . _periodButtonText ;
88
88
}
89
89
90
90
/** The aria description for the current calendar view. */
91
91
get periodButtonDescription ( ) : string {
92
- if ( this . calendar . currentView == 'month' ) {
93
- return this . _dateAdapter
94
- . format ( this . calendar . activeDate , this . _dateFormats . display . monthYearLabel )
95
- . toLocaleUpperCase ( ) ;
96
- }
97
- if ( this . calendar . currentView == 'year' ) {
98
- return this . _dateAdapter . getYearName ( this . calendar . activeDate ) ;
99
- }
100
-
101
- // Format a label for the window of years displayed in the multi-year calendar view. Use
102
- // `formatYearRangeLabel` because it is TTS friendly.
103
- return this . _intl . formatYearRangeLabel ( ...this . _formatMinAndMaxYearLabels ( ) ) ;
92
+ return this . _periodButtonDescription ;
104
93
}
105
94
106
95
/** The `aria-label` for changing the calendar view. */
107
96
get periodButtonLabel ( ) : string {
108
- return this . calendar . currentView == 'month'
109
- ? this . _intl . switchToMultiYearViewLabel
110
- : this . _intl . switchToMonthViewLabel ;
97
+ return this . _periodButtonLabel ;
111
98
}
112
99
113
100
/** The label for the previous button. */
114
101
get prevButtonLabel ( ) : string {
115
- return {
116
- 'month' : this . _intl . prevMonthLabel ,
117
- 'year' : this . _intl . prevYearLabel ,
118
- 'multi-year' : this . _intl . prevMultiYearLabel ,
119
- } [ this . calendar . currentView ] ;
102
+ return this . _prevButtonLabel ;
120
103
}
121
104
122
105
/** The label for the next button. */
123
106
get nextButtonLabel ( ) : string {
124
- return {
125
- 'month' : this . _intl . nextMonthLabel ,
126
- 'year' : this . _intl . nextYearLabel ,
127
- 'multi-year' : this . _intl . nextMultiYearLabel ,
128
- } [ this . calendar . currentView ] ;
107
+ return this . _nextButtonLabel ;
129
108
}
130
109
131
110
/** Handles user clicks on the period label. */
@@ -172,6 +151,41 @@ export class MatCalendarHeader<D> {
172
151
) ;
173
152
}
174
153
154
+ /** Updates the labels for the various sections of the header. */
155
+ private _updateLabels ( ) {
156
+ const calendar = this . calendar ;
157
+ const intl = this . _intl ;
158
+ const adapter = this . _dateAdapter ;
159
+
160
+ if ( calendar . currentView === 'month' ) {
161
+ this . _periodButtonText = adapter
162
+ . format ( calendar . activeDate , this . _dateFormats . display . monthYearLabel )
163
+ . toLocaleUpperCase ( ) ;
164
+ this . _periodButtonDescription = adapter
165
+ . format ( calendar . activeDate , this . _dateFormats . display . monthYearLabel )
166
+ . toLocaleUpperCase ( ) ;
167
+ this . _periodButtonLabel = intl . switchToMultiYearViewLabel ;
168
+ this . _prevButtonLabel = intl . prevMonthLabel ;
169
+ this . _nextButtonLabel = intl . nextMonthLabel ;
170
+ } else if ( calendar . currentView === 'year' ) {
171
+ this . _periodButtonText = adapter . getYearName ( calendar . activeDate ) ;
172
+ this . _periodButtonDescription = adapter . getYearName ( calendar . activeDate ) ;
173
+ this . _periodButtonLabel = intl . switchToMonthViewLabel ;
174
+ this . _prevButtonLabel = intl . prevYearLabel ;
175
+ this . _nextButtonLabel = intl . nextYearLabel ;
176
+ } else {
177
+ this . _periodButtonText = intl . formatYearRange ( ...this . _formatMinAndMaxYearLabels ( ) ) ;
178
+ // Format a label for the window of years displayed in the multi-year calendar view. Use
179
+ // `formatYearRangeLabel` because it is TTS friendly.
180
+ this . _periodButtonDescription = intl . formatYearRangeLabel (
181
+ ...this . _formatMinAndMaxYearLabels ( ) ,
182
+ ) ;
183
+ this . _periodButtonLabel = intl . switchToMonthViewLabel ;
184
+ this . _prevButtonLabel = intl . prevMultiYearLabel ;
185
+ this . _nextButtonLabel = intl . nextMultiYearLabel ;
186
+ }
187
+ }
188
+
175
189
/** Whether the two dates represent the same view in the current view mode (month or year). */
176
190
private _isSameView ( date1 : D , date2 : D ) : boolean {
177
191
if ( this . calendar . currentView == 'month' ) {
@@ -387,6 +401,7 @@ export class MatCalendar<D> implements AfterContentInit, AfterViewChecked, OnDes
387
401
this . _moveFocusOnNextTick = true ;
388
402
this . _changeDetectorRef . markForCheck ( ) ;
389
403
if ( viewChangedResult ) {
404
+ this . stateChanges . next ( ) ;
390
405
this . viewChanged . emit ( viewChangedResult ) ;
391
406
}
392
407
}
0 commit comments