Skip to content

Commit

Permalink
feat(material/bottom-sheet): expose rendered ComponentRef
Browse files Browse the repository at this point in the history
Exposes the `ComponentRef` to the component that is rendered inside of a bottom sheet container. This allows users to interact with the component's inputs.
  • Loading branch information
crisbeto committed Jul 31, 2023
1 parent 30c3e13 commit 956aec2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/material/bottom-sheet/bottom-sheet-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ComponentRef} from '@angular/core';
import {DialogRef} from '@angular/cdk/dialog';
import {ESCAPE, hasModifierKey} from '@angular/cdk/keycodes';
import {merge, Observable, Subject} from 'rxjs';
Expand All @@ -22,6 +23,14 @@ export class MatBottomSheetRef<T = any, R = any> {
return this._ref.componentInstance!;
}

/**
* `ComponentRef` of the component opened into the bottom sheet. Will be
* null when the bottom sheet is opened using a `TemplateRef`.
*/
get componentRef(): ComponentRef<T> | null {
return this._ref.componentRef;
}

/**
* Instance of the component into which the bottom sheet content is projected.
* @docs-private
Expand Down
2 changes: 2 additions & 0 deletions src/material/bottom-sheet/bottom-sheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Location} from '@angular/common';
import {SpyLocation} from '@angular/common/testing';
import {
Component,
ComponentRef,
Directive,
Inject,
Injector,
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('MatBottomSheet', () => {

expect(overlayContainerElement.textContent).toContain('Pizza');
expect(bottomSheetRef.instance instanceof PizzaMsg).toBe(true);
expect(bottomSheetRef.componentRef instanceof ComponentRef).toBe(true);
expect(bottomSheetRef.instance.bottomSheetRef).toBe(bottomSheetRef);
});

Expand Down
2 changes: 2 additions & 0 deletions tools/public_api_guard/material/bottom-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AnimationTriggerMetadata } from '@angular/animations';
import { BreakpointObserver } from '@angular/cdk/layout';
import { CdkDialogContainer } from '@angular/cdk/dialog';
import { ChangeDetectorRef } from '@angular/core';
import { ComponentRef } from '@angular/core';
import { ComponentType } from '@angular/cdk/portal';
import { DialogConfig } from '@angular/cdk/dialog';
import { DialogRef } from '@angular/cdk/dialog';
Expand Down Expand Up @@ -117,6 +118,7 @@ export class MatBottomSheetRef<T = any, R = any> {
afterDismissed(): Observable<R | undefined>;
afterOpened(): Observable<void>;
backdropClick(): Observable<MouseEvent>;
get componentRef(): ComponentRef<T> | null;
containerInstance: MatBottomSheetContainer;
disableClose: boolean | undefined;
dismiss(result?: R): void;
Expand Down

0 comments on commit 956aec2

Please sign in to comment.