Skip to content

fix(elements): Add check for elements templates in case container is detached. #15970

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

Open
wants to merge 2 commits into
base: 19.2.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Component, ViewChild, ViewContainerRef } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { html } from 'lit-html';
import { AsyncDirective, directive } from 'lit/async-directive.js';
Expand All @@ -13,22 +14,33 @@ class ToLowerAsyncDirective extends AsyncDirective {
protected override disconnected(): void {
directiveLog.push('disconnected');
}
}
export const toLowerAsync = directive(ToLowerAsyncDirective);
}
export const toLowerAsync = directive(ToLowerAsyncDirective);

@Component({
template: `<igx-template-wrapper></igx-template-wrapper>`,
imports: [TemplateWrapperComponent]
})
class TestTemplateWrapperComponent {
@ViewChild(TemplateWrapperComponent)
public templateWrapper;

constructor(public viewContainerRef: ViewContainerRef) { }
}

describe('WrapperComponent', () => {
let component: TemplateWrapperComponent;
let fixture: ComponentFixture<TemplateWrapperComponent>;
let component: TestTemplateWrapperComponent;
let fixture: ComponentFixture<TestTemplateWrapperComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [TemplateWrapperComponent]
imports: [TestTemplateWrapperComponent]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(TemplateWrapperComponent);
fixture = TestBed.createComponent(TestTemplateWrapperComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
Expand All @@ -39,8 +51,9 @@ describe('WrapperComponent', () => {

it('should render template', () => {
const context = { text: "Oh hi" };
const templateRef = component.addTemplate((ctx) => html`<span id="template1">${ctx.text}</span>`);
const templateRef = component.templateWrapper.addTemplate((ctx) => html`<span id="template1">${ctx.text}</span>`);
const embeddedView = templateRef.createEmbeddedView(context);
component.viewContainerRef.insert(embeddedView);
embeddedView.detectChanges();

const span = embeddedView.rootNodes[0].querySelector("#template1");
Expand All @@ -51,8 +64,9 @@ describe('WrapperComponent', () => {

it('should update connectivity on template with AsyncDirective', () => {
const context = { text: "OH HI" };
const templateRef = component.addTemplate((ctx) => html`<span id="template1">${toLowerAsync(ctx.text)}</span>`);
const templateRef = component.templateWrapper.addTemplate((ctx) => html`<span id="template1">${toLowerAsync(ctx.text)}</span>`);
const embeddedView = templateRef.createEmbeddedView(context);
component.viewContainerRef.insert(embeddedView);
embeddedView.detectChanges();

const span = embeddedView.rootNodes[0].querySelector("#template1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TemplateRefWrapper } from './template-ref-wrapper';
import { render, type RootPart, type TemplateResult } from 'lit-html';

type TemplateFunction = (arg: any) => TemplateResult;
const SCHEDULE_DELAY = 10;

@Component({
selector: 'igx-template-wrapper',
Expand All @@ -18,6 +19,7 @@ export class TemplateWrapperComponent {
public templateRendered = new Subject<HTMLElement>();

private childParts: WeakMap<HTMLElement, RootPart> = new WeakMap();
private timeoutId: NodeJS.Timeout | string | number | undefined;

/**
* All template refs
Expand All @@ -30,6 +32,15 @@ export class TemplateWrapperComponent {
constructor(private cdr: ChangeDetectorRef) { }

protected litRender(container: HTMLElement, templateFunc: (arg: any) => TemplateResult, arg: any) {
if (!container.isConnected) {
// Wait a bit if it gets attached back, otherwise do nothing
this.timeoutId = setTimeout(() =>{
if (container.isConnected) {
this.litRender(container, templateFunc, arg);
}
}, SCHEDULE_DELAY);
return;
}
const part = render(templateFunc(arg), container);

let existingPart = this.childParts.get(container);
Expand Down Expand Up @@ -71,5 +82,6 @@ export class TemplateWrapperComponent {
this.childParts.get(container).setConnected(false);
this.childParts.delete(container);
}
clearTimeout(this.timeoutId);
}
}
3 changes: 2 additions & 1 deletion projects/igniteui-angular-elements/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"allowJs": true,
"outDir": "../../out-tsc/spec",
"types": [
"jasmine"
"jasmine",
"node"
]
},
"files": [
Expand Down
Loading