Skip to content

Commit

Permalink
fix: handle empty string attr and 0 attr
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehwagz committed Feb 16, 2022
1 parent a5b591b commit c9a0e1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/__tests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ test('h - undefined attr', async () => {
assert.equal(html, `<div>foo</div>`)
})

test('h - empty string attr', async () => {
const html = <input value="" />
assert.equal(html, `<input value />`)
})

test('h - 0 as attr', async () => {
const html = <input value={0} />
assert.equal(html, `<input value="0" />`)
})

test('h - null style', async () => {
const html = <div style={null}>foo</div>
assert.equal(html, `<div>foo</div>`)
Expand Down
4 changes: 3 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ export function h(tag: Element, props: Props, ...children: Child[] | Child[][]):
let value = props[k]
const key = aliases[k] || k

if (typeof value === 'boolean') {
if (typeof value === 'boolean' || value === '') {
attrs += `${key} `
continue
}

if (value === 0) value += ''

if (k === 'style') value = styleObjectToString(value)

if (value) attrs += `${key}="${value}"`
Expand Down

0 comments on commit c9a0e1d

Please sign in to comment.