Skip to content

Commit

Permalink
fix(hono-jsx): make ref unrequried for forward ref (#2715)
Browse files Browse the repository at this point in the history
* make ref unrequried for forward ref

* make ref unrequried for forward ref
  • Loading branch information
dygy committed May 19, 2024
1 parent ee9ccf8 commit ba644d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions deno_dist/jsx/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ export const createRef = <T>(): RefObject<T> => {
}

export const forwardRef = <T, P = {}>(
Component: (props: P, ref: RefObject<T>) => JSX.Element
): ((props: P & { ref: RefObject<T> }) => JSX.Element) => {
Component: (props: P, ref?: RefObject<T>) => JSX.Element
): ((props: P & { ref?: RefObject<T> }) => JSX.Element) => {
return (props) => {
const { ref, ...rest } = props
return Component(rest as P, ref)
Expand Down
8 changes: 8 additions & 0 deletions src/jsx/hooks/dom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,14 @@ describe('Hooks', () => {
expect(root.innerHTML).toBe('<div></div>')
expect(ref.current).toBeInstanceOf(HTMLElement)
})

it('can run without ref', () => {
const App = forwardRef((props) => {
return <div {...props} />
})
render(<App />, root)
expect(root.innerHTML).toBe('<div></div>')
})
})

describe('useImperativeHandle()', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/jsx/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ export const createRef = <T>(): RefObject<T> => {
}

export const forwardRef = <T, P = {}>(
Component: (props: P, ref: RefObject<T>) => JSX.Element
): ((props: P & { ref: RefObject<T> }) => JSX.Element) => {
Component: (props: P, ref?: RefObject<T>) => JSX.Element
): ((props: P & { ref?: RefObject<T> }) => JSX.Element) => {
return (props) => {
const { ref, ...rest } = props
return Component(rest as P, ref)
Expand Down

0 comments on commit ba644d4

Please sign in to comment.