final-form / react-final-form

🏁 High performance subscription-based form state management for React
https://final-form.org/react
MIT License
7.39k stars 481 forks source link

InitialFormValues dosen't bridge to FormApi #997

Open 8823-scholar opened 2 years ago

8823-scholar commented 2 years ago

Are you submitting a bug report or a feature request?

bug report

What is the current behavior?

type Data = {
  user: {
    name: string;
    email: string;
  };
};

type RecursivePartial<T> = {
  [P in keyof T]?: T[P] extends (infer U)[]
    ? RecursivePartial<U>[]
    : T[P] extends object
    ? RecursivePartial<T[P]>
    : T[P];
};

<Form<Data, RecursivePartial<Data>> onSubmit={onSubmit}>
    {({ handleSubmit, form }) => {
      useEffect(() => {
        form.initialize({
          user: {
            name: "aaa"
          }
        });
      }, [form]);
      return (
        <form onSubmit={handleSubmit}>
          <Field name="user.name" component="input" />
          <Field name="user.email" component="input" />
        </form>
      );
    }}
  </Form>

form.initialize values type is Partial<Data>.

What is the expected behavior?

form.initialize values type should be RecursivePartial<Data>.

Sandbox Link

https://codesandbox.io/s/laughing-sound-fk5rz4?file=/src/Form.tsx

What's your environment?

    "final-form": "4.20.7",
    "react": "18.0.0",
    "react-dom": "18.0.0",
    "react-final-form": "6.5.9",

Other information

nothing.