@octanejs/tanstack-form 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +102 -0
- package/package.json +54 -0
- package/src/createFormHook.tsrx +864 -0
- package/src/createFormHook.tsrx.d.ts +101 -0
- package/src/index.ts +11 -0
- package/src/types.ts +122 -0
- package/src/useField.tsrx +656 -0
- package/src/useField.tsrx.d.ts +55 -0
- package/src/useFieldGroup.tsrx +252 -0
- package/src/useFieldGroup.tsrx.d.ts +37 -0
- package/src/useForm.tsrx +296 -0
- package/src/useForm.tsrx.d.ts +35 -0
- package/src/useFormGroup.tsrx +633 -0
- package/src/useFormGroup.tsrx.d.ts +16 -0
- package/src/useFormId.ts +4 -0
- package/src/useIsomorphicLayoutEffect.ts +4 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Declaration companion generated from useField.tsrx.
|
|
2
|
+
import { FieldApi } from '@tanstack/form-core';
|
|
3
|
+
import type { DeepKeys, DeepValue, FieldAsyncValidateOrFn, FieldValidateOrFn, FieldValidators, FormAsyncValidateOrFn, FormValidateOrFn } from '@tanstack/form-core';
|
|
4
|
+
import type { UseFieldOptions, UseFieldOptionsBound } from './types';
|
|
5
|
+
type FieldRenderable = unknown;
|
|
6
|
+
type FieldFunctionComponent<P = object> = (props: P) => FieldRenderable;
|
|
7
|
+
/**
|
|
8
|
+
* A type representing a hook for using a field in a form with the given form data type.
|
|
9
|
+
*
|
|
10
|
+
* A function that takes an optional object with a `name` property and field options, and returns a `FieldApi` instance for the specified field.
|
|
11
|
+
*/
|
|
12
|
+
export type UseField<TParentData, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>, TPatentSubmitMeta> = <TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>>(opts: UseFieldOptionsBound<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync>) => FieldApi<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TPatentSubmitMeta>;
|
|
13
|
+
/**
|
|
14
|
+
* A hook for managing a field in a form.
|
|
15
|
+
* @param opts An object with field options.
|
|
16
|
+
*
|
|
17
|
+
* @returns The `FieldApi` instance for the specified field.
|
|
18
|
+
*/
|
|
19
|
+
export declare function useField<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>, TPatentSubmitMeta>(opts: UseFieldOptions<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TPatentSubmitMeta>): FieldApi<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TPatentSubmitMeta>;
|
|
20
|
+
/**
|
|
21
|
+
* @param children A render function that takes a field API instance and returns a renderable value.
|
|
22
|
+
*/
|
|
23
|
+
interface FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>, TPatentSubmitMeta, ExtendedApi = {}> extends UseFieldOptions<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TPatentSubmitMeta> {
|
|
24
|
+
children: (fieldApi: FieldApi<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TPatentSubmitMeta> & ExtendedApi) => FieldRenderable;
|
|
25
|
+
}
|
|
26
|
+
interface FieldComponentBoundProps<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>, TPatentSubmitMeta, ExtendedApi = {}> extends UseFieldOptionsBound<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync> {
|
|
27
|
+
children: (fieldApi: FieldApi<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TPatentSubmitMeta> & ExtendedApi) => FieldRenderable;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A type alias representing a field component for a specific form data type.
|
|
31
|
+
*/
|
|
32
|
+
export type FieldComponent<in out TParentData, in out TFormOnMount extends undefined | FormValidateOrFn<TParentData>, in out TFormOnChange extends undefined | FormValidateOrFn<TParentData>, in out TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, in out TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, in out TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, in out TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, in out TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>, in out TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>, in out TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>, in out TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>, in out TPatentSubmitMeta, in out ExtendedApi = {}> = <TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>>({ children, ...fieldOptions }: FieldComponentBoundProps<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TPatentSubmitMeta, ExtendedApi>) => ReturnType<FieldFunctionComponent>;
|
|
33
|
+
/**
|
|
34
|
+
* A type alias representing a field component for a form lens data type.
|
|
35
|
+
*/
|
|
36
|
+
export type LensFieldComponent<in out TLensData, in out TParentSubmitMeta, in out ExtendedApi = {}> = <TName extends DeepKeys<TLensData>, TData extends DeepValue<TLensData, TName>, TOnMount extends undefined | FieldValidateOrFn<unknown, string, TData>, TOnChange extends undefined | FieldValidateOrFn<unknown, string, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<unknown, string, TData>, TOnBlur extends undefined | FieldValidateOrFn<unknown, string, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<unknown, string, TData>, TOnSubmit extends undefined | FieldValidateOrFn<unknown, string, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<unknown, string, TData>, TOnDynamic extends undefined | FieldValidateOrFn<unknown, string, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<unknown, string, TData>>({ children, ...fieldOptions }: Omit<FieldComponentBoundProps<unknown, string, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, undefined | FormValidateOrFn<unknown>, undefined | FormValidateOrFn<unknown>, undefined | FormAsyncValidateOrFn<unknown>, undefined | FormValidateOrFn<unknown>, undefined | FormAsyncValidateOrFn<unknown>, undefined | FormValidateOrFn<unknown>, undefined | FormAsyncValidateOrFn<unknown>, undefined | FormValidateOrFn<unknown>, undefined | FormAsyncValidateOrFn<unknown>, undefined | FormAsyncValidateOrFn<unknown>, TParentSubmitMeta, ExtendedApi>, 'name' | 'validators'> & {
|
|
37
|
+
name: TName;
|
|
38
|
+
validators?: Omit<FieldValidators<unknown, string, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync>, 'onChangeListenTo' | 'onBlurListenTo'> & {
|
|
39
|
+
/**
|
|
40
|
+
* An optional list of field names that should trigger this field's `onChange` and `onChangeAsync` events when its value changes
|
|
41
|
+
*/
|
|
42
|
+
onChangeListenTo?: DeepKeys<TLensData>[];
|
|
43
|
+
/**
|
|
44
|
+
* An optional list of field names that should trigger this field's `onBlur` and `onBlurAsync` events when its value changes
|
|
45
|
+
*/
|
|
46
|
+
onBlurListenTo?: DeepKeys<TLensData>[];
|
|
47
|
+
};
|
|
48
|
+
}) => ReturnType<FieldFunctionComponent>;
|
|
49
|
+
/**
|
|
50
|
+
* A function component that takes field options and a render function as children.
|
|
51
|
+
*
|
|
52
|
+
* The `Field` component uses the `useField` hook internally to manage the field instance.
|
|
53
|
+
*/
|
|
54
|
+
export declare const Field: <TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnDynamic extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnDynamicAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnDynamic extends undefined | FormValidateOrFn<TParentData>, TFormOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>, TPatentSubmitMeta>({ children, ...fieldOptions }: FieldComponentProps<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnDynamic, TFormOnDynamicAsync, TFormOnServer, TPatentSubmitMeta>) => ReturnType<FieldFunctionComponent>;
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { useState } from 'octane';
|
|
2
|
+
import { useSelector } from '@octanejs/tanstack-store';
|
|
3
|
+
import { FieldGroupApi, functionalUpdate } from '@tanstack/form-core';
|
|
4
|
+
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
|
|
5
|
+
import type {
|
|
6
|
+
AnyFieldGroupApi,
|
|
7
|
+
DeepKeysOfType,
|
|
8
|
+
FieldGroupState,
|
|
9
|
+
FieldsMap,
|
|
10
|
+
FormAsyncValidateOrFn,
|
|
11
|
+
FormValidateOrFn,
|
|
12
|
+
} from '@tanstack/form-core';
|
|
13
|
+
import type { AppFieldExtendedOctaneFormApi } from './createFormHook.tsrx';
|
|
14
|
+
import type { LensFieldComponent } from './useField.tsrx';
|
|
15
|
+
|
|
16
|
+
type FieldGroupRenderable = unknown;
|
|
17
|
+
type FieldGroupPropsWithChildren<P = object> = P & { children?: FieldGroupRenderable };
|
|
18
|
+
type FieldGroupFunctionComponent<P = object> = (props: P) => FieldGroupRenderable;
|
|
19
|
+
type FieldGroupComponentType<P = object> = FieldGroupFunctionComponent<P>;
|
|
20
|
+
|
|
21
|
+
function LocalSubscribe({
|
|
22
|
+
lens,
|
|
23
|
+
selector = (state) => state,
|
|
24
|
+
children,
|
|
25
|
+
}: FieldGroupPropsWithChildren<{
|
|
26
|
+
lens: AnyFieldGroupApi;
|
|
27
|
+
selector?: (state: FieldGroupState<any>) => FieldGroupState<any>;
|
|
28
|
+
}>): ReturnType<FieldGroupFunctionComponent> {
|
|
29
|
+
const data = useSelector(lens.store, selector);
|
|
30
|
+
|
|
31
|
+
return <>{functionalUpdate(children, data)}</>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
export type AppFieldExtendedOctaneFieldGroupApi<
|
|
38
|
+
TFormData,
|
|
39
|
+
TFieldGroupData,
|
|
40
|
+
TFields extends
|
|
41
|
+
| DeepKeysOfType<TFormData, TFieldGroupData | null | undefined>
|
|
42
|
+
| FieldsMap<TFormData, TFieldGroupData>,
|
|
43
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
44
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
45
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
46
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
47
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
48
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
49
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
50
|
+
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
51
|
+
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
52
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
53
|
+
TSubmitMeta,
|
|
54
|
+
TFieldComponents extends Record<string, FieldGroupComponentType<any>>,
|
|
55
|
+
TFormComponents extends Record<string, FieldGroupComponentType<any>>,
|
|
56
|
+
> = FieldGroupApi<
|
|
57
|
+
TFormData,
|
|
58
|
+
TFieldGroupData,
|
|
59
|
+
TFields,
|
|
60
|
+
TOnMount,
|
|
61
|
+
TOnChange,
|
|
62
|
+
TOnChangeAsync,
|
|
63
|
+
TOnBlur,
|
|
64
|
+
TOnBlurAsync,
|
|
65
|
+
TOnSubmit,
|
|
66
|
+
TOnSubmitAsync,
|
|
67
|
+
TOnDynamic,
|
|
68
|
+
TOnDynamicAsync,
|
|
69
|
+
TOnServer,
|
|
70
|
+
TSubmitMeta
|
|
71
|
+
> &
|
|
72
|
+
NoInfer<TFormComponents> & {
|
|
73
|
+
AppField: LensFieldComponent<TFieldGroupData, TSubmitMeta, NoInfer<TFieldComponents>>;
|
|
74
|
+
AppForm: FieldGroupComponentType<FieldGroupPropsWithChildren<{}>>;
|
|
75
|
+
/**
|
|
76
|
+
* A component to render form fields. With this, you can render and manage individual form fields.
|
|
77
|
+
*/
|
|
78
|
+
Field: LensFieldComponent<TFieldGroupData, TSubmitMeta>;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A `Subscribe` function that allows you to listen and react 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.
|
|
82
|
+
*/
|
|
83
|
+
Subscribe: <TSelected = NoInfer<FieldGroupState<TFieldGroupData>>>(props: {
|
|
84
|
+
selector?: (state: NoInfer<FieldGroupState<TFieldGroupData>>) => TSelected;
|
|
85
|
+
children: ((state: NoInfer<TSelected>) => FieldGroupRenderable) | FieldGroupRenderable;
|
|
86
|
+
}) => ReturnType<FieldGroupFunctionComponent>;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export function useFieldGroup<
|
|
90
|
+
TFormData,
|
|
91
|
+
TFieldGroupData,
|
|
92
|
+
TFields extends
|
|
93
|
+
| DeepKeysOfType<TFormData, TFieldGroupData | null | undefined>
|
|
94
|
+
| FieldsMap<TFormData, TFieldGroupData>,
|
|
95
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
96
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
97
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
98
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
99
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
100
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
101
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
102
|
+
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
103
|
+
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
104
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
105
|
+
TComponents extends Record<string, FieldGroupComponentType<any>>,
|
|
106
|
+
TFormComponents extends Record<string, FieldGroupComponentType<any>>,
|
|
107
|
+
TSubmitMeta = never,
|
|
108
|
+
>(opts: {
|
|
109
|
+
form:
|
|
110
|
+
| AppFieldExtendedOctaneFormApi<
|
|
111
|
+
TFormData,
|
|
112
|
+
TOnMount,
|
|
113
|
+
TOnChange,
|
|
114
|
+
TOnChangeAsync,
|
|
115
|
+
TOnBlur,
|
|
116
|
+
TOnBlurAsync,
|
|
117
|
+
TOnSubmit,
|
|
118
|
+
TOnSubmitAsync,
|
|
119
|
+
TOnDynamic,
|
|
120
|
+
TOnDynamicAsync,
|
|
121
|
+
TOnServer,
|
|
122
|
+
TSubmitMeta,
|
|
123
|
+
TComponents,
|
|
124
|
+
TFormComponents
|
|
125
|
+
>
|
|
126
|
+
| AppFieldExtendedOctaneFieldGroupApi<
|
|
127
|
+
// Since this only occurs if you nest it within other form lenses, it can be more
|
|
128
|
+
// lenient with the types.
|
|
129
|
+
unknown,
|
|
130
|
+
TFormData,
|
|
131
|
+
string | FieldsMap<unknown, TFormData>,
|
|
132
|
+
any,
|
|
133
|
+
any,
|
|
134
|
+
any,
|
|
135
|
+
any,
|
|
136
|
+
any,
|
|
137
|
+
any,
|
|
138
|
+
any,
|
|
139
|
+
any,
|
|
140
|
+
any,
|
|
141
|
+
any,
|
|
142
|
+
TSubmitMeta,
|
|
143
|
+
TComponents,
|
|
144
|
+
TFormComponents
|
|
145
|
+
>;
|
|
146
|
+
fields: TFields;
|
|
147
|
+
defaultValues?: TFieldGroupData;
|
|
148
|
+
onSubmitMeta?: TSubmitMeta;
|
|
149
|
+
formComponents: TFormComponents;
|
|
150
|
+
}): AppFieldExtendedOctaneFieldGroupApi<
|
|
151
|
+
TFormData,
|
|
152
|
+
TFieldGroupData,
|
|
153
|
+
TFields,
|
|
154
|
+
TOnMount,
|
|
155
|
+
TOnChange,
|
|
156
|
+
TOnChangeAsync,
|
|
157
|
+
TOnBlur,
|
|
158
|
+
TOnBlurAsync,
|
|
159
|
+
TOnSubmit,
|
|
160
|
+
TOnSubmitAsync,
|
|
161
|
+
TOnDynamic,
|
|
162
|
+
TOnDynamicAsync,
|
|
163
|
+
TOnServer,
|
|
164
|
+
TSubmitMeta,
|
|
165
|
+
TComponents,
|
|
166
|
+
TFormComponents
|
|
167
|
+
> {
|
|
168
|
+
const [formLensApi] = useState(() => {
|
|
169
|
+
const api = new FieldGroupApi(opts);
|
|
170
|
+
const form =
|
|
171
|
+
opts.form instanceof FieldGroupApi
|
|
172
|
+
? (opts.form.form as AppFieldExtendedOctaneFormApi<
|
|
173
|
+
TFormData,
|
|
174
|
+
TOnMount,
|
|
175
|
+
TOnChange,
|
|
176
|
+
TOnChangeAsync,
|
|
177
|
+
TOnBlur,
|
|
178
|
+
TOnBlurAsync,
|
|
179
|
+
TOnSubmit,
|
|
180
|
+
TOnSubmitAsync,
|
|
181
|
+
TOnDynamic,
|
|
182
|
+
TOnDynamicAsync,
|
|
183
|
+
TOnServer,
|
|
184
|
+
TSubmitMeta,
|
|
185
|
+
TComponents,
|
|
186
|
+
TFormComponents
|
|
187
|
+
>)
|
|
188
|
+
: opts.form;
|
|
189
|
+
|
|
190
|
+
const extendedApi: AppFieldExtendedOctaneFieldGroupApi<
|
|
191
|
+
TFormData,
|
|
192
|
+
TFieldGroupData,
|
|
193
|
+
TFields,
|
|
194
|
+
TOnMount,
|
|
195
|
+
TOnChange,
|
|
196
|
+
TOnChangeAsync,
|
|
197
|
+
TOnBlur,
|
|
198
|
+
TOnBlurAsync,
|
|
199
|
+
TOnSubmit,
|
|
200
|
+
TOnSubmitAsync,
|
|
201
|
+
TOnDynamic,
|
|
202
|
+
TOnDynamicAsync,
|
|
203
|
+
TOnServer,
|
|
204
|
+
TSubmitMeta,
|
|
205
|
+
TComponents,
|
|
206
|
+
TFormComponents
|
|
207
|
+
> = api as never;
|
|
208
|
+
|
|
209
|
+
extendedApi.AppForm = function AppForm(appFormProps) {
|
|
210
|
+
return <form.AppForm {...appFormProps} />;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
extendedApi.AppField = function AppField(props) {
|
|
214
|
+
return <form.AppField {...(formLensApi.getFormFieldOptions(props) as any)} />;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
extendedApi.Field = function Field(props) {
|
|
218
|
+
return <form.Field {...(formLensApi.getFormFieldOptions(props) as any)} />;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
extendedApi.Subscribe = function Subscribe(props: any) {
|
|
222
|
+
return (
|
|
223
|
+
<LocalSubscribe lens={formLensApi} selector={props.selector} children={props.children} />
|
|
224
|
+
);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
return Object.assign(extendedApi, {
|
|
228
|
+
...opts.formComponents,
|
|
229
|
+
}) as AppFieldExtendedOctaneFieldGroupApi<
|
|
230
|
+
TFormData,
|
|
231
|
+
TFieldGroupData,
|
|
232
|
+
TFields,
|
|
233
|
+
TOnMount,
|
|
234
|
+
TOnChange,
|
|
235
|
+
TOnChangeAsync,
|
|
236
|
+
TOnBlur,
|
|
237
|
+
TOnBlurAsync,
|
|
238
|
+
TOnSubmit,
|
|
239
|
+
TOnSubmitAsync,
|
|
240
|
+
TOnDynamic,
|
|
241
|
+
TOnDynamicAsync,
|
|
242
|
+
TOnServer,
|
|
243
|
+
TSubmitMeta,
|
|
244
|
+
TComponents,
|
|
245
|
+
TFormComponents
|
|
246
|
+
>;
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
useIsomorphicLayoutEffect(formLensApi.mount, [formLensApi]);
|
|
250
|
+
|
|
251
|
+
return formLensApi;
|
|
252
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Declaration companion generated from useFieldGroup.tsrx.
|
|
2
|
+
import { FieldGroupApi } from '@tanstack/form-core';
|
|
3
|
+
import type { DeepKeysOfType, FieldGroupState, FieldsMap, FormAsyncValidateOrFn, FormValidateOrFn } from '@tanstack/form-core';
|
|
4
|
+
import type { AppFieldExtendedOctaneFormApi } from './createFormHook.tsrx';
|
|
5
|
+
import type { LensFieldComponent } from './useField.tsrx';
|
|
6
|
+
type FieldGroupRenderable = unknown;
|
|
7
|
+
type FieldGroupPropsWithChildren<P = object> = P & {
|
|
8
|
+
children?: FieldGroupRenderable;
|
|
9
|
+
};
|
|
10
|
+
type FieldGroupFunctionComponent<P = object> = (props: P) => FieldGroupRenderable;
|
|
11
|
+
type FieldGroupComponentType<P = object> = FieldGroupFunctionComponent<P>;
|
|
12
|
+
/**
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
export type AppFieldExtendedOctaneFieldGroupApi<TFormData, TFieldGroupData, TFields extends DeepKeysOfType<TFormData, TFieldGroupData | null | undefined> | FieldsMap<TFormData, TFieldGroupData>, 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, TFieldComponents extends Record<string, FieldGroupComponentType<any>>, TFormComponents extends Record<string, FieldGroupComponentType<any>>> = FieldGroupApi<TFormData, TFieldGroupData, TFields, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta> & NoInfer<TFormComponents> & {
|
|
16
|
+
AppField: LensFieldComponent<TFieldGroupData, TSubmitMeta, NoInfer<TFieldComponents>>;
|
|
17
|
+
AppForm: FieldGroupComponentType<FieldGroupPropsWithChildren<{}>>;
|
|
18
|
+
/**
|
|
19
|
+
* A component to render form fields. With this, you can render and manage individual form fields.
|
|
20
|
+
*/
|
|
21
|
+
Field: LensFieldComponent<TFieldGroupData, TSubmitMeta>;
|
|
22
|
+
/**
|
|
23
|
+
* A `Subscribe` function that allows you to listen and react 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.
|
|
24
|
+
*/
|
|
25
|
+
Subscribe: <TSelected = NoInfer<FieldGroupState<TFieldGroupData>>>(props: {
|
|
26
|
+
selector?: (state: NoInfer<FieldGroupState<TFieldGroupData>>) => TSelected;
|
|
27
|
+
children: ((state: NoInfer<TSelected>) => FieldGroupRenderable) | FieldGroupRenderable;
|
|
28
|
+
}) => ReturnType<FieldGroupFunctionComponent>;
|
|
29
|
+
};
|
|
30
|
+
export declare function useFieldGroup<TFormData, TFieldGroupData, TFields extends DeepKeysOfType<TFormData, TFieldGroupData | null | undefined> | FieldsMap<TFormData, TFieldGroupData>, 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>, TComponents extends Record<string, FieldGroupComponentType<any>>, TFormComponents extends Record<string, FieldGroupComponentType<any>>, TSubmitMeta = never>(opts: {
|
|
31
|
+
form: AppFieldExtendedOctaneFormApi<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, TComponents, TFormComponents> | AppFieldExtendedOctaneFieldGroupApi<unknown, TFormData, string | FieldsMap<unknown, TFormData>, any, any, any, any, any, any, any, any, any, any, TSubmitMeta, TComponents, TFormComponents>;
|
|
32
|
+
fields: TFields;
|
|
33
|
+
defaultValues?: TFieldGroupData;
|
|
34
|
+
onSubmitMeta?: TSubmitMeta;
|
|
35
|
+
formComponents: TFormComponents;
|
|
36
|
+
}): AppFieldExtendedOctaneFieldGroupApi<TFormData, TFieldGroupData, TFields, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, TComponents, TFormComponents>;
|
|
37
|
+
export {};
|
package/src/useForm.tsrx
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { FormApi, functionalUpdate, mergeAndUpdate } from '@tanstack/form-core';
|
|
2
|
+
import { useSelector } from '@octanejs/tanstack-store';
|
|
3
|
+
import { useMemo, useRef, useState } from 'octane';
|
|
4
|
+
import { Field } from './useField.tsrx';
|
|
5
|
+
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
|
|
6
|
+
import { useFormId } from './useFormId';
|
|
7
|
+
import { FormGroup } from './useFormGroup.tsrx';
|
|
8
|
+
import type { FormGroupComponent } from './useFormGroup.tsrx';
|
|
9
|
+
import type {
|
|
10
|
+
AnyFormApi,
|
|
11
|
+
AnyFormState,
|
|
12
|
+
FormAsyncValidateOrFn,
|
|
13
|
+
FormOptions,
|
|
14
|
+
FormState,
|
|
15
|
+
FormValidateOrFn,
|
|
16
|
+
} from '@tanstack/form-core';
|
|
17
|
+
import type { FieldComponent } from './useField.tsrx';
|
|
18
|
+
|
|
19
|
+
type FormRenderable = unknown;
|
|
20
|
+
type FormPropsWithChildren<P = object> = P & { children?: FormRenderable };
|
|
21
|
+
type FormFunctionComponent<P = object> = (props: P) => FormRenderable;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Fields that are added onto the `FormAPI` from `@tanstack/form-core` and returned from `useForm`
|
|
25
|
+
*/
|
|
26
|
+
export interface OctaneFormApi<
|
|
27
|
+
in out TFormData,
|
|
28
|
+
in out TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
29
|
+
in out TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
30
|
+
in out TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
31
|
+
in out TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
32
|
+
in out TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
33
|
+
in out TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
34
|
+
in out TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
35
|
+
in out TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
36
|
+
in out TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
37
|
+
in out TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
38
|
+
in out TSubmitMeta,
|
|
39
|
+
> {
|
|
40
|
+
/**
|
|
41
|
+
* A component to render form fields. With this, you can render and manage individual form fields.
|
|
42
|
+
*/
|
|
43
|
+
Field: FieldComponent<
|
|
44
|
+
TFormData,
|
|
45
|
+
TOnMount,
|
|
46
|
+
TOnChange,
|
|
47
|
+
TOnChangeAsync,
|
|
48
|
+
TOnBlur,
|
|
49
|
+
TOnBlurAsync,
|
|
50
|
+
TOnSubmit,
|
|
51
|
+
TOnSubmitAsync,
|
|
52
|
+
TOnDynamic,
|
|
53
|
+
TOnDynamicAsync,
|
|
54
|
+
TOnServer,
|
|
55
|
+
TSubmitMeta
|
|
56
|
+
>;
|
|
57
|
+
FormGroup: FormGroupComponent<
|
|
58
|
+
TFormData,
|
|
59
|
+
TOnMount,
|
|
60
|
+
TOnChange,
|
|
61
|
+
TOnChangeAsync,
|
|
62
|
+
TOnBlur,
|
|
63
|
+
TOnBlurAsync,
|
|
64
|
+
TOnSubmit,
|
|
65
|
+
TOnSubmitAsync,
|
|
66
|
+
TOnDynamic,
|
|
67
|
+
TOnDynamicAsync,
|
|
68
|
+
TOnServer,
|
|
69
|
+
TSubmitMeta
|
|
70
|
+
>;
|
|
71
|
+
/**
|
|
72
|
+
* A `Subscribe` function that allows you to listen and react 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.
|
|
73
|
+
*/
|
|
74
|
+
Subscribe: <
|
|
75
|
+
TSelected = NoInfer<
|
|
76
|
+
FormState<
|
|
77
|
+
TFormData,
|
|
78
|
+
TOnMount,
|
|
79
|
+
TOnChange,
|
|
80
|
+
TOnChangeAsync,
|
|
81
|
+
TOnBlur,
|
|
82
|
+
TOnBlurAsync,
|
|
83
|
+
TOnSubmit,
|
|
84
|
+
TOnSubmitAsync,
|
|
85
|
+
TOnDynamic,
|
|
86
|
+
TOnDynamicAsync,
|
|
87
|
+
TOnServer
|
|
88
|
+
>
|
|
89
|
+
>,
|
|
90
|
+
>(props: {
|
|
91
|
+
selector?: (
|
|
92
|
+
state: NoInfer<
|
|
93
|
+
FormState<
|
|
94
|
+
TFormData,
|
|
95
|
+
TOnMount,
|
|
96
|
+
TOnChange,
|
|
97
|
+
TOnChangeAsync,
|
|
98
|
+
TOnBlur,
|
|
99
|
+
TOnBlurAsync,
|
|
100
|
+
TOnSubmit,
|
|
101
|
+
TOnSubmitAsync,
|
|
102
|
+
TOnDynamic,
|
|
103
|
+
TOnDynamicAsync,
|
|
104
|
+
TOnServer
|
|
105
|
+
>
|
|
106
|
+
>,
|
|
107
|
+
) => TSelected;
|
|
108
|
+
children: ((state: NoInfer<TSelected>) => FormRenderable) | FormRenderable;
|
|
109
|
+
}) => ReturnType<FormFunctionComponent>;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* An extended version of the `FormApi` class that includes renderer-specific components from `OctaneFormApi`
|
|
114
|
+
*/
|
|
115
|
+
export type OctaneFormExtendedApi<
|
|
116
|
+
TFormData,
|
|
117
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
118
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
119
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
120
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
121
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
122
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
123
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
124
|
+
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
125
|
+
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
126
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
127
|
+
TSubmitMeta,
|
|
128
|
+
> = FormApi<
|
|
129
|
+
TFormData,
|
|
130
|
+
TOnMount,
|
|
131
|
+
TOnChange,
|
|
132
|
+
TOnChangeAsync,
|
|
133
|
+
TOnBlur,
|
|
134
|
+
TOnBlurAsync,
|
|
135
|
+
TOnSubmit,
|
|
136
|
+
TOnSubmitAsync,
|
|
137
|
+
TOnDynamic,
|
|
138
|
+
TOnDynamicAsync,
|
|
139
|
+
TOnServer,
|
|
140
|
+
TSubmitMeta
|
|
141
|
+
> &
|
|
142
|
+
OctaneFormApi<
|
|
143
|
+
TFormData,
|
|
144
|
+
TOnMount,
|
|
145
|
+
TOnChange,
|
|
146
|
+
TOnChangeAsync,
|
|
147
|
+
TOnBlur,
|
|
148
|
+
TOnBlurAsync,
|
|
149
|
+
TOnSubmit,
|
|
150
|
+
TOnSubmitAsync,
|
|
151
|
+
TOnDynamic,
|
|
152
|
+
TOnDynamicAsync,
|
|
153
|
+
TOnServer,
|
|
154
|
+
TSubmitMeta
|
|
155
|
+
>;
|
|
156
|
+
|
|
157
|
+
function LocalSubscribe({
|
|
158
|
+
form,
|
|
159
|
+
selector = (state) => state,
|
|
160
|
+
children,
|
|
161
|
+
}: FormPropsWithChildren<{
|
|
162
|
+
form: AnyFormApi;
|
|
163
|
+
selector?: (state: AnyFormState) => AnyFormState;
|
|
164
|
+
}>): ReturnType<FormFunctionComponent> {
|
|
165
|
+
const data = useSelector(form.store, selector);
|
|
166
|
+
|
|
167
|
+
return <>{functionalUpdate(children, data)}</>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* A custom hook that returns an extended instance of the `FormApi` class.
|
|
172
|
+
*
|
|
173
|
+
* This API encapsulates all the necessary functionalities related to the form. It allows you to manage form state, handle submissions, and interact with form fields
|
|
174
|
+
*/
|
|
175
|
+
export function useForm<
|
|
176
|
+
TFormData,
|
|
177
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
178
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
179
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
180
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
181
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
182
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
183
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
184
|
+
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
185
|
+
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
186
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
187
|
+
TSubmitMeta,
|
|
188
|
+
>(
|
|
189
|
+
opts?: FormOptions<
|
|
190
|
+
TFormData,
|
|
191
|
+
TOnMount,
|
|
192
|
+
TOnChange,
|
|
193
|
+
TOnChangeAsync,
|
|
194
|
+
TOnBlur,
|
|
195
|
+
TOnBlurAsync,
|
|
196
|
+
TOnSubmit,
|
|
197
|
+
TOnSubmitAsync,
|
|
198
|
+
TOnDynamic,
|
|
199
|
+
TOnDynamicAsync,
|
|
200
|
+
TOnServer,
|
|
201
|
+
TSubmitMeta
|
|
202
|
+
>,
|
|
203
|
+
) {
|
|
204
|
+
const fallbackFormId = useFormId();
|
|
205
|
+
const formId = opts?.formId ?? fallbackFormId;
|
|
206
|
+
const [prevFormId, setPrevFormId] = useState(formId);
|
|
207
|
+
|
|
208
|
+
const [formApi, setFormApi] = useState(() => {
|
|
209
|
+
return new FormApi<
|
|
210
|
+
TFormData,
|
|
211
|
+
TOnMount,
|
|
212
|
+
TOnChange,
|
|
213
|
+
TOnChangeAsync,
|
|
214
|
+
TOnBlur,
|
|
215
|
+
TOnBlurAsync,
|
|
216
|
+
TOnSubmit,
|
|
217
|
+
TOnSubmitAsync,
|
|
218
|
+
TOnDynamic,
|
|
219
|
+
TOnDynamicAsync,
|
|
220
|
+
TOnServer,
|
|
221
|
+
TSubmitMeta
|
|
222
|
+
>({ ...opts, formId });
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
if (prevFormId !== formId) {
|
|
226
|
+
setFormApi(new FormApi({ ...opts, formId }));
|
|
227
|
+
setPrevFormId(formId);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const extendedFormApi = useMemo(() => {
|
|
231
|
+
const extendedApi: OctaneFormExtendedApi<
|
|
232
|
+
TFormData,
|
|
233
|
+
TOnMount,
|
|
234
|
+
TOnChange,
|
|
235
|
+
TOnChangeAsync,
|
|
236
|
+
TOnBlur,
|
|
237
|
+
TOnBlurAsync,
|
|
238
|
+
TOnSubmit,
|
|
239
|
+
TOnSubmitAsync,
|
|
240
|
+
TOnDynamic,
|
|
241
|
+
TOnDynamicAsync,
|
|
242
|
+
TOnServer,
|
|
243
|
+
TSubmitMeta
|
|
244
|
+
> = {
|
|
245
|
+
...formApi,
|
|
246
|
+
handleSubmit: ((...props: never[]) => {
|
|
247
|
+
return formApi._handleSubmit(...props);
|
|
248
|
+
}) as typeof formApi.handleSubmit,
|
|
249
|
+
// We must add all `get`ters from `core`'s `FormApi` here, as otherwise the spread operator won't catch those
|
|
250
|
+
get formId(): string {
|
|
251
|
+
return formApi._formId;
|
|
252
|
+
},
|
|
253
|
+
get state() {
|
|
254
|
+
return formApi.store.state;
|
|
255
|
+
},
|
|
256
|
+
} as never;
|
|
257
|
+
|
|
258
|
+
extendedApi.Field = function APIField(props) {
|
|
259
|
+
return <Field {...props} form={formApi} />;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
extendedApi.FormGroup = function APIFormGroup(props) {
|
|
263
|
+
return <FormGroup {...props} form={formApi} />;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
extendedApi.Subscribe = function Subscribe(props: any) {
|
|
267
|
+
return <LocalSubscribe form={formApi} selector={props.selector} children={props.children} />;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
return extendedApi;
|
|
271
|
+
}, [formApi]);
|
|
272
|
+
|
|
273
|
+
useIsomorphicLayoutEffect(formApi.mount, [formApi]);
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* formApi.update should not have any side effects. Think of it like a `useRef`
|
|
277
|
+
* that we need to keep updated every render with the most up-to-date information.
|
|
278
|
+
*/
|
|
279
|
+
useIsomorphicLayoutEffect(() => {
|
|
280
|
+
formApi.update(opts);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
const hasRan = useRef(false);
|
|
284
|
+
|
|
285
|
+
useIsomorphicLayoutEffect(() => {
|
|
286
|
+
if (!hasRan.current) return;
|
|
287
|
+
if (!opts?.transform) return;
|
|
288
|
+
mergeAndUpdate(formApi, opts.transform as never);
|
|
289
|
+
}, [formApi, opts?.transform]);
|
|
290
|
+
|
|
291
|
+
useIsomorphicLayoutEffect(() => {
|
|
292
|
+
hasRan.current = true;
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
return extendedFormApi;
|
|
296
|
+
}
|