Skip to content

Commit edf4541

Browse files
crisbetojelbourn
authored andcommittedFeb 20, 2019
fix(list): disableRipple on selection list not affecting list options after init (#14858)
Along the same lines as #14836. Fixes changes to `disableRipple` not being propagated immediately the list options.
1 parent 2c7bc1c commit edf4541

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed
 

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ <h2>Selection list</h2>
119119
<mat-selection-list #groceries [ngModel]="selectedOptions"
120120
(ngModelChange)="onSelectedOptionsChange($event)"
121121
(change)="changeEventCount = changeEventCount + 1"
122-
[disabled]="selectionListDisabled">
122+
[disabled]="selectionListDisabled"
123+
[disableRipple]="selectionListRippleDisabled">
123124
<h3 mat-subheader>Groceries</h3>
124125

125126
<mat-list-option value="bananas" checkboxPosition="before">Bananas</mat-list-option>
@@ -128,7 +129,7 @@ <h3 mat-subheader>Groceries</h3>
128129
<mat-list-option value="strawberries">Strawberries</mat-list-option>
129130
</mat-selection-list>
130131

131-
<mat-selection-list>
132+
<mat-selection-list [disableRipple]="selectionListRippleDisabled">
132133
<h3 mat-subheader>Dogs</h3>
133134

134135
<mat-list-option checkboxPosition="before">
@@ -145,8 +146,16 @@ <h3 mat-subheader>Dogs</h3>
145146
<p>Selected: {{selectedOptions | json}}</p>
146147
<p>Change Event Count {{changeEventCount}}</p>
147148
<p>Model Change Event Count {{modelChangeEventCount}}</p>
148-
<mat-checkbox [(ngModel)]="selectionListDisabled">Disable Selection List</mat-checkbox>
149-
149+
<p>
150+
<mat-checkbox [(ngModel)]="selectionListDisabled">
151+
Disable Selection List
152+
</mat-checkbox>
153+
</p>
154+
<p>
155+
<mat-checkbox [(ngModel)]="selectionListRippleDisabled">
156+
Disable Selection List ripples
157+
</mat-checkbox>
158+
</p>
150159
<p>
151160
<button mat-raised-button (click)="groceries.selectAll()">Select all</button>
152161
<button mat-raised-button (click)="groceries.deselectAll()">Deselect all</button>

‎src/dev-app/list/list-demo.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class ListDemo {
6060
thirdLine = false;
6161
infoClicked = false;
6262
selectionListDisabled = false;
63+
selectionListRippleDisabled = false;
6364

6465
selectedOptions: string[] = ['apples'];
6566
changeEventCount = 0;

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

+34-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
dispatchFakeEvent,
55
dispatchEvent,
66
dispatchKeyboardEvent,
7+
dispatchMouseEvent,
78
} from '@angular/cdk/testing';
89
import {
910
Component,
@@ -13,7 +14,7 @@ import {
1314
ViewChildren,
1415
} from '@angular/core';
1516
import {async, ComponentFixture, fakeAsync, TestBed, tick, flush} from '@angular/core/testing';
16-
import {MatRipple} from '@angular/material/core';
17+
import {MatRipple, defaultRippleAnimationConfig} from '@angular/material/core';
1718
import {By} from '@angular/platform-browser';
1819
import {
1920
MatListModule,
@@ -521,6 +522,33 @@ describe('MatSelectionList without forms', () => {
521522
expect(list.options.toArray().every(option => option.selected)).toBe(true);
522523
});
523524

525+
it('should disable list item ripples when the ripples on the list have been disabled',
526+
fakeAsync(() => {
527+
const rippleTarget = fixture.nativeElement
528+
.querySelector('.mat-list-option:not(.mat-list-item-disabled) .mat-list-item-content');
529+
const {enterDuration, exitDuration} = defaultRippleAnimationConfig;
530+
531+
dispatchMouseEvent(rippleTarget, 'mousedown');
532+
dispatchMouseEvent(rippleTarget, 'mouseup');
533+
534+
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length)
535+
.toBe(1, 'Expected ripples to be enabled by default.');
536+
537+
// Wait for the ripples to go away.
538+
tick(enterDuration + exitDuration);
539+
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length)
540+
.toBe(0, 'Expected ripples to go away.');
541+
542+
fixture.componentInstance.listRippleDisabled = true;
543+
fixture.detectChanges();
544+
545+
dispatchMouseEvent(rippleTarget, 'mousedown');
546+
dispatchMouseEvent(rippleTarget, 'mouseup');
547+
548+
expect(rippleTarget.querySelectorAll('.mat-ripple-element').length)
549+
.toBe(0, 'Expected no ripples after list ripples are disabled.');
550+
}));
551+
524552
});
525553

526554
describe('with list option selected', () => {
@@ -1091,7 +1119,10 @@ describe('MatSelectionList with forms', () => {
10911119

10921120

10931121
@Component({template: `
1094-
<mat-selection-list id="selection-list-1" (selectionChange)="onValueChange($event)">
1122+
<mat-selection-list
1123+
id="selection-list-1"
1124+
(selectionChange)="onValueChange($event)"
1125+
[disableRipple]="listRippleDisabled">
10951126
<mat-list-option checkboxPosition="before" disabled="true" value="inbox">
10961127
Inbox (disabled selection-option)
10971128
</mat-list-option>
@@ -1108,6 +1139,7 @@ describe('MatSelectionList with forms', () => {
11081139
</mat-selection-list>`})
11091140
class SelectionListWithListOptions {
11101141
showLastOption: boolean = true;
1142+
listRippleDisabled = false;
11111143

11121144
onValueChange(_change: MatSelectionListChange) {}
11131145
}

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import {
3838
QueryList,
3939
ViewChild,
4040
ViewEncapsulation,
41+
SimpleChanges,
42+
OnChanges,
4143
} from '@angular/core';
4244
import {
4345
CanDisableRipple, CanDisableRippleCtor,
@@ -299,7 +301,7 @@ export class MatListOption extends _MatListOptionMixinBase
299301
changeDetection: ChangeDetectionStrategy.OnPush
300302
})
301303
export class MatSelectionList extends _MatSelectionListMixinBase implements FocusableOption,
302-
CanDisableRipple, AfterContentInit, ControlValueAccessor, OnDestroy {
304+
CanDisableRipple, AfterContentInit, ControlValueAccessor, OnDestroy, OnChanges {
303305

304306
/** The FocusKeyManager which handles focus. */
305307
_keyManager: FocusKeyManager<MatListOption>;
@@ -331,9 +333,7 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
331333
// strategy. Therefore the options will not check for any changes if the `MatSelectionList`
332334
// changed its state. Since we know that a change to `disabled` property of the list affects
333335
// the state of the options, we manually mark each option for check.
334-
if (this.options) {
335-
this.options.forEach(option => option._markForCheck());
336-
}
336+
this._markOptionsForCheck();
337337
}
338338
private _disabled: boolean = false;
339339

@@ -387,6 +387,14 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
387387
});
388388
}
389389

390+
ngOnChanges(changes: SimpleChanges) {
391+
const disableRippleChanges = changes.disableRipple;
392+
393+
if (disableRippleChanges && !disableRippleChanges.firstChange) {
394+
this._markOptionsForCheck();
395+
}
396+
}
397+
390398
ngOnDestroy() {
391399
this._modelChanges.unsubscribe();
392400
}
@@ -581,4 +589,11 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
581589
private _getOptionIndex(option: MatListOption): number {
582590
return this.options.toArray().indexOf(option);
583591
}
592+
593+
/** Marks all the options to be checked in the next change detection run. */
594+
private _markOptionsForCheck() {
595+
if (this.options) {
596+
this.options.forEach(option => option._markForCheck());
597+
}
598+
}
584599
}

‎tools/public_api_guard/lib/list.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export declare class MatNavList extends _MatListMixinBase implements CanDisableR
8181
ngOnDestroy(): void;
8282
}
8383

84-
export declare class MatSelectionList extends _MatSelectionListMixinBase implements FocusableOption, CanDisableRipple, AfterContentInit, ControlValueAccessor, OnDestroy {
84+
export declare class MatSelectionList extends _MatSelectionListMixinBase implements FocusableOption, CanDisableRipple, AfterContentInit, ControlValueAccessor, OnDestroy, OnChanges {
8585
_keyManager: FocusKeyManager<MatListOption>;
8686
_onTouched: () => void;
8787
compareWith: (o1: any, o2: any) => boolean;
@@ -99,6 +99,7 @@ export declare class MatSelectionList extends _MatSelectionListMixinBase impleme
9999
deselectAll(): void;
100100
focus(): void;
101101
ngAfterContentInit(): void;
102+
ngOnChanges(changes: SimpleChanges): void;
102103
ngOnDestroy(): void;
103104
registerOnChange(fn: (value: any) => void): void;
104105
registerOnTouched(fn: () => void): void;

0 commit comments

Comments
 (0)
Please sign in to comment.