Skip to content

Commit e7aeee4

Browse files
crisbetommalerba
authored andcommitted
fix(material-experimental/mdc-chips): chip set not picking up… (#17568)
Fixes the MDC-based chip set not picking up indirect descendant chips.
1 parent bc1c454 commit e7aeee4

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/material-experimental/mdc-chips/chip-set.spec.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import {Component, DebugElement, QueryList} from '@angular/core';
22
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
3+
import {CommonModule} from '@angular/common';
34
import {By} from '@angular/platform-browser';
45
import {MatChip, MatChipSet, MatChipsModule} from './index';
56

67

78
describe('MatChipSet', () => {
8-
let fixture: ComponentFixture<any>;
9-
let chipSetDebugElement: DebugElement;
10-
let chipSetNativeElement: HTMLElement;
11-
let chipSetInstance: MatChipSet;
12-
let chips: QueryList<MatChip>;
13-
149
beforeEach(async(() => {
1510
TestBed.configureTestingModule({
16-
imports: [MatChipsModule],
17-
declarations: [BasicChipSet],
11+
imports: [MatChipsModule, CommonModule],
12+
declarations: [BasicChipSet, IndirectDescendantsChipSet],
1813
});
1914

2015
TestBed.compileComponents();
2116
}));
2217

2318
describe('BasicChipSet', () => {
19+
let fixture: ComponentFixture<any>;
20+
let chipSetDebugElement: DebugElement;
21+
let chipSetNativeElement: HTMLElement;
22+
let chipSetInstance: MatChipSet;
23+
let chips: QueryList<MatChip>;
24+
2425
describe('basic behaviors', () => {
2526
beforeEach(() => {
2627
fixture = TestBed.createComponent(BasicChipSet);
@@ -71,6 +72,28 @@ describe('MatChipSet', () => {
7172
});
7273
});
7374
});
75+
76+
it('should sync the disabled state to indirect descendant chips', () => {
77+
const fixture = TestBed.createComponent(IndirectDescendantsChipSet);
78+
fixture.detectChanges();
79+
80+
const chipSetDebugElement = fixture.debugElement.query(By.directive(MatChipSet))!;
81+
const chipSetInstance = chipSetDebugElement.componentInstance;
82+
const chips: QueryList<MatChip> = chipSetInstance._chips;
83+
84+
expect(chips.toArray().every(chip => chip.disabled)).toBe(false);
85+
86+
chipSetInstance.disabled = true;
87+
fixture.detectChanges();
88+
89+
expect(chips.toArray().every(chip => chip.disabled)).toBe(true);
90+
91+
chipSetInstance.disabled = false;
92+
fixture.detectChanges();
93+
94+
expect(chips.toArray().every(chip => chip.disabled)).toBe(false);
95+
});
96+
7497
});
7598

7699
@Component({
@@ -86,3 +109,17 @@ class BasicChipSet {
86109
name: string = 'Test';
87110
chips = [0, 1, 2, 3, 4];
88111
}
112+
113+
@Component({
114+
template: `
115+
<mat-chip-set>
116+
<ng-container [ngSwitch]="true">
117+
<mat-chip *ngFor="let i of chips">
118+
{{name}} {{i + 1}}
119+
</mat-chip>
120+
</ng-container>
121+
</mat-chip-set>
122+
`
123+
})
124+
class IndirectDescendantsChipSet extends BasicChipSet {
125+
}

src/material-experimental/mdc-chips/chip-set.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ export class MatChipSet extends _MatChipSetMixinBase implements AfterContentInit
152152
}
153153

154154
/** The chips that are part of this chip set. */
155-
@ContentChildren(MatChip) _chips: QueryList<MatChip>;
155+
@ContentChildren(MatChip, {
156+
// We need to use `descendants: true`, because Ivy will no longer match
157+
// indirect descendants if it's left as false.
158+
descendants: true
159+
}) _chips: QueryList<MatChip>;
156160

157161
constructor(protected _elementRef: ElementRef,
158162
protected _changeDetectorRef: ChangeDetectorRef,

0 commit comments

Comments
 (0)