Skip to content

Commit 7e1560d

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 f95f832 commit 7e1560d

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,28 +1519,58 @@ describe('MatAutocomplete', () => {
15191519
expect(panel.classList).toContain('class-two');
15201520
}));
15211521

1522-
1523-
it('should reset correctly when closed programmatically', async(() => {
1522+
it('should reset correctly when closed programmatically', fakeAsync(() => {
15241523
TestBed.overrideProvider(MAT_AUTOCOMPLETE_SCROLL_STRATEGY, {
15251524
useFactory: (overlay: Overlay) => () => overlay.scrollStrategies.close(),
15261525
deps: [Overlay]
15271526
});
15281527

1529-
const fixture = TestBed.createComponent(SimpleAutocomplete);
1528+
const fixture = createComponent(SimpleAutocomplete);
15301529
fixture.detectChanges();
15311530
const trigger = fixture.componentInstance.trigger;
15321531

15331532
trigger.openPanel();
15341533
fixture.detectChanges();
1534+
zone.simulateZoneExit();
15351535

1536-
fixture.whenStable().then(() => {
1537-
expect(trigger.panelOpen).toBe(true, 'Expected panel to be open.');
1536+
expect(trigger.panelOpen).toBe(true, 'Expected panel to be open.');
15381537

1539-
scrolledSubject.next();
1540-
fixture.detectChanges();
1538+
scrolledSubject.next();
1539+
fixture.detectChanges();
15411540

1542-
expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
1543-
});
1541+
expect(trigger.panelOpen).toBe(false, 'Expected panel to be closed.');
1542+
}));
1543+
1544+
it('should be able to disable option ripples', () => {
1545+
const fixture: ComponentFixture<SimpleAutocomplete> = createComponent(SimpleAutocomplete);
1546+
fixture.detectChanges();
1547+
1548+
expect(fixture.componentInstance.options.toArray().every(option => option.disableRipple!))
1549+
.toBe(false, 'Expected option ripples to be enabled');
1550+
1551+
fixture.componentInstance.disableRipple = true;
1552+
fixture.detectChanges();
1553+
1554+
expect(fixture.componentInstance.options.toArray().every(option => option.disableRipple!))
1555+
.toBe(true, 'Expected option ripples to be disabled');
1556+
});
1557+
1558+
it('should not show option ripples if they were disabled', fakeAsync(() => {
1559+
const fixture: ComponentFixture<SimpleAutocomplete> = createComponent(SimpleAutocomplete);
1560+
1561+
fixture.componentInstance.disableRipple = true;
1562+
fixture.detectChanges();
1563+
1564+
fixture.componentInstance.trigger.openPanel();
1565+
zone.simulateZoneExit();
1566+
fixture.detectChanges();
1567+
1568+
const option = overlayContainerElement.querySelector('mat-option')!;
1569+
1570+
dispatchFakeEvent(option, 'mousedown');
1571+
dispatchFakeEvent(option, 'mouseup');
1572+
1573+
expect(option.querySelectorAll('.mat-ripple-element').length).toBe(0);
15441574
}));
15451575

15461576
});
@@ -1672,7 +1702,8 @@ describe('MatAutocomplete', () => {
16721702
<input matInput placeholder="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
16731703
</mat-form-field>
16741704
1675-
<mat-autocomplete class="class-one class-two" #auto="matAutocomplete" [displayWith]="displayFn">
1705+
<mat-autocomplete class="class-one class-two" #auto="matAutocomplete"
1706+
[displayWith]="displayFn" [disableRipple]="disableRipple">
16761707
<mat-option *ngFor="let state of filteredStates" [value]="state">
16771708
<span> {{ state.code }}: {{ state.name }} </span>
16781709
</mat-option>
@@ -1685,6 +1716,7 @@ class SimpleAutocomplete implements OnDestroy {
16851716
valueSub: Subscription;
16861717
floatLabel = 'auto';
16871718
width: number;
1719+
disableRipple = false;
16881720

16891721
@ViewChild(MatAutocompleteTrigger) trigger: MatAutocompleteTrigger;
16901722
@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)