Skip to content

Commit f15a939

Browse files
authored
fix(material/datepicker): set aria-expanded on datepicker toggle (#30438)
Fixes that the datepicker toggle's button wasn't setting `aria-expanded` properly. Fixes #30406.
1 parent bf20426 commit f15a939

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/material/datepicker/datepicker-toggle.html

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[attr.aria-haspopup]="datepicker ? 'dialog' : null"
66
[attr.aria-label]="ariaLabel || _intl.openCalendarLabel"
77
[attr.tabindex]="disabled ? -1 : tabIndex"
8+
[attr.aria-expanded]="datepicker ? datepicker.opened : null"
89
[disabled]="disabled"
910
[disableRipple]="disableRipple">
1011

src/material/datepicker/datepicker.spec.ts

+42-18
Original file line numberDiff line numberDiff line change
@@ -272,27 +272,23 @@ describe('MatDatepicker', () => {
272272
expect(popup.getAttribute('role')).toBe('dialog');
273273
}));
274274

275-
it(
276-
'should set aria-labelledby to the one from the input, if not placed inside ' +
277-
'a mat-form-field',
278-
fakeAsync(() => {
279-
expect(fixture.nativeElement.querySelector('mat-form-field')).toBeFalsy();
275+
it('should set aria-labelledby to the one from the input, if not placed inside a mat-form-field', fakeAsync(() => {
276+
expect(fixture.nativeElement.querySelector('mat-form-field')).toBeFalsy();
280277

281-
const input: HTMLInputElement = fixture.nativeElement.querySelector('input');
282-
input.setAttribute('aria-labelledby', 'test-label');
278+
const input: HTMLInputElement = fixture.nativeElement.querySelector('input');
279+
input.setAttribute('aria-labelledby', 'test-label');
283280

284-
testComponent.datepicker.open();
285-
fixture.detectChanges();
286-
tick();
287-
flush();
281+
testComponent.datepicker.open();
282+
fixture.detectChanges();
283+
tick();
284+
flush();
288285

289-
const popup = document.querySelector(
290-
'.cdk-overlay-pane .mat-datepicker-content-container',
291-
)!;
292-
expect(popup).toBeTruthy();
293-
expect(popup.getAttribute('aria-labelledby')).toBe('test-label');
294-
}),
295-
);
286+
const popup = document.querySelector(
287+
'.cdk-overlay-pane .mat-datepicker-content-container',
288+
)!;
289+
expect(popup).toBeTruthy();
290+
expect(popup.getAttribute('aria-labelledby')).toBe('test-label');
291+
}));
296292

297293
it('close should close dialog', fakeAsync(() => {
298294
testComponent.touch = true;
@@ -1444,6 +1440,26 @@ describe('MatDatepicker', () => {
14441440

14451441
expect(toggle.classList).not.toContain('mat-datepicker-toggle-active');
14461442
}));
1443+
1444+
it('should set aria-expanded on the toggle', fakeAsync(() => {
1445+
const button = fixture.nativeElement.querySelector('mat-datepicker-toggle button');
1446+
1447+
expect(button.getAttribute('aria-expanded')).toBe('false');
1448+
1449+
fixture.componentInstance.datepicker.open();
1450+
fixture.detectChanges();
1451+
tick();
1452+
flush();
1453+
1454+
expect(button.getAttribute('aria-expanded')).toBe('true');
1455+
1456+
fixture.componentInstance.datepicker.close();
1457+
fixture.detectChanges();
1458+
flush();
1459+
fixture.detectChanges();
1460+
1461+
expect(button.getAttribute('aria-expanded')).toBe('false');
1462+
}));
14471463
});
14481464

14491465
describe('datepicker with custom mat-datepicker-toggle icon', () => {
@@ -2178,6 +2194,14 @@ describe('MatDatepicker', () => {
21782194
expect(toggle.hasAttribute('aria-haspopup')).toBe(false);
21792195
});
21802196

2197+
it('should not set aria-expanded if toggle does not have a datepicker', () => {
2198+
const fixture = createComponent(DatepickerToggleWithNoDatepicker, [MatNativeDateModule]);
2199+
fixture.detectChanges();
2200+
const toggle = fixture.nativeElement.querySelector('.mat-datepicker-toggle button');
2201+
2202+
expect(toggle.hasAttribute('aria-expanded')).toBe(false);
2203+
});
2204+
21812205
it('should not throw on init if input does not have a datepicker', () => {
21822206
expect(() => {
21832207
const fixture = createComponent(DatepickerInputWithNoDatepicker, [MatNativeDateModule]);

0 commit comments

Comments
 (0)