Skip to content

Commit e02e6e1

Browse files
committed
feat(autocomplete): allow option ripples to be disabled
For consistency with other components like `MatSelect`, these changes add the ability for all of the option ripples to be disabled by using the `disableRipple` attribute on `MatAutocomplete`.
1 parent cb2eac2 commit e02e6e1

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,39 @@ describe('MatAutocomplete', () => {
14861486
}));
14871487

14881488

1489+
it('should be able to disable option ripples', () => {
1490+
const fixture: ComponentFixture<SimpleAutocomplete> = createComponent(SimpleAutocomplete);
1491+
fixture.detectChanges();
1492+
1493+
expect(fixture.componentInstance.options.toArray().every(option => option.disableRipple!))
1494+
.toBe(false, 'Expected option ripples to be enabled');
1495+
1496+
fixture.componentInstance.disableRipple = true;
1497+
fixture.detectChanges();
1498+
1499+
expect(fixture.componentInstance.options.toArray().every(option => option.disableRipple!))
1500+
.toBe(true, 'Expected option ripples to be disabled');
1501+
});
1502+
1503+
it('should not show option ripples if they were disabled', fakeAsync(() => {
1504+
const fixture: ComponentFixture<SimpleAutocomplete> = createComponent(SimpleAutocomplete);
1505+
1506+
fixture.componentInstance.disableRipple = true;
1507+
fixture.detectChanges();
1508+
1509+
fixture.componentInstance.trigger.openPanel();
1510+
zone.simulateZoneExit();
1511+
fixture.detectChanges();
1512+
1513+
const option = overlayContainerElement.querySelector('mat-option')!;
1514+
1515+
dispatchFakeEvent(option, 'mousedown');
1516+
dispatchFakeEvent(option, 'mouseup');
1517+
1518+
expect(option.querySelectorAll('.mat-ripple-element').length).toBe(0);
1519+
}));
1520+
1521+
14891522
});
14901523

14911524
it('should have correct width when opened', () => {
@@ -1615,7 +1648,8 @@ describe('MatAutocomplete', () => {
16151648
<input matInput placeholder="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
16161649
</mat-form-field>
16171650
1618-
<mat-autocomplete class="class-one class-two" #auto="matAutocomplete" [displayWith]="displayFn">
1651+
<mat-autocomplete class="class-one class-two" #auto="matAutocomplete"
1652+
[displayWith]="displayFn" [disableRipple]="disableRipple">
16191653
<mat-option *ngFor="let state of filteredStates" [value]="state">
16201654
<span> {{ state.code }}: {{ state.name }} </span>
16211655
</mat-option>
@@ -1628,6 +1662,7 @@ class SimpleAutocomplete implements OnDestroy {
16281662
valueSub: Subscription;
16291663
floatLabel = 'auto';
16301664
width: number;
1665+
disableRipple = false;
16311666

16321667
@ViewChild(MatAutocompleteTrigger) trigger: MatAutocompleteTrigger;
16331668
@ViewChild(MatAutocomplete) panel: MatAutocomplete;

src/lib/autocomplete/autocomplete.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
98
import {
109
AfterContentInit,
1110
Component,
@@ -21,7 +20,13 @@ import {
2120
EventEmitter,
2221
Output,
2322
} from '@angular/core';
24-
import {MatOption, MatOptgroup} from '@angular/material/core';
23+
import {
24+
MatOption,
25+
MatOptgroup,
26+
MAT_OPTION_PARENT_COMPONENT,
27+
mixinDisableRipple,
28+
CanDisableRipple,
29+
} from '@angular/material/core';
2530
import {ActiveDescendantKeyManager} from '@angular/cdk/a11y';
2631

2732

@@ -40,6 +45,11 @@ export class MatAutocompleteSelectedEvent {
4045
public option: MatOption) { }
4146
}
4247

48+
// Boilerplate for applying mixins to MatAutocomplete.
49+
/** @docs-private */
50+
export class MatAutocompleteBase {}
51+
export const _MatAutocompleteMixinBase = mixinDisableRipple(MatAutocompleteBase);
52+
4353

4454
@Component({
4555
moduleId: module.id,
@@ -50,11 +60,16 @@ export class MatAutocompleteSelectedEvent {
5060
preserveWhitespaces: false,
5161
changeDetection: ChangeDetectionStrategy.OnPush,
5262
exportAs: 'matAutocomplete',
63+
inputs: ['disableRipple'],
5364
host: {
5465
'class': 'mat-autocomplete'
55-
}
66+
},
67+
providers: [
68+
{provide: MAT_OPTION_PARENT_COMPONENT, useExisting: MatAutocomplete}
69+
]
5670
})
57-
export class MatAutocomplete implements AfterContentInit {
71+
export class MatAutocomplete extends _MatAutocompleteMixinBase implements AfterContentInit,
72+
CanDisableRipple {
5873

5974
/** Manages active item in option list based on key events. */
6075
_keyManager: ActiveDescendantKeyManager<MatOption>;
@@ -103,7 +118,9 @@ export class MatAutocomplete implements AfterContentInit {
103118
/** Unique ID to be used by autocomplete trigger's "aria-owns" property. */
104119
id: string = `mat-autocomplete-${_uniqueAutocompleteIdCounter++}`;
105120

106-
constructor(private _changeDetectorRef: ChangeDetectorRef, private _elementRef: ElementRef) { }
121+
constructor(private _changeDetectorRef: ChangeDetectorRef, private _elementRef: ElementRef) {
122+
super();
123+
}
107124

108125
ngAfterContentInit() {
109126
this._keyManager = new ActiveDescendantKeyManager<MatOption>(this.options).withWrap();

0 commit comments

Comments
 (0)