diff --git a/package.json b/package.json index fa5c807..4f21b2f 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "@preconstruct/cli": "^2.8.1", "@types/assert": "^1.5.10", "@types/jest": "^26.0.15", - "@types/react": "^18", + "@types/react": "^18.3.17", "@types/react-dom": "^18", "assert": "^2.0.0", "eslint": "^9.13.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cd9bd83..04c0e3b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -28,8 +28,8 @@ importers: specifier: ^26.0.15 version: 26.0.24 '@types/react': - specifier: ^18 - version: 18.2.14 + specifier: ^18.3.17 + version: 18.3.18 '@types/react-dom': specifier: ^18 version: 18.2.6 @@ -984,15 +984,12 @@ packages: '@types/react-dom@18.2.6': resolution: {integrity: sha512-2et4PDvg6PVCyS7fuTc4gPoksV58bW0RwSxWKcPRcHZf0PRUGq03TKcD/rUHe3azfV6/5/biUBJw+HhCQjaP0A==} - '@types/react@18.2.14': - resolution: {integrity: sha512-A0zjq+QN/O0Kpe30hA1GidzyFjatVvrpIvWLxD+xv67Vt91TWWgco9IvrJBkeyHm1trGaFS/FSGqPlhyeZRm0g==} + '@types/react@18.3.18': + resolution: {integrity: sha512-t4yC+vtgnkYjNSKlFx1jkAhH8LgTo2N/7Qvi83kdEaUtMDiwpbLAktKDaAMlRcJ5eSxZkH74eEGt1ky31d7kfQ==} '@types/resolve@1.17.1': resolution: {integrity: sha512-yy7HuzQhj0dhGpD8RLXSZWEkLsV9ibvxvi6EiJ3bkqLAO1RGo0WbkWQiwpRlSFymTJRz0d3k5LM3kkx8ArDbLw==} - '@types/scheduler@0.16.3': - resolution: {integrity: sha512-5cJ8CB4yAx7BH1oMvdU0Jh9lrEXyPkar6F9G/ERswkCuvP4KQZfZkSjcMbAICCpQTN4OuZn8tz0HiKv9TGZgrQ==} - '@types/stack-utils@2.0.1': resolution: {integrity: sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==} @@ -4667,20 +4664,17 @@ snapshots: '@types/react-dom@18.2.6': dependencies: - '@types/react': 18.2.14 + '@types/react': 18.3.18 - '@types/react@18.2.14': + '@types/react@18.3.18': dependencies: '@types/prop-types': 15.7.5 - '@types/scheduler': 0.16.3 csstype: 3.1.2 '@types/resolve@1.17.1': dependencies: '@types/node': 20.3.3 - '@types/scheduler@0.16.3': {} - '@types/stack-utils@2.0.1': {} '@types/tough-cookie@4.0.2': {} diff --git a/src/withErrorBoundary.test.tsx b/src/withErrorBoundary.test.tsx index 1ff1186..3c88bb7 100644 --- a/src/withErrorBoundary.test.tsx +++ b/src/withErrorBoundary.test.tsx @@ -2,7 +2,7 @@ * @jest-environment jsdom */ -import { Component, createRef, PropsWithChildren } from "react"; +import { Component, createRef, forwardRef, PropsWithChildren } from "react"; import { createRoot } from "react-dom/client"; import { act } from "react-dom/test-utils"; import { withErrorBoundary } from "./withErrorBoundary"; @@ -81,4 +81,26 @@ describe("withErrorBoundary", () => { expect(ref.current).not.toBeNull(); expect(typeof ref.current?.test).toBe("function"); }); + + it("should forward dom refs", () => { + type Props = { foo: string }; + + const Div = forwardRef((props, ref) => { + return
{props.foo}
; + }); + Div.displayName = "Div"; + + const Wrapped = withErrorBoundary(Div, { + fallback:
Error
, + }); + + const ref = createRef(); + + act(() => { + root.render(); + }); + + expect(ref.current).not.toBeNull(); + expect(ref.current).toBeInstanceOf(HTMLDivElement); + }); }); diff --git a/src/withErrorBoundary.ts b/src/withErrorBoundary.ts index 9c6df97..5b62576 100644 --- a/src/withErrorBoundary.ts +++ b/src/withErrorBoundary.ts @@ -1,26 +1,28 @@ import { createElement, forwardRef, - ForwardedRef, RefAttributes, ForwardRefExoticComponent, PropsWithoutRef, ComponentType, + ComponentRef, + ComponentProps, } from "react"; import { ErrorBoundary } from "./ErrorBoundary"; import { ErrorBoundaryProps } from "./types"; -export function withErrorBoundary( - component: ComponentType, +export function withErrorBoundary>( + component: T, errorBoundaryProps: ErrorBoundaryProps -): ForwardRefExoticComponent & RefAttributes> { - const Wrapped = forwardRef, Props>( - (props: Props, ref: ForwardedRef>) => - createElement( - ErrorBoundary, - errorBoundaryProps, - createElement(component, { ...props, ref }) - ) +): ForwardRefExoticComponent< + PropsWithoutRef> & RefAttributes> +> { + const Wrapped = forwardRef, ComponentProps>((props, ref) => + createElement( + ErrorBoundary, + errorBoundaryProps, + createElement(component, { ...props, ref }) + ) ); // Format for display in DevTools