Skip to content

Commit

Permalink
feat(modal): set strong typing in componentInstance getter property
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljota committed Oct 18, 2018
1 parent d952f9c commit 749df8d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/modal/modal-ref.ts
Expand Up @@ -24,15 +24,15 @@ export class NgbActiveModal {
/**
* A reference to a newly opened modal.
*/
export class NgbModalRef {
export class NgbModalRef<T = any> {
private _resolve: (result?: any) => void;
private _reject: (reason?: any) => void;

/**
* The instance of component used as modal's content.
* Undefined when a TemplateRef is used as modal's content.
*/
get componentInstance(): any {
get componentInstance(): T extends new (...args: any[]) => any ? InstanceType<T> : undefined {
if (this._contentRef.componentRef) {
return this._contentRef.componentRef.instance;
}
Expand Down
6 changes: 3 additions & 3 deletions src/modal/modal-stack.ts
Expand Up @@ -39,7 +39,7 @@ export class NgbModalStack {
});
}

open(moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: any, options): NgbModalRef {
open<T>(moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: T, options): NgbModalRef<T> {
const containerEl =
isDefined(options.container) ? this._document.querySelector(options.container) : this._document.body;
const renderer = this._rendererFactory.createRenderer(null, null);
Expand All @@ -61,7 +61,7 @@ export class NgbModalStack {
let backdropCmptRef: ComponentRef<NgbModalBackdrop> =
options.backdrop !== false ? this._attachBackdrop(moduleCFR, containerEl) : null;
let windowCmptRef: ComponentRef<NgbModalWindow> = this._attachWindowComponent(moduleCFR, containerEl, contentRef);
let ngbModalRef: NgbModalRef = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef, options.beforeDismiss);
let ngbModalRef: NgbModalRef<T> = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef, options.beforeDismiss);

this._registerModalRef(ngbModalRef);
this._registerWindowCmpt(windowCmptRef);
Expand Down Expand Up @@ -159,7 +159,7 @@ export class NgbModalStack {
return new ContentRef([[componentRef.location.nativeElement]], componentRef.hostView, componentRef);
}

private _registerModalRef(ngbModalRef: NgbModalRef) {
private _registerModalRef<T>(ngbModalRef: NgbModalRef<T>) {
const unregisterModalRef = () => {
const index = this._modalRefs.indexOf(ngbModalRef);
if (index > -1) {
Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal.spec.ts
Expand Up @@ -914,7 +914,7 @@ export class WithFirstFocusableModalCmpt {
})
class TestComponent {
name = 'World';
openedModal: NgbModalRef;
openedModal: NgbModalRef<string>;
show = true;
@ViewChild('content') tplContent;
@ViewChild('destroyableContent') tplDestroyableContent;
Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal.ts
Expand Up @@ -20,7 +20,7 @@ export class NgbModal {
* components can be injected with an instance of the NgbActiveModal class. You can use methods on the
* NgbActiveModal class to close / dismiss modals from "inside" of a component.
*/
open(content: any, options: NgbModalOptions = {}): NgbModalRef {
open<T>(content: T, options: NgbModalOptions = {}): NgbModalRef<T> {
const combinedOptions = Object.assign({}, this._config, options);
return this._modalStack.open(this._moduleCFR, this._injector, content, combinedOptions);
}
Expand Down

0 comments on commit 749df8d

Please sign in to comment.