Skip to content

feat(material/datepicker): allow for datepicker toggle aria-label to be customized #21077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/material/datepicker/datepicker-toggle.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mat-icon-button
type="button"
[attr.aria-haspopup]="datepicker ? 'dialog' : null"
[attr.aria-label]="_intl.openCalendarLabel"
[attr.aria-label]="ariaLabel || _intl.openCalendarLabel"
[attr.tabindex]="disabled ? -1 : tabIndex"
[disabled]="disabled"
[disableRipple]="disableRipple"
Expand Down
3 changes: 3 additions & 0 deletions src/material/datepicker/datepicker-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export class MatDatepickerToggle<D> implements AfterContentInit, OnChanges, OnDe
/** Tabindex for the toggle. */
@Input() tabIndex: number | null;

/** Screenreader label for the button. */
@Input('aria-label') ariaLabel: string;

/** Whether the toggle button is disabled. */
@Input()
get disabled(): boolean {
Expand Down
19 changes: 18 additions & 1 deletion src/material/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,22 @@ describe('MatDatepicker', () => {
expect(button.nativeElement.getAttribute('aria-haspopup')).toBe('dialog');
});

it('should set a default `aria-label` on the toggle button', () => {
const button = fixture.debugElement.query(By.css('button'))!;

expect(button).toBeTruthy();
expect(button.nativeElement.getAttribute('aria-label')).toBe('Open calendar');
});

it('should be able to change the button `aria-label`', () => {
fixture.componentInstance.ariaLabel = 'Toggle the datepicker';
fixture.detectChanges();
const button = fixture.debugElement.query(By.css('button'))!;

expect(button).toBeTruthy();
expect(button.nativeElement.getAttribute('aria-label')).toBe('Toggle the datepicker');
});

it('should open calendar when toggle clicked', () => {
expect(document.querySelector('mat-dialog-container')).toBeNull();

Expand Down Expand Up @@ -2262,14 +2278,15 @@ class DatepickerWithFormControl {
@Component({
template: `
<input [matDatepicker]="d">
<mat-datepicker-toggle [for]="d"></mat-datepicker-toggle>
<mat-datepicker-toggle [for]="d" [aria-label]="ariaLabel"></mat-datepicker-toggle>
<mat-datepicker #d [touchUi]="touchUI"></mat-datepicker>
`,
})
class DatepickerWithToggle {
@ViewChild('d') datepicker: MatDatepicker<Date>;
@ViewChild(MatDatepickerInput) input: MatDatepickerInput<Date>;
touchUI = true;
ariaLabel: string;
}


Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/material/datepicker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export declare class MatDatepickerToggle<D> implements AfterContentInit, OnChang
_button: MatButton;
_customIcon: MatDatepickerToggleIcon;
_intl: MatDatepickerIntl;
ariaLabel: string;
datepicker: MatDatepickerPanel<MatDatepickerControl<any>, D>;
disableRipple: boolean;
get disabled(): boolean;
Expand All @@ -283,7 +284,7 @@ export declare class MatDatepickerToggle<D> implements AfterContentInit, OnChang
ngOnChanges(changes: SimpleChanges): void;
ngOnDestroy(): void;
static ngAcceptInputType_disabled: BooleanInput;
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatDatepickerToggle<any>, "mat-datepicker-toggle", ["matDatepickerToggle"], { "datepicker": "for"; "tabIndex": "tabIndex"; "disabled": "disabled"; "disableRipple": "disableRipple"; }, {}, ["_customIcon"], ["[matDatepickerToggleIcon]"]>;
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatDatepickerToggle<any>, "mat-datepicker-toggle", ["matDatepickerToggle"], { "datepicker": "for"; "tabIndex": "tabIndex"; "ariaLabel": "aria-label"; "disabled": "disabled"; "disableRipple": "disableRipple"; }, {}, ["_customIcon"], ["[matDatepickerToggleIcon]"]>;
static ɵfac: i0.ɵɵFactoryDef<MatDatepickerToggle<any>, [null, null, { attribute: "tabindex"; }]>;
}

Expand Down