Skip to content

Commit

Permalink
feat(jsx): handle falsy like React
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperskei committed Jun 6, 2024
1 parent fc58024 commit 72b401b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
82 changes: 82 additions & 0 deletions packages/jsx/src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,86 @@ test('linked list', () => {
// assert.not.ok(isConnected(ctx, jsxList))
})

test('boolean as child', () => {
const { h, hf } = setup()

const trueAtom = atom(true, 'true')
const trueValue = true
const falseAtom = atom(false, 'false')
const falseValue = false

const element = <div>
{trueAtom}
{trueValue}
{falseAtom}
{falseValue}
</div>

assert.is(element.childNodes.length, 2)
assert.is(element.textContent, '')
})

test('null as child', () => {
const { h, hf } = setup()

const nullAtom = atom(null, 'null')
const nullValue = null

const element = <div>
{nullAtom}
{nullValue}
</div>

assert.is(element.childNodes.length, 1)
assert.is(element.textContent, '')
})

test('undefined as child', () => {
const { h, hf } = setup()

const undefinedAtom = atom(undefined, 'undefined')
const undefinedValue = undefined

const element = <div>
{undefinedAtom}
{undefinedValue}
</div>

assert.is(element.childNodes.length, 1)
assert.is(element.textContent, '')
})

test('empty string as child', () => {
const { h, hf } = setup()

const emptyStringAtom = atom('', 'emptyString')
const emptyStringValue = ''

const element = <div>
{emptyStringAtom}
{emptyStringValue}
</div>

assert.is(element.childNodes.length, 1)
assert.is(element.textContent, '')
})

test('update skipped atom', () => {
const { ctx, h, hf, mount, parent } = setup()

const valueAtom = atom<number | undefined>(undefined, 'value')

const element = <div>{valueAtom}</div>

mount(parent, element)

assert.is(parent.childNodes.length, 1)
assert.is(parent.textContent, '')

valueAtom(ctx, 123)

assert.is(parent.childNodes.length, 1)
assert.is(parent.textContent, '123')
})

test.run()
6 changes: 4 additions & 2 deletions packages/jsx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type DomApis = Pick<
| 'DocumentFragment'
>

const isSkipped = (value: unknown): value is boolean | '' | null | undefined => typeof value === 'boolean' || value === '' || value == null

let unsubscribesMap = new WeakMap<HTMLElement, Array<Fn>>()
let unlink = (parent: any, un: Unsubscribe) => {
// check the connection in the next tick
Expand Down Expand Up @@ -241,7 +243,7 @@ export const reatomJsx = (ctx: Ctx, DOM: DomApis = globalThis.window) => {
}
} else {
// TODO more tests
innerChild.textContent = String(v)
innerChild.textContent = isSkipped(v) ? '' : String(v)
}
}
} catch (e) {
Expand All @@ -251,7 +253,7 @@ export const reatomJsx = (ctx: Ctx, DOM: DomApis = globalThis.window) => {
if (error) throw error
unlink(element, un)
element.appendChild(innerChild)
} else {
} else if (!isSkipped(child)) {
element.appendChild(
isObject(child) && 'nodeType' in child
? (child as JSX.Element)
Expand Down
2 changes: 1 addition & 1 deletion packages/jsx/src/jsx.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Respectfully copied from https://github.com/ryansolid/dom-expressions/blob/ae71a417aa13b33517082628aff09513629df8b2/packages/dom-expressions/src/jsx.d.ts
*/

Expand Down

0 comments on commit 72b401b

Please sign in to comment.