Skip to content

Commit 792c246

Browse files
committed
chore(*): making elements work with new ng underlying code
1 parent a9295b3 commit 792c246

3 files changed

Lines changed: 33 additions & 31 deletions

File tree

projects/igniteui-angular-elements/src/app/create-custom-element.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Type } from '@angular/core';
1+
import { reflectComponentType, Type } from '@angular/core';
22
import { createCustomElement, NgElementConfig, NgElementConstructor } from '@angular/elements';
33
import { FilteringExpressionsTree, FilteringLogic, IgxBooleanFilteringOperand, IgxDateFilteringOperand, IgxDateTimeFilteringOperand, IgxGridComponent, IgxNumberFilteringOperand, IgxStringFilteringOperand, IgxTimeFilteringOperand } from 'igniteui-angular';
44
import { ComponentConfig } from './component-config';
@@ -9,7 +9,7 @@ export type IgxNgElementConfig = Omit<NgElementConfig, 'strategyFactory'> & { re
99
type IgxElementConstructor<T> = NgElementConstructor<T> & { tagName: string};
1010

1111
export function createIgxCustomElement<T>(component: Type<T>, config: IgxNgElementConfig): IgxElementConstructor<T> {
12-
const strategyFactory = new IgxCustomNgElementStrategyFactory(component, config.injector, config.registerConfig);
12+
const strategyFactory = new IgxCustomNgElementStrategyFactory(component, config.registerConfig);
1313

1414
guardAttributeNames<T>(strategyFactory);
1515

@@ -111,10 +111,10 @@ function guardAttributeNames<T>(strategyFactory: IgxCustomNgElementStrategyFacto
111111

112112
// getComponentDef not public, also technically readonly map
113113
// the key is the non-minified (template) name
114-
const inputs: {[P in keyof T]: string} = (strategyFactory.component as any).ɵcmp.inputs;
114+
const inputs = reflectComponentType((strategyFactory as any).component).inputs;
115115

116-
for (const key in inputs) {
117-
const input = inputs[key];
116+
inputs.forEach((input) => {
117+
const key = input.templateName;
118118
// detect consecutive capital letters in `templateName` and lower a portion of them
119119
if (/[A-Z]{2,}/.test(key)) {
120120
// showPivotConfigurationUI -> showPivotConfigurationUi -> show-pivot-configuration-ui
@@ -126,5 +126,5 @@ function guardAttributeNames<T>(strategyFactory: IgxCustomNgElementStrategyFacto
126126
inputs[newKey] = input;
127127
// TODO: consider deleting the original key
128128
}
129-
}
129+
});
130130
}

projects/igniteui-angular-elements/src/app/custom-strategy.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApplicationRef, ComponentRef, createComponent, DestroyRef, EnvironmentInjector, EventEmitter, Injector, QueryList, Type, ViewContainerRef, reflectComponentType } from '@angular/core';
1+
import { ComponentRef, createComponent, DestroyRef, EventEmitter, Injector, QueryList, Type, ViewContainerRef, reflectComponentType } from '@angular/core';
22
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
33
import { NgElement, NgElementStrategyEvent } from '@angular/elements';
44
import { fromEvent, Observable } from 'rxjs';
@@ -82,8 +82,10 @@ class IgxCustomNgElementStrategy extends ComponentNgElementStrategy {
8282
// set componentRef to non-null to prevent DOM moves from re-initializing
8383
// TODO: Fail handling or cancellation needed?
8484
(this as any).componentRef = {};
85-
const ngContentSelectors = reflectComponentType(this._component).ngContentSelectors;
86-
const contentChildrenTags = Array.from(element.children).filter(x => ngContentSelectors.some(sel => x.matches(sel))).map(x => x.tagName.toLocaleLowerCase());
85+
const ngContentSelectors = [...reflectComponentType(this._component).ngContentSelectors];
86+
const contentChildrenTags = Array.from(element.children)
87+
.filter(x => ngContentSelectors.some(sel => x.matches(sel)))
88+
.map(x => x.tagName.toLocaleLowerCase());
8789

8890
// const toBeOrphanedChildren = Array.from(element.children).filter(x => !ngContentSelectors.some(sel => x.matches(sel)));
8991
// for (const iterator of toBeOrphanedChildren) {
@@ -149,10 +151,10 @@ class IgxCustomNgElementStrategy extends ComponentNgElementStrategy {
149151
const childInjector = Injector.create({ providers: [], parent: (this as any).injector });
150152
const projectableNodes = extractProjectableNodes(
151153
element,
152-
ngContentSelectors as string[],
154+
ngContentSelectors,
153155
);
154156
(this as any).componentRef = createComponent(this._component, {
155-
environmentInjector: ((this as any).injector as Injector).get(EnvironmentInjector),
157+
environmentInjector: (this as any).injector,
156158
elementInjector: childInjector,
157159
hostElement: element,
158160
projectableNodes,
@@ -557,8 +559,8 @@ export class IgxCustomNgElementStrategyFactory extends ComponentNgElementStrateg
557559
* @param injector The injector for the component
558560
* @param config Additional component hierarchy configuration
559561
*/
560-
constructor(component: Type<any>, injector: Injector, private config: ComponentConfig[]) {
561-
super(component, injector);
562+
constructor(component: Type<any>, private config: ComponentConfig[]) {
563+
super(component);
562564
}
563565

564566
public override create(injector: Injector) {

projects/igniteui-angular-elements/src/app/ng-element-strategy.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
OutputRef,
2525
reflectComponentType,
2626
createComponent,
27+
ComponentMirror,
2728
} from '@angular/core';
2829
import {merge, Observable, ReplaySubject} from 'rxjs';
2930
import {switchMap} from 'rxjs/operators';
@@ -40,18 +41,17 @@ import {
4041
const DESTROY_DELAY = 10;
4142

4243
/**
43-
* Factory that creates new ComponentNgElementStrategy instance. Uses reflectComponentType
44-
* to obtain component metadata and passes the component type to each strategy.
44+
* Factory that creates new ComponentNgElementStrategy instance. Gets the component factory with the
45+
* constructor's injector's factory resolver and passes that factory to each strategy.
4546
*/
4647
export class ComponentNgElementStrategyFactory implements NgElementStrategyFactory {
47-
component: Type<any>;
48+
componentMirror: ComponentMirror<any>;
4849

4950
inputMap = new Map<string, string>();
5051

51-
constructor(component: Type<any>, injector: Injector) {
52-
this.component = component;
53-
const mirror = reflectComponentType(component);
54-
for (const input of mirror.inputs) {
52+
constructor(protected component: Type<any>) {
53+
this.componentMirror = reflectComponentType(component)!;
54+
for (const input of this.componentMirror.inputs) {
5555
this.inputMap.set(input.propName, input.templateName);
5656
}
5757
}
@@ -200,10 +200,10 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
200200
const childInjector = Injector.create({providers: [], parent: this.injector});
201201
const projectableNodes = extractProjectableNodes(
202202
element,
203-
reflectComponentType(this.component).ngContentSelectors as string[],
203+
reflectComponentType(this.component)!.ngContentSelectors as string[],
204204
);
205205
this.componentRef = createComponent(this.component, {
206-
environmentInjector: this.injector.get(EnvironmentInjector),
206+
environmentInjector: this.injector as EnvironmentInjector,
207207
elementInjector: childInjector,
208208
hostElement: element,
209209
projectableNodes,
@@ -227,15 +227,15 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
227227

228228
/** Sets up listeners for the component's outputs so that the events stream emits the events. */
229229
protected initializeOutputs(componentRef: ComponentRef<any>): void {
230-
const eventEmitters: Observable<NgElementStrategyEvent>[] = reflectComponentType(this.component).outputs.map(
231-
({propName, templateName}) => {
232-
const emitter: EventEmitter<any> | OutputRef<any> = componentRef.instance[propName];
233-
return new Observable((observer) => {
234-
const sub = emitter.subscribe((value) => observer.next({name: templateName, value}));
235-
return () => sub.unsubscribe();
236-
});
237-
},
238-
);
230+
const eventEmitters: Observable<NgElementStrategyEvent>[] = reflectComponentType(
231+
this.component,
232+
)!.outputs.map(({propName, templateName}) => {
233+
const emitter: EventEmitter<any> | OutputRef<any> = componentRef.instance[propName];
234+
return new Observable((observer) => {
235+
const sub = emitter.subscribe((value) => observer.next({name: templateName, value}));
236+
return () => sub.unsubscribe();
237+
});
238+
});
239239

240240
this.eventEmitters.next(eventEmitters);
241241
}

0 commit comments

Comments
 (0)