Skip to content

Commit

Permalink
feat(material/form-field): make mat-errors more polite
Browse files Browse the repository at this point in the history
Make the "politeness" of mat-error "polite" by default (instead of "assertive") and also make this
configurable via an input binding.

Fixes #21781

update mdc code
  • Loading branch information
chrisbubernak committed Feb 12, 2021
1 parent 70ff528 commit 5ee4539
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/material-experimental/mdc-chips/chip-grid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,11 +905,11 @@ describe('MDC-based MatChipGrid', () => {
});
}));

it('should set the proper role on the error messages', () => {
it('should set the proper aria-live attribute on the error messages', () => {
errorTestComponent.formControl.markAsTouched();
fixture.detectChanges();

expect(containerEl.querySelector('mat-error')!.getAttribute('role')).toBe('alert');
expect(containerEl.querySelector('mat-error')!.getAttribute('aria-live')).toBe('polite');
});

it('sets the aria-describedby to reference errors when in error state', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/material-experimental/mdc-form-field/directives/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export const MAT_ERROR = new InjectionToken<MatError>('MatError');
selector: 'mat-error',
host: {
'class': 'mat-mdc-form-field-error mat-mdc-form-field-bottom-align',
'role': 'alert',
'[attr.aria-live]': 'ariaLive',
'[id]': 'id',
},
providers: [{provide: MAT_ERROR, useExisting: MatError}],
})
export class MatError {
@Input() id: string = `mat-mdc-error-${nextUniqueId++}`;

@Input('aria-live') ariaLive = 'polite';
}
4 changes: 2 additions & 2 deletions src/material-experimental/mdc-input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,11 +1037,11 @@ describe('MatMdcInput with forms', () => {
.toBe(1, 'Expected one hint to still be shown.');
}));

it('should set the proper role on the error messages', fakeAsync(() => {
it('should set the proper aria-live attribute on the error messages', fakeAsync(() => {
testComponent.formControl.markAsTouched();
fixture.detectChanges();

expect(containerEl.querySelector('mat-error')!.getAttribute('role')).toBe('alert');
expect(containerEl.querySelector('mat-error')!.getAttribute('aria-live')).toBe('polite');
}));

it('sets the aria-describedby to reference errors when in error state', fakeAsync(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/material/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1282,11 +1282,11 @@ describe('MatChipList', () => {
});
}));

it('should set the proper role on the error messages', () => {
it('should set the proper aria-live attribute on the error messages', () => {
errorTestComponent.formControl.markAsTouched();
fixture.detectChanges();

expect(containerEl.querySelector('mat-error')!.getAttribute('role')).toBe('alert');
expect(containerEl.querySelector('mat-error')!.getAttribute('aria-live')).toBe('polite');
});

it('sets the aria-describedby to reference errors when in error state', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/material/form-field/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export const MAT_ERROR = new InjectionToken<MatError>('MatError');
selector: 'mat-error',
host: {
'class': 'mat-error',
'role': 'alert',
'[attr.id]': 'id',
'[attr.aria-live]': 'ariaLive',
},
providers: [{provide: MAT_ERROR, useExisting: MatError}],
})
export class MatError {
@Input() id: string = `mat-error-${nextUniqueId++}`;

@Input('aria-live') ariaLive = 'polite';
}
4 changes: 2 additions & 2 deletions src/material/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1193,11 +1193,11 @@ describe('MatInput with forms', () => {
.toBe(1, 'Expected one hint to still be shown.');
}));

it('should set the proper role on the error messages', fakeAsync(() => {
it('should set the proper aria-live attribute on the error messages', fakeAsync(() => {
testComponent.formControl.markAsTouched();
fixture.detectChanges();

expect(containerEl.querySelector('mat-error')!.getAttribute('role')).toBe('alert');
expect(containerEl.querySelector('mat-error')!.getAttribute('aria-live')).toBe('polite');
}));

it('sets the aria-describedby to reference errors when in error state', fakeAsync(() => {
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/material/form-field.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export declare const MAT_PREFIX: InjectionToken<MatPrefix>;
export declare const MAT_SUFFIX: InjectionToken<MatSuffix>;

export declare class MatError {
ariaLive: string;
id: string;
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatError, "mat-error", never, { "id": "id"; }, {}, never>;
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatError, "mat-error", never, { "id": "id"; "ariaLive": "aria-live"; }, {}, never>;
static ɵfac: i0.ɵɵFactoryDef<MatError, never>;
}

Expand Down

0 comments on commit 5ee4539

Please sign in to comment.