Skip to content

Commit 22d7f77

Browse files
vanessanschmittmmalerba
authored andcommitted
fix(mdc-chips): Set aria-required on input instead of grid (#18049)
The aria-required attribute is not allowed on elements with role=grid, which causes axe to report an error. If the grid is required, set aria-required on the matChipInputFor input element instead.
1 parent 55ee988 commit 22d7f77

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

src/dev-app/mdc-chips/mdc-chips-demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ <h4>Stacked</h4>
184184
You can also stack the chips if you want them on top of each other.
185185
</p>
186186

187-
<mat-chip-set class="mat-mdc-chip-set-stacked" aria-orientation="vertical">
187+
<mat-chip-set class="mat-mdc-chip-set-stacked">
188188
<mat-chip *ngFor="let aColor of availableColors" highlighted="true"
189189
[color]="aColor.color">
190190
{{aColor.name}}

src/material-experimental/mdc-chips/chip-grid.spec.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,6 @@ describe('MDC-based MatChipGrid', () => {
103103

104104
expect(chipGridNativeElement.hasAttribute('role')).toBe(false);
105105
});
106-
107-
it('should not set aria-required when it does not have a role', () => {
108-
testComponent.chips = [];
109-
fixture.detectChanges();
110-
111-
expect(chipGridNativeElement.hasAttribute('role')).toBe(false);
112-
expect(chipGridNativeElement.hasAttribute('aria-required')).toBe(false);
113-
});
114-
115106
});
116107

117108
describe('focus behaviors', () => {

src/material-experimental/mdc-chips/chip-grid.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ const _MatChipGridMixinBase: CanUpdateErrorStateCtor & typeof MatChipGridBase =
8787
'[tabIndex]': '_chips && _chips.length === 0 ? -1 : tabIndex',
8888
// TODO: replace this binding with use of AriaDescriber
8989
'[attr.aria-describedby]': '_ariaDescribedby || null',
90-
'[attr.aria-required]': 'role ? required : null',
9190
'[attr.aria-disabled]': 'disabled.toString()',
9291
'[attr.aria-invalid]': 'errorState',
9392
'[class.mat-mdc-chip-list-disabled]': 'disabled',

src/material-experimental/mdc-chips/chip-input.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('MDC-based MatChipInput', () => {
9595
expect(label.textContent).toContain('or don\'t');
9696
});
9797

98-
it('should become disabled if the chip list is disabled', () => {
98+
it('should become disabled if the chip grid is disabled', () => {
9999
expect(inputNativeElement.hasAttribute('disabled')).toBe(false);
100100
expect(chipInputDirective.disabled).toBe(false);
101101

@@ -106,6 +106,15 @@ describe('MDC-based MatChipInput', () => {
106106
expect(chipInputDirective.disabled).toBe(true);
107107
});
108108

109+
it('should be aria-required if the chip grid is required', () => {
110+
expect(inputNativeElement.hasAttribute('aria-required')).toBe(false);
111+
112+
fixture.componentInstance.required = true;
113+
fixture.detectChanges();
114+
115+
expect(inputNativeElement.getAttribute('aria-required')).toBe('true');
116+
});
117+
109118
it('should allow focus to escape when tabbing forwards', fakeAsync(() => {
110119
const gridElement: HTMLElement = fixture.nativeElement.querySelector('mat-chip-grid');
111120

@@ -249,7 +258,7 @@ describe('MDC-based MatChipInput', () => {
249258
@Component({
250259
template: `
251260
<mat-form-field>
252-
<mat-chip-grid #chipGrid>
261+
<mat-chip-grid #chipGrid [required]="required">
253262
<mat-chip-row>Hello</mat-chip-row>
254263
<input matInput [matChipInputFor]="chipGrid"
255264
[matChipInputAddOnBlur]="addOnBlur"
@@ -263,6 +272,7 @@ class TestChipInput {
263272
@ViewChild(MatChipGrid) chipGridInstance: MatChipGrid;
264273
addOnBlur: boolean = false;
265274
placeholder = '';
275+
required = false;
266276

267277
add(_: MatChipInputEvent) {
268278
}

src/material-experimental/mdc-chips/chip-input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ let nextUniqueId = 0;
4343
'[attr.disabled]': 'disabled || null',
4444
'[attr.placeholder]': 'placeholder || null',
4545
'[attr.aria-invalid]': '_chipGrid && _chipGrid.ngControl ? _chipGrid.ngControl.invalid : null',
46+
'[attr.aria-required]': '_chipGrid && _chipGrid.required || null',
4647
}
4748
})
4849
export class MatChipInput implements MatChipTextControl, OnChanges {

0 commit comments

Comments
 (0)