Skip to content

Commit 949a69b

Browse files
tinayuangaojelbourn
authored andcommitted
fix(chips): Fix chip and chip list selectable (#9955)
1 parent c819267 commit 949a69b

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ describe('MatChipList', () => {
6363
it('should add the `mat-chip-list` class', () => {
6464
expect(chipListNativeElement.classList).toContain('mat-chip-list');
6565
});
66+
67+
it('should not have the aria-selected attribute when is not selectable', () => {
68+
testComponent.selectable = false;
69+
fixture.detectChanges();
70+
71+
const chipsValid = chips.toArray().every(chip =>
72+
!chip.selectable && !chip._elementRef.nativeElement.hasAttribute('aria-selected'));
73+
74+
expect(chipsValid).toBe(true);
75+
});
6676
});
6777

6878
describe('with selected chips', () => {
@@ -1027,7 +1037,7 @@ describe('MatChipList', () => {
10271037

10281038
@Component({
10291039
template: `
1030-
<mat-chip-list [tabIndex]="tabIndex">
1040+
<mat-chip-list [tabIndex]="tabIndex" [selectable]="selectable">
10311041
<div *ngFor="let i of [0,1,2,3,4]">
10321042
<div *ngIf="remove != i">
10331043
<mat-chip (select)="chipSelect(i)" (deselect)="chipDeselect(i)">

src/lib/chips/chip-list.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,17 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
274274
@Input('aria-orientation') ariaOrientation: 'horizontal' | 'vertical' = 'horizontal';
275275

276276
/**
277-
* Whether or not this chip is selectable. When a chip is not selectable,
278-
* its selected state is always ignored.
277+
* Whether or not this chip list is selectable. When a chip list is not selectable,
278+
* the selected states for all the chips inside the chip list are always ignored.
279279
*/
280280
@Input()
281281
get selectable(): boolean { return this._selectable; }
282-
set selectable(value: boolean) { this._selectable = coerceBooleanProperty(value); }
282+
set selectable(value: boolean) {
283+
this._selectable = coerceBooleanProperty(value);
284+
if (this.chips) {
285+
this.chips.forEach(chip => chip.chipListSelectable = this._selectable);
286+
}
287+
}
283288
protected _selectable: boolean = true;
284289

285290
@Input()

src/lib/chips/chip.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
134134
/** Whether the chip has focus. */
135135
_hasFocus: boolean = false;
136136

137+
/** Whether the chip list is selectable */
138+
chipListSelectable: boolean = true;
139+
137140
/** The chip avatar */
138141
@ContentChild(MatChipAvatar) avatar: MatChipAvatar;
139142

@@ -167,11 +170,13 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
167170
protected _value: any;
168171

169172
/**
170-
* Whether or not the chips are selectable. When a chip is not selectable,
171-
* changes to it's selected state are always ignored.
173+
* Whether or not the chip is selectable. When a chip is not selectable,
174+
* changes to it's selected state are always ignored. By default a chip is
175+
* selectable, and it becomes non-selectable if it's parent chip list is
176+
* not selectable.
172177
*/
173178
@Input()
174-
get selectable(): boolean { return this._selectable; }
179+
get selectable(): boolean { return this._selectable && this.chipListSelectable; }
175180
set selectable(value: boolean) {
176181
this._selectable = coerceBooleanProperty(value);
177182
}

0 commit comments

Comments
 (0)