Skip to content

Commit 744d033

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 68a2f89 commit 744d033

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
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
@@ -164,7 +164,18 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
164164
}
165165

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

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

src/material/chips/chip.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ describe('MatChip', () => {
6969

7070
expect(chip.getAttribute('tabindex')).toBe('15');
7171
});
72+
73+
it('should have the correct role', () => {
74+
expect(chipNativeElement.getAttribute('role')).toBe('option');
75+
});
76+
77+
it('should be able to set a custom role', () => {
78+
chipInstance.role = 'gridcell';
79+
fixture.detectChanges();
80+
81+
expect(chipNativeElement.getAttribute('role')).toBe('gridcell');
82+
});
7283
});
7384

7485

src/material/chips/chip.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class MatChipTrailingIcon {}
107107
host: {
108108
'class': 'mat-chip',
109109
'[attr.tabindex]': 'disabled ? null : tabIndex',
110-
'role': 'option',
110+
'[attr.role]': 'role',
111111
'[class.mat-chip-selected]': 'selected',
112112
'[class.mat-chip-with-avatar]': 'avatar',
113113
'[class.mat-chip-with-trailing-icon]': 'trailingIcon || removeIcon',
@@ -165,6 +165,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
165165
/** The chip's remove toggler. */
166166
@ContentChild(forwardRef(() => MatChipRemove)) removeIcon: MatChipRemove;
167167

168+
/** ARIA role that should be applied to the chip. */
169+
@Input() role: string = 'option';
170+
168171
/** Whether the chip is selected. */
169172
@Input()
170173
get selected(): boolean { return this._selected; }

tools/public_api_guard/material/chips.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export declare class MatChip extends _MatChipMixinBase implements FocusableOptio
2121
readonly removed: EventEmitter<MatChipEvent>;
2222
rippleConfig: RippleConfig & RippleGlobalOptions;
2323
get rippleDisabled(): boolean;
24+
role: string;
2425
get selectable(): boolean;
2526
set selectable(value: boolean);
2627
get selected(): boolean;
@@ -46,7 +47,7 @@ export declare class MatChip extends _MatChipMixinBase implements FocusableOptio
4647
static ngAcceptInputType_removable: BooleanInput;
4748
static ngAcceptInputType_selectable: BooleanInput;
4849
static ngAcceptInputType_selected: BooleanInput;
49-
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatChip, "mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]", ["matChip"], { "color": "color"; "disabled": "disabled"; "disableRipple": "disableRipple"; "tabIndex": "tabIndex"; "selected": "selected"; "value": "value"; "selectable": "selectable"; "removable": "removable"; }, { "selectionChange": "selectionChange"; "destroyed": "destroyed"; "removed": "removed"; }, ["avatar", "trailingIcon", "removeIcon"]>;
50+
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatChip, "mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]", ["matChip"], { "color": "color"; "disabled": "disabled"; "disableRipple": "disableRipple"; "tabIndex": "tabIndex"; "role": "role"; "selected": "selected"; "value": "value"; "selectable": "selectable"; "removable": "removable"; }, { "selectionChange": "selectionChange"; "destroyed": "destroyed"; "removed": "removed"; }, ["avatar", "trailingIcon", "removeIcon"]>;
5051
static ɵfac: i0.ɵɵFactoryDef<MatChip>;
5152
}
5253

@@ -134,6 +135,7 @@ export declare class MatChipList extends _MatChipListMixinBase implements MatFor
134135
get required(): boolean;
135136
set required(value: boolean);
136137
get role(): string | null;
138+
set role(role: string | null);
137139
get selectable(): boolean;
138140
set selectable(value: boolean);
139141
get selected(): MatChip[] | MatChip;
@@ -168,7 +170,7 @@ export declare class MatChipList extends _MatChipListMixinBase implements MatFor
168170
static ngAcceptInputType_multiple: BooleanInput;
169171
static ngAcceptInputType_required: BooleanInput;
170172
static ngAcceptInputType_selectable: BooleanInput;
171-
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatChipList, "mat-chip-list", ["matChipList"], { "errorStateMatcher": "errorStateMatcher"; "multiple": "multiple"; "compareWith": "compareWith"; "value": "value"; "required": "required"; "placeholder": "placeholder"; "disabled": "disabled"; "ariaOrientation": "aria-orientation"; "selectable": "selectable"; "tabIndex": "tabIndex"; }, { "change": "change"; "valueChange": "valueChange"; }, ["chips"]>;
173+
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatChipList, "mat-chip-list", ["matChipList"], { "role": "role"; "errorStateMatcher": "errorStateMatcher"; "multiple": "multiple"; "compareWith": "compareWith"; "value": "value"; "required": "required"; "placeholder": "placeholder"; "disabled": "disabled"; "ariaOrientation": "aria-orientation"; "selectable": "selectable"; "tabIndex": "tabIndex"; }, { "change": "change"; "valueChange": "valueChange"; }, ["chips"]>;
172174
static ɵfac: i0.ɵɵFactoryDef<MatChipList>;
173175
}
174176

0 commit comments

Comments
 (0)