Skip to content

Commit

Permalink
fix(list): deselect option if value doesn't match up (#14800)
Browse files Browse the repository at this point in the history
Along the same lines as #14734. Deselects a selected list option, if its value is changed to something that doesn't match up with the group.
  • Loading branch information
crisbeto authored and vivian-hu-zz committed Jan 17, 2019
1 parent 5a8dd1e commit 551ded5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,21 @@ describe('MatSelectionList with forms', () => {
.toBe(true, 'Expected every list option to be unselected.');
});

it('should deselect option whose value no longer matches', () => {
const option = listOptions[1];

fixture.componentInstance.formControl.setValue(['opt2']);
fixture.detectChanges();

expect(option.selected).toBe(true, 'Expected option to be selected.');

option.value = 'something-different';
fixture.detectChanges();

expect(option.selected).toBe(false, 'Expected option not to be selected.');
expect(fixture.componentInstance.formControl.value).toEqual([]);
});

it('should mark options as selected when the value is set before they are initialized', () => {
fixture.destroy();
fixture = TestBed.createComponent(SelectionListWithFormControl);
Expand Down
11 changes: 10 additions & 1 deletion src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,16 @@ export class MatListOption extends _MatListOptionMixinBase
@Input() checkboxPosition: 'before' | 'after' = 'after';

/** Value of the option */
@Input() value: any;
@Input()
get value(): any { return this._value; }
set value(newValue: any) {
if (this.selected && newValue !== this.value) {
this.selected = false;
}

this._value = newValue;
}
private _value: any;

/** Whether the option is disabled. */
@Input()
Expand Down

0 comments on commit 551ded5

Please sign in to comment.