Skip to content

Commit

Permalink
fix(jsx): h function not working
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Oct 28, 2021
1 parent 0846a39 commit 29582b0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 13 additions & 14 deletions src/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ import { kebab } from './m';
import type { VDelta, VNode, VProps } from './types/base';
import type { FC, JSX, JSXVNode } from './types/jsx';

const h = (tag: string, props?: VProps, children?: JSXVNode[], delta?: VDelta) => {
const h = (tag: string, props?: VProps, ...children: JSXVNode[]) => {
let flag = VFlags.NO_CHILDREN;
let delta: VDelta | undefined;
const normalizedChildren: VNode[] = [];
if (props) {
const rawDelta = <VDelta>(<unknown>props.delta);
if (rawDelta && rawDelta.length) {
delta = rawDelta;
delete props.delta;
}
}
if (children) {
children = Array.isArray(children) ? children : [children];
const keysInChildren = new Set();
Expand All @@ -18,7 +26,7 @@ const h = (tag: string, props?: VProps, children?: JSXVNode[], delta?: VDelta) =
if (children.every((child) => typeof child === 'string')) {
flag = VFlags.ONLY_TEXT_CHILDREN;
}
for (let i = 0; i < children.length; i++) {
for (let i = 0; i < children.length; ++i) {
if (
children[i] !== undefined &&
children[i] !== null &&
Expand All @@ -32,7 +40,7 @@ const h = (tag: string, props?: VProps, children?: JSXVNode[], delta?: VDelta) =
if (subChildren[i] || subChildren[i] === '') {
normalizedChildren.push(subChildren[i]);
if (typeof subChildren[i] === 'object') {
if (!hasVElementChildren) hasVElementChildren = true;
hasVElementChildren = true;
if (typeof subChildren[i].key === 'string' && subChildren[i].key !== '') {
keysInChildren.add(subChildren[i].key);
}
Expand Down Expand Up @@ -84,25 +92,16 @@ const normalize = (jsxVNode: JSXVNode): VNode | VNode[] | undefined => {
};

const jsx = (tag: string | FC, props?: VProps, key?: string | null): VNode => {
let delta: VDelta | undefined;
if (typeof tag === 'function') return tag(props, key);
let children: JSXVNode[] = [];
if (props) {
const rawDelta = <VDelta>(<unknown>props.delta);
if (props.delta && rawDelta.length > 0) {
delta = rawDelta;
delete props.delta;
}
if (props.children) {
children = <JSXVNode[]>(<unknown>props.children);
delete props.children;
}
if (key) props.key = key;
}
if (typeof tag === 'function') {
return tag(props, children, delta);
} else {
return h(tag, props, children, delta);
}
return h(tag, props, ...children);
};

const Fragment = (props?: VProps): VNode[] | undefined => <VNode[] | undefined>props?.children;
Expand Down
2 changes: 1 addition & 1 deletion src/types/jsx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { VDelta, VElement, VNode, VProps } from './base';

export type JSXVNode = VNode | number | boolean | undefined | null;
export type FC = (props?: VProps, children?: JSXVNode[], delta?: VDelta) => VElement;
export type FC = (props?: VProps, key?: string) => VElement;

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace JSX {
Expand Down

0 comments on commit 29582b0

Please sign in to comment.