Skip to content
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

feat(slide-toggle): add option to disable ripple #3195

Merged
merged 1 commit into from
Feb 23, 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
2 changes: 1 addition & 1 deletion src/lib/slide-toggle/slide-toggle.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="mat-slide-toggle-ripple" md-ripple
[mdRippleTrigger]="label"
[mdRippleCentered]="true"
[mdRippleDisabled]="disabled"
[mdRippleDisabled]="disableRipple || disabled"
[mdRippleSpeedFactor]="0.3">
</div>
</div>
Expand Down
30 changes: 30 additions & 0 deletions src/lib/slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,27 @@ describe('MdSlideToggle', () => {
expect(slideToggleElement.classList).toContain('mat-slide-toggle-label-before');
});

it('should show ripples on label mousedown', () => {
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length).toBe(0);

dispatchFakeEvent(labelElement, 'mousedown');
dispatchFakeEvent(labelElement, 'mouseup');

expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length).toBe(1);
});

it('should not show ripples when disableRipple is set', () => {
testComponent.disableRipple = true;
fixture.detectChanges();

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

dispatchFakeEvent(labelElement, 'mousedown');
dispatchFakeEvent(labelElement, 'mouseup');

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

});

describe('custom template', () => {
Expand Down Expand Up @@ -620,6 +641,7 @@ function dispatchFocusChangeEvent(eventName: string, element: HTMLElement): void
[aria-labelledby]="slideLabelledBy"
[tabIndex]="slideTabindex"
[labelPosition]="labelPosition"
[disableRipple]="disableRipple"
(change)="onSlideChange($event)"
(click)="onSlideClick($event)">

Expand All @@ -630,6 +652,7 @@ function dispatchFocusChangeEvent(eventName: string, element: HTMLElement): void
class SlideToggleTestApp {
isDisabled: boolean = false;
isRequired: boolean = false;
disableRipple: boolean = false;
slideModel: boolean = false;
slideChecked: boolean = false;
slideColor: string;
Expand Down Expand Up @@ -671,3 +694,10 @@ class SlideToggleFormsTestApp {
class SlideToggleWithFormControl {
formControl = new FormControl();
}

// TODO(devversion): replace with global utility once pull request #2943 is merged.
function dispatchFakeEvent(element: HTMLElement, eventName: string): void {
let event = document.createEvent('Event');
event.initEvent(eventName, true, true);
element.dispatchEvent(event);
}
6 changes: 6 additions & 0 deletions src/lib/slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor {
private _slideRenderer: SlideToggleRenderer = null;
private _disabled: boolean = false;
private _required: boolean = false;
private _disableRipple: boolean = false;

// Needs to be public to support AOT compilation (as host binding).
_hasFocus: boolean = false;
Expand Down Expand Up @@ -107,6 +108,11 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor {
get required(): boolean { return this._required; }
set required(value) { this._required = coerceBooleanProperty(value); }

/** Whether the ripple effect for this slide-toggle is disabled. */
@Input()
get disableRipple(): boolean { return this._disableRipple; }
set disableRipple(value) { this._disableRipple = coerceBooleanProperty(value); }

private _change: EventEmitter<MdSlideToggleChange> = new EventEmitter<MdSlideToggleChange>();
/** An event will be dispatched each time the slide-toggle changes its value. */
@Output() change: Observable<MdSlideToggleChange> = this._change.asObservable();
Expand Down