Skip to content

Commit

Permalink
feat(jsx): export FC (#1420)
Browse files Browse the repository at this point in the history
* feat(jsx): export `FC`

* denoify
  • Loading branch information
yusukebe authored Sep 9, 2023
1 parent 665f456 commit 27d2620
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
27 changes: 27 additions & 0 deletions deno_dist/jsx/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { html } from '../helper/html/index.ts'
import { Hono } from '../hono.ts'
import type { FC } from './index.ts'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jsx, memo, Fragment } from './index.ts'

Expand Down Expand Up @@ -271,6 +272,32 @@ describe('render to string', () => {
})
})

describe('FC', () => {
it('Should define the type correctly', () => {
const Layout: FC<{ title: string }> = (props) => {
return (
<html>
<head>
<title>{props.title}</title>
</head>
<body>{props.children}</body>
</html>
)
}

const Top = (
<Layout title='Home page'>
<h1>Hono</h1>
<p>Hono is great</p>
</Layout>
)

expect(Top.toString()).toBe(
'<html><head><title>Home page</title></head><body><h1>Hono</h1><p>Hono is great</p></body></html>'
)
})
})

describe('style attribute', () => {
it('should convert the object to strings', () => {
const template = (
Expand Down
4 changes: 2 additions & 2 deletions deno_dist/jsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const jsxFn = (
}
}

type FC<T = Props> = (props: T) => HtmlEscapedString
export type FC<T = Props> = (props: T & { children?: Child }) => HtmlEscapedString

const shallowEqual = (a: Props, b: Props): boolean => {
if (a === b) {
Expand Down Expand Up @@ -226,7 +226,7 @@ export const memo = <T>(
): FC<T> => {
let computed = undefined
let prevProps: T | undefined = undefined
return ((props: T): HtmlEscapedString => {
return ((props: T & { children?: Child }): HtmlEscapedString => {
if (prevProps && !propsAreEqual(prevProps, props)) {
computed = undefined
}
Expand Down
27 changes: 27 additions & 0 deletions src/jsx/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { html } from '../helper/html'
import { Hono } from '../hono'
import type { FC } from './index'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jsx, memo, Fragment } from './index'

Expand Down Expand Up @@ -271,6 +272,32 @@ describe('render to string', () => {
})
})

describe('FC', () => {
it('Should define the type correctly', () => {
const Layout: FC<{ title: string }> = (props) => {
return (
<html>
<head>
<title>{props.title}</title>
</head>
<body>{props.children}</body>
</html>
)
}

const Top = (
<Layout title='Home page'>
<h1>Hono</h1>
<p>Hono is great</p>
</Layout>
)

expect(Top.toString()).toBe(
'<html><head><title>Home page</title></head><body><h1>Hono</h1><p>Hono is great</p></body></html>'
)
})
})

describe('style attribute', () => {
it('should convert the object to strings', () => {
const template = (
Expand Down
4 changes: 2 additions & 2 deletions src/jsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const jsxFn = (
}
}

type FC<T = Props> = (props: T) => HtmlEscapedString
export type FC<T = Props> = (props: T & { children?: Child }) => HtmlEscapedString

const shallowEqual = (a: Props, b: Props): boolean => {
if (a === b) {
Expand Down Expand Up @@ -226,7 +226,7 @@ export const memo = <T>(
): FC<T> => {
let computed = undefined
let prevProps: T | undefined = undefined
return ((props: T): HtmlEscapedString => {
return ((props: T & { children?: Child }): HtmlEscapedString => {
if (prevProps && !propsAreEqual(prevProps, props)) {
computed = undefined
}
Expand Down

0 comments on commit 27d2620

Please sign in to comment.