[@mantine/form] make Values and TransformedValues covariant
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
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.
@rtivital could you already take a look at this PR? Would be happy about any feedback.
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.
Okay, take your time 👍🏻 Thanks for letting me know.