import { FormGroupApi } from '@tanstack/form-core' import { useSelector } from '@tanstack/vue-store' import { defineComponent, onMounted, onUnmounted, watch } from 'vue' import type { DeepKeys, DeepValue, FormAsyncValidateOrFn, FormGroupApiOptions, FormGroupAsyncValidateOrFn, FormGroupOptions, FormGroupValidateOrFn, FormValidateOrFn, } from '@tanstack/form-core' import type { ComponentOptionsMixin, CreateComponentPublicInstanceWithMixins, EmitsOptions, EmitsToProps, PublicProps, SetupContext, SlotsType, } from 'vue' export type FormGroupComponent< TParentData, TFormOnMount extends undefined | FormValidateOrFn, TFormOnChange extends undefined | FormValidateOrFn, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn, TFormOnBlur extends undefined | FormValidateOrFn, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn, TFormOnSubmit extends undefined | FormValidateOrFn, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn, TFormOnDynamic extends undefined | FormValidateOrFn, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn, TFormOnServer extends undefined | FormAsyncValidateOrFn, TParentSubmitMeta, // This complex type comes from Vue's return type for `DefineSetupFnComponent` but with our own types sprinkled in // This allows us to pre-bind some generics while keeping the props type unbound generics for props-based inferencing > = new < TName extends DeepKeys, TData extends DeepValue, TOnMount extends undefined | FormGroupValidateOrFn, TOnChange extends | undefined | FormGroupValidateOrFn, TOnChangeAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnBlur extends undefined | FormGroupValidateOrFn, TOnBlurAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnSubmit extends | undefined | FormGroupValidateOrFn, TOnSubmitAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnDynamic extends | undefined | FormGroupValidateOrFn, TOnDynamicAsync extends | undefined | FormGroupAsyncValidateOrFn, TSubmitMeta, >( props: FormGroupComponentBoundProps< TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TSubmitMeta, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta > & EmitsToProps & PublicProps, ) => CreateComponentPublicInstanceWithMixins< FormGroupComponentBoundProps< TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TSubmitMeta, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta >, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, EmitsOptions, PublicProps, {}, false, {}, SlotsType<{ default: { group: FormGroupApi< TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TSubmitMeta, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta > state: FormGroupApi< TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TSubmitMeta, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta >['state'] } }> > export interface VueFormGroupApi< TParentData, TFormOnMount extends undefined | FormValidateOrFn, TFormOnChange extends undefined | FormValidateOrFn, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn, TFormOnBlur extends undefined | FormValidateOrFn, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn, TFormOnSubmit extends undefined | FormValidateOrFn, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn, TFormOnDynamic extends undefined | FormValidateOrFn, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn, TFormOnServer extends undefined | FormAsyncValidateOrFn, TParentSubmitMeta, > { FormGroup: FormGroupComponent< TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta > } export function useFormGroup< TParentData, TName extends DeepKeys, TData extends DeepValue, TOnMount extends undefined | FormGroupValidateOrFn, TOnChange extends | undefined | FormGroupValidateOrFn, TOnChangeAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnBlur extends undefined | FormGroupValidateOrFn, TOnBlurAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnSubmit extends | undefined | FormGroupValidateOrFn, TOnSubmitAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnDynamic extends | undefined | FormGroupValidateOrFn, TOnDynamicAsync extends | undefined | FormGroupAsyncValidateOrFn, TSubmitMeta, TFormOnMount extends undefined | FormValidateOrFn, TFormOnChange extends undefined | FormValidateOrFn, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn, TFormOnBlur extends undefined | FormValidateOrFn, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn, TFormOnSubmit extends undefined | FormValidateOrFn, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn, TFormOnDynamic extends undefined | FormValidateOrFn, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn, TFormOnServer extends undefined | FormAsyncValidateOrFn, TParentSubmitMeta, >( opts: FormGroupApiOptions< TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TSubmitMeta, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta >, ) { const formGroupApi = (() => { const api = new FormGroupApi({ ...opts, }) return api })() const groupState = useSelector(formGroupApi.store, (state) => state) let cleanup!: () => void onMounted(() => { cleanup = formGroupApi.mount() }) onUnmounted(() => { cleanup() }) watch( () => opts, () => { // Keep options up to date as they are rendered formGroupApi.update({ ...opts } as never) }, ) return { api: formGroupApi, state: groupState } as const } export type FormGroupComponentProps< TParentData, TName extends DeepKeys, TData extends DeepValue, TOnMount extends undefined | FormGroupValidateOrFn, TOnChange extends | undefined | FormGroupValidateOrFn, TOnChangeAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnBlur extends undefined | FormGroupValidateOrFn, TOnBlurAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnSubmit extends | undefined | FormGroupValidateOrFn, TOnSubmitAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnDynamic extends | undefined | FormGroupValidateOrFn, TOnDynamicAsync extends | undefined | FormGroupAsyncValidateOrFn, TSubmitMeta, TFormOnMount extends undefined | FormValidateOrFn, TFormOnChange extends undefined | FormValidateOrFn, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn, TFormOnBlur extends undefined | FormValidateOrFn, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn, TFormOnSubmit extends undefined | FormValidateOrFn, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn, TFormOnDynamic extends undefined | FormValidateOrFn, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn, TFormOnServer extends undefined | FormAsyncValidateOrFn, TParentSubmitMeta, > = FormGroupApiOptions< TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TSubmitMeta, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta > export type FormGroupComponentBoundProps< TParentData, TName extends DeepKeys, TData extends DeepValue, TOnMount extends undefined | FormGroupValidateOrFn, TOnChange extends | undefined | FormGroupValidateOrFn, TOnChangeAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnBlur extends undefined | FormGroupValidateOrFn, TOnBlurAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnSubmit extends | undefined | FormGroupValidateOrFn, TOnSubmitAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnDynamic extends | undefined | FormGroupValidateOrFn, TOnDynamicAsync extends | undefined | FormGroupAsyncValidateOrFn, TSubmitMeta, TFormOnMount extends undefined | FormValidateOrFn, TFormOnChange extends undefined | FormValidateOrFn, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn, TFormOnBlur extends undefined | FormValidateOrFn, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn, TFormOnSubmit extends undefined | FormValidateOrFn, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn, TFormOnDynamic extends undefined | FormValidateOrFn, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn, TFormOnServer extends undefined | FormAsyncValidateOrFn, TParentSubmitMeta, > = FormGroupOptions< TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TSubmitMeta, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta > export const FormGroup = defineComponent( < TParentData, TName extends DeepKeys, TData extends DeepValue, TOnMount extends | undefined | FormGroupValidateOrFn, TOnChange extends | undefined | FormGroupValidateOrFn, TOnChangeAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnBlur extends | undefined | FormGroupValidateOrFn, TOnBlurAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnSubmit extends | undefined | FormGroupValidateOrFn, TOnSubmitAsync extends | undefined | FormGroupAsyncValidateOrFn, TOnDynamic extends | undefined | FormGroupValidateOrFn, TOnDynamicAsync extends | undefined | FormGroupAsyncValidateOrFn, TSubmitMeta, TFormOnMount extends undefined | FormValidateOrFn, TFormOnChange extends undefined | FormValidateOrFn, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn, TFormOnBlur extends undefined | FormValidateOrFn, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn, TFormOnSubmit extends undefined | FormValidateOrFn, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn, TFormOnDynamic extends undefined | FormValidateOrFn, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn, TFormOnServer extends undefined | FormAsyncValidateOrFn, TParentSubmitMeta, >( formGroupOptions: FormGroupApiOptions< TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TSubmitMeta, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TParentSubmitMeta >, context: SetupContext, ) => { const groupApi = useFormGroup({ ...formGroupOptions, ...context.attrs }) return () => context.slots.default!({ group: groupApi.api, state: groupApi.state.value, }) }, { name: 'FormGroup', inheritAttrs: false }, )