Skip to content

Commit

Permalink
allow numbers as children in components
Browse files Browse the repository at this point in the history
  • Loading branch information
Bloomca committed Jun 3, 2024
1 parent b3e2335 commit 9538ea1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
20 changes: 20 additions & 0 deletions integration-tests/create-element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,24 @@ describe("createElement", () => {

expect(await screen.findByText("parent component")).toBeVisible();
});

test("support numbers as a children element", async () => {
function App() {
return createElement("div", {
children: [
createElement("h1", { children: ["parent component"] }),
createElement("div", {
"data-testid": "container",
children: ["text ", 5],
}),
],
});
}
cleanup = attachComponent({
htmlElement: document.body,
component: createElement(App),
});

expect(screen.getByTestId("container").textContent).toBe("text 5");
});
});
3 changes: 3 additions & 0 deletions src/create-element/parse-children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ function parseChildren({
if (typeof childComponent === "string") {
const text = document.createTextNode(childComponent);
htmlElement.append(text);
} else if (typeof childComponent === "number") {
const text = document.createTextNode(String(childComponent));
htmlElement.append(text);
} else if (
typeof childComponent === "object" &&
childComponent &&
Expand Down
7 changes: 6 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ export type VelesComponent = {
};

// all supported child options
type velesChild = string | VelesElement | VelesComponent | VelesStringElement;
type velesChild =
| string
| number
| VelesElement
| VelesComponent
| VelesStringElement;
export type VelesChildren = velesChild | velesChild[] | undefined | null;

export type VelesElementProps = {
Expand Down

0 comments on commit 9538ea1

Please sign in to comment.