@3kles/kles-material-dynamicforms 21.7.5 → 21.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -344,16 +344,16 @@ function isDestroyable(value) {
344
344
  }
345
345
 
346
346
  class KlesDynamicFieldDirective {
347
- constructor(container, injector) {
347
+ constructor(container, injector, applicationRef) {
348
348
  this.container = container;
349
349
  this.injector = injector;
350
+ this.applicationRef = applicationRef;
350
351
  this.context = null;
351
352
  this.subComponents = [];
353
+ this.subComponentViews = new Map();
352
354
  }
353
355
  ngOnDestroy() {
354
- if (this.componentRef) {
355
- this.componentRef.destroy();
356
- }
356
+ this.destroyComponents();
357
357
  }
358
358
  ngOnInit() {
359
359
  this.buildComponent();
@@ -384,11 +384,7 @@ class KlesDynamicFieldDirective {
384
384
  }
385
385
  }
386
386
  buildComponent() {
387
- if (this.componentRef) {
388
- this.subComponents.forEach((c) => c.destroy());
389
- this.subComponents = [];
390
- this.componentRef.destroy();
391
- }
387
+ this.destroyComponents();
392
388
  const options = {
393
389
  providers: [
394
390
  ...(this.field.providers || []),
@@ -481,6 +477,14 @@ class KlesDynamicFieldDirective {
481
477
  createSubComponent(componentType, options) {
482
478
  const injector = Injector.create(options);
483
479
  const component = this.container.createComponent(componentType, { injector });
480
+ // The host element is projected into the field component. Keeping its view
481
+ // in this container would make Angular's logical DOM differ from the real DOM.
482
+ const viewIndex = this.container.indexOf(component.hostView);
483
+ const detachedView = viewIndex !== -1 ? this.container.detach(viewIndex) : null;
484
+ if (detachedView) {
485
+ this.applicationRef.attachView(detachedView);
486
+ this.subComponentViews.set(component, detachedView);
487
+ }
484
488
  component.onDestroy(() => {
485
489
  if (isDestroyable(injector)) {
486
490
  injector.destroy();
@@ -488,7 +492,20 @@ class KlesDynamicFieldDirective {
488
492
  });
489
493
  return component;
490
494
  }
491
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.20", ngImport: i0, type: KlesDynamicFieldDirective, deps: [{ token: i0.ViewContainerRef }, { token: i0.Injector }], target: i0.ɵɵFactoryTarget.Directive }); }
495
+ destroyComponents() {
496
+ this.componentRef?.destroy();
497
+ this.componentRef = undefined;
498
+ this.subComponents.forEach((component) => {
499
+ const view = this.subComponentViews.get(component);
500
+ if (view) {
501
+ this.applicationRef.detachView(view);
502
+ }
503
+ component.destroy();
504
+ });
505
+ this.subComponents = [];
506
+ this.subComponentViews.clear();
507
+ }
508
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.20", ngImport: i0, type: KlesDynamicFieldDirective, deps: [{ token: i0.ViewContainerRef }, { token: i0.Injector }, { token: i0.ApplicationRef }], target: i0.ɵɵFactoryTarget.Directive }); }
492
509
  static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.2.20", type: KlesDynamicFieldDirective, isStandalone: true, selector: "[klesDynamicField]", inputs: { field: "field", group: "group", ui: "ui", siblingFields: "siblingFields", context: "context" }, usesOnChanges: true, ngImport: i0 }); }
493
510
  }
494
511
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.20", ngImport: i0, type: KlesDynamicFieldDirective, decorators: [{
@@ -497,7 +514,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.20", ngImpo
497
514
  selector: '[klesDynamicField]',
498
515
  standalone: true,
499
516
  }]
500
- }], ctorParameters: () => [{ type: i0.ViewContainerRef }, { type: i0.Injector }], propDecorators: { field: [{
517
+ }], ctorParameters: () => [{ type: i0.ViewContainerRef }, { type: i0.Injector }, { type: i0.ApplicationRef }], propDecorators: { field: [{
501
518
  type: Input
502
519
  }], group: [{
503
520
  type: Input