Skip to content

Commit

Permalink
fix(material/chips): styling not updating when actions are removed (#…
Browse files Browse the repository at this point in the history
…26696)

Some of the chip styling depends on the presence of chips actions and it wasn't being updated when they were toggled.

Fixes #26691.

(cherry picked from commit 6c1da5e)
  • Loading branch information
crisbeto committed Feb 27, 2023
1 parent 4338e5e commit 5e67c1a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
40 changes: 38 additions & 2 deletions src/material/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
import {
AfterViewInit,
AfterContentInit,
Component,
ChangeDetectionStrategy,
ChangeDetectorRef,
Expand All @@ -25,6 +26,8 @@ import {
ViewEncapsulation,
ViewChild,
Attribute,
ContentChildren,
QueryList,
} from '@angular/core';
import {DOCUMENT} from '@angular/common';
import {
Expand All @@ -41,7 +44,7 @@ import {
RippleGlobalOptions,
} from '@angular/material/core';
import {FocusMonitor} from '@angular/cdk/a11y';
import {Subject} from 'rxjs';
import {merge, Subject, Subscription} from 'rxjs';
import {take} from 'rxjs/operators';
import {MatChipAvatar, MatChipTrailingIcon, MatChipRemove} from './chip-icons';
import {MatChipAction} from './chip-action';
Expand Down Expand Up @@ -112,7 +115,14 @@ const _MatChipMixinBase = mixinTabIndex(
})
export class MatChip
extends _MatChipMixinBase
implements AfterViewInit, CanColor, CanDisableRipple, CanDisable, HasTabIndex, OnDestroy
implements
AfterViewInit,
AfterContentInit,
CanColor,
CanDisableRipple,
CanDisable,
HasTabIndex,
OnDestroy
{
protected _document: Document;

Expand All @@ -137,9 +147,24 @@ export class MatChip
/** Whether moving focus into the chip is pending. */
private _pendingFocus: boolean;

/** Subscription to changes in the chip's actions. */
private _actionChanges: Subscription | undefined;

/** Whether animations for the chip are enabled. */
_animationsDisabled: boolean;

/** All avatars present in the chip. */
@ContentChildren(MAT_CHIP_AVATAR, {descendants: true})
protected _allLeadingIcons: QueryList<MatChipAvatar>;

/** All trailing icons present in the chip. */
@ContentChildren(MAT_CHIP_TRAILING_ICON, {descendants: true})
protected _allTrailingIcons: QueryList<MatChipTrailingIcon>;

/** All remove icons present in the chip. */
@ContentChildren(MAT_CHIP_REMOVE, {descendants: true})
protected _allRemoveIcons: QueryList<MatChipRemove>;

_hasFocus() {
return this._hasFocusInternal;
}
Expand Down Expand Up @@ -259,8 +284,19 @@ export class MatChip
}
}

ngAfterContentInit(): void {
// Since the styling depends on the presence of some
// actions, we have to mark for check on changes.
this._actionChanges = merge(
this._allLeadingIcons.changes,
this._allTrailingIcons.changes,
this._allRemoveIcons.changes,
).subscribe(() => this._changeDetectorRef.markForCheck());
}

ngOnDestroy() {
this._focusMonitor.stopMonitoring(this._elementRef);
this._actionChanges?.unsubscribe();
this.destroyed.emit({chip: this});
this.destroyed.complete();
}
Expand Down
9 changes: 7 additions & 2 deletions tools/public_api_guard/material/chips.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ export const MAT_CHIP_TRAILING_ICON: InjectionToken<unknown>;
export const MAT_CHIPS_DEFAULT_OPTIONS: InjectionToken<MatChipsDefaultOptions>;

// @public
export class MatChip extends _MatChipMixinBase implements AfterViewInit, CanColor, CanDisableRipple, CanDisable, HasTabIndex, OnDestroy {
export class MatChip extends _MatChipMixinBase implements AfterViewInit, AfterContentInit, CanColor, CanDisableRipple, CanDisable, HasTabIndex, OnDestroy {
constructor(_changeDetectorRef: ChangeDetectorRef, elementRef: ElementRef<HTMLElement>, _ngZone: NgZone, _focusMonitor: FocusMonitor, _document: any, animationMode?: string, _globalRippleOptions?: RippleGlobalOptions | undefined, tabIndex?: string);
protected _allLeadingIcons: QueryList<MatChipAvatar>;
protected _allRemoveIcons: QueryList<MatChipRemove>;
protected _allTrailingIcons: QueryList<MatChipTrailingIcon>;
_animationsDisabled: boolean;
ariaDescription: string | null;
_ariaDescriptionId: string;
Expand Down Expand Up @@ -91,6 +94,8 @@ export class MatChip extends _MatChipMixinBase implements AfterViewInit, CanColo
_isRippleDisabled(): boolean;
leadingIcon: MatChipAvatar;
// (undocumented)
ngAfterContentInit(): void;
// (undocumented)
ngAfterViewInit(): void;
// (undocumented)
ngOnDestroy(): void;
Expand All @@ -114,7 +119,7 @@ export class MatChip extends _MatChipMixinBase implements AfterViewInit, CanColo
// (undocumented)
protected _value: any;
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<MatChip, "mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]", ["matChip"], { "color": "color"; "disabled": "disabled"; "disableRipple": "disableRipple"; "tabIndex": "tabIndex"; "role": "role"; "id": "id"; "ariaLabel": "aria-label"; "ariaDescription": "aria-description"; "value": "value"; "removable": "removable"; "highlighted": "highlighted"; }, { "removed": "removed"; "destroyed": "destroyed"; }, ["leadingIcon", "trailingIcon", "removeIcon"], ["mat-chip-avatar, [matChipAvatar]", "*", "mat-chip-trailing-icon,[matChipRemove],[matChipTrailingIcon]"], false, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatChip, "mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]", ["matChip"], { "color": "color"; "disabled": "disabled"; "disableRipple": "disableRipple"; "tabIndex": "tabIndex"; "role": "role"; "id": "id"; "ariaLabel": "aria-label"; "ariaDescription": "aria-description"; "value": "value"; "removable": "removable"; "highlighted": "highlighted"; }, { "removed": "removed"; "destroyed": "destroyed"; }, ["leadingIcon", "trailingIcon", "removeIcon", "_allLeadingIcons", "_allTrailingIcons", "_allRemoveIcons"], ["mat-chip-avatar, [matChipAvatar]", "*", "mat-chip-trailing-icon,[matChipRemove],[matChipTrailingIcon]"], false, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatChip, [null, null, null, null, null, { optional: true; }, { optional: true; }, { attribute: "tabindex"; }]>;
}
Expand Down

0 comments on commit 5e67c1a

Please sign in to comment.