Skip to content

fix(chips): selectionChange event firing when value has not changed #13173

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
Oct 2, 2018
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
49 changes: 49 additions & 0 deletions src/lib/chips/chip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,55 @@ describe('Chips', () => {
expect(event.defaultPrevented).toBe(true);
});

it('should not dispatch `selectionChange` event when deselecting a non-selected chip', () => {
chipInstance.deselect();

const spy = jasmine.createSpy('selectionChange spy');
const subscription = chipInstance.selectionChange.subscribe(spy);

chipInstance.deselect();

expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});

it('should not dispatch `selectionChange` event when selecting a selected chip', () => {
chipInstance.select();

const spy = jasmine.createSpy('selectionChange spy');
const subscription = chipInstance.selectionChange.subscribe(spy);

chipInstance.select();

expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});

it('should not dispatch `selectionChange` event when selecting a selected chip via ' +
'user interaction', () => {
chipInstance.select();

const spy = jasmine.createSpy('selectionChange spy');
const subscription = chipInstance.selectionChange.subscribe(spy);

chipInstance.selectViaInteraction();

expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});

it('should not dispatch `selectionChange` through setter if the value did not change', () => {
chipInstance.selected = false;

const spy = jasmine.createSpy('selectionChange spy');
const subscription = chipInstance.selectionChange.subscribe(spy);

chipInstance.selected = false;

expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});

});

describe('keyboard behavior', () => {
Expand Down
59 changes: 27 additions & 32 deletions src/lib/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
@Input()
get selected(): boolean { return this._selected; }
set selected(value: boolean) {
this._selected = coerceBooleanProperty(value);
this.selectionChange.emit({
source: this,
isUserInput: false,
selected: value
});
const coercedValue = coerceBooleanProperty(value);

if (coercedValue !== this._selected) {
this._selected = coercedValue;
this._dispatchSelectionChange();
}
}
protected _selected: boolean = false;

Expand Down Expand Up @@ -262,45 +262,32 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes

/** Selects the chip. */
select(): void {
this._selected = true;
this.selectionChange.emit({
source: this,
isUserInput: false,
selected: true
});
if (!this._selected) {
this._selected = true;
this._dispatchSelectionChange();
}
}

/** Deselects the chip. */
deselect(): void {
this._selected = false;
this.selectionChange.emit({
source: this,
isUserInput: false,
selected: false
});
if (this._selected) {
this._selected = false;
this._dispatchSelectionChange();
}
}

/** Select this chip and emit selected event */
selectViaInteraction(): void {
this._selected = true;
// Emit select event when selected changes.
this.selectionChange.emit({
source: this,
isUserInput: true,
selected: true
});
if (!this._selected) {
this._selected = true;
this._dispatchSelectionChange(true);
}
}

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

this.selectionChange.emit({
source: this,
isUserInput,
selected: this._selected
});

this._dispatchSelectionChange(isUserInput);
return this.selected;
}

Expand Down Expand Up @@ -375,6 +362,14 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
});
});
}

private _dispatchSelectionChange(isUserInput = false) {
this.selectionChange.emit({
source: this,
isUserInput,
selected: this._selected
});
}
}


Expand Down