Skip to content

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

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
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
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