Skip to content

Commit

Permalink
fix: handle numbers and booleans as children resolving error
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehwagz committed Feb 6, 2022
1 parent 9063cf6 commit 83ba18b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 10 additions & 0 deletions lib/__tests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ test('h', async () => {
assert.equal(html, `<div>foo</div>`)
})

test('h - number as child', async () => {
const html = <div>{1}</div>
assert.equal(html, `<div>1</div>`)
})

test('h - boolean as child', async () => {
const html = <div>Hello {true}</div>
assert.equal(html, `<div>Hello </div>`)
})

test('h - nested', async () => {
const html = (
<div>
Expand Down
17 changes: 14 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type Props = {
style?: { [property in CSSPropertyNames]?: string | number }
[attribute: string]: any
}
export type Child = string | boolean | null
export type Child = string | boolean | number | null
export type PropsWithChildren<T> = T & {
children?: Child | Child[]
}
Expand Down Expand Up @@ -58,8 +58,19 @@ export function h(tag: Element, props: Props, ...children: Child[] | Child[][]):
while (children.length) {
const child = children.shift()
if (!child) continue
// @ts-expect-error
typeof child === 'string' ? c.push(child) : children.push(...child)
switch (typeof child) {
case 'string':
c.push(child)
break
case 'number':
c.push(`${child}`)
break
case 'boolean':
continue
default:
// @ts-expect-error
children.push(...child)
}
}

// needed for JSX
Expand Down

0 comments on commit 83ba18b

Please sign in to comment.