@@ -47,9 +47,6 @@ import {MAT_SINGLE_DATE_SELECTION_MODEL_PROVIDER, DateRange} from './date-select
47
47
*/
48
48
export type MatCalendarView = 'month' | 'year' | 'multi-year' ;
49
49
50
- /** Counter used to generate unique IDs. */
51
- let uniqueId = 0 ;
52
-
53
50
/** Default header for MatCalendar */
54
51
@Component ( {
55
52
selector : 'mat-calendar-header' ,
@@ -59,8 +56,6 @@ let uniqueId = 0;
59
56
changeDetection : ChangeDetectionStrategy . OnPush ,
60
57
} )
61
58
export class MatCalendarHeader < D > {
62
- _buttonDescriptionId = `mat-calendar-button-${ uniqueId ++ } ` ;
63
-
64
59
constructor (
65
60
private _intl : MatDatepickerIntl ,
66
61
@Inject ( forwardRef ( ( ) => MatCalendar ) ) public calendar : MatCalendar < D > ,
@@ -71,7 +66,7 @@ export class MatCalendarHeader<D> {
71
66
this . calendar . stateChanges . subscribe ( ( ) => changeDetectorRef . markForCheck ( ) ) ;
72
67
}
73
68
74
- /** The label for the current calendar view. */
69
+ /** The display text for the current calendar view. */
75
70
get periodButtonText ( ) : string {
76
71
if ( this . calendar . currentView == 'month' ) {
77
72
return this . _dateAdapter
@@ -82,28 +77,26 @@ export class MatCalendarHeader<D> {
82
77
return this . _dateAdapter . getYearName ( this . calendar . activeDate ) ;
83
78
}
84
79
85
- // The offset from the active year to the "slot" for the starting year is the
86
- // *actual* first rendered year in the multi-year view, and the last year is
87
- // just yearsPerPage - 1 away.
88
- const activeYear = this . _dateAdapter . getYear ( this . calendar . activeDate ) ;
89
- const minYearOfPage =
90
- activeYear -
91
- getActiveOffset (
92
- this . _dateAdapter ,
93
- this . calendar . activeDate ,
94
- this . calendar . minDate ,
95
- this . calendar . maxDate ,
96
- ) ;
97
- const maxYearOfPage = minYearOfPage + yearsPerPage - 1 ;
98
- const minYearName = this . _dateAdapter . getYearName (
99
- this . _dateAdapter . createDate ( minYearOfPage , 0 , 1 ) ,
100
- ) ;
101
- const maxYearName = this . _dateAdapter . getYearName (
102
- this . _dateAdapter . createDate ( maxYearOfPage , 0 , 1 ) ,
103
- ) ;
104
- return this . _intl . formatYearRange ( minYearName , maxYearName ) ;
80
+ return this . _intl . formatYearRange ( ...this . _formatMinAndMaxYearLabels ( ) ) ;
81
+ }
82
+
83
+ /** The `aria-description` for the current calendar view. */
84
+ get periodButtonDescription ( ) : string {
85
+ if ( this . calendar . currentView == 'month' ) {
86
+ return this . _dateAdapter
87
+ . format ( this . calendar . activeDate , this . _dateFormats . display . monthYearLabel )
88
+ . toLocaleUpperCase ( ) ;
89
+ }
90
+ if ( this . calendar . currentView == 'year' ) {
91
+ return this . _dateAdapter . getYearName ( this . calendar . activeDate ) ;
92
+ }
93
+
94
+ // Format a label for the window of years displayed in the multi-year calendar view. Use
95
+ // `formatYearRangeLabel` because it is TTS friendly.
96
+ return this . _intl . formatYearRangeLabel ( ...this . _formatMinAndMaxYearLabels ( ) ) ;
105
97
}
106
98
99
+ /** The `aria-label` for changing the calendar view. */
107
100
get periodButtonLabel ( ) : string {
108
101
return this . calendar . currentView == 'month'
109
102
? this . _intl . switchToMultiYearViewLabel
@@ -192,6 +185,35 @@ export class MatCalendarHeader<D> {
192
185
this . calendar . maxDate ,
193
186
) ;
194
187
}
188
+
189
+ /**
190
+ * Format two individual labels for the minimum year and maximum year available in the multi-year
191
+ * calendar view. Returns an array of two strings where the first string is the formatted label
192
+ * for the minimum year, and the second string is the formatted label for the maximum year.
193
+ */
194
+ private _formatMinAndMaxYearLabels ( ) : [ minYearLabel : string , maxYearLabel : string ] {
195
+ // The offset from the active year to the "slot" for the starting year is the
196
+ // *actual* first rendered year in the multi-year view, and the last year is
197
+ // just yearsPerPage - 1 away.
198
+ const activeYear = this . _dateAdapter . getYear ( this . calendar . activeDate ) ;
199
+ const minYearOfPage =
200
+ activeYear -
201
+ getActiveOffset (
202
+ this . _dateAdapter ,
203
+ this . calendar . activeDate ,
204
+ this . calendar . minDate ,
205
+ this . calendar . maxDate ,
206
+ ) ;
207
+ const maxYearOfPage = minYearOfPage + yearsPerPage - 1 ;
208
+ const minYearLabel = this . _dateAdapter . getYearName (
209
+ this . _dateAdapter . createDate ( minYearOfPage , 0 , 1 ) ,
210
+ ) ;
211
+ const maxYearLabel = this . _dateAdapter . getYearName (
212
+ this . _dateAdapter . createDate ( maxYearOfPage , 0 , 1 ) ,
213
+ ) ;
214
+
215
+ return [ minYearLabel , maxYearLabel ] ;
216
+ }
195
217
}
196
218
197
219
/** A calendar that is used as part of the datepicker. */
0 commit comments