@tanstack/react-form 0.0.5 → 0.0.7

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.
Files changed (37) hide show
  1. package/build/cjs/Field.js +2 -5
  2. package/build/cjs/Field.js.map +1 -1
  3. package/build/cjs/createFormFactory.js +32 -0
  4. package/build/cjs/createFormFactory.js.map +1 -0
  5. package/build/cjs/formContext.js +1 -5
  6. package/build/cjs/formContext.js.map +1 -1
  7. package/build/cjs/index.js +14 -9
  8. package/build/cjs/index.js.map +1 -1
  9. package/build/cjs/useField.js +4 -12
  10. package/build/cjs/useField.js.map +1 -1
  11. package/build/cjs/useForm.js +6 -31
  12. package/build/cjs/useForm.js.map +1 -1
  13. package/build/esm/index.js +24 -50
  14. package/build/esm/index.js.map +1 -1
  15. package/build/stats-html.html +1 -1
  16. package/build/stats-react.json +145 -107
  17. package/build/types/form-core/src/FieldApi.d.ts +11 -10
  18. package/build/types/form-core/src/FormApi.d.ts +12 -12
  19. package/build/types/form-core/src/utils.d.ts +10 -10
  20. package/build/types/index.d.ts +22 -10
  21. package/build/types/react-form/src/Field.d.ts +3 -3
  22. package/build/types/react-form/src/createFormFactory.d.ts +9 -0
  23. package/build/types/react-form/src/formContext.d.ts +1 -1
  24. package/build/types/react-form/src/index.d.ts +10 -4
  25. package/build/types/react-form/src/useField.d.ts +9 -3
  26. package/build/types/react-form/src/useForm.d.ts +5 -3
  27. package/build/umd/index.development.js +33 -61
  28. package/build/umd/index.development.js.map +1 -1
  29. package/build/umd/index.production.js +2 -2
  30. package/build/umd/index.production.js.map +1 -1
  31. package/package.json +2 -2
  32. package/src/Field.tsx +2 -3
  33. package/src/createFormFactory.ts +22 -0
  34. package/src/formContext.ts +1 -5
  35. package/src/index.ts +35 -4
  36. package/src/useField.ts +13 -17
  37. package/src/useForm.tsx +11 -43
@@ -8,27 +8,40 @@
8
8
  *
9
9
  * @license MIT
10
10
  */
11
- import { DeepKeys, FieldApi, DeepValue, FieldOptions, FormApi, FormState, FormOptions } from '@tanstack/form-core';
12
- export * from '@tanstack/form-core';
11
+ import { DeepKeys, FieldApi, DeepValue, FieldOptions, FormOptions, FormApi, FormState } from '@tanstack/form-core';
12
+ export { ChangeProps, DeepKeys, DeepValue, FieldApi, FieldApiOptions, FieldInfo, FieldMeta, FieldOptions, FieldState, FormApi, FormOptions, FormState, InputProps, RequiredByKey, Updater, UpdaterFn, UserChangeProps, UserInputProps, ValidationCause, ValidationError, ValidationMeta, functionalUpdate } from '@tanstack/form-core';
13
13
  import { NoInfer } from '@tanstack/react-store';
14
14
  import React from 'react';
15
15
 
16
- declare type FieldComponent<TFormData> = <TField extends DeepKeys<TFormData>>({ children, ...fieldOptions }: {
16
+ type FieldComponent<TFormData> = <TField extends DeepKeys<TFormData>>({ children, ...fieldOptions }: {
17
17
  children: (fieldApi: FieldApi<DeepValue<TFormData, TField>, TFormData>) => any;
18
18
  name: TField;
19
19
  } & Omit<FieldOptions<DeepValue<TFormData, TField>, TFormData>, 'name'>) => any;
20
- declare function createFieldComponent<TFormData>(formApi: FormApi<TFormData>): FieldComponent<TFormData>;
21
20
  declare function Field<TData, TFormData>({ children, ...fieldOptions }: {
22
21
  children: (fieldApi: FieldApi<TData, TFormData>) => any;
23
22
  } & FieldOptions<TData, TFormData>): any;
24
23
 
25
- declare type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(opts?: {
24
+ type FormFactory<TFormData> = {
25
+ useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>;
26
+ useField: UseField<TFormData>;
27
+ Field: FieldComponent<TFormData>;
28
+ };
29
+ declare function createFormFactory<TFormData>(defaultOpts?: FormOptions<TFormData>): FormFactory<TFormData>;
30
+
31
+ declare module '@tanstack/form-core' {
32
+ interface FieldOptions<TData, TFormData> {
33
+ formFactory?: FormFactory<TFormData>;
34
+ }
35
+ }
36
+ type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(opts?: {
26
37
  name: TField;
27
38
  } & FieldOptions<DeepValue<TFormData, TField>, TFormData>) => FieldApi<DeepValue<TFormData, TField>, TFormData>;
28
- declare function createUseField<TFormData>(formApi: FormApi<TFormData>): UseField<TFormData>;
29
39
  declare function useField<TData, TFormData>(opts: FieldOptions<TData, TFormData> & {}): FieldApi<TData, TFormData>;
30
40
 
31
41
  declare module '@tanstack/form-core' {
42
+ interface Register {
43
+ FormSubmitEvent: React.FormEvent<HTMLFormElement>;
44
+ }
32
45
  interface FormApi<TFormData> {
33
46
  Form: FormComponent;
34
47
  Field: FieldComponent<TFormData>;
@@ -41,11 +54,10 @@ declare module '@tanstack/form-core' {
41
54
  }
42
55
  }
43
56
  declare function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData>;
44
- declare type FormProps = React.HTMLProps<HTMLFormElement> & {
57
+ type FormProps = React.HTMLProps<HTMLFormElement> & {
45
58
  children: React.ReactNode;
46
59
  noFormElement?: boolean;
47
60
  };
48
- declare type FormComponent = (props: FormProps) => any;
49
- declare function createFormComponent(formApi: FormApi<any>): FormComponent;
61
+ type FormComponent = (props: FormProps) => any;
50
62
 
51
- export { Field, FieldComponent, FormComponent, FormProps, UseField, createFieldComponent, createFormComponent, createUseField, useField, useForm };
63
+ export { Field, FieldComponent, FormComponent, FormFactory, FormProps, UseField, createFormFactory, useField, useForm };
@@ -1,9 +1,9 @@
1
- import { type DeepKeys, type DeepValue, type FieldApi, type FieldOptions, type FormApi } from '@tanstack/form-core';
2
- export declare type FieldComponent<TFormData> = <TField extends DeepKeys<TFormData>>({ children, ...fieldOptions }: {
1
+ import { type DeepKeys, type DeepValue, type FieldApi, type FieldOptions } from '@tanstack/form-core';
2
+ export type FieldComponent<TFormData> = <TField extends DeepKeys<TFormData>>({ children, ...fieldOptions }: {
3
3
  children: (fieldApi: FieldApi<DeepValue<TFormData, TField>, TFormData>) => any;
4
4
  name: TField;
5
5
  } & Omit<FieldOptions<DeepValue<TFormData, TField>, TFormData>, 'name'>) => any;
6
- export declare function createFieldComponent<TFormData>(formApi: FormApi<TFormData>): FieldComponent<TFormData>;
6
+ export declare function createFieldComponent<TFormData>(): FieldComponent<TFormData>;
7
7
  export declare function Field<TData, TFormData>({ children, ...fieldOptions }: {
8
8
  children: (fieldApi: FieldApi<TData, TFormData>) => any;
9
9
  } & FieldOptions<TData, TFormData>): any;
@@ -0,0 +1,9 @@
1
+ import type { FormApi, FormOptions } from '@tanstack/form-core';
2
+ import { type UseField } from './useField';
3
+ import { type FieldComponent } from './Field';
4
+ export type FormFactory<TFormData> = {
5
+ useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>;
6
+ useField: UseField<TFormData>;
7
+ Field: FieldComponent<TFormData>;
8
+ };
9
+ export declare function createFormFactory<TFormData>(defaultOpts?: FormOptions<TFormData>): FormFactory<TFormData>;
@@ -1,4 +1,4 @@
1
1
  import type { FormApi } from '@tanstack/form-core';
2
2
  import * as React from 'react';
3
3
  export declare const formContext: React.Context<FormApi<any> | null>;
4
- export declare function useFormContext(customFormApi?: FormApi<any>): FormApi<any>;
4
+ export declare function useFormContext(): FormApi<any>;
@@ -1,4 +1,10 @@
1
- export * from '@tanstack/form-core';
2
- export * from './useForm';
3
- export * from './Field';
4
- export * from './useField';
1
+ export type { ChangeProps, DeepKeys, DeepValue, FieldApiOptions, FieldInfo, FieldMeta, FieldOptions, FieldState, FormOptions, FormState, InputProps, RequiredByKey, Updater, UpdaterFn, UserChangeProps, UserInputProps, ValidationCause, ValidationError, ValidationMeta, } from '@tanstack/form-core';
2
+ export { FormApi, FieldApi, functionalUpdate } from '@tanstack/form-core';
3
+ export type { FormComponent, FormProps } from './useForm';
4
+ export { useForm } from './useForm';
5
+ export type { FieldComponent } from './Field';
6
+ export { Field } from './Field';
7
+ export type { UseField } from './useField';
8
+ export { useField } from './useField';
9
+ export type { FormFactory } from './createFormFactory';
10
+ export { createFormFactory } from './createFormFactory';
@@ -1,7 +1,13 @@
1
- import type { DeepKeys, DeepValue, FieldOptions, FormApi } from '@tanstack/form-core';
1
+ import type { DeepKeys, DeepValue, FieldOptions } from '@tanstack/form-core';
2
2
  import { FieldApi } from '@tanstack/form-core';
3
- export declare type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(opts?: {
3
+ import type { FormFactory } from './createFormFactory';
4
+ declare module '@tanstack/form-core' {
5
+ interface FieldOptions<TData, TFormData> {
6
+ formFactory?: FormFactory<TFormData>;
7
+ }
8
+ }
9
+ export type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(opts?: {
4
10
  name: TField;
5
11
  } & FieldOptions<DeepValue<TFormData, TField>, TFormData>) => FieldApi<DeepValue<TFormData, TField>, TFormData>;
6
- export declare function createUseField<TFormData>(formApi: FormApi<TFormData>): UseField<TFormData>;
12
+ export declare function createUseField<TFormData>(): UseField<TFormData>;
7
13
  export declare function useField<TData, TFormData>(opts: FieldOptions<TData, TFormData> & {}): FieldApi<TData, TFormData>;
@@ -5,6 +5,9 @@ import React from 'react';
5
5
  import { type FieldComponent } from './Field';
6
6
  import { type UseField } from './useField';
7
7
  declare module '@tanstack/form-core' {
8
+ interface Register {
9
+ FormSubmitEvent: React.FormEvent<HTMLFormElement>;
10
+ }
8
11
  interface FormApi<TFormData> {
9
12
  Form: FormComponent;
10
13
  Field: FieldComponent<TFormData>;
@@ -17,9 +20,8 @@ declare module '@tanstack/form-core' {
17
20
  }
18
21
  }
19
22
  export declare function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData>;
20
- export declare type FormProps = React.HTMLProps<HTMLFormElement> & {
23
+ export type FormProps = React.HTMLProps<HTMLFormElement> & {
21
24
  children: React.ReactNode;
22
25
  noFormElement?: boolean;
23
26
  };
24
- export declare type FormComponent = (props: FormProps) => any;
25
- export declare function createFormComponent(formApi: FormApi<any>): FormComponent;
27
+ export type FormComponent = (props: FormProps) => any;
@@ -101,10 +101,6 @@
101
101
  return typeof updater === 'function' ? updater(input) : updater;
102
102
  }
103
103
  function getBy(obj, path) {
104
- if (!path) {
105
- throw new Error('A path string is required to use getBy');
106
- }
107
-
108
104
  const pathArray = makePathArray(path);
109
105
  const pathObj = pathArray;
110
106
  return pathObj.reduce((current, pathPart) => {
@@ -156,11 +152,17 @@
156
152
  const reFindNumbers2 = /^(\d*)\./gm;
157
153
  const reFindNumbers3 = /\.(\d*$)/gm;
158
154
  const reFindMultiplePeriods = /\.{2,}/gm;
155
+ const intPrefix = '__int__';
156
+ const intReplace = intPrefix + "$1";
159
157
 
160
158
  function makePathArray(str) {
161
- return str.replace('[', '.').replace(']', '').replace(reFindNumbers0, '__int__$1').replace(reFindNumbers1, '.__int__$1.').replace(reFindNumbers2, '__int__$1.').replace(reFindNumbers3, '.__int__$1').replace(reFindMultiplePeriods, '.').split('.').map(d => {
162
- if (d.indexOf('__int__') === 0) {
163
- return parseInt(d.substring('__int__'.length), 10);
159
+ if (typeof str !== 'string') {
160
+ throw new Error();
161
+ }
162
+
163
+ return str.replace('[', '.').replace(']', '').replace(reFindNumbers0, intReplace).replace(reFindNumbers1, "." + intReplace + ".").replace(reFindNumbers2, intReplace + ".").replace(reFindNumbers3, "." + intReplace).replace(reFindMultiplePeriods, '.').split('.').map(d => {
164
+ if (d.indexOf(intPrefix) === 0) {
165
+ return parseInt(d.substring(intPrefix.length), 10);
164
166
  }
165
167
 
166
168
  return d;
@@ -186,6 +188,7 @@
186
188
  ...defaultState
187
189
  };
188
190
  }
191
+
189
192
  class FormApi {
190
193
  // // This carries the context for nested fields
191
194
  constructor(_opts) {
@@ -855,13 +858,9 @@
855
858
  }
856
859
 
857
860
  const formContext = /*#__PURE__*/React__namespace.createContext(null);
858
- function useFormContext(customFormApi) {
861
+ function useFormContext() {
859
862
  const formApi = React__namespace.useContext(formContext);
860
863
 
861
- if (customFormApi) {
862
- return customFormApi;
863
- }
864
-
865
864
  if (!formApi) {
866
865
  throw new Error("You are trying to use the form API outside of a form!");
867
866
  }
@@ -869,22 +868,14 @@
869
868
  return formApi;
870
869
  }
871
870
 
872
- function createUseField(formApi) {
873
- const useFormField = opts => {
874
- return useField({ ...opts,
875
- form: formApi
876
- });
871
+ function createUseField() {
872
+ return opts => {
873
+ return useField(opts);
877
874
  };
878
-
879
- return useFormField;
880
875
  }
881
876
  function useField(opts) {
882
- // invariant( // TODO:
883
- // opts.name,
884
- // `useField: A field is required to use this hook. eg, useField('myField', options)`
885
- // )
886
877
  // Get the form API either manually or from context
887
- const formApi = useFormContext(opts.form);
878
+ const formApi = useFormContext();
888
879
  const [fieldApi] = React__namespace.useState(() => new FieldApi({ ...opts,
889
880
  form: formApi
890
881
  })); // Keep options up to date as they are rendered
@@ -898,10 +889,8 @@
898
889
  return fieldApi;
899
890
  }
900
891
 
901
- function createFieldComponent(formApi) {
902
- const ConnectedField = props => /*#__PURE__*/React__namespace.createElement(Field, _extends({}, props, {
903
- form: formApi
904
- }));
892
+ function createFieldComponent() {
893
+ const ConnectedField = props => /*#__PURE__*/React__namespace.createElement(Field, props);
905
894
 
906
895
  return ConnectedField;
907
896
  }
@@ -913,14 +902,13 @@
913
902
  return functionalUpdate(children, fieldApi);
914
903
  }
915
904
 
916
- //
917
905
  function useForm(opts) {
918
906
  const [formApi] = React__default["default"].useState(() => {
919
907
  // @ts-ignore
920
- const api = new FormApi(opts || {});
908
+ const api = new FormApi(opts);
921
909
  api.Form = createFormComponent(api);
922
- api.Field = createFieldComponent(api);
923
- api.useField = createUseField(api);
910
+ api.Field = createFieldComponent();
911
+ api.useField = createUseField();
924
912
 
925
913
  api.useStore = ( // @ts-ignore
926
914
  selector) => {
@@ -935,10 +923,10 @@
935
923
  };
936
924
 
937
925
  return api;
938
- }); // React.useEffect(() => formApi.mount(), [])
939
-
926
+ });
940
927
  return formApi;
941
928
  }
929
+
942
930
  function createFormComponent(formApi) {
943
931
  const Form = ({
944
932
  children,
@@ -951,45 +939,29 @@
951
939
  }, noFormElement ? children : /*#__PURE__*/React__default["default"].createElement("form", _extends({
952
940
  onSubmit: formApi.handleSubmit,
953
941
  disabled: isSubmitting
954
- }, rest), formApi.options.debugForm ? /*#__PURE__*/React__default["default"].createElement("div", {
955
- style: {
956
- margin: '2rem 0'
957
- }
958
- }, /*#__PURE__*/React__default["default"].createElement("div", {
959
- style: {
960
- fontWeight: 'bolder'
961
- }
962
- }, "Form State"), /*#__PURE__*/React__default["default"].createElement("pre", null, /*#__PURE__*/React__default["default"].createElement("code", null, JSON.stringify(formApi, safeStringifyReplace(), 2)))) : null, children));
942
+ }, rest), children));
963
943
  };
964
944
 
965
945
  return Form;
966
946
  }
967
947
 
968
- function safeStringifyReplace() {
969
- const set = new Set();
970
- return (_key, value) => {
971
- if (typeof value === 'object' || Array.isArray(value)) {
972
- if (set.has(value)) {
973
- return '(circular value)';
974
- }
975
-
976
- set.add(value);
977
- }
978
-
979
- return typeof value === 'function' ? undefined : value;
948
+ function createFormFactory(defaultOpts) {
949
+ return {
950
+ useForm: opts => {
951
+ return useForm({ ...defaultOpts,
952
+ ...opts
953
+ });
954
+ },
955
+ useField: createUseField(),
956
+ Field: createFieldComponent()
980
957
  };
981
958
  }
982
959
 
983
960
  exports.Field = Field;
984
961
  exports.FieldApi = FieldApi;
985
962
  exports.FormApi = FormApi;
986
- exports.createFieldComponent = createFieldComponent;
987
- exports.createFormComponent = createFormComponent;
988
- exports.createUseField = createUseField;
963
+ exports.createFormFactory = createFormFactory;
989
964
  exports.functionalUpdate = functionalUpdate;
990
- exports.getBy = getBy;
991
- exports.getDefaultFormState = getDefaultFormState;
992
- exports.setBy = setBy;
993
965
  exports.useField = useField;
994
966
  exports.useForm = useForm;
995
967