Skip to content

Commit 13363e6

Browse files
crisbetommalerba
authored andcommitted
fix(datepicker): year not formatted in multi-year view button (#17202)
Fixes the years that are rendered in the calendar's perioud button not being formatted through the date adapter in the multi-view. This can result in an incorrect value being displayed in some locales. Fixes #17187. (cherry picked from commit ae28fce)
1 parent 9894ab2 commit 13363e6

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

src/material/datepicker/calendar-header.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Directionality} from '@angular/cdk/bidi';
22
import {Component} from '@angular/core';
33
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
4-
import {MatNativeDateModule} from '@angular/material/core';
4+
import {MatNativeDateModule, DateAdapter} from '@angular/material/core';
55
import {DEC, FEB, JAN} from '@angular/material/testing';
66
import {By} from '@angular/platform-browser';
77
import {MatCalendar} from './calendar';
@@ -149,6 +149,18 @@ describe('MatCalendarHeader', () => {
149149
expect(calendarInstance.activeDate).toEqual(new Date(2016, DEC, 31));
150150
expect(testComponent.selected).toBeFalsy('no date should be selected yet');
151151
});
152+
153+
it('should format the year in the period button using the date adapter', () => {
154+
const adapter = fixture.debugElement.injector.get(DateAdapter);
155+
156+
spyOn(adapter, 'getYearName').and.returnValue('FAKE_YEAR');
157+
158+
periodButton.click();
159+
fixture.detectChanges();
160+
161+
expect(calendarInstance.currentView).toBe('multi-year');
162+
expect(periodButton.textContent).toContain('FAKE_YEAR');
163+
});
152164
});
153165

154166
describe('calendar with minDate only', () => {

src/material/datepicker/calendar.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ export class MatCalendarHeader<D> {
8282
const minYearOfPage = activeYear - getActiveOffset(
8383
this._dateAdapter, this.calendar.activeDate, this.calendar.minDate, this.calendar.maxDate);
8484
const maxYearOfPage = minYearOfPage + yearsPerPage - 1;
85-
return `${minYearOfPage} \u2013 ${maxYearOfPage}`;
85+
const minYearName =
86+
this._dateAdapter.getYearName(this._dateAdapter.createDate(minYearOfPage, 0, 1));
87+
const maxYearName =
88+
this._dateAdapter.getYearName(this._dateAdapter.createDate(maxYearOfPage, 0, 1));
89+
return this._intl.formatYearRange(minYearName, maxYearName);
8690
}
8791

8892
get periodButtonLabel(): string {

src/material/datepicker/datepicker-intl.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ export class MatDatepickerIntl {
4848

4949
/** A label for the 'switch to year view' button (used by screen readers). */
5050
switchToMultiYearViewLabel: string = 'Choose month and year';
51+
52+
/** Formats a range of years. */
53+
formatYearRange(start: string, end: string): string {
54+
return `${start} \u2013 ${end}`;
55+
}
5156
}

tools/public_api_guard/material/datepicker.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ export declare class MatDatepickerIntl {
199199
prevYearLabel: string;
200200
switchToMonthViewLabel: string;
201201
switchToMultiYearViewLabel: string;
202+
formatYearRange(start: string, end: string): string;
202203
}
203204

204205
export declare class MatDatepickerModule {

0 commit comments

Comments
 (0)