import { FieldGroupApi, functionalUpdate } from '@tanstack/form-core' import { useStore } from '@tanstack/solid-store' import { onCleanup, onMount } from 'solid-js' import type { Component, JSX, ParentProps } from 'solid-js' import type { DeepKeysOfType, FieldGroupState, FieldsMap, FormAsyncValidateOrFn, FormValidateOrFn, } from '@tanstack/form-core' import type { LensFieldComponent } from './createField' import type { AppFieldExtendedSolidFormApi } from './createFormHook' /** * @private */ export type AppFieldExtendedSolidFieldGroupApi< TFormData, TFieldGroupData, TFields extends | DeepKeysOfType | FieldsMap, TOnMount extends undefined | FormValidateOrFn, TOnChange extends undefined | FormValidateOrFn, TOnChangeAsync extends undefined | FormAsyncValidateOrFn, TOnBlur extends undefined | FormValidateOrFn, TOnBlurAsync extends undefined | FormAsyncValidateOrFn, TOnSubmit extends undefined | FormValidateOrFn, TOnSubmitAsync extends undefined | FormAsyncValidateOrFn, TOnDynamic extends undefined | FormValidateOrFn, TOnDynamicAsync extends undefined | FormAsyncValidateOrFn, TOnServer extends undefined | FormAsyncValidateOrFn, TSubmitMeta, TFieldComponents extends Record>, TFormComponents extends Record>, > = FieldGroupApi< TFormData, TFieldGroupData, TFields, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta > & NoInfer & { AppField: LensFieldComponent< TFieldGroupData, TSubmitMeta, NoInfer > AppForm: Component /** * A solid component to render form fields. With this, you can render and manage individual form fields. */ Field: LensFieldComponent /** * A `Subscribe` function that allows you to listen and solid to changes in the form's state. It's especially useful when you need to execute side effects or render specific components in response to state updates. */ Subscribe: >>(props: { selector?: (state: NoInfer>) => TSelected children: ((state: NoInfer) => JSX.Element) | JSX.Element }) => JSX.Element } export function createFieldGroup< TFormData, TFieldGroupData, TFields extends | DeepKeysOfType | FieldsMap, TOnMount extends undefined | FormValidateOrFn, TOnChange extends undefined | FormValidateOrFn, TOnChangeAsync extends undefined | FormAsyncValidateOrFn, TOnBlur extends undefined | FormValidateOrFn, TOnBlurAsync extends undefined | FormAsyncValidateOrFn, TOnSubmit extends undefined | FormValidateOrFn, TOnSubmitAsync extends undefined | FormAsyncValidateOrFn, TOnDynamic extends undefined | FormValidateOrFn, TOnDynamicAsync extends undefined | FormAsyncValidateOrFn, TOnServer extends undefined | FormAsyncValidateOrFn, TComponents extends Record>, TFormComponents extends Record>, TSubmitMeta = never, >( opts: () => { form: | AppFieldExtendedSolidFormApi< TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, TComponents, TFormComponents > | AppFieldExtendedSolidFieldGroupApi< // Since this only occurs if you nest it within other form lenses, it can be more // lenient with the types. unknown, TFormData, string | FieldsMap, any, any, any, any, any, any, any, any, any, any, TSubmitMeta, TComponents, TFormComponents > fields: TFields defaultValues?: TFieldGroupData onSubmitMeta?: TSubmitMeta formComponents: TFormComponents }, ): AppFieldExtendedSolidFieldGroupApi< TFormData, TFieldGroupData, TFields, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, TComponents, TFormComponents > { const options = opts() const api = new FieldGroupApi(options) const form = options.form instanceof FieldGroupApi ? (options.form.form as AppFieldExtendedSolidFormApi< TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, TComponents, TFormComponents >) : options.form const extendedApi: AppFieldExtendedSolidFieldGroupApi< TFormData, TFieldGroupData, TFields, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, TComponents, TFormComponents > = api as never extendedApi.AppForm = (appFormProps) => extendedApi.AppField = (props) => ( ) extendedApi.Field = (props) => ( ) extendedApi.Subscribe = (props) => { const data = useStore(api.store, props.selector) return functionalUpdate(props.children, data()) as Element } let mounted = false onMount(() => { const cleanupFn = api.mount() mounted = true onCleanup(() => { cleanupFn() mounted = false }) }) return Object.assign(extendedApi, { ...options.formComponents, }) as AppFieldExtendedSolidFieldGroupApi< TFormData, TFieldGroupData, TFields, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, TComponents, TFormComponents > }