Skip to content

Commit

Permalink
feat(selection-list): add selectAll and deselectAll functions
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
crisbeto committed Sep 10, 2017
1 parent f2cae6e commit 31f4757
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/demo-app/list/list-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,22 @@ <h2>Nav lists</h2>
</md-list-item>
</md-nav-list>
</div>

<div>
<h2>Selection list</h2>

<md-selection-list #groceries>
<h3 md-subheader>Groceries</h3>

<md-list-option *ngFor="let item of items" [value]="item">
{{item}}
</md-list-option>
</md-selection-list>

<p>Selected: {{groceries.selectedOptions.selected.length}}</p>
<p>
<button md-raised-button (click)="groceries.selectAll()">Select all</button>
<button md-raised-button (click)="groceries.deselectAll()">Deselect all</button>
</p>
</div>
</div>
2 changes: 1 addition & 1 deletion src/demo-app/list/list-demo.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
display: flex;
flex-flow: row wrap;

.mat-list, .mat-nav-list {
.mat-list, .mat-nav-list, .mat-selection-list {
border: 1px solid rgba(0, 0, 0, 0.12);
width: 350px;
margin: 20px 20px 0 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ $_mat-pseudo-checkmark-size: $mat-checkbox-size - (2 * $_mat-pseudo-checkbox-pad
vertical-align: middle;
box-sizing: border-box;
position: relative;
flex-shrink: 0;
transition:
border-color $mat-checkbox-transition-duration $mat-linear-out-slow-in-timing-function,
background-color $mat-checkbox-transition-duration $mat-linear-out-slow-in-timing-function;
Expand Down
6 changes: 6 additions & 0 deletions src/lib/list/list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,9 @@ $mat-dense-list-icon-size: 20px;
}
}
}

.mat-list-option {
&:not([disabled]) {
cursor: pointer;
}
}
23 changes: 23 additions & 0 deletions src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,29 @@ describe('MdSelectionList', () => {

expect(manager.activeItemIndex).toEqual(3);
});

it('should be able to select all options', () => {
const list: MdSelectionList = selectionList.componentInstance;

expect(list.options.toArray().every(option => option.selected)).toBe(false);

list.selectAll();
fixture.detectChanges();

expect(list.options.toArray().every(option => option.selected)).toBe(true);
});

it('should be able to deselect all options', () => {
const list: MdSelectionList = selectionList.componentInstance;

list.options.forEach(option => option.toggle());
expect(list.options.toArray().every(option => option.selected)).toBe(true);

list.deselectAll();
fixture.detectChanges();

expect(list.options.toArray().every(option => option.selected)).toBe(false);
});
});

describe('with single option', () => {
Expand Down
18 changes: 18 additions & 0 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,24 @@ export class MdSelectionList extends _MdSelectionListMixinBase
this._element.nativeElement.focus();
}

/** Selects all of the options. */
selectAll() {
this.options.forEach(option => {
if (!option.selected) {
option.toggle();
}
});
}

/** Deselects all of the options. */
deselectAll() {
this.options.forEach(option => {
if (option.selected) {
option.toggle();
}
});
}

/** Map all the options' destroy event subscriptions and merge them into one stream. */
private _onDestroySubscription(): Subscription {
return RxChain.from(this.options.changes)
Expand Down

0 comments on commit 31f4757

Please sign in to comment.