mirror of
https://github.com/kennethnym/jrx.git
synced 2026-03-20 03:41:18 +00:00
@@ -1,3 +1,3 @@
|
||||
export { render } from "./render";
|
||||
export { isJrxNode, JRX_NODE, FRAGMENT } from "./types";
|
||||
export type { JrxNode, JrxComponent, RenderOptions } from "./types";
|
||||
export { isJfxNode, JFX_NODE, FRAGMENT } from "./types";
|
||||
export type { JfxNode, JfxComponent, RenderOptions } from "./types";
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/** @jsxImportSource react */
|
||||
|
||||
/**
|
||||
* Integration tests: verify that Specs produced by jrx are consumable
|
||||
* Integration tests: verify that Specs produced by jfx are consumable
|
||||
* by @json-render/react's Renderer.
|
||||
*
|
||||
* This file uses React JSX (via the pragma above) for the React component
|
||||
* tree, and jrx's jsx()/jsxs() via the component wrappers for building Specs.
|
||||
* tree, and jfx's jsx()/jsxs() via the component wrappers for building Specs.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, mock } from "bun:test";
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
} from "@json-render/react";
|
||||
import { useStateStore } from "@json-render/react";
|
||||
import { jsx, jsxs } from "./jsx-runtime";
|
||||
import { render as jrxRender } from "./render";
|
||||
import { render as jfxRender } from "./render";
|
||||
import {
|
||||
Stack as JStack,
|
||||
Card as JCard,
|
||||
@@ -64,7 +64,7 @@ function StateProbe() {
|
||||
const registry = { Button, Text, Stack, Card };
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helper: render a jrx spec with @json-render/react
|
||||
// Helper: render a jfx spec with @json-render/react
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function renderSpec(spec: Spec, handlers?: Record<string, (...args: unknown[]) => void>) {
|
||||
@@ -80,15 +80,15 @@ function renderSpec(spec: Spec, handlers?: Record<string, (...args: unknown[]) =
|
||||
// Basic rendering
|
||||
// =============================================================================
|
||||
|
||||
describe("jrx → @json-render/react round-trip", () => {
|
||||
describe("jfx → @json-render/react round-trip", () => {
|
||||
it("renders a single element", () => {
|
||||
const spec = jrxRender(jsx(JText, { content: "Hello from jrx" }));
|
||||
const spec = jfxRender(jsx(JText, { content: "Hello from jfx" }));
|
||||
renderSpec(spec);
|
||||
expect(screen.getByTestId("text").textContent).toBe("Hello from jrx");
|
||||
expect(screen.getByTestId("text").textContent).toBe("Hello from jfx");
|
||||
});
|
||||
|
||||
it("renders nested elements with children", () => {
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsxs(JCard, {
|
||||
title: "My Card",
|
||||
children: [jsx(JText, { content: "Inside card" })],
|
||||
@@ -101,7 +101,7 @@ describe("jrx → @json-render/react round-trip", () => {
|
||||
});
|
||||
|
||||
it("renders a tree with multiple children", () => {
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsxs(JStack, {
|
||||
children: [
|
||||
jsx(JText, { content: "First" }),
|
||||
@@ -116,7 +116,7 @@ describe("jrx → @json-render/react round-trip", () => {
|
||||
});
|
||||
|
||||
it("renders a deep tree", () => {
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsxs(JStack, {
|
||||
children: [
|
||||
jsxs(JCard, {
|
||||
@@ -136,9 +136,9 @@ describe("jrx → @json-render/react round-trip", () => {
|
||||
// State + actions (adapted from chained-actions.test.tsx)
|
||||
// =============================================================================
|
||||
|
||||
describe("jrx specs with state and actions", () => {
|
||||
describe("jfx specs with state and actions", () => {
|
||||
it("renders with initial state", () => {
|
||||
const spec = jrxRender(jsx(JText, { content: "Stateful" }), {
|
||||
const spec = jfxRender(jsx(JText, { content: "Stateful" }), {
|
||||
state: { count: 42 },
|
||||
});
|
||||
renderSpec(spec);
|
||||
@@ -148,7 +148,7 @@ describe("jrx specs with state and actions", () => {
|
||||
});
|
||||
|
||||
it("setState action updates state on button press", async () => {
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsx(JButton, {
|
||||
label: "Set",
|
||||
on: {
|
||||
@@ -172,7 +172,7 @@ describe("jrx specs with state and actions", () => {
|
||||
});
|
||||
|
||||
it("chained pushState + setState resolves correctly", async () => {
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsx(JButton, {
|
||||
label: "Chain",
|
||||
on: {
|
||||
@@ -206,7 +206,7 @@ describe("jrx specs with state and actions", () => {
|
||||
});
|
||||
|
||||
it("multiple pushState chain resolves correctly", async () => {
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsx(JButton, {
|
||||
label: "Go",
|
||||
on: {
|
||||
@@ -242,9 +242,9 @@ describe("jrx specs with state and actions", () => {
|
||||
// Spec structural validity
|
||||
// =============================================================================
|
||||
|
||||
describe("jrx spec structural validity", () => {
|
||||
describe("jfx spec structural validity", () => {
|
||||
it("all child references resolve to existing elements", () => {
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsxs(JStack, {
|
||||
children: [
|
||||
jsxs(JCard, {
|
||||
@@ -272,12 +272,12 @@ describe("jrx spec structural validity", () => {
|
||||
});
|
||||
|
||||
it("root element exists in elements map", () => {
|
||||
const spec = jrxRender(jsx(JCard, { title: "Root" }));
|
||||
const spec = jfxRender(jsx(JCard, { title: "Root" }));
|
||||
expect(spec.elements[spec.root]).toBeDefined();
|
||||
});
|
||||
|
||||
it("element count matches node count", () => {
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsxs(JStack, {
|
||||
children: [
|
||||
jsx(JCard, { title: "A" }),
|
||||
@@ -294,9 +294,9 @@ describe("jrx spec structural validity", () => {
|
||||
// Dynamic features (ported from json-render's dynamic-forms.test.tsx)
|
||||
// =============================================================================
|
||||
|
||||
describe("jrx specs with dynamic features", () => {
|
||||
describe("jfx specs with dynamic features", () => {
|
||||
it("$state prop expressions resolve at render time", () => {
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsx(JText, { content: { $state: "/message" } }),
|
||||
{ state: { message: "Dynamic hello" } },
|
||||
);
|
||||
@@ -306,7 +306,7 @@ describe("jrx specs with dynamic features", () => {
|
||||
});
|
||||
|
||||
it("visibility condition hides element when false", () => {
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsxs(JStack, {
|
||||
children: [
|
||||
jsx(JText, {
|
||||
@@ -324,7 +324,7 @@ describe("jrx specs with dynamic features", () => {
|
||||
|
||||
it("visibility condition shows element when true", () => {
|
||||
cleanup();
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsxs(JStack, {
|
||||
children: [
|
||||
jsx(JText, {
|
||||
@@ -343,7 +343,7 @@ describe("jrx specs with dynamic features", () => {
|
||||
it("watchers fire when watched state changes", async () => {
|
||||
const loadCities = mock();
|
||||
|
||||
const spec = jrxRender(
|
||||
const spec = jfxRender(
|
||||
jsxs(JStack, {
|
||||
children: [
|
||||
jsx(JButton, {
|
||||
|
||||
@@ -2,12 +2,12 @@ import type {
|
||||
ActionBinding,
|
||||
VisibilityCondition,
|
||||
} from "@json-render/core";
|
||||
import { JRX_NODE, FRAGMENT, type JrxNode } from "./types";
|
||||
import type { JrxComponent } from "./types";
|
||||
import { JFX_NODE, FRAGMENT, type JfxNode } from "./types";
|
||||
import type { JfxComponent } from "./types";
|
||||
|
||||
export { FRAGMENT as Fragment };
|
||||
|
||||
/** Props reserved by jrx — extracted from JSX props and placed on the UIElement level. */
|
||||
/** Props reserved by jfx — extracted from JSX props and placed on the UIElement level. */
|
||||
const RESERVED_PROPS = new Set([
|
||||
"key",
|
||||
"children",
|
||||
@@ -18,26 +18,26 @@ const RESERVED_PROPS = new Set([
|
||||
]);
|
||||
|
||||
/**
|
||||
* Normalize a raw `children` value from JSX props into a flat array of JrxNodes.
|
||||
* Normalize a raw `children` value from JSX props into a flat array of JfxNodes.
|
||||
* Handles: undefined, single node, nested arrays, and filters out nulls/booleans.
|
||||
*/
|
||||
function normalizeChildren(raw: unknown): JrxNode[] {
|
||||
function normalizeChildren(raw: unknown): JfxNode[] {
|
||||
if (raw == null || typeof raw === "boolean") return [];
|
||||
|
||||
if (Array.isArray(raw)) {
|
||||
const result: JrxNode[] = [];
|
||||
const result: JfxNode[] = [];
|
||||
for (const child of raw) {
|
||||
if (child == null || typeof child === "boolean") continue;
|
||||
if (Array.isArray(child)) {
|
||||
result.push(...normalizeChildren(child));
|
||||
} else {
|
||||
result.push(child as JrxNode);
|
||||
result.push(child as JfxNode);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return [raw as JrxNode];
|
||||
return [raw as JfxNode];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,20 +56,20 @@ function extractProps(
|
||||
}
|
||||
|
||||
/** Accepted tag types: string literal, Fragment symbol, or a function component. */
|
||||
type JsxType = string | typeof FRAGMENT | JrxComponent;
|
||||
type JsxType = string | typeof FRAGMENT | JfxComponent;
|
||||
|
||||
/**
|
||||
* Core factory — shared by `jsx` and `jsxs`.
|
||||
*
|
||||
* If `type` is a function, it is called with props (like React calls
|
||||
* function components). The function returns a JrxNode directly.
|
||||
* function components). The function returns a JfxNode directly.
|
||||
*
|
||||
* If `type` is a string or Fragment, a JrxNode is constructed inline.
|
||||
* If `type` is a string or Fragment, a JfxNode is constructed inline.
|
||||
*/
|
||||
function createNode(
|
||||
type: JsxType,
|
||||
rawProps: Record<string, unknown> | null,
|
||||
): JrxNode {
|
||||
): JfxNode {
|
||||
const p = rawProps ?? {};
|
||||
|
||||
// Function component — call it, just like React does.
|
||||
@@ -78,7 +78,7 @@ function createNode(
|
||||
}
|
||||
|
||||
return {
|
||||
$$typeof: JRX_NODE,
|
||||
$$typeof: JFX_NODE,
|
||||
type,
|
||||
props: extractProps(p),
|
||||
children: normalizeChildren(p.children),
|
||||
@@ -102,7 +102,7 @@ export function jsx(
|
||||
type: JsxType,
|
||||
props: Record<string, unknown> | null,
|
||||
key?: string,
|
||||
): JrxNode {
|
||||
): JfxNode {
|
||||
const node = createNode(type, props);
|
||||
if (key != null) node.key = String(key);
|
||||
return node;
|
||||
@@ -116,7 +116,7 @@ export function jsxs(
|
||||
type: JsxType,
|
||||
props: Record<string, unknown> | null,
|
||||
key?: string,
|
||||
): JrxNode {
|
||||
): JfxNode {
|
||||
const node = createNode(type, props);
|
||||
if (key != null) node.key = String(key);
|
||||
return node;
|
||||
@@ -133,7 +133,7 @@ export namespace JSX {
|
||||
}
|
||||
|
||||
/** The type returned by JSX expressions. */
|
||||
export type Element = JrxNode;
|
||||
export type Element = JfxNode;
|
||||
|
||||
export interface ElementChildrenAttribute {
|
||||
children: {};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { render } from "./render";
|
||||
import { isJrxNode, FRAGMENT } from "./types";
|
||||
import { isJfxNode, FRAGMENT } from "./types";
|
||||
import { jsx, jsxs, Fragment } from "./jsx-runtime";
|
||||
import {
|
||||
Stack,
|
||||
@@ -18,25 +18,25 @@ import {
|
||||
// =============================================================================
|
||||
|
||||
describe("jsx factory", () => {
|
||||
it("jsx() with string type returns a JrxNode", () => {
|
||||
it("jsx() with string type returns a JfxNode", () => {
|
||||
const node = jsx("Card", { title: "Hello" });
|
||||
expect(isJrxNode(node)).toBe(true);
|
||||
expect(isJfxNode(node)).toBe(true);
|
||||
expect(node.type).toBe("Card");
|
||||
expect(node.props).toEqual({ title: "Hello" });
|
||||
});
|
||||
|
||||
it("jsx() with component function resolves typeName", () => {
|
||||
const node = jsx(Card, { title: "Hello" });
|
||||
expect(isJrxNode(node)).toBe(true);
|
||||
expect(isJfxNode(node)).toBe(true);
|
||||
expect(node.type).toBe("Card");
|
||||
expect(node.props).toEqual({ title: "Hello" });
|
||||
});
|
||||
|
||||
it("jsxs() returns a JrxNode with children", () => {
|
||||
it("jsxs() returns a JfxNode with children", () => {
|
||||
const node = jsxs(Stack, {
|
||||
children: [jsx(Text, { content: "A" }), jsx(Text, { content: "B" })],
|
||||
});
|
||||
expect(isJrxNode(node)).toBe(true);
|
||||
expect(isJfxNode(node)).toBe(true);
|
||||
expect(node.children).toHaveLength(2);
|
||||
});
|
||||
|
||||
@@ -73,7 +73,7 @@ describe("jsx factory", () => {
|
||||
|
||||
it("jsx() handles null props", () => {
|
||||
const node = jsx("Divider", null);
|
||||
expect(isJrxNode(node)).toBe(true);
|
||||
expect(isJfxNode(node)).toBe(true);
|
||||
expect(node.props).toEqual({});
|
||||
expect(node.children).toEqual([]);
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { render } from "./render";
|
||||
import { FRAGMENT, type JrxNode } from "./types";
|
||||
import { FRAGMENT, type JfxNode } from "./types";
|
||||
import { jsx } from "./jsx-runtime";
|
||||
import {
|
||||
Stack,
|
||||
@@ -300,7 +300,7 @@ describe("state passthrough", () => {
|
||||
// =============================================================================
|
||||
|
||||
describe("error handling", () => {
|
||||
it("throws for non-JrxNode input", () => {
|
||||
expect(() => render({} as JrxNode)).toThrow(/expects a JrxNode/);
|
||||
it("throws for non-JfxNode input", () => {
|
||||
expect(() => render({} as JfxNode)).toThrow(/expects a JfxNode/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import type { Spec, UIElement } from "@json-render/core";
|
||||
import { FRAGMENT, type JrxNode, type RenderOptions, isJrxNode } from "./types";
|
||||
import { FRAGMENT, type JfxNode, type RenderOptions, isJfxNode } from "./types";
|
||||
|
||||
/**
|
||||
* Flatten a JrxNode tree into a json-render `Spec`.
|
||||
* Flatten a JfxNode tree into a json-render `Spec`.
|
||||
*
|
||||
* Analogous to `ReactDOM.render` but produces JSON instead of DOM mutations.
|
||||
*
|
||||
* @param node - Root JrxNode (produced by JSX)
|
||||
* @param node - Root JfxNode (produced by JSX)
|
||||
* @param options - Optional render configuration (e.g. initial state)
|
||||
* @returns A json-render `Spec` ready for any renderer
|
||||
*
|
||||
@@ -20,9 +20,9 @@ import { FRAGMENT, type JrxNode, type RenderOptions, isJrxNode } from "./types";
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
export function render(node: JrxNode, options?: RenderOptions): Spec {
|
||||
if (!isJrxNode(node)) {
|
||||
throw new Error("render() expects a JrxNode produced by JSX.");
|
||||
export function render(node: JfxNode, options?: RenderOptions): Spec {
|
||||
if (!isJfxNode(node)) {
|
||||
throw new Error("render() expects a JfxNode produced by JSX.");
|
||||
}
|
||||
|
||||
if (node.type === FRAGMENT) {
|
||||
@@ -66,12 +66,12 @@ function generateKey(
|
||||
|
||||
/**
|
||||
* Resolve the children of a node, expanding fragments inline.
|
||||
* Returns an array of concrete (non-fragment) JrxNodes.
|
||||
* Returns an array of concrete (non-fragment) JfxNodes.
|
||||
*/
|
||||
function expandChildren(children: JrxNode[]): JrxNode[] {
|
||||
const result: JrxNode[] = [];
|
||||
function expandChildren(children: JfxNode[]): JfxNode[] {
|
||||
const result: JfxNode[] = [];
|
||||
for (const child of children) {
|
||||
if (!isJrxNode(child)) continue;
|
||||
if (!isJfxNode(child)) continue;
|
||||
if (child.type === FRAGMENT) {
|
||||
// Recursively expand nested fragments
|
||||
result.push(...expandChildren(child.children));
|
||||
@@ -83,11 +83,11 @@ function expandChildren(children: JrxNode[]): JrxNode[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively flatten a JrxNode into the elements map.
|
||||
* Recursively flatten a JfxNode into the elements map.
|
||||
* Returns the key assigned to this node.
|
||||
*/
|
||||
function flattenNode(
|
||||
node: JrxNode,
|
||||
node: JfxNode,
|
||||
elements: Record<string, UIElement>,
|
||||
counters: Map<string, number>,
|
||||
usedKeys: Set<string>,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Ported from json-render's core/src/spec-validator.test.ts.
|
||||
*
|
||||
* Runs @json-render/core's validateSpec against Specs produced by jrx
|
||||
* Runs @json-render/core's validateSpec against Specs produced by jfx
|
||||
* to prove structural correctness.
|
||||
*/
|
||||
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
Select,
|
||||
} from "./test-components";
|
||||
|
||||
describe("validateSpec on jrx-produced specs", () => {
|
||||
describe("validateSpec on jfx-produced specs", () => {
|
||||
it("validates a simple single-element spec", () => {
|
||||
const spec = render(<Text text="hello" />);
|
||||
const result = validateSpec(spec);
|
||||
@@ -90,7 +90,7 @@ describe("validateSpec on jrx-produced specs", () => {
|
||||
expect(result.issues.some((i) => i.code === "watch_in_props")).toBe(false);
|
||||
});
|
||||
|
||||
it("no orphaned elements in jrx output", () => {
|
||||
it("no orphaned elements in jfx output", () => {
|
||||
const spec = render(
|
||||
<Stack>
|
||||
<Text text="A" />
|
||||
@@ -101,7 +101,7 @@ describe("validateSpec on jrx-produced specs", () => {
|
||||
expect(result.issues.some((i) => i.code === "orphaned_element")).toBe(false);
|
||||
});
|
||||
|
||||
it("no missing children in jrx output", () => {
|
||||
it("no missing children in jfx output", () => {
|
||||
const spec = render(
|
||||
<Stack>
|
||||
<Card title="X">
|
||||
|
||||
@@ -1,42 +1,42 @@
|
||||
import { jsx } from "./jsx-runtime";
|
||||
import type { JrxNode } from "./types";
|
||||
import type { JfxNode } from "./types";
|
||||
|
||||
export function Stack(props: Record<string, unknown>): JrxNode {
|
||||
export function Stack(props: Record<string, unknown>): JfxNode {
|
||||
return jsx("Stack", props);
|
||||
}
|
||||
|
||||
export function Card(props: Record<string, unknown>): JrxNode {
|
||||
export function Card(props: Record<string, unknown>): JfxNode {
|
||||
return jsx("Card", props);
|
||||
}
|
||||
|
||||
export function Text(props: Record<string, unknown>): JrxNode {
|
||||
export function Text(props: Record<string, unknown>): JfxNode {
|
||||
return jsx("Text", props);
|
||||
}
|
||||
|
||||
export function Button(props: Record<string, unknown>): JrxNode {
|
||||
export function Button(props: Record<string, unknown>): JfxNode {
|
||||
return jsx("Button", props);
|
||||
}
|
||||
|
||||
export function Badge(props: Record<string, unknown>): JrxNode {
|
||||
export function Badge(props: Record<string, unknown>): JfxNode {
|
||||
return jsx("Badge", props);
|
||||
}
|
||||
|
||||
export function List(props: Record<string, unknown>): JrxNode {
|
||||
export function List(props: Record<string, unknown>): JfxNode {
|
||||
return jsx("List", props);
|
||||
}
|
||||
|
||||
export function ListItem(props: Record<string, unknown>): JrxNode {
|
||||
export function ListItem(props: Record<string, unknown>): JfxNode {
|
||||
return jsx("ListItem", props);
|
||||
}
|
||||
|
||||
export function Select(props: Record<string, unknown>): JrxNode {
|
||||
export function Select(props: Record<string, unknown>): JfxNode {
|
||||
return jsx("Select", props);
|
||||
}
|
||||
|
||||
export function Input(props: Record<string, unknown>): JrxNode {
|
||||
export function Input(props: Record<string, unknown>): JfxNode {
|
||||
return jsx("Input", props);
|
||||
}
|
||||
|
||||
export function Divider(props: Record<string, unknown>): JrxNode {
|
||||
export function Divider(props: Record<string, unknown>): JfxNode {
|
||||
return jsx("Divider", props);
|
||||
}
|
||||
|
||||
44
src/types.ts
44
src/types.ts
@@ -4,19 +4,19 @@ import type {
|
||||
} from "@json-render/core";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// JrxNode — intermediate representation produced by the JSX factory
|
||||
// JfxNode — intermediate representation produced by the JSX factory
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sentinel symbol identifying a JrxNode (prevents plain objects from
|
||||
* Sentinel symbol identifying a JfxNode (prevents plain objects from
|
||||
* being mistaken for nodes).
|
||||
*/
|
||||
export const JRX_NODE = Symbol.for("jrx.node");
|
||||
export const JFX_NODE = Symbol.for("jfx.node");
|
||||
|
||||
/**
|
||||
* Sentinel symbol for Fragment grouping.
|
||||
*/
|
||||
export const FRAGMENT = Symbol.for("jrx.fragment");
|
||||
export const FRAGMENT = Symbol.for("jfx.fragment");
|
||||
|
||||
/**
|
||||
* A node in the intermediate JSX tree.
|
||||
@@ -24,9 +24,9 @@ export const FRAGMENT = Symbol.for("jrx.fragment");
|
||||
* Created by the `jsx` / `jsxs` factory functions and consumed by `render()`
|
||||
* which flattens the tree into a json-render `Spec`.
|
||||
*/
|
||||
export interface JrxNode {
|
||||
/** Brand symbol — always `JRX_NODE` */
|
||||
$$typeof: typeof JRX_NODE;
|
||||
export interface JfxNode {
|
||||
/** Brand symbol — always `JFX_NODE` */
|
||||
$$typeof: typeof JFX_NODE;
|
||||
|
||||
/**
|
||||
* Component type name (e.g. `"Card"`, `"Button"`).
|
||||
@@ -38,7 +38,7 @@ export interface JrxNode {
|
||||
props: Record<string, unknown>;
|
||||
|
||||
/** Child nodes */
|
||||
children: JrxNode[];
|
||||
children: JfxNode[];
|
||||
|
||||
// -- Reserved / meta fields (extracted from JSX props) --
|
||||
|
||||
@@ -59,20 +59,20 @@ export interface JrxNode {
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// JrxComponent — a function usable as a JSX tag that maps to a type string
|
||||
// JfxComponent — a function usable as a JSX tag that maps to a type string
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* A jrx component function. Works like a React function component:
|
||||
* A jfx component function. Works like a React function component:
|
||||
* when used as a JSX tag (`<Card />`), the factory calls the function
|
||||
* with props and gets back a JrxNode.
|
||||
* with props and gets back a JfxNode.
|
||||
*/
|
||||
export type JrxComponent = (props: Record<string, unknown>) => JrxNode;
|
||||
export type JfxComponent = (props: Record<string, unknown>) => JfxNode;
|
||||
|
||||
/**
|
||||
* Define a jrx component for use as a JSX tag.
|
||||
* Define a jfx component for use as a JSX tag.
|
||||
*
|
||||
* Creates a function that, when called with props, produces a JrxNode
|
||||
* Creates a function that, when called with props, produces a JfxNode
|
||||
* with the given type name — just like a React component returns
|
||||
* React elements.
|
||||
*
|
||||
@@ -82,12 +82,12 @@ export type JrxComponent = (props: Record<string, unknown>) => JrxNode;
|
||||
* const spec = render(<Card title="Hello"><Text content="World" /></Card>);
|
||||
* ```
|
||||
*/
|
||||
export function component(typeName: string): JrxComponent {
|
||||
export function component(typeName: string): JfxComponent {
|
||||
// Import createNodeFromString lazily to avoid circular dep
|
||||
// (jsx-runtime imports types). Instead, we build the node inline.
|
||||
return (props: Record<string, unknown>) => {
|
||||
return {
|
||||
$$typeof: JRX_NODE,
|
||||
$$typeof: JFX_NODE,
|
||||
type: typeName,
|
||||
props: filterReserved(props),
|
||||
children: normalizeChildrenRaw(props.children),
|
||||
@@ -110,21 +110,21 @@ function filterReserved(props: Record<string, unknown>): Record<string, unknown>
|
||||
return out;
|
||||
}
|
||||
|
||||
function normalizeChildrenRaw(raw: unknown): JrxNode[] {
|
||||
function normalizeChildrenRaw(raw: unknown): JfxNode[] {
|
||||
if (raw == null || typeof raw === "boolean") return [];
|
||||
if (Array.isArray(raw)) {
|
||||
const result: JrxNode[] = [];
|
||||
const result: JfxNode[] = [];
|
||||
for (const child of raw) {
|
||||
if (child == null || typeof child === "boolean") continue;
|
||||
if (Array.isArray(child)) {
|
||||
result.push(...normalizeChildrenRaw(child));
|
||||
} else {
|
||||
result.push(child as JrxNode);
|
||||
result.push(child as JfxNode);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return [raw as JrxNode];
|
||||
return [raw as JfxNode];
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -140,10 +140,10 @@ export interface RenderOptions {
|
||||
// Type guard
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function isJrxNode(value: unknown): value is JrxNode {
|
||||
export function isJfxNode(value: unknown): value is JfxNode {
|
||||
return (
|
||||
typeof value === "object" &&
|
||||
value !== null &&
|
||||
(value as JrxNode).$$typeof === JRX_NODE
|
||||
(value as JfxNode).$$typeof === JFX_NODE
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user