Skip to content

Commit 097f49d

Browse files
fix(material/autocomplete): allow overlay backdrop by setting hasBackdrop option (#30631)
Currently when we open the panel, backdrop is not allowed and is not inline with `mat-select`, `mat-menu`, etc. This fix will align autocomplete with those components and give an option to configure it. Fixes #30457
1 parent e482240 commit 097f49d

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

goldens/material/autocomplete/index.api.md

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ export interface MatAutocompleteActivatedEvent {
131131
export interface MatAutocompleteDefaultOptions {
132132
autoActiveFirstOption?: boolean;
133133
autoSelectActiveOption?: boolean;
134+
backdropClass?: string;
135+
hasBackdrop?: boolean;
134136
hideSingleSelectionIndicator?: boolean;
135137
overlayPanelClass?: string | string[];
136138
requireSelection?: boolean;

src/material/autocomplete/autocomplete-trigger.ts

+2
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,8 @@ export class MatAutocompleteTrigger
900900
scrollStrategy: this._scrollStrategy(),
901901
width: this._getPanelWidth(),
902902
direction: this._dir ?? undefined,
903+
hasBackdrop: this._defaults?.hasBackdrop,
904+
backdropClass: this._defaults?.backdropClass,
903905
panelClass: this._defaults?.overlayPanelClass,
904906
});
905907
}

src/material/autocomplete/autocomplete.spec.ts

+48
Original file line numberDiff line numberDiff line change
@@ -3014,6 +3014,54 @@ describe('MatAutocomplete', () => {
30143014
});
30153015
});
30163016

3017+
describe('with backdrop in options', () => {
3018+
it('should not contain backdrop by default', fakeAsync(() => {
3019+
const fixture = createComponent(SimpleAutocomplete, []);
3020+
fixture.detectChanges();
3021+
fixture.componentInstance.trigger.openPanel();
3022+
fixture.detectChanges();
3023+
3024+
tick(500);
3025+
3026+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
3027+
}));
3028+
3029+
it('should be able to add the backdrop using hasBackdrop option', fakeAsync(() => {
3030+
const fixture = createComponent(SimpleAutocomplete, [
3031+
{
3032+
provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
3033+
useValue: {hasBackdrop: true},
3034+
},
3035+
]);
3036+
fixture.detectChanges();
3037+
fixture.componentInstance.trigger.openPanel();
3038+
fixture.detectChanges();
3039+
3040+
tick(500);
3041+
3042+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
3043+
}));
3044+
});
3045+
3046+
describe('with hasBackdrop and backdropClass in options', () => {
3047+
it('should be able to configure custom backdrop class', fakeAsync(() => {
3048+
const fixture = createComponent(SimpleAutocomplete, [
3049+
{
3050+
provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
3051+
useValue: {backdropClass: 'my-custom-backdrop-class', hasBackdrop: true},
3052+
},
3053+
]);
3054+
fixture.detectChanges();
3055+
fixture.componentInstance.trigger.openPanel();
3056+
fixture.detectChanges();
3057+
3058+
tick(500);
3059+
3060+
const cdkPanelElement = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
3061+
expect(cdkPanelElement?.classList).toContain('my-custom-backdrop-class');
3062+
}));
3063+
});
3064+
30173065
describe('misc', () => {
30183066
it('should allow basic use without any forms directives', () => {
30193067
expect(() => {

src/material/autocomplete/autocomplete.ts

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ export interface MatAutocompleteDefaultOptions {
7070
*/
7171
requireSelection?: boolean;
7272

73+
/** Class to be applied to the autocomplete's backdrop. */
74+
backdropClass?: string;
75+
76+
/** Whether the autocomplete has a backdrop. */
77+
hasBackdrop?: boolean;
78+
7379
/** Class or list of classes to be applied to the autocomplete's overlay panel. */
7480
overlayPanelClass?: string | string[];
7581

@@ -97,6 +103,7 @@ export function MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY(): MatAutocompleteDefau
97103
autoSelectActiveOption: false,
98104
hideSingleSelectionIndicator: false,
99105
requireSelection: false,
106+
hasBackdrop: false,
100107
};
101108
}
102109

0 commit comments

Comments
 (0)