Skip to content

Update withErrorBoundary types for forwards compat #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"@typescript-eslint/eslint-plugin": "^5.52.0",
"assert": "^2.0.0",
Expand Down
33 changes: 20 additions & 13 deletions pnpm-lock.yaml

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

24 changes: 23 additions & 1 deletion src/withErrorBoundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<HTMLDivElement, Props>((props, ref) => {
return <div ref={ref}>{props.foo}</div>;
});
Div.displayName = "Div";

const Wrapped = withErrorBoundary(Div, {
fallback: <div>Error</div>,
});

const ref = createRef<HTMLDivElement>();

act(() => {
root.render(<Wrapped foo="abc" ref={ref} />);
});

expect(ref.current).not.toBeNull();
expect(ref.current).toBeInstanceOf(HTMLDivElement);
});
});
24 changes: 13 additions & 11 deletions src/withErrorBoundary.ts
Original file line number Diff line number Diff line change
@@ -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<Props extends object>(
component: ComponentType<Props>,
export function withErrorBoundary<T extends ComponentType<any>>(
component: T,
errorBoundaryProps: ErrorBoundaryProps
): ForwardRefExoticComponent<PropsWithoutRef<Props> & RefAttributes<any>> {
const Wrapped = forwardRef<ComponentType<Props>, Props>(
(props: Props, ref: ForwardedRef<ComponentType<Props>>) =>
createElement(
ErrorBoundary,
errorBoundaryProps,
createElement(component, { ...props, ref })
)
): ForwardRefExoticComponent<
PropsWithoutRef<ComponentProps<T>> & RefAttributes<ComponentRef<T>>
> {
const Wrapped = forwardRef<ComponentRef<T>, ComponentProps<T>>((props, ref) =>
createElement(
ErrorBoundary,
errorBoundaryProps,
createElement(component, { ...props, ref })
)
);

// Format for display in DevTools
Expand Down