fix(list): deselect option if value doesn't match up (#14800)

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.
This commit is contained in:
Kristiyan Kostadinov
2019-01-17 22:16:19 +01:00
committed by vivian-hu
parent 5a8dd1e9a5
commit 551ded50c1
2 changed files with 25 additions and 1 deletions
+15
View File
@@ -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);
+10 -1
View File
@@ -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()