Skip to content

Commit addf1ce

Browse files
devversionkara
authored andcommitted
feat(option): support for disableRipple binding (#5915)
1 parent 281de25 commit addf1ce

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

src/lib/core/option/option.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
</span>
77

88
<ng-content></ng-content>
9-
<div class="mat-option-ripple" *ngIf="!disabled" md-ripple [mdRippleTrigger]="_getHostElement()">
9+
<div class="mat-option-ripple" md-ripple
10+
[mdRippleTrigger]="_getHostElement()"
11+
[mdRippleDisabled]="disabled || disableRipple">
1012
</div>

src/lib/core/option/option.spec.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
2+
import {Component, DebugElement} from '@angular/core';
3+
import {By} from '@angular/platform-browser';
4+
import {dispatchFakeEvent} from '@angular/cdk/testing';
5+
import {MdOption, MdOptionModule} from './index';
6+
7+
describe('MdOption component', () => {
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
imports: [MdOptionModule],
12+
declarations: [OptionWithDisableRipple]
13+
}).compileComponents();
14+
}));
15+
16+
describe('ripples', () => {
17+
let fixture: ComponentFixture<OptionWithDisableRipple>;
18+
let optionDebugElement: DebugElement;
19+
let optionNativeElement: HTMLElement;
20+
let optionInstance: MdOption;
21+
22+
beforeEach(() => {
23+
fixture = TestBed.createComponent(OptionWithDisableRipple);
24+
fixture.detectChanges();
25+
26+
optionDebugElement = fixture.debugElement.query(By.directive(MdOption));
27+
optionNativeElement = optionDebugElement.nativeElement;
28+
optionInstance = optionDebugElement.componentInstance;
29+
});
30+
31+
it('should show ripples by default', () => {
32+
expect(optionInstance.disableRipple).toBe(false, 'Expected ripples to be enabled by default');
33+
expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length)
34+
.toBe(0, 'Expected no ripples to show up initially');
35+
36+
dispatchFakeEvent(optionNativeElement, 'mousedown');
37+
dispatchFakeEvent(optionNativeElement, 'mouseup');
38+
39+
expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length)
40+
.toBe(1, 'Expected one ripple to show up after a fake click.');
41+
});
42+
43+
it('should not show ripples if the option is disabled', () => {
44+
expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length)
45+
.toBe(0, 'Expected no ripples to show up initially');
46+
47+
fixture.componentInstance.disabled = true;
48+
fixture.detectChanges();
49+
50+
dispatchFakeEvent(optionNativeElement, 'mousedown');
51+
dispatchFakeEvent(optionNativeElement, 'mouseup');
52+
53+
expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length)
54+
.toBe(0, 'Expected no ripples to show up after click on a disabled option.');
55+
});
56+
57+
it('should not show ripples if the ripples are disabled using disableRipple', () => {
58+
expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length)
59+
.toBe(0, 'Expected no ripples to show up initially');
60+
61+
fixture.componentInstance.disableRipple = true;
62+
fixture.detectChanges();
63+
64+
dispatchFakeEvent(optionNativeElement, 'mousedown');
65+
dispatchFakeEvent(optionNativeElement, 'mouseup');
66+
67+
expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length)
68+
.toBe(0, 'Expected no ripples to show up after click when ripples are disabled.');
69+
});
70+
71+
});
72+
73+
});
74+
75+
@Component({
76+
template: `<md-option [disableRipple]="disableRipple" [disabled]="disabled"></md-option>`
77+
})
78+
export class OptionWithDisableRipple {
79+
disableRipple: boolean;
80+
disabled: boolean;
81+
}

src/lib/core/option/option.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {ENTER, SPACE} from '../keyboard/keycodes';
2222
import {coerceBooleanProperty} from '@angular/cdk';
2323
import {MATERIAL_COMPATIBILITY_MODE} from '../../core/compatibility/compatibility';
2424
import {MdOptgroup} from './optgroup';
25+
import {CanDisableRipple, mixinDisableRipple} from '../common-behaviors/disable-ripple';
2526

2627
/**
2728
* Option IDs need to be unique across components, so this counter exists outside of
@@ -34,13 +35,19 @@ export class MdOptionSelectionChange {
3435
constructor(public source: MdOption, public isUserInput = false) { }
3536
}
3637

38+
// Boilerplate for applying mixins to MdOption.
39+
/** @docs-private */
40+
export class MdOptionBase {}
41+
export const _MdOptionMixinBase = mixinDisableRipple(MdOptionBase);
42+
3743

3844
/**
3945
* Single option inside of a `<md-select>` element.
4046
*/
4147
@Component({
4248
moduleId: module.id,
4349
selector: 'md-option, mat-option',
50+
inputs: ['disableRipple'],
4451
host: {
4552
'role': 'option',
4653
'[attr.tabindex]': '_getTabIndex()',
@@ -59,7 +66,7 @@ export class MdOptionSelectionChange {
5966
encapsulation: ViewEncapsulation.None,
6067
changeDetection: ChangeDetectionStrategy.OnPush,
6168
})
62-
export class MdOption {
69+
export class MdOption extends _MdOptionMixinBase implements CanDisableRipple {
6370
private _selected: boolean = false;
6471
private _active: boolean = false;
6572
private _multiple: boolean = false;
@@ -99,7 +106,10 @@ export class MdOption {
99106
private _element: ElementRef,
100107
private _changeDetectorRef: ChangeDetectorRef,
101108
@Optional() public readonly group: MdOptgroup,
102-
@Optional() @Inject(MATERIAL_COMPATIBILITY_MODE) public _isCompatibilityMode: boolean) {}
109+
@Optional() @Inject(MATERIAL_COMPATIBILITY_MODE) public _isCompatibilityMode: boolean
110+
) {
111+
super();
112+
}
103113

104114
/**
105115
* Whether or not the option is currently active and ready to be selected.

0 commit comments

Comments
 (0)