@@ -64,68 +64,47 @@ export class MatCalendarHeader<D> {
6464 calendar = inject < MatCalendar < D > > ( MatCalendar ) ;
6565 private _dateAdapter = inject < DateAdapter < D > > ( DateAdapter , { optional : true } ) ! ;
6666 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 ;
6772
6873 constructor ( ...args : unknown [ ] ) ;
6974
7075 constructor ( ) {
7176 inject ( _CdkPrivateStyleLoader ) . load ( _VisuallyHiddenLoader ) ;
7277 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+ } ) ;
7483 }
7584
7685 /** The display text for the current calendar view. */
7786 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 ;
8888 }
8989
9090 /** The aria description for the current calendar view. */
9191 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 ;
10493 }
10594
10695 /** The `aria-label` for changing the calendar view. */
10796 get periodButtonLabel ( ) : string {
108- return this . calendar . currentView == 'month'
109- ? this . _intl . switchToMultiYearViewLabel
110- : this . _intl . switchToMonthViewLabel ;
97+ return this . _periodButtonLabel ;
11198 }
11299
113100 /** The label for the previous button. */
114101 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 ;
120103 }
121104
122105 /** The label for the next button. */
123106 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 ;
129108 }
130109
131110 /** Handles user clicks on the period label. */
@@ -172,6 +151,41 @@ export class MatCalendarHeader<D> {
172151 ) ;
173152 }
174153
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+
175189 /** Whether the two dates represent the same view in the current view mode (month or year). */
176190 private _isSameView ( date1 : D , date2 : D ) : boolean {
177191 if ( this . calendar . currentView == 'month' ) {
@@ -387,6 +401,7 @@ export class MatCalendar<D> implements AfterContentInit, AfterViewChecked, OnDes
387401 this . _moveFocusOnNextTick = true ;
388402 this . _changeDetectorRef . markForCheck ( ) ;
389403 if ( viewChangedResult ) {
404+ this . stateChanges . next ( ) ;
390405 this . viewChanged . emit ( viewChangedResult ) ;
391406 }
392407 }
0 commit comments