Skip to content

fix(material/autocomplete): allow overlay backdrop by setting hasBackdrop option #30631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions goldens/material/autocomplete/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export interface MatAutocompleteActivatedEvent {
export interface MatAutocompleteDefaultOptions {
autoActiveFirstOption?: boolean;
autoSelectActiveOption?: boolean;
backdropClass?: string;
hasBackdrop?: boolean;
hideSingleSelectionIndicator?: boolean;
overlayPanelClass?: string | string[];
requireSelection?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@ export class MatAutocompleteTrigger
scrollStrategy: this._scrollStrategy(),
width: this._getPanelWidth(),
direction: this._dir ?? undefined,
hasBackdrop: this._defaults?.hasBackdrop,
backdropClass: this._defaults?.backdropClass,
panelClass: this._defaults?.overlayPanelClass,
});
}
Expand Down
48 changes: 48 additions & 0 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3014,6 +3014,54 @@ describe('MatAutocomplete', () => {
});
});

describe('with backdrop in options', () => {
it('should not contain backdrop by default', fakeAsync(() => {
const fixture = createComponent(SimpleAutocomplete, []);
fixture.detectChanges();
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();

tick(500);

expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
}));

it('should be able to add the backdrop using hasBackdrop option', fakeAsync(() => {
const fixture = createComponent(SimpleAutocomplete, [
{
provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
useValue: {hasBackdrop: true},
},
]);
fixture.detectChanges();
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();

tick(500);

expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
}));
});

describe('with hasBackdrop and backdropClass in options', () => {
it('should be able to configure custom backdrop class', fakeAsync(() => {
const fixture = createComponent(SimpleAutocomplete, [
{
provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
useValue: {backdropClass: 'my-custom-backdrop-class', hasBackdrop: true},
},
]);
fixture.detectChanges();
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();

tick(500);

const cdkPanelElement = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
expect(cdkPanelElement?.classList).toContain('my-custom-backdrop-class');
}));
});

describe('misc', () => {
it('should allow basic use without any forms directives', () => {
expect(() => {
Expand Down
7 changes: 7 additions & 0 deletions src/material/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export interface MatAutocompleteDefaultOptions {
*/
requireSelection?: boolean;

/** Class to be applied to the autocomplete's backdrop. */
backdropClass?: string;

/** Whether the autocomplete has a backdrop. */
hasBackdrop?: boolean;

/** Class or list of classes to be applied to the autocomplete's overlay panel. */
overlayPanelClass?: string | string[];

Expand Down Expand Up @@ -97,6 +103,7 @@ export function MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY(): MatAutocompleteDefau
autoSelectActiveOption: false,
hideSingleSelectionIndicator: false,
requireSelection: false,
hasBackdrop: false,
};
}

Expand Down
Loading