Skip to content
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

fix(angular): fix change detection on modal when using a TemplateRef #714

Merged
merged 1 commit into from
Aug 21, 2023
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
3 changes: 2 additions & 1 deletion packages/angular/src/modal/modal.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
* LICENSE file in the root directory of this source tree.
*/

import { TemplateRef, Type } from '@angular/core';
import { ModalConfig as IxModalConfig } from '@siemens/ix';

export type ModalConfig<TDATA = any> = Omit<IxModalConfig, 'content'> & {
content: any;
content: TemplateRef<unknown> | Type<unknown>;
data?: TDATA;
};
15 changes: 14 additions & 1 deletion packages/angular/src/modal/modal.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ jest.mock('@siemens/ix', () => ({
}));

test('should create modal by templateRef', () => {
const appRefMock = {
attachView: jest.fn(),
};
const createEmbeddedViewMock = jest.fn((_: { $implicit: any }) => ({
rootNodes: [{}],
detectChanges: jest.fn(),
}));
const modalService = new ModalService({} as any, {} as any, {} as any);
const modalService = new ModalService(
appRefMock as any,
{} as any,
{} as any
);
modalService.open({
content: {
createEmbeddedView: createEmbeddedViewMock,
Expand All @@ -46,6 +53,7 @@ test('should create modal by templateRef', () => {
dismiss: expect.any(Function),
},
});
expect(appRefMock.attachView).toHaveBeenCalled();
});

test('should create modal by component typ', async () => {
Expand All @@ -58,6 +66,11 @@ test('should create modal by component typ', async () => {
rootNodes: [jest.fn()],
detectChanges: jest.fn(),
},
injector: {
get: jest.fn(() => ({
nativeElement: {}
})),
},
})),
};
const componentFactoryMock = {
Expand Down
59 changes: 32 additions & 27 deletions packages/angular/src/modal/modal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import {
ApplicationRef,
ComponentFactoryResolver,
EmbeddedViewRef,
ElementRef,
Injectable,
Injector,
TemplateRef,
Type,
ViewRef,
} from '@angular/core';
import { closeModal, dismissModal, showModal } from '@siemens/ix';
import { InternalIxActiveModal, IxActiveModal } from './modal-ref';
Expand Down Expand Up @@ -43,10 +45,15 @@ export class ModalService {
};

if (config.content instanceof Type) {
return this.createContentByComponentType<TData, TReason>(config, context);
return this.createContentByComponentType<TData, TReason>(
config.content,
config,
context
);
}

const modalInstance = await this.createContentByTemplateRef<TData, TReason>(
config.content,
config,
context
);
Expand All @@ -55,14 +62,14 @@ export class ModalService {
}

private async createContentByComponentType<TData = any, TReason = any>(
componentType: Type<unknown>,
config: ModalConfig<TData>,
context: ModalContext<TData>
) {
const activeModal = new InternalIxActiveModal<TData>(context.data);

const modalFactory = this.componentFactoryResolver.resolveComponentFactory(
config.content
);
const modalFactory =
this.componentFactoryResolver.resolveComponentFactory(componentType);

const modalInjector = Injector.create({
providers: [
Expand All @@ -77,10 +84,12 @@ export class ModalService {
const instance = modalFactory.create(modalInjector);
this.appRef.attachView(instance.hostView);

const embeddedView = instance.hostView as EmbeddedViewRef<any>;
const element = instance.injector.get(ElementRef);

const modalInstance = await this.createModalInstance<TData, TReason>(
context,
embeddedView,
element.nativeElement,
instance.hostView,
config
);

Expand All @@ -90,55 +99,51 @@ export class ModalService {
}

private async createContentByTemplateRef<TData = any, TReason = any>(
templateRef: TemplateRef<unknown>,
config: ModalConfig<TData>,
context: {
close: ((result: any) => void) | null;
dismiss: ((result?: any) => void) | null;
data?: TData | undefined;
}
context: ModalContext<TData>
) {
const embeddedView = config.content.createEmbeddedView({
const embeddedView = templateRef.createEmbeddedView({
$implicit: context,
});

this.appRef.attachView(embeddedView);

return await this.createModalInstance<TData, TReason>(
context,
embeddedView.rootNodes[0],
embeddedView,
config
);
}

private async createModalInstance<TData = any, TReason = any>(
context: {
close: ((result: any) => void) | null;
dismiss: ((result?: any) => void) | null;
data?: TData | undefined;
},
embeddedView: EmbeddedViewRef<any>,
context: ModalContext<TData>,
htmlElement: HTMLElement,
viewRef: ViewRef,
config: ModalConfig<TData>
) {
const node = embeddedView.rootNodes[0];

context.close = (result: any) => {
closeModal(node, result);
closeModal(htmlElement, result);
};

context.dismiss = (result?: any) => {
dismissModal(node, result);
dismissModal(htmlElement, result);
};

embeddedView.detectChanges();
viewRef.detectChanges();

const modalInstance = await showModal<TReason>({
...config,
content: node,
content: htmlElement,
});

modalInstance.onClose.once(() => {
embeddedView.destroy();
viewRef.destroy();
});

modalInstance.onDismiss.once(() => {
embeddedView.destroy();
viewRef.destroy();
});
return modalInstance;
}
Expand Down