feat(middleware/jsx): We need Fragment. (#310)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Taku Amano · committed by GitHub · parent cfc8c6e · Browse files · 3 files·+37 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+37 added−6 removed
- tests:+25 added−3 removed
- other:+12 added−3 removed
Provenance
- Commit
5993b61- Tree
6221c3e- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
cfc8c6e
3 files changed+37 added−6 removed
modified:src/middleware/jsx/index.test.tsx+25 added-3 removed
1223456138 unmodified linesExpand all1451461471481491501491501511521531544 unmodified lines159160161162163164165166167168169170171172173174175176177178179180181182183184import { Hono } from '../../hono'import { h, jsx, memo } from '.'import { h, jsx, memo, Fragment } from '.'
describe('JSX middleware', () => { const app = new Hono() app.use('*', jsx())138 unmodified linesExpand all })
it('custom propsAreEqual', () => { const Body = memo( ({ counter }: { counter: number, refresh?: boolean }) => <span>{counter}</span>, (_, nextProps) => typeof nextProps.refresh == 'undefined' ? true : !nextProps.refresh ({ counter }: { counter: number; refresh?: boolean }) => <span>{counter}</span>, (_, nextProps) => (typeof nextProps.refresh == 'undefined' ? true : !nextProps.refresh) )
let template = <Body counter={0} /> expect(template.toString()).toBe('<span>0</span>')4 unmodified lines template = <Body counter={2} refresh={true} /> expect(template.toString()).toBe('<span>2</span>') })})
describe('Fragment', () => { it('Should render children', () => { const template = ( <> <p>1</p> <p>2</p> </> ) expect(template.toString()).toBe('<p>1</p><p>2</p>') })
it('Should render nothing for empty Fragment', () => { const template = <></> expect(template.toString()).toBe('') })
it('Should render nothing for undefined', () => { const template = <>{undefined}</> expect(template.toString()).toBe('') })})modified:src/middleware/jsx/index.ts+11 added-3 removed
30 unmodified lines31 if (typeof tag === 'function') {32 return tag.call(null, { ...props, children: children.length <= 1 ? children[0] : children })33 }34 35- let result = `<${tag}`35+ let result = tag !== '' ? `<${tag}` : ''36 37 const propsKeys = Object.keys(props || {})38 for (let i = 0, len = propsKeys.length; i < len; i++) {39 const v = props[propsKeys[i]]12 unmodified lines52 53 result += ` ${propsKeys[i]}="${escape(v.toString())}"`54 }55 56+ if (tag !== '') {56- result += '>'57+ result += '>'58+ }59 60 const flattenChildren = children.flat(Infinity)61 for (let i = 0, len = flattenChildren.length; i < len; i++) {62 const child = flattenChildren[i]5 unmodified lines68 result += escape(child.toString())69 }70 }71 72+ if (tag !== '') {70- result += `</${tag}>`73+ result += `</${tag}>`74+ }75 76 const escapedString = new String(result) as EscapedString77 escapedString.isEscaped = true78 35 unmodified lines114 prevProps = props115 return (computed ||= component(props))116 }) as FC<T>117 }118+119+export const Fragment = (props: { key?: string; children?: any }): EscapedString => {120+ return h('', {}, ...(props.children || []))121+}modified:tsconfig.json+1 added
18 unmodified lines19 "@cloudflare/workers-types"20 ],21 "jsx": "react",22 "jsxFactory": "h",23+ "jsxFragmentFactory": "Fragment",24 },25 "include": [26 "src/**/*.ts",27 "src/**/*.mts",3 unmodified lines