Skip to content

Commit

Permalink
fix(button): complete ripple when button becomes disabled (#4372)
Browse files Browse the repository at this point in the history
* Do not destroy the ripple when it is disabled

* Update tests
  • Loading branch information
willshowell authored and tinayuangao committed May 22, 2017
1 parent c45dee2 commit fce2868
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/demo-app/button/button-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@
<button md-button>Check<md-icon class="md-24">favorite</md-icon></button>
<button md-button>Last<md-icon>navigate_before</md-icon></button>
</section>
<section>
<button md-raised-button [disabled]="toggleDisable" (click)="toggleDisable = true">Disable</button>
<button md-button [disabled]="!toggleDisable" (click)="toggleDisable = false">Disable</button>
</section>
</div>
1 change: 1 addition & 0 deletions src/demo-app/button/button-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import {Component} from '@angular/core';
export class ButtonDemo {
isDisabled: boolean = false;
clickCounter: number = 0;
toggleDisable: boolean = false;
}
3 changes: 2 additions & 1 deletion src/lib/button/button.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<span class="mat-button-wrapper"><ng-content></ng-content></span>
<div md-ripple *ngIf="!_isRippleDisabled()" class="mat-button-ripple"
<div md-ripple class="mat-button-ripple"
[class.mat-button-ripple-round]="_isRoundButton || _isIconButton"
[mdRippleDisabled]="_isRippleDisabled()"
[mdRippleCentered]="_isIconButton"
[mdRippleTrigger]="_getHostElement()"></div>
<div class="mat-button-focus-overlay"></div>
52 changes: 33 additions & 19 deletions src/lib/button/button.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Component} from '@angular/core';
import {Component, DebugElement} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MdButtonModule} from './index';
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
import {MdRipple} from '../core/ripple/index';


describe('MdButton', () => {
Expand Down Expand Up @@ -156,41 +157,54 @@ describe('MdButton', () => {
describe('button ripples', () => {
let fixture: ComponentFixture<TestApp>;
let testComponent: TestApp;
let buttonElement: HTMLButtonElement;
let anchorElement: HTMLAnchorElement;
let buttonDebugElement: DebugElement;
let buttonRippleDebugElement: DebugElement;
let buttonRippleInstance: MdRipple;
let anchorDebugElement: DebugElement;
let anchorRippleDebugElement: DebugElement;
let anchorRippleInstance: MdRipple;

beforeEach(() => {
fixture = TestBed.createComponent(TestApp);
fixture.detectChanges();

testComponent = fixture.componentInstance;
buttonElement = fixture.nativeElement.querySelector('button[md-button]');
anchorElement = fixture.nativeElement.querySelector('a[md-button]');

buttonDebugElement = fixture.debugElement.query(By.css('button[md-button]'));
buttonRippleDebugElement = buttonDebugElement.query(By.directive(MdRipple));
buttonRippleInstance = buttonRippleDebugElement.injector.get<MdRipple>(MdRipple);

anchorDebugElement = fixture.debugElement.query(By.css('a[md-button]'));
anchorRippleDebugElement = anchorDebugElement.query(By.directive(MdRipple));
anchorRippleInstance = anchorRippleDebugElement.injector.get<MdRipple>(MdRipple);
});

it('should remove ripple if mdRippleDisabled input is set', () => {
expect(buttonElement.querySelectorAll('[md-ripple]').length).toBe(1);
it('should disable the ripple if mdRippleDisabled input is set', () => {
expect(buttonRippleInstance.disabled).toBeFalsy();

testComponent.rippleDisabled = true;
fixture.detectChanges();
expect(buttonElement.querySelectorAll('[md-ripple]').length).toBe(0);
});

it('should not have a ripple when the button is disabled', () => {
let buttonRipple = buttonElement.querySelector('[md-ripple]');
let anchorRipple = anchorElement.querySelector('[md-ripple]');
expect(buttonRippleInstance.disabled).toBeTruthy();
});

expect(buttonRipple).toBeTruthy('Expected an enabled button[md-button] to have a ripple');
expect(anchorRipple).toBeTruthy('Expected an enabled a[md-button] to have a ripple');
it('should disable the ripple when the button is disabled', () => {
expect(buttonRippleInstance.disabled).toBeFalsy(
'Expected an enabled button[md-button] to have an enabled ripple'
);
expect(anchorRippleInstance.disabled).toBeFalsy(
'Expected an enabled a[md-button] to have an enabled ripple'
);

testComponent.isDisabled = true;
fixture.detectChanges();

buttonRipple = buttonElement.querySelector('button [md-ripple]');
anchorRipple = anchorElement.querySelector('a [md-ripple]');

expect(buttonRipple).toBeFalsy('Expected a disabled button[md-button] not to have a ripple');
expect(anchorRipple).toBeFalsy('Expected a disabled a[md-button] not to have a ripple');
expect(buttonRippleInstance.disabled).toBeTruthy(
'Expected a disabled button[md-button] not to have an enabled ripple'
);
expect(anchorRippleInstance.disabled).toBeTruthy(
'Expected a disabled a[md-button] not to have an enabled ripple'
);
});
});
});
Expand Down

0 comments on commit fce2868

Please sign in to comment.