Skip to content

Commit

Permalink
fix(element): correct createElement props and children
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 8, 2024
1 parent ffd1998 commit b2cfbc1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
20 changes: 10 additions & 10 deletions src/element/__snapshots__/create.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

exports[`creates element %p 1`] = `
{
"props": {
"children": [],
},
"key": null,
"props": {},
"type": undefined,
}
`;

exports[`creates element [Function Component] 1`] = `
{
"props": {
"children": [],
},
"key": null,
"props": {},
"type": [Function],
}
`;

exports[`creates element [Function Component] 2`] = `
{
"props": {
"children": [],
},
"key": null,
"props": {},
"type": [Function],
}
`;

exports[`creates element [Function Component] 3`] = `
{
"key": null,
"props": {
"children": [
null,
Expand All @@ -40,8 +38,8 @@ exports[`creates element [Function Component] 3`] = `

exports[`creates element [Function Component] 4`] = `
{
"key": null,
"props": {
"children": [],
"style": {
"height": 200,
"width": 100,
Expand All @@ -53,6 +51,7 @@ exports[`creates element [Function Component] 4`] = `

exports[`creates element [Function Component] 5`] = `
{
"key": null,
"props": {
"children": [
null,
Expand All @@ -68,6 +67,7 @@ exports[`creates element [Function Component] 5`] = `

exports[`creates element [Function Component] 6`] = `
{
"key": null,
"props": {
"children": [
[Function],
Expand Down
20 changes: 14 additions & 6 deletions src/element/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ComponentClass, FC, JSX } from 'react';

import type { Props } from '../types';

/**
* Creates an element.
*
Expand All @@ -10,14 +12,20 @@ import type { ComponentClass, FC, JSX } from 'react';
*/
export function createElement(
type: FC | ComponentClass,
props?: React.Attributes | null,
props?: Props | null,
...children: JSX.Element[]
) {
): JSX.Element {
if (!props) {
props = {};
}

if (children.length) {
props.children = children;
}

return {
type,
props: {
...props,
children,
},
props,
key: null,
};
}

0 comments on commit b2cfbc1

Please sign in to comment.