Skip to content

Commit 31f4757

Browse files
committed
feat(selection-list): add selectAll and deselectAll functions
* Adds the ability to select and deselect all options at once. * Adds a demo of the selection list to the demo app. * Fixes an issue that caused the checkbox inside the selection list to collapse. * Sets the proper cursor on the list items. Fixes #6969.
1 parent f2cae6e commit 31f4757

File tree

6 files changed

+67
-1
lines changed

6 files changed

+67
-1
lines changed

src/demo-app/list/list-demo.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,22 @@ <h2>Nav lists</h2>
101101
</md-list-item>
102102
</md-nav-list>
103103
</div>
104+
105+
<div>
106+
<h2>Selection list</h2>
107+
108+
<md-selection-list #groceries>
109+
<h3 md-subheader>Groceries</h3>
110+
111+
<md-list-option *ngFor="let item of items" [value]="item">
112+
{{item}}
113+
</md-list-option>
114+
</md-selection-list>
115+
116+
<p>Selected: {{groceries.selectedOptions.selected.length}}</p>
117+
<p>
118+
<button md-raised-button (click)="groceries.selectAll()">Select all</button>
119+
<button md-raised-button (click)="groceries.deselectAll()">Deselect all</button>
120+
</p>
121+
</div>
104122
</div>

src/demo-app/list/list-demo.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
display: flex;
44
flex-flow: row wrap;
55

6-
.mat-list, .mat-nav-list {
6+
.mat-list, .mat-nav-list, .mat-selection-list {
77
border: 1px solid rgba(0, 0, 0, 0.12);
88
width: 350px;
99
margin: 20px 20px 0 0;

src/lib/core/selection/pseudo-checkbox/pseudo-checkbox.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ $_mat-pseudo-checkmark-size: $mat-checkbox-size - (2 * $_mat-pseudo-checkbox-pad
1717
vertical-align: middle;
1818
box-sizing: border-box;
1919
position: relative;
20+
flex-shrink: 0;
2021
transition:
2122
border-color $mat-checkbox-transition-duration $mat-linear-out-slow-in-timing-function,
2223
background-color $mat-checkbox-transition-duration $mat-linear-out-slow-in-timing-function;

src/lib/list/list.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,9 @@ $mat-dense-list-icon-size: 20px;
216216
}
217217
}
218218
}
219+
220+
.mat-list-option {
221+
&:not([disabled]) {
222+
cursor: pointer;
223+
}
224+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,29 @@ describe('MdSelectionList', () => {
181181

182182
expect(manager.activeItemIndex).toEqual(3);
183183
});
184+
185+
it('should be able to select all options', () => {
186+
const list: MdSelectionList = selectionList.componentInstance;
187+
188+
expect(list.options.toArray().every(option => option.selected)).toBe(false);
189+
190+
list.selectAll();
191+
fixture.detectChanges();
192+
193+
expect(list.options.toArray().every(option => option.selected)).toBe(true);
194+
});
195+
196+
it('should be able to deselect all options', () => {
197+
const list: MdSelectionList = selectionList.componentInstance;
198+
199+
list.options.forEach(option => option.toggle());
200+
expect(list.options.toArray().every(option => option.selected)).toBe(true);
201+
202+
list.deselectAll();
203+
fixture.detectChanges();
204+
205+
expect(list.options.toArray().every(option => option.selected)).toBe(false);
206+
});
184207
});
185208

186209
describe('with single option', () => {

src/lib/list/selection-list.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ export class MdSelectionList extends _MdSelectionListMixinBase
244244
this._element.nativeElement.focus();
245245
}
246246

247+
/** Selects all of the options. */
248+
selectAll() {
249+
this.options.forEach(option => {
250+
if (!option.selected) {
251+
option.toggle();
252+
}
253+
});
254+
}
255+
256+
/** Deselects all of the options. */
257+
deselectAll() {
258+
this.options.forEach(option => {
259+
if (option.selected) {
260+
option.toggle();
261+
}
262+
});
263+
}
264+
247265
/** Map all the options' destroy event subscriptions and merge them into one stream. */
248266
private _onDestroySubscription(): Subscription {
249267
return RxChain.from(this.options.changes)

0 commit comments

Comments
 (0)