Skip to content

Commit c7c3925

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 dc859fe commit c7c3925

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) 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
@@ -100,7 +100,7 @@ export class MatChipTrailingIcon {}
100100
host: {
101101
'class': 'mat-chip',
102102
'[attr.tabindex]': 'disabled ? null : -1',
103-
'role': 'option',
103+
'[attr.role]': 'role',
104104
'[class.mat-chip-selected]': 'selected',
105105
'[class.mat-chip-with-avatar]': 'avatar',
106106
'[class.mat-chip-with-trailing-icon]': 'trailingIcon || removeIcon',
@@ -154,6 +154,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
154154
/** The chip's remove toggler. */
155155
@ContentChild(forwardRef(() => MatChipRemove), {static: false}) removeIcon: MatChipRemove;
156156

157+
/** ARIA role that should be applied to the chip. */
158+
@Input() role: string = 'option';
159+
157160
/** Whether the chip is selected. */
158161
@Input()
159162
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
@@ -19,6 +19,7 @@ export declare class MatChip extends _MatChipMixinBase implements FocusableOptio
1919
readonly removed: EventEmitter<MatChipEvent>;
2020
rippleConfig: RippleConfig & RippleGlobalOptions;
2121
readonly rippleDisabled: boolean;
22+
role: string;
2223
selectable: boolean;
2324
selected: boolean;
2425
readonly selectionChange: EventEmitter<MatChipSelectionChange>;
@@ -108,7 +109,7 @@ export declare class MatChipList extends _MatChipListMixinBase implements MatFor
108109
ngControl: NgControl;
109110
placeholder: string;
110111
required: boolean;
111-
readonly role: string | null;
112+
role: string | null;
112113
selectable: boolean;
113114
readonly selected: MatChip[] | MatChip;
114115
readonly shouldLabelFloat: boolean;

0 commit comments

Comments
 (0)