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: support primitive context in templates and components #782

Merged
merged 2 commits into from
Mar 16, 2025
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
17 changes: 15 additions & 2 deletions projects/ng-polymorpheus/src/directives/outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {PolymorpheusComponent} from '../classes/component';
import {PolymorpheusContext} from '../classes/context';
import type {PolymorpheusContent} from '../types/content';
import type {PolymorpheusPrimitive} from '../types/primitive';
import {isPrimitive} from '../utils/is-primitive';
import {PolymorpheusTemplate} from './template';

@Directive({
Expand Down Expand Up @@ -53,9 +54,11 @@ export class PolymorpheusOutlet<C> implements OnChanges, DoCheck {

const proxy =
context &&
(new Proxy(context as object, {
(new Proxy(ensureContext(context) as object, {
get: (_, key) =>
this.getContext()?.[key as keyof (C | PolymorpheusContext<any>)],
ensureContext(this.getContext())?.[
key as keyof (C | PolymorpheusContext<any>)
],
}) as unknown as C);

if (isComponent(this.content)) {
Expand Down Expand Up @@ -117,3 +120,13 @@ function isTemplate<C>(
): content is PolymorpheusTemplate<C> | TemplateRef<C> {
return isDirective(content) || content instanceof TemplateRef;
}

function ensureContext<C>(
context: C | PolymorpheusContext<any> | undefined,
): C | PolymorpheusContext<any> | undefined {
if (context && isPrimitive(context)) {
return new PolymorpheusContext(context);
}

return context;
}
8 changes: 8 additions & 0 deletions projects/ng-polymorpheus/src/tests/outlet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,13 @@ describe('PolymorpheusOutlet', () => {
expect(text()).toBe('Component: number');
expect(COUNTER).toBe(counter);
});

it('create a non-object context', () => {
testComponent.context = 'Hello World';
testComponent.content = new PolymorpheusComponent(ComponentContent);
fixture.detectChanges();

expect(text()).toBe('Component: Hello World');
});
});
});
29 changes: 29 additions & 0 deletions projects/ng-polymorpheus/src/tests/primitive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable sonarjs/no-primitive-wrappers,no-new-wrappers,unicorn/new-for-builtins */
import {describe, expect, it} from '@jest/globals';

import {isPrimitive} from '../utils/is-primitive';

describe('isPrimitive', () => {
it('should return true for primitive', () => {
expect(isPrimitive(undefined)).toBe(true);
expect(isPrimitive(null)).toBe(true);
expect(isPrimitive(12)).toBe(true);
expect(isPrimitive(Number(12))).toBe(true);
expect(isPrimitive(Number(12))).toBe(true);
expect(isPrimitive('Hello world')).toBe(true);
expect(isPrimitive(true)).toBe(true);
});

it('should return false for non primitive', () => {
expect(isPrimitive([])).toBe(false);
expect(isPrimitive({})).toBe(false);
expect(isPrimitive(() => {})).toBe(false);
expect(isPrimitive(new (class {})())).toBe(false);

// noinspection JSPrimitiveTypeWrapperUsage
expect(isPrimitive(new String('Hello world'))).toBe(false);

// noinspection JSPrimitiveTypeWrapperUsage
expect(isPrimitive(new Boolean(true))).toBe(false);
});
});
3 changes: 3 additions & 0 deletions projects/ng-polymorpheus/src/utils/is-primitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isPrimitive(value: unknown): boolean {
return Object(value) !== value;
}
2 changes: 1 addition & 1 deletion projects/ng-polymorpheus/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"declaration": true,
"inlineSources": true,
"types": [],
"lib": ["dom", "es2018"]
"lib": ["dom", "es2020"]
},
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
Expand Down