Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

fix Form ref #32

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
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
32 changes: 24 additions & 8 deletions packages/@next-fetch/react-query/src/form.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type HTMLProps, createElement } from "react";
import { type HTMLProps, createElement, forwardRef } from "react";
import { useForm as useForm_ } from "@next-fetch/core-plugin/form";
import type {
UseMutationResult,
Expand Down Expand Up @@ -32,15 +32,31 @@ export function useForm<Data, Error, Input, Context>(
* This enables progressive enhancement, as the form can be submitted
* without having to re-render the app using JavaScript code.
*/
export function Form<Data, Error, Input, Context>({
mutation,
mutationConfig,
...props
}: React.HTMLProps<HTMLFormElement> &

// 4. https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref/58473012#58473012
declare module "react" {
function forwardRef<T, P = {}>(
render: (props: P, ref: React.ForwardedRef<T>) => React.ReactElement | null
): (props: P & React.RefAttributes<T>) => React.ReactElement | null;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry about this changing customer code because AFAIK TypeScript declarations are global. Maybe we should "unsafely" type-cast?

// typed-forward-ref.ts

import { forwardRef as forwardRef_ } from "react";
const forwardRef = forwardRef_ as unknown as TYPEDEF_HERE;
export default forwardRef;

wdyt? 👀


type FormProps<Data, Error, Input, Context> = React.HTMLProps<HTMLFormElement> &
React.PropsWithChildren<{
mutation: HookWithFormSubmission<Data, Error, Input, Context>;
mutationConfig?: UseMutationOptions<Data, Error, Input, Context>;
}>) {
}>;

export function FormImpl<Data, Error, Input, Context>(
{
mutation,
mutationConfig,
...props
}: FormProps<Data, Error, Input, Context>,
ref: React.ForwardedRef<HTMLFormElement>
) {
const { formProps } = useForm(mutation, mutationConfig);
return createElement("form", { ...formProps, ...props }, props.children);

return createElement("form", { ...formProps, ...props, ref }, props.children);
}

export const Form = forwardRef(FormImpl)
28 changes: 20 additions & 8 deletions packages/@next-fetch/swr/src/form.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type HTMLProps, createElement } from "react";
import { type HTMLProps, createElement, forwardRef } from "react";
import { useForm as useForm_ } from "@next-fetch/core-plugin/form";
import type {
SWRMutationResponse,
Expand Down Expand Up @@ -32,15 +32,27 @@ export function useForm<Data, Error>(
* This enables progressive enhancement, as the form can be submitted
* without having to re-render the app using JavaScript code.
*/
export function Form<Data, Error>({
mutation,
mutationConfig,
...props
}: React.HTMLProps<HTMLFormElement> &

// 4. https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref/58473012#58473012
declare module "react" {
function forwardRef<T, P = {}>(
render: (props: P, ref: React.ForwardedRef<T>) => React.ReactElement | null
): (props: P & React.RefAttributes<T>) => React.ReactElement | null;
}

type FormProps<Data, Error> = React.HTMLProps<HTMLFormElement> &
React.PropsWithChildren<{
mutation: HookWithFormSubmission<Data, Error>;
mutationConfig?: SWRMutationConfiguration<Data, Error>;
}>) {
}>;

function FormImpl<Data, Error>(
{ mutation, mutationConfig, ...props }: FormProps<Data, Error>,
ref?: React.ForwardedRef<HTMLFormElement>
) {
const { formProps } = useForm(mutation, mutationConfig);
return createElement("form", { ...formProps, ...props }, props.children);

return createElement("form", { ...formProps, ...props, ref }, props.children);
}

export const Form = forwardRef(FormImpl)