mantine icon indicating copy to clipboard operation
mantine copied to clipboard

[@mantine/form] make Values and TransformedValues covariant

Open jvllmr opened this issue 6 months ago • 4 comments

This allows type-safely passing a form to a func which accepts a form with only selected fields of the original form.

Here an example:

import { UseFormReturnType, useForm } from '@mantine/form';



function PasswordSection<TForm extends UseFormReturnType<{password: string}>(
  {form}: {form: TForm}
) {
  // add buttons such as password generator button
  // ...
}

interface SignupFormValues {
  username: string;
  password: string;
}

export function SignupForm() {
  const form = useForm<SignupFormValues>({ initialValues: { username: '', password: '' } })
   
  return <form>
  {
  // ...
  }
  <PasswordSection form={form} />
  {
  // ...
  }
 </form>;
}

Related discussions:

  • https://github.com/orgs/mantinedev/discussions/7171
  • https://github.com/orgs/mantinedev/discussions/7195

This is just a proposal. Feel free to close the PR if you'd like. I needed to create a new UseFormReturnType, but kept the old one around so that no breaking changes are made. Edit: It is possible without an extra type

jvllmr avatar Aug 24 '25 10:08 jvllmr

I also looked into improved inference and value passing for the TransformValues / TransformedValues type, but I think for that TransformValues must be removed in favor of using only TransformedValues. But that would be a breaking change. I could start to create a draft PR for v9 though if there is interest.

jvllmr avatar Aug 24 '25 19:08 jvllmr

@rtivital could you already take a look at this PR? Would be happy about any feedback.

jvllmr avatar Sep 08 '25 17:09 jvllmr

Yes, I've reviewed it, but I do not fully understand the new TS syntax (out part). I need more time for a deep dive.

rtivital avatar Sep 08 '25 19:09 rtivital

Okay, take your time 👍🏻 Thanks for letting me know.

jvllmr avatar Sep 08 '25 20:09 jvllmr