Skip to content

Commit 4874320

Browse files
authored
feat(material/datepicker): allow for datepicker toggle aria-label to be customized (#21077)
Currently the only way to control the accessible label of a `mat-datepicker-toggle` is globally through the i18n provider. These changes add an input so it can be changed per instance. Fixes #20590.
1 parent 5ab55fc commit 4874320

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

src/material/datepicker/datepicker-toggle.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mat-icon-button
44
type="button"
55
[attr.aria-haspopup]="datepicker ? 'dialog' : null"
6-
[attr.aria-label]="_intl.openCalendarLabel"
6+
[attr.aria-label]="ariaLabel || _intl.openCalendarLabel"
77
[attr.tabindex]="disabled ? -1 : tabIndex"
88
[disabled]="disabled"
99
[disableRipple]="disableRipple"

src/material/datepicker/datepicker-toggle.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ export class MatDatepickerToggle<D> implements AfterContentInit, OnChanges, OnDe
6464
/** Tabindex for the toggle. */
6565
@Input() tabIndex: number | null;
6666

67+
/** Screenreader label for the button. */
68+
@Input('aria-label') ariaLabel: string;
69+
6770
/** Whether the toggle button is disabled. */
6871
@Input()
6972
get disabled(): boolean {

src/material/datepicker/datepicker.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,22 @@ describe('MatDatepicker', () => {
10291029
expect(button.nativeElement.getAttribute('aria-haspopup')).toBe('dialog');
10301030
});
10311031

1032+
it('should set a default `aria-label` on the toggle button', () => {
1033+
const button = fixture.debugElement.query(By.css('button'))!;
1034+
1035+
expect(button).toBeTruthy();
1036+
expect(button.nativeElement.getAttribute('aria-label')).toBe('Open calendar');
1037+
});
1038+
1039+
it('should be able to change the button `aria-label`', () => {
1040+
fixture.componentInstance.ariaLabel = 'Toggle the datepicker';
1041+
fixture.detectChanges();
1042+
const button = fixture.debugElement.query(By.css('button'))!;
1043+
1044+
expect(button).toBeTruthy();
1045+
expect(button.nativeElement.getAttribute('aria-label')).toBe('Toggle the datepicker');
1046+
});
1047+
10321048
it('should open calendar when toggle clicked', () => {
10331049
expect(document.querySelector('mat-dialog-container')).toBeNull();
10341050

@@ -2262,14 +2278,15 @@ class DatepickerWithFormControl {
22622278
@Component({
22632279
template: `
22642280
<input [matDatepicker]="d">
2265-
<mat-datepicker-toggle [for]="d"></mat-datepicker-toggle>
2281+
<mat-datepicker-toggle [for]="d" [aria-label]="ariaLabel"></mat-datepicker-toggle>
22662282
<mat-datepicker #d [touchUi]="touchUI"></mat-datepicker>
22672283
`,
22682284
})
22692285
class DatepickerWithToggle {
22702286
@ViewChild('d') datepicker: MatDatepicker<Date>;
22712287
@ViewChild(MatDatepickerInput) input: MatDatepickerInput<Date>;
22722288
touchUI = true;
2289+
ariaLabel: string;
22732290
}
22742291

22752292

tools/public_api_guard/material/datepicker.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ export declare class MatDatepickerToggle<D> implements AfterContentInit, OnChang
272272
_button: MatButton;
273273
_customIcon: MatDatepickerToggleIcon;
274274
_intl: MatDatepickerIntl;
275+
ariaLabel: string;
275276
datepicker: MatDatepickerPanel<MatDatepickerControl<any>, D>;
276277
disableRipple: boolean;
277278
get disabled(): boolean;
@@ -283,7 +284,7 @@ export declare class MatDatepickerToggle<D> implements AfterContentInit, OnChang
283284
ngOnChanges(changes: SimpleChanges): void;
284285
ngOnDestroy(): void;
285286
static ngAcceptInputType_disabled: BooleanInput;
286-
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatDatepickerToggle<any>, "mat-datepicker-toggle", ["matDatepickerToggle"], { "datepicker": "for"; "tabIndex": "tabIndex"; "disabled": "disabled"; "disableRipple": "disableRipple"; }, {}, ["_customIcon"], ["[matDatepickerToggleIcon]"]>;
287+
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatDatepickerToggle<any>, "mat-datepicker-toggle", ["matDatepickerToggle"], { "datepicker": "for"; "tabIndex": "tabIndex"; "ariaLabel": "aria-label"; "disabled": "disabled"; "disableRipple": "disableRipple"; }, {}, ["_customIcon"], ["[matDatepickerToggleIcon]"]>;
287288
static ɵfac: i0.ɵɵFactoryDef<MatDatepickerToggle<any>, [null, null, { attribute: "tabindex"; }]>;
288289
}
289290

0 commit comments

Comments
 (0)