Replies: 2 comments
-
|
This is the closest I was able to come up with for now: import { FormAsyncValidateOrFn, FormValidateOrFn } from "@tanstack/react-form";
import React, { type FormHTMLAttributes, type ReactNode, Suspense } from "react";
import { useAppForm } from "../../hooks/useAppForm";
type AppFieldExtendedReactFormApi<
TFormData,
TOnMount extends undefined | FormValidateOrFn<TFormData>,
TOnChange extends undefined | FormValidateOrFn<TFormData>,
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
TSubmitMeta,
> = ReturnType<
typeof useAppForm<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
>
>;
interface FormProps<
TFormData,
TOnMount extends undefined | FormValidateOrFn<TFormData>,
TOnChange extends undefined | FormValidateOrFn<TFormData>,
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
TSubmitMeta,
> extends FormHTMLAttributes<HTMLFormElement> {
form: AppFieldExtendedReactFormApi<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
>;
fallback?: ReactNode;
}
export const Form = <
TFormData,
TOnMount extends undefined | FormValidateOrFn<TFormData>,
TOnChange extends undefined | FormValidateOrFn<TFormData>,
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
TSubmitMeta,
>({
children,
form,
fallback = null,
...props
}: FormProps<
TFormData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
>) => {
return (
<form {...props}>
<Suspense fallback={fallback}>
<form.AppForm>{children}</form.AppForm>
</Suspense>
</form>
);
};Usage: const form = useForm({ ... });
return (
<Form form={form} onSubmit={...}>
{...}
</Form>
); |
Beta Was this translation helpful? Give feedback.
-
|
I would love the ability to do this as well! I'm creating a custom wizard hook and components with tanstack form and the ability to embellish An API like this has occurred to me. I'm sure getting the typing working properly for the custom components will be difficult, but maybe it could be helped by a higher-order component, like export const { useAppForm: useForm } = createFormHook({
formContext,
fieldContext,
formComponents,
fieldComponents,
formComponent: MyForm, // Auto-wraps in formContext.Provider
fieldComponent: MyField, // Auto-wraps in fieldContext.Provider
});And then at the callsites: const form = useForm(formOptions)
<form.Form customProp={value}>
<form.Field customProp={value} />
</form.Form>Replacing the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Similar to creating custom fields, I'd like to make a custom form component so that I don't have to create both a
formelement and aform.AppFormchild beneath that. (And perhaps even handle theSuspenseaspect as well for lazy-loaded fields.) I'd like to do something like this:Then use it to render a form like so:
Beta Was this translation helpful? Give feedback.
All reactions