How to make part of the forms reusable? #1129
-
Hi, // this is my form definition
const form useForm({
defaultValues: {
customerData: {
email: "",
phone: "",
name: "",
surname: "",
note: "",
},
},
})
// it generates this type
type Form = ReactFormExtendedApi<{
customerData: {
email: string;
phone: string;
name: string;
surname: string;
note: string;
};
}, undefined>
// then used like
<SectionCustomerData
form={form} // here is thrown the error
/> Then I want to use it like this in reused component export function SectionCustomerData({
form,
}: {
form: ReactFormExtendedApi<
{
customerData: {
name: string;
surname: string;
};
},
undefined
>;
}) {
// just an example, but it should be enough
return <form.Field
name="customerData.name"
children={(field) => (
<InputField
labelProps={{
htmlFor: field.name,
}}
inputProps={{
value: field.state.value,
onChange: (e) => field.handleChange(e.target.value),
onBlur: field.handleBlur,
}}
errors={field.state.meta.errors}
/>
)}
/>
} The problem is the types are incompatible??? I really do not understand it, because the form fields in the Can you help me? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I also tried some generics magic yet with no result. |
Beta Was this translation helpful? Give feedback.
-
@thefrana We accomplished this by doing something like this:
The main issue is having to cast for the
but you don't get any type safety on
and you get type safety but I haven't been able to clear the error if there is one. The casting isn't too big a problem for us as we use strict |
Beta Was this translation helpful? Give feedback.
-
@thefrana the answer to your question is "you'll need to wait"... Theres an open PR #825 that will allow you to create re-usable components. But, it's not merged yet, theres a few internal types that need to be exposed, so unless you want to recreate them in your local code base you'll have to wait. As you'll see from everyones else’s attempts (including my own #1084), you run into typing issues, and this only becomes more of an issue the more you have things like validators, listeners, and other functionality. But the maintainers are working on it, in-face it's the last checkmark on the v1.0 checklist, so you wont have to wait long. Hope this helps 🤟 |
Beta Was this translation helpful? Give feedback.
@thefrana the answer to your question is "you'll need to wait"...
Theres an open PR #825 that will allow you to create re-usable components. But, it's not merged yet, theres a few internal types that need to be exposed, so unless you want to recreate them in your local code base you'll have to wait.
As you'll see from everyones else’s attempts (including my own #1084), you run into typing issues, and this only becomes more of an issue the more you have things like validators, listeners, and other functionality.
But the maintainers are working on it, in-face it's the last checkmark on the v1.0 checklist, so you wont have to wait long.
Hope this helps 🤟