Skip to content

Commit 7ae4141

Browse files
georgmuGeorg Müller
authored andcommitted
make modals type-safe
it is now possible to create a modal and return a correctly typed ComponentRef. The BaseModal class is now also typed, allowing a correctly typed output value.
1 parent ca741fd commit 7ae4141

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/modal/base-modal.class.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import {
1313
@Directive({
1414
selector: "[cdsBaseModal], [ibmBaseModal]"
1515
})
16-
export class BaseModal {
16+
export class BaseModal<R = any> {
1717
/**
1818
* Base event emitter to propagate close events
1919
*/
20-
@Output() close = new EventEmitter();
20+
@Output() close = new EventEmitter<R>();
2121

2222
/**
2323
* Controls the open state of the modal
@@ -27,7 +27,7 @@ export class BaseModal {
2727
/**
2828
* Default method to handle closing the modal
2929
*/
30-
closeModal(): void {
31-
this.close.emit();
30+
closeModal(value?: R): void {
31+
this.close.emit(value);
3232
}
3333
}

src/modal/base-modal.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import {
33
Injector,
44
Injectable,
55
inject,
6-
EnvironmentInjector
6+
EnvironmentInjector,
7+
Type
78
} from "@angular/core";
89
import { PlaceholderService } from "carbon-components-angular/placeholder";
910
import { tap, delay } from "rxjs/operators";
11+
import {BaseModal} from "./base-modal.class";
1012

1113

1214
/**
@@ -44,7 +46,8 @@ export class BaseModalService {
4446
* Creates and renders the modal component that is passed in.
4547
* `inputs` is an optional parameter of `data` that can be passed to the `Modal` component.
4648
*/
47-
create<T>(data: { component: any, inputs?: any }): ComponentRef<any> {
49+
create<T extends BaseModal, I extends Record<any, unknown> = Record<string, unknown>>(
50+
data: { component: Type<T>, inputs?: I }): ComponentRef<T> {
4851
let defaults = { inputs: {} };
4952
data = Object.assign({}, defaults, data);
5053

src/placeholder/placeholder.service.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
ViewContainerRef,
44
Injector,
55
EnvironmentInjector,
6-
inject
6+
inject,
7+
Type
78
} from "@angular/core";
89
import { Injectable } from "@angular/core";
910

@@ -35,24 +36,24 @@ export class PlaceholderService {
3536
/**
3637
* Creates and returns component in the view.
3738
*/
38-
createComponent(
39-
component: ComponentRef<any>,
39+
createComponent<T>(
40+
component: Type<T>,
4041
injector: Injector,
4142
id?: any,
4243
environment: EnvironmentInjector = undefined
43-
): ComponentRef<any> {
44+
): ComponentRef<T> {
4445
if (id) {
4546
if (!this.viewContainerMap.has(id)) {
4647
console.error(`No view container with id ${id} found`);
4748
return;
4849
}
49-
return this.viewContainerMap.get(id).createComponent(component as any, { index: this.viewContainerMap.size, injector });
50+
return this.viewContainerMap.get(id).createComponent(component, { index: this.viewContainerMap.size, injector });
5051
}
5152
if (!this.viewContainerRef) {
5253
console.error("No view container defined! Likely due to a missing `cds-placeholder`");
5354
return;
5455
}
55-
return this.viewContainerRef.createComponent(component as any,
56+
return this.viewContainerRef.createComponent(component,
5657
{
5758
index: this.viewContainerRef.length,
5859
injector,

0 commit comments

Comments
 (0)