Skip to content

feat(selection-list): add selectAll and deselectAll functions #6971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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