Skip to content

Commit

Permalink
fix(ripple): global ripple configuration on init (#4238)
Browse files Browse the repository at this point in the history
* Currently when no bindings are set on the `mdRipple` target, the `ngOnChanges` will not fire and the `globalRippleOptions` will be ignored forever.

Fixes #4235
  • Loading branch information
devversion authored and mmalerba committed Apr 25, 2017
1 parent 2db3d61 commit 9a2c4d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
27 changes: 24 additions & 3 deletions src/lib/core/ripple/ripple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('MdRipple', () => {
declarations: [
BasicRippleContainer,
RippleContainerWithInputBindings,
RippleContainerWithoutBindings,
RippleContainerWithNgIf,
],
});
Expand Down Expand Up @@ -351,23 +352,38 @@ describe('MdRipple', () => {
describe('global ripple options', () => {
let rippleDirective: MdRipple;

function createTestComponent(rippleConfig: RippleGlobalOptions) {
function createTestComponent(rippleConfig: RippleGlobalOptions,
testComponent: any = BasicRippleContainer) {
// Reset the previously configured testing module to be able set new providers.
// The testing module has been initialized in the root describe group for the ripples.
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [MdRippleModule],
declarations: [BasicRippleContainer],
declarations: [testComponent],
providers: [{ provide: MD_RIPPLE_GLOBAL_OPTIONS, useValue: rippleConfig }]
});

fixture = TestBed.createComponent(BasicRippleContainer);
fixture = TestBed.createComponent(testComponent);
fixture.detectChanges();

rippleTarget = fixture.nativeElement.querySelector('[mat-ripple]');
rippleDirective = fixture.componentInstance.ripple;
}

it('should work without having any binding set', () => {
createTestComponent({ disabled: true }, RippleContainerWithoutBindings);

dispatchMouseEvent(rippleTarget, 'mousedown');
dispatchMouseEvent(rippleTarget, 'mouseup');

expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(0);

dispatchMouseEvent(rippleTarget, 'mousedown');
dispatchMouseEvent(rippleTarget, 'mouseup');

expect(rippleTarget.querySelectorAll('.mat-ripple-element').length).toBe(0);
});

it('when disabled should not show any ripples on mousedown', () => {
createTestComponent({ disabled: true });

Expand Down Expand Up @@ -577,6 +593,11 @@ class RippleContainerWithInputBindings {
@ViewChild(MdRipple) ripple: MdRipple;
}

@Component({
template: `<div id="container" #ripple="mdRipple" mat-ripple></div>`,
})
class RippleContainerWithoutBindings {}

@Component({ template: `<div id="container" mat-ripple [mdRippleSpeedFactor]="0"
*ngIf="!isDestroyed"></div>` })
class RippleContainerWithNgIf {
Expand Down
11 changes: 9 additions & 2 deletions src/lib/core/ripple/ripple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ export class MdRipple implements OnChanges, OnDestroy {
) {
this._rippleRenderer = new RippleRenderer(elementRef, ngZone, ruler);
this._globalOptions = globalOptions ? globalOptions : {};

this._updateRippleRenderer();
}

ngOnChanges(changes: SimpleChanges) {
if (changes['trigger'] && this.trigger) {
this._rippleRenderer.setTriggerElement(this.trigger);
}

this._rippleRenderer.rippleDisabled = this._globalOptions.disabled || this.disabled;
this._rippleRenderer.rippleConfig = this.rippleConfig;
this._updateRippleRenderer();
}

ngOnDestroy() {
Expand All @@ -122,4 +123,10 @@ export class MdRipple implements OnChanges, OnDestroy {
color: this.color
};
}

/** Updates the ripple renderer with the latest ripple configuration. */
private _updateRippleRenderer() {
this._rippleRenderer.rippleDisabled = this._globalOptions.disabled || this.disabled;
this._rippleRenderer.rippleConfig = this.rippleConfig;
}
}

0 comments on commit 9a2c4d6

Please sign in to comment.