Check if a key exists in Fieldset
#253
-
I have a UI component that can take 2 different const [form, formFieldset] = useForm<FormSchemaCreate | FormSchemaCreateMany>({
lastSubmission,
});
when I try to access the error message for <Form {...form.props}>
{isCreateMany && (
<TextField
id="sets-input"
name="sets"
required={required}
error={Boolean(formFieldset?.sets.error)} // type error: "sets" doesn't exist on FormSchemaCreate
helperText={formFieldset?.sets.error} // type error: "sets" doesn't exist on FormSchemaCreate
inputProps={{
inputMode: "numeric",
pattern: "[0-9]*",
}}
/>
)}
... other form fields
</Form> Is there a way to check if a key exists on a const isSetsInFieldset = "sets" in formFieldset; Currently the above snippet will always return |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
This seems to work fine for me on v0.7.4: const [form, { name, a, b }] = useForm<{ name: string, a: string; } | { name: string; b: string }>({
lastSubmission,
});
console.log({ name, a, b }); Which version are you using? There were some improvements with union support back in v0.6.2. Conform doesn't actually know about which fields you had on the form. The |
Beta Was this translation helpful? Give feedback.
-
Ah interesting, So I am passing the import { Fieldset, Submission, useForm } from "@conform-to/react";
type FormCreateSchema = z.output<typeof formCreateRepsSchema>;
type FormCreateManySchema = z.output<typeof formCreateManyRepsSchema>;
type myUIComponentProps = {
formFieldset: Fieldset<FormCreateSchema> | Fieldset<FormCreateManySchema>;
};
export function MyMainComponent(): JSX.Element {
const [form, formFieldset] = useForm<FormSchemaCreate | FormSchemaCreateMany>({
lastSubmission,
});
return (
<Form {...form.props}>
<MyUIComponent formFieldset={formFieldset} />
</Form>
)
}
export function MyUIComponent(props: myUIComponentProps): JSX.Element {
return
({isCreateMany && (
<TextField
id="sets-input"
name="sets"
required={required}
error={Boolean(formFieldset.sets.error)} // type error: "sets" doesn't exist on FormSchemaCreate
helperText={formFieldset.sets.error} // type error: "sets" doesn't exist on FormSchemaCreate
inputProps={{
inputMode: "numeric",
pattern: "[0-9]*",
}}
/>
)}
... other form fields)
} Maybe I am typing the |
Beta Was this translation helpful? Give feedback.
Maybe try this instead: