Skip to content

Commit ce51421

Browse files
committed
Reintroduce MatListOptionChange for deprecation
1 parent 18a54e6 commit ce51421

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/lib/list/selection-list.spec.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import {createKeyboardEvent, dispatchFakeEvent} from '@angular/cdk/testing';
44
import {Component, DebugElement} from '@angular/core';
55
import {async, ComponentFixture, fakeAsync, inject, TestBed, tick} from '@angular/core/testing';
66
import {By} from '@angular/platform-browser';
7-
import {MatListModule, MatListOption, MatSelectionList, MatSelectionListChange} from './index';
7+
import {
8+
MatListModule,
9+
MatListOption,
10+
MatListOptionChange,
11+
MatSelectionList,
12+
MatSelectionListChange
13+
} from './index';
814
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
915

1016
describe('MatSelectionList without forms', () => {
@@ -85,16 +91,18 @@ describe('MatSelectionList without forms', () => {
8591

8692
it('should emit a deprecated selectionChange event on the list option that got clicked', () => {
8793
const optionInstance = listOptions[2].componentInstance as MatListOption;
88-
const selectionChangeSpy = jasmine.createSpy('selectionChange spy');
94+
let lastChangeEvent: MatListOptionChange | null = null;
8995

90-
optionInstance.selectionChange.subscribe(selectionChangeSpy);
96+
optionInstance.selectionChange.subscribe(ev => lastChangeEvent = ev);
9197

92-
expect(selectionChangeSpy).toHaveBeenCalledTimes(0);
98+
expect(lastChangeEvent).toBeNull();
9399

94100
dispatchFakeEvent(listOptions[2].nativeElement, 'click');
95101
fixture.detectChanges();
96102

97-
expect(selectionChangeSpy).toHaveBeenCalledTimes(1);
103+
expect(lastChangeEvent).not.toBeNull();
104+
expect(lastChangeEvent!.source).toBe(optionInstance);
105+
expect(lastChangeEvent!.selected).toBe(true);
98106
});
99107

100108
it('should be able to dispatch one selected item', () => {

src/lib/list/selection-list.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,25 @@ export const MAT_SELECTION_LIST_VALUE_ACCESSOR: any = {
5858
multi: true
5959
};
6060

61+
/**
62+
* Change event object emitted by MatListOption whenever the selected state changes.
63+
* @deprecated Use the `MatSelectionListChange` event on the selection list instead.
64+
*/
65+
export class MatListOptionChange {
66+
constructor(
67+
/** Reference to the list option that changed. */
68+
public source: MatListOption,
69+
/** The new selected state of the option. */
70+
public selected: boolean) {}
71+
}
72+
6173
/** Change event that is being fired whenever the selected state of an option changes. */
6274
export class MatSelectionListChange {
63-
constructor(public source: MatSelectionList, public option: MatListOption) {}
75+
constructor(
76+
/** Reference to the selection list that emitted the event. */
77+
public source: MatSelectionList,
78+
/** Reference to the option that has been changed. */
79+
public option: MatListOption) {}
6480
}
6581

6682
/**
@@ -136,8 +152,8 @@ export class MatListOption extends _MatListOptionMixinBase
136152
* Emits a change event whenever the selected state of an option changes.
137153
* @deprecated Use the `selectionChange` event on the `<mat-selection-list>` instead.
138154
*/
139-
@Output() selectionChange: EventEmitter<MatSelectionListChange> =
140-
new EventEmitter<MatSelectionListChange>();
155+
@Output() selectionChange: EventEmitter<MatListOptionChange> =
156+
new EventEmitter<MatListOptionChange>();
141157

142158
constructor(private _element: ElementRef,
143159
private _changeDetector: ChangeDetectorRef,
@@ -227,7 +243,7 @@ export class MatListOption extends _MatListOptionMixinBase
227243
/** Emits a selectionChange event for this option. */
228244
_emitDeprecatedChangeEvent() {
229245
// TODO: the `selectionChange` event on the option is deprecated. Remove that in the future.
230-
this.selectionChange.emit(new MatSelectionListChange(this.selectionList, this));
246+
this.selectionChange.emit(new MatListOptionChange(this, this.selected));
231247
}
232248
}
233249

0 commit comments

Comments
 (0)