Skip to content

Commit 27e88c3

Browse files
amcdnljelbourn
authored andcommitted
fix(collections): align SelectionModel to changed naming (#8286)
1 parent e765d8e commit 27e88c3

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

src/cdk/collections/collections.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1-
### Collections
1+
The `collections` package provides a set of utilities for managing collections.
22

3-
A set of utilities for managing collections.
3+
### `SelectionModel`
4+
`SelectionModel` is a utility for powering selection of one or more options from a list.
5+
This model is used in components such as the selection list, table selections and chip lists.
6+
7+
#### Basic Usage
8+
```javascript
9+
const model = new SelectionModel(
10+
true, // multiple selection or not
11+
[2,1,3] // initial selected values
12+
);
13+
14+
// select a value
15+
model.select(4);
16+
console.log(model.selected.length) //-> 4
17+
18+
// deselect a value
19+
model.deselect(4);
20+
console.log(model.selected.length) //-> 3
21+
22+
// toggle a value
23+
model.toggle(4);
24+
console.log(model.selected.length) //-> 4
25+
26+
// check for selection
27+
console.log(model.isSelected(4)) //-> true
28+
29+
// sort the selections
30+
console.log(model.sort()) //-> [1,2,3,4]
31+
32+
// listen for changes
33+
model.changes.subscribe(s => console.log(s));
34+
```

src/cdk/collections/selection.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('SelectionModel', () => {
4343
it('should be able to select multiple options', () => {
4444
const onChangeSpy = jasmine.createSpy('onChange spy');
4545

46-
model.onChange!.subscribe(onChangeSpy);
46+
model.changed!.subscribe(onChangeSpy);
4747
model.select(1);
4848
model.select(2);
4949

@@ -56,7 +56,7 @@ describe('SelectionModel', () => {
5656
it('should be able to select multiple options at the same time', () => {
5757
const onChangeSpy = jasmine.createSpy('onChange spy');
5858

59-
model.onChange!.subscribe(onChangeSpy);
59+
model.changed!.subscribe(onChangeSpy);
6060
model.select(1, 2);
6161

6262
expect(model.selected.length).toBe(2);
@@ -112,7 +112,7 @@ describe('SelectionModel', () => {
112112

113113
model.select(1);
114114

115-
model.onChange!.subscribe(spy);
115+
model.changed!.subscribe(spy);
116116

117117
model.select(2);
118118

@@ -144,7 +144,7 @@ describe('SelectionModel', () => {
144144
model = new SelectionModel(true);
145145
spy = jasmine.createSpy('SelectionModel change event');
146146

147-
model.onChange!.subscribe(spy);
147+
model.changed!.subscribe(spy);
148148
});
149149

150150
it('should emit an event when a value is selected', () => {
@@ -167,7 +167,7 @@ describe('SelectionModel', () => {
167167
it('should not emit an event when preselecting values', () => {
168168
model = new SelectionModel(false, [1]);
169169
spy = jasmine.createSpy('SelectionModel initial change event');
170-
model.onChange!.subscribe(spy);
170+
model.changed!.subscribe(spy);
171171

172172
expect(spy).not.toHaveBeenCalled();
173173
});
@@ -181,7 +181,7 @@ describe('SelectionModel', () => {
181181
model = new SelectionModel(true, [1, 2, 3]);
182182
spy = jasmine.createSpy('SelectionModel change event');
183183

184-
model.onChange!.subscribe(spy);
184+
model.changed!.subscribe(spy);
185185
});
186186

187187
it('should emit an event when a value is deselected', () => {
@@ -218,7 +218,7 @@ describe('SelectionModel', () => {
218218
});
219219

220220
it('should not have an onChange stream if change events are disabled', () => {
221-
expect(model.onChange).toBeFalsy();
221+
expect(model.changed).toBeFalsy();
222222
});
223223

224224
it('should still update the select value', () => {

src/cdk/collections/selection.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ export class SelectionModel<T> {
3434
}
3535

3636
/** Event emitted when the value has changed. */
37-
onChange: Subject<SelectionChange<T>> | null = this._emitChanges ? new Subject() : null;
37+
changed: Subject<SelectionChange<T>> | null = this._emitChanges ? new Subject() : null;
38+
39+
/**
40+
* Event emitted when the value has changed.
41+
* @deprecated Use `changed` instead.
42+
* @deletion-target 8.0.0 To be changed to `changed`
43+
*/
44+
onChange: Subject<SelectionChange<T>> | null = this.changed;
3845

3946
constructor(
4047
private _multiple = false,
@@ -129,8 +136,8 @@ export class SelectionModel<T> {
129136
this._selected = null;
130137

131138
if (this._selectedToEmit.length || this._deselectedToEmit.length) {
132-
if (this.onChange) {
133-
this.onChange.next({
139+
if (this.changed) {
140+
this.changed.next({
134141
source: this,
135142
added: this._selectedToEmit,
136143
removed: this._deselectedToEmit

0 commit comments

Comments
 (0)