Skip to content

Commit 982bc37

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 d9977bd commit 982bc37

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
@@ -131,6 +131,13 @@ describe('MatChipList', () => {
131131

132132
expect(chipListNativeElement.getAttribute('role')).toBeNull('Expect no role attribute');
133133
});
134+
135+
it('should be able to set a custom role', () => {
136+
fixture.componentInstance.chipList.role = 'grid';
137+
fixture.detectChanges();
138+
139+
expect(chipListNativeElement.getAttribute('role')).toBe('grid');
140+
});
134141
});
135142

136143
describe('focus behaviors', () => {
@@ -1516,9 +1523,9 @@ class FalsyValueChipList {
15161523
@Component({
15171524
template: `
15181525
<mat-chip-list>
1519-
<mat-chip *ngFor="let food of foods" [value]="food.value" [selected]="food.selected">
1520-
{{ food.viewValue }}
1521-
</mat-chip>
1526+
<mat-chip *ngFor="let food of foods" [value]="food.value" [selected]="food.selected">
1527+
{{ food.viewValue }}
1528+
</mat-chip>
15221529
</mat-chip-list>
15231530
`
15241531
})
@@ -1529,6 +1536,7 @@ class SelectedChipList {
15291536
{value: 2, viewValue: 'Pasta', selected: true},
15301537
];
15311538
@ViewChildren(MatChip) chips: QueryList<MatChip>;
1539+
@ViewChild(MatChipList, {static: false}) chipList: MatChipList;
15321540
}
15331541

15341542
@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('Chips', () => {
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
@@ -101,7 +101,7 @@ export class MatChipTrailingIcon {}
101101
host: {
102102
'class': 'mat-chip',
103103
'[attr.tabindex]': 'disabled ? null : -1',
104-
'role': 'option',
104+
'[attr.role]': 'role',
105105
'[class.mat-chip-selected]': 'selected',
106106
'[class.mat-chip-with-avatar]': 'avatar',
107107
'[class.mat-chip-with-trailing-icon]': 'trailingIcon || removeIcon',
@@ -159,6 +159,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
159159
/** The chip's remove toggler. */
160160
@ContentChild(forwardRef(() => MatChipRemove), {static: false}) removeIcon: MatChipRemove;
161161

162+
/** ARIA role that should be applied to the chip. */
163+
@Input() role: string = 'option';
164+
162165
/** Whether the chip is selected. */
163166
@Input()
164167
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)