Skip to content
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

fix(material/list): dispatching model change event multiple times in single selection mode #22376

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
5 changes: 4 additions & 1 deletion src/material-experimental/mdc-list/list-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ export class MatListOption extends MatListItemBase implements ListOption, OnInit

if (isSelected !== this._selected) {
this._setSelected(isSelected);
this._selectionList._reportValueChange();

if (isSelected || this._selectionList.multiple) {
this._selectionList._reportValueChange();
}
}
}
private _selected = false;
Expand Down
34 changes: 33 additions & 1 deletion src/material-experimental/mdc-list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,34 @@ describe('MDC-based MatSelectionList with forms', () => {

expect(listOptions.map(option => option.selected)).toEqual([true, true, true, false, false]);
}));

it('should dispatch one change event per change when updating a single-selection list',
fakeAsync(() => {
fixture.destroy();
fixture = TestBed.createComponent(SelectionListWithModel);
fixture.componentInstance.multiple = false;
fixture.componentInstance.selectedOptions = ['opt3'];
fixture.detectChanges();
const options = fixture.debugElement.queryAll(By.directive(MatListOption))
.map(optionDebugEl => optionDebugEl.nativeElement);

expect(fixture.componentInstance.modelChangeSpy).not.toHaveBeenCalled();

options[0].click();
fixture.detectChanges();
tick();

expect(fixture.componentInstance.modelChangeSpy).toHaveBeenCalledTimes(1);
expect(fixture.componentInstance.selectedOptions).toEqual(['opt1']);

options[1].click();
fixture.detectChanges();
tick();

expect(fixture.componentInstance.modelChangeSpy).toHaveBeenCalledTimes(2);
expect(fixture.componentInstance.selectedOptions).toEqual(['opt2']);
}));

});

describe('and formControl', () => {
Expand Down Expand Up @@ -1412,13 +1440,17 @@ class SelectionListWithOnlyOneOption {

@Component({
template: `
<mat-selection-list [(ngModel)]="selectedOptions" (ngModelChange)="modelChangeSpy()">
<mat-selection-list
[(ngModel)]="selectedOptions"
(ngModelChange)="modelChangeSpy()"
[multiple]="multiple">
<mat-list-option *ngFor="let option of options" [value]="option">{{option}}</mat-list-option>
</mat-selection-list>`
})
class SelectionListWithModel {
modelChangeSpy = jasmine.createSpy('model change spy');
selectedOptions: string[] = [];
multiple = true;
options = ['opt1', 'opt2', 'opt3'];
}

Expand Down
33 changes: 32 additions & 1 deletion src/material/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,33 @@ describe('MatSelectionList with forms', () => {
expect(selectionListDebug.nativeElement.tabIndex).toBe(-1);
});

it('should dispatch one change event per change when updating a single-selection list',
fakeAsync(() => {
fixture.destroy();
fixture = TestBed.createComponent(SelectionListWithModel);
fixture.componentInstance.multiple = false;
fixture.componentInstance.selectedOptions = ['opt3'];
fixture.detectChanges();
const options = fixture.debugElement.queryAll(By.directive(MatListOption))
.map(optionDebugEl => optionDebugEl.nativeElement);

expect(fixture.componentInstance.modelChangeSpy).not.toHaveBeenCalled();

options[0].click();
fixture.detectChanges();
tick();

expect(fixture.componentInstance.modelChangeSpy).toHaveBeenCalledTimes(1);
expect(fixture.componentInstance.selectedOptions).toEqual(['opt1']);

options[1].click();
fixture.detectChanges();
tick();

expect(fixture.componentInstance.modelChangeSpy).toHaveBeenCalledTimes(2);
expect(fixture.componentInstance.selectedOptions).toEqual(['opt2']);
}));

});

describe('and formControl', () => {
Expand Down Expand Up @@ -1642,13 +1669,17 @@ class SelectionListWithOnlyOneOption {

@Component({
template: `
<mat-selection-list [(ngModel)]="selectedOptions" (ngModelChange)="modelChangeSpy()">
<mat-selection-list
[(ngModel)]="selectedOptions"
(ngModelChange)="modelChangeSpy()"
[multiple]="multiple">
<mat-list-option *ngFor="let option of options" [value]="option">{{option}}</mat-list-option>
</mat-selection-list>`
})
class SelectionListWithModel {
modelChangeSpy = jasmine.createSpy('model change spy');
selectedOptions: string[] = [];
multiple = true;
options = ['opt1', 'opt2', 'opt3'];
}

Expand Down
5 changes: 4 additions & 1 deletion src/material/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ export class MatListOption extends _MatListOptionMixinBase implements AfterConte

if (isSelected !== this._selected) {
this._setSelected(isSelected);
this.selectionList._reportValueChange();

if (isSelected || this.selectionList.multiple) {
this.selectionList._reportValueChange();
}
}
}

Expand Down