Skip to content

Commit 4c08a88

Browse files
authored
fix(material-experimental/mdc-chips): leading icon not hidden on init when preselected (#17997)
Fixes the leading icon of a preselected chip not being hidden on init. Fixes #17979.
1 parent 99b27e8 commit 4c08a88

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/material-experimental/mdc-chips/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ ng_test_library(
7373
"@npm//@angular/common",
7474
"@npm//@angular/forms",
7575
"@npm//@angular/platform-browser",
76+
"@npm//@material/chips",
7677
"@npm//material-components-web",
7778
"@npm//rxjs",
7879
],

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {Component, DebugElement, ViewChild} from '@angular/core';
55
import {async, ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
66
import {MAT_RIPPLE_GLOBAL_OPTIONS, RippleGlobalOptions} from '@angular/material/core';
77
import {By} from '@angular/platform-browser';
8+
import {chipCssClasses} from '@material/chips';
89
import {Subject} from 'rxjs';
910
import {
1011
MatChipEvent,
@@ -269,6 +270,22 @@ describe('MDC-based Option Chips', () => {
269270
expect(chipNativeElement.getAttribute('aria-disabled')).toBe('true');
270271
});
271272
});
273+
274+
it('should hide the leading icon when initialized as selected', () => {
275+
// We need to recreate the fixture before change detection has
276+
// run so we can capture the behavior we're testing for.
277+
fixture.destroy();
278+
fixture = TestBed.createComponent(SingleChip);
279+
testComponent = fixture.debugElement.componentInstance;
280+
testComponent.selected = true;
281+
fixture.detectChanges();
282+
chipDebugElement = fixture.debugElement.query(By.directive(MatChipOption))!;
283+
chipNativeElement = chipDebugElement.nativeElement;
284+
chipInstance = chipDebugElement.injector.get<MatChipOption>(MatChipOption);
285+
286+
const avatar = fixture.nativeElement.querySelector('.avatar');
287+
expect(avatar.classList).toContain(chipCssClasses.HIDDEN_LEADING_ICON);
288+
});
272289
});
273290
});
274291

@@ -280,6 +297,7 @@ describe('MDC-based Option Chips', () => {
280297
[color]="color" [selected]="selected" [disabled]="disabled"
281298
(focus)="chipFocus($event)" (destroyed)="chipDestroy($event)"
282299
(selectionChange)="chipSelectionChange($event)">
300+
<span class="avatar" matChipAvatar></span>
283301
{{name}}
284302
</mat-chip-option>
285303
</div>

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import {
1414
EventEmitter,
1515
Input,
1616
Output,
17-
ViewEncapsulation
17+
ViewEncapsulation,
18+
AfterContentInit
1819
} from '@angular/core';
20+
import {chipCssClasses} from '@material/chips';
1921
import {take} from 'rxjs/operators';
2022
import {MatChip} from './chip';
2123

@@ -61,7 +63,7 @@ export class MatChipSelectionChange {
6163
encapsulation: ViewEncapsulation.None,
6264
changeDetection: ChangeDetectionStrategy.OnPush,
6365
})
64-
export class MatChipOption extends MatChip {
66+
export class MatChipOption extends MatChip implements AfterContentInit {
6567

6668
/** Whether the chip list is selectable. */
6769
chipListSelectable: boolean = true;
@@ -116,6 +118,14 @@ export class MatChipOption extends MatChip {
116118
@Output() readonly selectionChange: EventEmitter<MatChipSelectionChange> =
117119
new EventEmitter<MatChipSelectionChange>();
118120

121+
ngAfterContentInit() {
122+
super.ngAfterContentInit();
123+
124+
if (this.selected && this.leadingIcon) {
125+
this.leadingIcon.setClass(chipCssClasses.HIDDEN_LEADING_ICON, true);
126+
}
127+
}
128+
119129
/** Selects the chip. */
120130
select(): void {
121131
if (!this.selectable) {

0 commit comments

Comments
 (0)