Skip to content

Commit c2c46b5

Browse files
crisbetotinayuangao
authored andcommitted
feat(autocomplete): allow option ripples to be disabled (#8851)
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 1e2b96e commit c2c46b5

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,6 @@ describe('MatAutocomplete', () => {
15211521
expect(panel.classList).toContain('class-two');
15221522
}));
15231523

1524-
15251524
it('should reset correctly when closed programmatically', fakeAsync(() => {
15261525
TestBed.overrideProvider(MAT_AUTOCOMPLETE_SCROLL_STRATEGY, {
15271526
useFactory: (overlay: Overlay) => () => overlay.scrollStrategies.close(),
@@ -1673,7 +1672,8 @@ describe('MatAutocomplete', () => {
16731672
<input matInput placeholder="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
16741673
</mat-form-field>
16751674
1676-
<mat-autocomplete class="class-one class-two" #auto="matAutocomplete" [displayWith]="displayFn">
1675+
<mat-autocomplete class="class-one class-two" #auto="matAutocomplete"
1676+
[displayWith]="displayFn" [disableRipple]="disableRipple">
16771677
<mat-option *ngFor="let state of filteredStates" [value]="state">
16781678
<span> {{ state.code }}: {{ state.name }} </span>
16791679
</mat-option>
@@ -1686,6 +1686,7 @@ class SimpleAutocomplete implements OnDestroy {
16861686
valueSub: Subscription;
16871687
floatLabel = 'auto';
16881688
width: number;
1689+
disableRipple = false;
16891690

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