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