Skip to content

Commit 006d8ac

Browse files
committed
Rename to selectionChange. Add deprecated selectionChange event on option
1 parent fce9d58 commit 006d8ac

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('MatSelectionList without forms', () => {
6161
});
6262
});
6363

64-
it('should not emit a change event if an option changed programmatically', () => {
64+
it('should not emit a selectionChange event if an option changed programmatically', () => {
6565
spyOn(fixture.componentInstance, 'onValueChange');
6666

6767
expect(fixture.componentInstance.onValueChange).toHaveBeenCalledTimes(0);
@@ -72,7 +72,7 @@ describe('MatSelectionList without forms', () => {
7272
expect(fixture.componentInstance.onValueChange).toHaveBeenCalledTimes(0);
7373
});
7474

75-
it('should not emit a change event if an option got clicked', () => {
75+
it('should emit a selectionChange event if an option got clicked', () => {
7676
spyOn(fixture.componentInstance, 'onValueChange');
7777

7878
expect(fixture.componentInstance.onValueChange).toHaveBeenCalledTimes(0);
@@ -83,6 +83,20 @@ describe('MatSelectionList without forms', () => {
8383
expect(fixture.componentInstance.onValueChange).toHaveBeenCalledTimes(1);
8484
});
8585

86+
it('should emit a deprecated selectionChange event on the list option that got clicked', () => {
87+
const optionInstance = listOptions[2].componentInstance as MatListOption;
88+
const selectionChangeSpy = jasmine.createSpy('selectionChange spy');
89+
90+
optionInstance.selectionChange.subscribe(selectionChangeSpy);
91+
92+
expect(selectionChangeSpy).toHaveBeenCalledTimes(0);
93+
94+
dispatchFakeEvent(listOptions[2].nativeElement, 'click');
95+
fixture.detectChanges();
96+
97+
expect(selectionChangeSpy).toHaveBeenCalledTimes(1);
98+
});
99+
86100
it('should be able to dispatch one selected item', () => {
87101
let testListItem = listOptions[2].injector.get<MatListOption>(MatListOption);
88102
let selectList =
@@ -662,7 +676,7 @@ describe('MatSelectionList with forms', () => {
662676

663677

664678
@Component({template: `
665-
<mat-selection-list id="selection-list-1" (change)="onValueChange($event)">
679+
<mat-selection-list id="selection-list-1" (selectionChange)="onValueChange($event)">
666680
<mat-list-option checkboxPosition="before" disabled="true" value="inbox">
667681
Inbox (disabled selection-option)
668682
</mat-list-option>

src/lib/list/selection-list.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export class MatSelectionListChange {
9393
})
9494
export class MatListOption extends _MatListOptionMixinBase
9595
implements AfterContentInit, OnDestroy, OnInit, FocusableOption, CanDisableRipple {
96+
9697
private _lineSetter: MatLineSetter;
9798
private _selected: boolean = false;
9899
private _disabled: boolean = false;
@@ -132,6 +133,13 @@ export class MatListOption extends _MatListOptionMixinBase
132133
}
133134
}
134135

136+
/**
137+
* Emits a change event whenever the selected state of an option changes.
138+
* @deprecated Use the `selectionChange` event on the `<mat-selection-list>` instead.
139+
*/
140+
@Output() selectionChange: EventEmitter<MatSelectionListChange> =
141+
new EventEmitter<MatSelectionListChange>();
142+
135143
constructor(private _renderer: Renderer2,
136144
private _element: ElementRef,
137145
private _changeDetector: ChangeDetectorRef,
@@ -180,6 +188,9 @@ export class MatListOption extends _MatListOptionMixinBase
180188

181189
// Emit a change event if the selected state of the option changed through user interaction.
182190
this.selectionList._emitChangeEvent(this);
191+
192+
// TODO: the `selectionChange` event on the option is deprecated. Remove that in the future.
193+
this._emitDeprecatedChangeEvent();
183194
}
184195
}
185196

@@ -214,6 +225,12 @@ export class MatListOption extends _MatListOptionMixinBase
214225

215226
this._changeDetector.markForCheck();
216227
}
228+
229+
/** Emits a selectionChange event for this option. */
230+
_emitDeprecatedChangeEvent() {
231+
// TODO: the `selectionChange` event on the option is deprecated. Remove that in the future.
232+
this.selectionChange.emit(new MatSelectionListChange(this.selectionList, this));
233+
}
217234
}
218235

219236

@@ -250,7 +267,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
250267
@ContentChildren(MatListOption) options: QueryList<MatListOption>;
251268

252269
/** Emits a change event whenever the selected state of an option changes. */
253-
@Output() change: EventEmitter<MatSelectionListChange> =
270+
@Output() selectionChange: EventEmitter<MatSelectionListChange> =
254271
new EventEmitter<MatSelectionListChange>();
255272

256273
/** The currently selected options. */
@@ -330,7 +347,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
330347

331348
/** Emits a change event if the selected state of an option changed. */
332349
_emitChangeEvent(option: MatListOption) {
333-
this.change.emit(new MatSelectionListChange(this, option));
350+
this.selectionChange.emit(new MatSelectionListChange(this, option));
334351
}
335352

336353
/** Implemented as part of ControlValueAccessor. */
@@ -390,6 +407,9 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
390407
// Emit a change event because the focused option changed its state through user
391408
// interaction.
392409
this._emitChangeEvent(focusedOption);
410+
411+
// TODO: the `selectionChange` event on the option is deprecated. Remove that in the future.
412+
focusedOption._emitDeprecatedChangeEvent();
393413
}
394414
}
395415
}

0 commit comments

Comments
 (0)