Skip to content

Commit 86ffda4

Browse files
committed
fix(chips): allow for role to be overwritten on chip list and chip
Allows for the ARIA `role` of the `mat-chip-list` and `mat-chip` to be overwritten. Fixes #15787.
1 parent 3052304 commit 86ffda4

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ describe('MatChipList', () => {
140140
expect(chipListNativeElement.hasAttribute('role')).toBe(false);
141141
expect(chipListNativeElement.hasAttribute('aria-required')).toBe(false);
142142
});
143+
144+
it('should be able to set a custom role', () => {
145+
fixture.componentInstance.chipList.role = 'grid';
146+
fixture.detectChanges();
147+
148+
expect(chipListNativeElement.getAttribute('role')).toBe('grid');
149+
});
143150
});
144151

145152
describe('focus behaviors', () => {
@@ -1536,9 +1543,9 @@ class FalsyValueChipList {
15361543
@Component({
15371544
template: `
15381545
<mat-chip-list>
1539-
<mat-chip *ngFor="let food of foods" [value]="food.value" [selected]="food.selected">
1540-
{{ food.viewValue }}
1541-
</mat-chip>
1546+
<mat-chip *ngFor="let food of foods" [value]="food.value" [selected]="food.selected">
1547+
{{ food.viewValue }}
1548+
</mat-chip>
15421549
</mat-chip-list>
15431550
`
15441551
})
@@ -1549,6 +1556,7 @@ class SelectedChipList {
15491556
{value: 2, viewValue: 'Pasta', selected: true},
15501557
];
15511558
@ViewChildren(MatChip) chips: QueryList<MatChip>;
1559+
@ViewChild(MatChipList, {static: false}) chipList: MatChipList;
15521560
}
15531561

15541562
@Component({

src/material/chips/chip-list.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,18 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
165165
}
166166

167167
/** The ARIA role applied to the chip list. */
168-
get role(): string | null { return this.empty ? null : 'listbox'; }
168+
@Input()
169+
get role(): string | null {
170+
if (this._explicitRole) {
171+
return this._explicitRole;
172+
}
173+
174+
return this.empty ? null : 'listbox';
175+
}
176+
set role(role: string | null) {
177+
this._explicitRole = role;
178+
}
179+
private _explicitRole?: string | null;
169180

170181
/** An object used to control when error messages are shown. */
171182
@Input() errorStateMatcher: ErrorStateMatcher;

src/material/chips/chip.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ describe('MatChip', () => {
5656
expect(chipNativeElement.classList).toContain('mat-chip');
5757
expect(chipNativeElement.classList).toContain('mat-basic-chip');
5858
});
59+
60+
it('should have the correct role', () => {
61+
expect(chipNativeElement.getAttribute('role')).toBe('option');
62+
});
63+
64+
it('should be able to set a custom role', () => {
65+
chipInstance.role = 'gridcell';
66+
fixture.detectChanges();
67+
68+
expect(chipNativeElement.getAttribute('role')).toBe('gridcell');
69+
});
5970
});
6071

6172
describe('MatChip', () => {

src/material/chips/chip.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class MatChipTrailingIcon {}
102102
host: {
103103
'class': 'mat-chip',
104104
'[attr.tabindex]': 'disabled ? null : -1',
105-
'role': 'option',
105+
'[attr.role]': 'role',
106106
'[class.mat-chip-selected]': 'selected',
107107
'[class.mat-chip-with-avatar]': 'avatar',
108108
'[class.mat-chip-with-trailing-icon]': 'trailingIcon || removeIcon',
@@ -160,6 +160,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
160160
/** The chip's remove toggler. */
161161
@ContentChild(forwardRef(() => MatChipRemove), {static: false}) removeIcon: MatChipRemove;
162162

163+
/** ARIA role that should be applied to the chip. */
164+
@Input() role: string = 'option';
165+
163166
/** Whether the chip is selected. */
164167
@Input()
165168
get selected(): boolean { return this._selected; }

tools/public_api_guard/material/chips.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export declare class MatChip extends _MatChipMixinBase implements FocusableOptio
2020
readonly removed: EventEmitter<MatChipEvent>;
2121
rippleConfig: RippleConfig & RippleGlobalOptions;
2222
readonly rippleDisabled: boolean;
23+
role: string;
2324
selectable: boolean;
2425
selected: boolean;
2526
readonly selectionChange: EventEmitter<MatChipSelectionChange>;
@@ -109,7 +110,7 @@ export declare class MatChipList extends _MatChipListMixinBase implements MatFor
109110
ngControl: NgControl;
110111
placeholder: string;
111112
required: boolean;
112-
readonly role: string | null;
113+
role: string | null;
113114
selectable: boolean;
114115
readonly selected: MatChip[] | MatChip;
115116
readonly shouldLabelFloat: boolean;

0 commit comments

Comments
 (0)