Skip to content

Commit 939c18d

Browse files
crisbetommalerba
authored andcommitted
fix(chips): don't set aria-required when element doesn't have… (#17425)
Currently we always set `aria-required` `mat-chip-list`/`mat-chip-grid`, however it should only be set on form controls. If a chip list is empty we remove its role so we also have to clear the `aria-required`. Fixes #17397.
1 parent db879d5 commit 939c18d

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ describe('MatChipGrid', () => {
9696

9797
expect(chips.toArray().every(chip => chip.disabled)).toBe(true);
9898
}));
99+
100+
it('should not set a role on the grid when the list is empty', () => {
101+
testComponent.chips = [];
102+
fixture.detectChanges();
103+
104+
expect(chipGridNativeElement.hasAttribute('role')).toBe(false);
105+
});
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+
99115
});
100116

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

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

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

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ describe('MatChipListbox', () => {
9393

9494
expect(chips.toArray().every(chip => chip.disabled)).toBe(true);
9595
}));
96+
97+
it('should not set a role on the grid when the list is empty', () => {
98+
testComponent.chips = [];
99+
fixture.detectChanges();
100+
101+
expect(chipListboxNativeElement.hasAttribute('role')).toBe(false);
102+
});
103+
104+
it('should not set aria-required when it does not have a role', () => {
105+
testComponent.chips = [];
106+
fixture.detectChanges();
107+
108+
expect(chipListboxNativeElement.hasAttribute('role')).toBe(false);
109+
expect(chipListboxNativeElement.hasAttribute('aria-required')).toBe(false);
110+
});
111+
96112
});
97113

98114
describe('with selected chips', () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export const MAT_CHIP_LISTBOX_CONTROL_VALUE_ACCESSOR: any = {
7070
'[tabIndex]': 'empty ? -1 : tabIndex',
7171
// TODO: replace this binding with use of AriaDescriber
7272
'[attr.aria-describedby]': '_ariaDescribedby || null',
73-
'[attr.aria-required]': 'required.toString()',
73+
'[attr.aria-required]': 'role ? required : null',
7474
'[attr.aria-disabled]': 'disabled.toString()',
7575
'[attr.aria-multiselectable]': 'multiple',
7676
'[attr.aria-orientation]': 'ariaOrientation',

src/material/chips/chip-list.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ describe('MatChipList', () => {
132132

133133
expect(chipListNativeElement.getAttribute('role')).toBeNull('Expect no role attribute');
134134
});
135+
136+
it('should not have aria-required when it has no role', () => {
137+
fixture.componentInstance.foods = [];
138+
fixture.detectChanges();
139+
140+
expect(chipListNativeElement.hasAttribute('role')).toBe(false);
141+
expect(chipListNativeElement.hasAttribute('aria-required')).toBe(false);
142+
});
135143
});
136144

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

src/material/chips/chip-list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class MatChipListChange {
8080
host: {
8181
'[attr.tabindex]': 'disabled ? null : _tabIndex',
8282
'[attr.aria-describedby]': '_ariaDescribedby || null',
83-
'[attr.aria-required]': 'required.toString()',
83+
'[attr.aria-required]': 'role ? required : null',
8484
'[attr.aria-disabled]': 'disabled.toString()',
8585
'[attr.aria-invalid]': 'errorState',
8686
'[attr.aria-multiselectable]': 'multiple',

0 commit comments

Comments
 (0)