Skip to content

fix(chips): preselected chip not highlighted on init inside OnPush component #16868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/material/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
Type,
ViewChild,
ViewChildren,
ChangeDetectionStrategy,
} from '@angular/core';
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import {FormControl, FormsModule, NgForm, ReactiveFormsModule, Validators} from '@angular/forms';
Expand Down Expand Up @@ -1269,6 +1270,16 @@ describe('MatChipList', () => {
});
});

it('should preselected chip as selected inside an OnPush component', fakeAsync(() => {
fixture = createComponent(PreselectedChipInsideOnPush);
fixture.detectChanges();
tick();
fixture.detectChanges();

expect(fixture.nativeElement.querySelector('.mat-chip').classList)
.toContain('mat-chip-selected', 'Expected first chip to be selected.');
}));

function createComponent<T>(component: Type<T>, providers: Provider[] = [], animationsModule:
Type<NoopAnimationsModule> | Type<BrowserAnimationsModule> = NoopAnimationsModule):
ComponentFixture<T> {
Expand Down Expand Up @@ -1606,3 +1617,19 @@ class ChipListWithRemove {
this.chips.splice(event.chip.value, 1);
}
}


@Component({
template: `
<mat-form-field>
<mat-chip-list [formControl]="control">
<mat-chip>Pizza</mat-chip>
<mat-chip>Pasta</mat-chip>
</mat-chip-list>
</mat-form-field>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
class PreselectedChipInsideOnPush {
control = new FormControl('Pizza');
}
16 changes: 15 additions & 1 deletion src/material/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
OnDestroy,
Optional,
Output,
ChangeDetectorRef,
} from '@angular/core';
import {
CanColor,
Expand Down Expand Up @@ -235,7 +236,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS)
globalRippleOptions: RippleGlobalOptions | null,
// @breaking-change 8.0.0 `animationMode` parameter to become required.
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
// @breaking-change 9.0.0 `_changeDetectorRef` parameter to become required.
private _changeDetectorRef?: ChangeDetectorRef) {
super(_elementRef);

this._addHostClassName();
Expand Down Expand Up @@ -269,6 +272,7 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
if (!this._selected) {
this._selected = true;
this._dispatchSelectionChange();
this._markForCheck();
}
}

Expand All @@ -277,6 +281,7 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
if (this._selected) {
this._selected = false;
this._dispatchSelectionChange();
this._markForCheck();
}
}

Expand All @@ -285,13 +290,15 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
if (!this._selected) {
this._selected = true;
this._dispatchSelectionChange(true);
this._markForCheck();
}
}

/** Toggles the current selected state of this chip. */
toggleSelected(isUserInput: boolean = false): boolean {
this._selected = !this.selected;
this._dispatchSelectionChange(isUserInput);
this._markForCheck();
return this.selected;
}

Expand Down Expand Up @@ -374,6 +381,13 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
selected: this._selected
});
}

private _markForCheck() {
// @breaking-change 9.0.0 Remove this method once the _changeDetectorRef is a required param.
if (this._changeDetectorRef) {
this._changeDetectorRef.markForCheck();
}
}
}


Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/chips.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export declare class MatChip extends _MatChipMixinBase implements FocusableOptio
readonly selectionChange: EventEmitter<MatChipSelectionChange>;
trailingIcon: MatChipTrailingIcon;
value: any;
constructor(_elementRef: ElementRef<HTMLElement>, _ngZone: NgZone, platform: Platform, globalRippleOptions: RippleGlobalOptions | null, animationMode?: string);
constructor(_elementRef: ElementRef<HTMLElement>, _ngZone: NgZone, platform: Platform, globalRippleOptions: RippleGlobalOptions | null, animationMode?: string, _changeDetectorRef?: ChangeDetectorRef | undefined);
_addHostClassName(): void;
_blur(): void;
_handleClick(event: Event): void;
Expand Down