Skip to content

Commit

Permalink
Add support for null/undefined values in array classnames
Browse files Browse the repository at this point in the history
  • Loading branch information
oamaok committed Nov 10, 2024
1 parent d86d7a3 commit 88f6038
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/kaiku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ type Render = <PropertiesT extends DefaultProps>(
rootElement: HTMLElement | SVGElement
) => void

type ClassNames = string | { [key: string]: boolean } | ClassNames[]
type ClassNames =
| undefined
| null
| string
| { [key: string]: boolean }
| ClassNames[]

//
// Generic utilities
Expand Down Expand Up @@ -1161,6 +1166,10 @@ const destroyLazyUpdates = (instance: HtmlElementInstance) => {
///////////////

const stringifyClassNames = (names: ClassNames): string => {
if (names === null || typeof names === 'undefined') {
return ''
}

if (typeof names === 'string') {
return names
}
Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/kaiku.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ exports[`kaiku should be able to access this.props 1`] = `"<div>hello</div>"`;

exports[`kaiku should be able to inject raw HTML 1`] = `"<div><div>foo<span>bar</span></div></div>"`;

exports[`kaiku should handle array of classnames including \`null\` 1`] = `"<div class=\\"\\"></div>"`;
exports[`kaiku should handle array of classnames including \`undefined\` 1`] = `"<div class=\\"\\"></div>"`;
exports[`kaiku should handle changing rootNode.firstChild 1`] = `"<div>Hello, I am a div tag!</div>"`;
exports[`kaiku should handle changing rootNode.firstChild 2`] = `"<span>Hello, I am a span tag!</span>"`;
Expand Down
18 changes: 18 additions & 0 deletions test/kaiku.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1945,4 +1945,22 @@ describe('kaiku', () => {
render(<App />, rootNode)
expect(rootNode.innerHTML).toMatchSnapshot()
})

it('should handle array of classnames including `undefined`', async () => {
const App = () => {
return <div class={[undefined]} />
}

render(<App />, rootNode)
expect(rootNode.innerHTML).toMatchSnapshot()
})

it('should handle array of classnames including `null`', async () => {
const App = () => {
return <div class={[null]} />
}

render(<App />, rootNode)
expect(rootNode.innerHTML).toMatchSnapshot()
})
})

0 comments on commit 88f6038

Please sign in to comment.