Skip to content

Commit fce2868

Browse files
willshowelltinayuangao
authored andcommitted
fix(button): complete ripple when button becomes disabled (#4372)
* Do not destroy the ripple when it is disabled * Update tests
1 parent c45dee2 commit fce2868

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

src/demo-app/button/button-demo.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,8 @@
101101
<button md-button>Check<md-icon class="md-24">favorite</md-icon></button>
102102
<button md-button>Last<md-icon>navigate_before</md-icon></button>
103103
</section>
104+
<section>
105+
<button md-raised-button [disabled]="toggleDisable" (click)="toggleDisable = true">Disable</button>
106+
<button md-button [disabled]="!toggleDisable" (click)="toggleDisable = false">Disable</button>
107+
</section>
104108
</div>

src/demo-app/button/button-demo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ import {Component} from '@angular/core';
1010
export class ButtonDemo {
1111
isDisabled: boolean = false;
1212
clickCounter: number = 0;
13+
toggleDisable: boolean = false;
1314
}

src/lib/button/button.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<span class="mat-button-wrapper"><ng-content></ng-content></span>
2-
<div md-ripple *ngIf="!_isRippleDisabled()" class="mat-button-ripple"
2+
<div md-ripple class="mat-button-ripple"
33
[class.mat-button-ripple-round]="_isRoundButton || _isIconButton"
4+
[mdRippleDisabled]="_isRippleDisabled()"
45
[mdRippleCentered]="_isIconButton"
56
[mdRippleTrigger]="_getHostElement()"></div>
67
<div class="mat-button-focus-overlay"></div>

src/lib/button/button.spec.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
2-
import {Component} from '@angular/core';
2+
import {Component, DebugElement} from '@angular/core';
33
import {By} from '@angular/platform-browser';
44
import {MdButtonModule} from './index';
55
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
66
import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
7+
import {MdRipple} from '../core/ripple/index';
78

89

910
describe('MdButton', () => {
@@ -156,41 +157,54 @@ describe('MdButton', () => {
156157
describe('button ripples', () => {
157158
let fixture: ComponentFixture<TestApp>;
158159
let testComponent: TestApp;
159-
let buttonElement: HTMLButtonElement;
160-
let anchorElement: HTMLAnchorElement;
160+
let buttonDebugElement: DebugElement;
161+
let buttonRippleDebugElement: DebugElement;
162+
let buttonRippleInstance: MdRipple;
163+
let anchorDebugElement: DebugElement;
164+
let anchorRippleDebugElement: DebugElement;
165+
let anchorRippleInstance: MdRipple;
161166

162167
beforeEach(() => {
163168
fixture = TestBed.createComponent(TestApp);
164169
fixture.detectChanges();
165170

166171
testComponent = fixture.componentInstance;
167-
buttonElement = fixture.nativeElement.querySelector('button[md-button]');
168-
anchorElement = fixture.nativeElement.querySelector('a[md-button]');
172+
173+
buttonDebugElement = fixture.debugElement.query(By.css('button[md-button]'));
174+
buttonRippleDebugElement = buttonDebugElement.query(By.directive(MdRipple));
175+
buttonRippleInstance = buttonRippleDebugElement.injector.get<MdRipple>(MdRipple);
176+
177+
anchorDebugElement = fixture.debugElement.query(By.css('a[md-button]'));
178+
anchorRippleDebugElement = anchorDebugElement.query(By.directive(MdRipple));
179+
anchorRippleInstance = anchorRippleDebugElement.injector.get<MdRipple>(MdRipple);
169180
});
170181

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

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

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

183-
expect(buttonRipple).toBeTruthy('Expected an enabled button[md-button] to have a ripple');
184-
expect(anchorRipple).toBeTruthy('Expected an enabled a[md-button] to have a ripple');
191+
it('should disable the ripple when the button is disabled', () => {
192+
expect(buttonRippleInstance.disabled).toBeFalsy(
193+
'Expected an enabled button[md-button] to have an enabled ripple'
194+
);
195+
expect(anchorRippleInstance.disabled).toBeFalsy(
196+
'Expected an enabled a[md-button] to have an enabled ripple'
197+
);
185198

186199
testComponent.isDisabled = true;
187200
fixture.detectChanges();
188201

189-
buttonRipple = buttonElement.querySelector('button [md-ripple]');
190-
anchorRipple = anchorElement.querySelector('a [md-ripple]');
191-
192-
expect(buttonRipple).toBeFalsy('Expected a disabled button[md-button] not to have a ripple');
193-
expect(anchorRipple).toBeFalsy('Expected a disabled a[md-button] not to have a ripple');
202+
expect(buttonRippleInstance.disabled).toBeTruthy(
203+
'Expected a disabled button[md-button] not to have an enabled ripple'
204+
);
205+
expect(anchorRippleInstance.disabled).toBeTruthy(
206+
'Expected a disabled a[md-button] not to have an enabled ripple'
207+
);
194208
});
195209
});
196210
});

0 commit comments

Comments
 (0)