@tanstack/react-form 0.3.1 → 0.3.3
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/build/legacy/createFormFactory.cjs +42 -0
- package/build/legacy/createFormFactory.cjs.map +1 -0
- package/build/legacy/createFormFactory.d.cts +12 -0
- package/build/legacy/createFormFactory.d.ts +12 -0
- package/build/legacy/createFormFactory.js +17 -0
- package/build/legacy/createFormFactory.js.map +1 -0
- package/build/legacy/formContext.cjs +51 -0
- package/build/legacy/formContext.cjs.map +1 -0
- package/build/legacy/formContext.d.cts +13 -0
- package/build/legacy/formContext.d.ts +13 -0
- package/build/legacy/formContext.js +15 -0
- package/build/legacy/formContext.js.map +1 -0
- package/build/legacy/index.cjs +46 -0
- package/build/legacy/index.cjs.map +1 -0
- package/build/legacy/index.d.cts +7 -0
- package/build/legacy/index.d.ts +7 -0
- package/build/legacy/index.js +15 -0
- package/build/legacy/index.js.map +1 -0
- package/build/legacy/types.cjs +19 -0
- package/build/legacy/types.cjs.map +1 -0
- package/build/legacy/types.d.cts +7 -0
- package/build/legacy/types.d.ts +7 -0
- package/build/legacy/types.js +1 -0
- package/build/legacy/types.js.map +1 -0
- package/build/legacy/useField.cjs +84 -0
- package/build/legacy/useField.cjs.map +1 -0
- package/build/legacy/useField.d.cts +29 -0
- package/build/legacy/useField.d.ts +29 -0
- package/build/legacy/useField.js +48 -0
- package/build/legacy/useField.js.map +1 -0
- package/build/legacy/useForm.cjs +72 -0
- package/build/legacy/useForm.cjs.map +1 -0
- package/build/legacy/useForm.d.cts +23 -0
- package/build/legacy/useForm.d.ts +23 -0
- package/build/legacy/useForm.js +37 -0
- package/build/legacy/useForm.js.map +1 -0
- package/build/modern/createFormFactory.cjs +42 -0
- package/build/modern/createFormFactory.cjs.map +1 -0
- package/build/modern/createFormFactory.d.cts +12 -0
- package/build/modern/createFormFactory.d.ts +12 -0
- package/build/modern/createFormFactory.js +17 -0
- package/build/modern/createFormFactory.js.map +1 -0
- package/build/modern/formContext.cjs +51 -0
- package/build/modern/formContext.cjs.map +1 -0
- package/build/modern/formContext.d.cts +13 -0
- package/build/modern/formContext.d.ts +13 -0
- package/build/modern/formContext.js +15 -0
- package/build/modern/formContext.js.map +1 -0
- package/build/modern/index.cjs +46 -0
- package/build/modern/index.cjs.map +1 -0
- package/build/modern/index.d.cts +7 -0
- package/build/modern/index.d.ts +7 -0
- package/build/modern/index.js +15 -0
- package/build/modern/index.js.map +1 -0
- package/build/modern/types.cjs +19 -0
- package/build/modern/types.cjs.map +1 -0
- package/build/modern/types.d.cts +7 -0
- package/build/modern/types.d.ts +7 -0
- package/build/modern/types.js +1 -0
- package/build/modern/types.js.map +1 -0
- package/build/modern/useField.cjs +84 -0
- package/build/modern/useField.cjs.map +1 -0
- package/build/modern/useField.d.cts +29 -0
- package/build/modern/useField.d.ts +29 -0
- package/build/modern/useField.js +48 -0
- package/build/modern/useField.js.map +1 -0
- package/build/modern/useForm.cjs +72 -0
- package/build/modern/useForm.cjs.map +1 -0
- package/build/modern/useForm.d.cts +23 -0
- package/build/modern/useForm.d.ts +23 -0
- package/build/modern/useForm.js +37 -0
- package/build/modern/useForm.js.map +1 -0
- package/package.json +2 -2
- package/src/tests/useField.test-d.tsx +63 -0
- package/src/types.ts +5 -4
- package/src/useField.tsx +60 -60
- package/src/useForm.tsx +3 -4
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/createFormFactory.ts
|
|
21
|
+
var createFormFactory_exports = {};
|
|
22
|
+
__export(createFormFactory_exports, {
|
|
23
|
+
createFormFactory: () => createFormFactory
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(createFormFactory_exports);
|
|
26
|
+
var import_useField = require("./useField.cjs");
|
|
27
|
+
var import_useForm = require("./useForm.cjs");
|
|
28
|
+
function createFormFactory(defaultOpts) {
|
|
29
|
+
return {
|
|
30
|
+
useForm: (opts) => {
|
|
31
|
+
const formOptions = Object.assign({}, defaultOpts, opts);
|
|
32
|
+
return (0, import_useForm.useForm)(formOptions);
|
|
33
|
+
},
|
|
34
|
+
useField: import_useField.useField,
|
|
35
|
+
Field: import_useField.Field
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
+
0 && (module.exports = {
|
|
40
|
+
createFormFactory
|
|
41
|
+
});
|
|
42
|
+
//# sourceMappingURL=createFormFactory.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions } from '@tanstack/form-core'\n\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { useForm } from './useForm'\n\nexport type FormFactory<TFormData> = {\n useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>\n useField: UseField<TFormData>\n Field: FieldComponent<TFormData, TFormData>\n}\n\nexport function createFormFactory<TFormData>(\n defaultOpts?: FormOptions<TFormData>,\n): FormFactory<TFormData> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData>(formOptions)\n },\n useField: useField as any,\n Field: Field as any,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,sBAAoE;AACpE,qBAAwB;AAQjB,SAAS,kBACd,aACwB;AACxB,SAAO;AAAA,IACL,SAAS,CAAC,SAAS;AACjB,YAAM,cAAc,OAAO,OAAO,CAAC,GAAG,aAAa,IAAI;AACvD,iBAAO,wBAAmB,WAAW;AAAA,IACvC;AAAA,IACA,UAAU;AAAA,IACV,OAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FormOptions, FormApi } from '@tanstack/form-core';
|
|
2
|
+
import { UseField, FieldComponent } from './useField.cjs';
|
|
3
|
+
import './types.cjs';
|
|
4
|
+
|
|
5
|
+
type FormFactory<TFormData> = {
|
|
6
|
+
useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>;
|
|
7
|
+
useField: UseField<TFormData>;
|
|
8
|
+
Field: FieldComponent<TFormData, TFormData>;
|
|
9
|
+
};
|
|
10
|
+
declare function createFormFactory<TFormData>(defaultOpts?: FormOptions<TFormData>): FormFactory<TFormData>;
|
|
11
|
+
|
|
12
|
+
export { FormFactory, createFormFactory };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FormOptions, FormApi } from '@tanstack/form-core';
|
|
2
|
+
import { UseField, FieldComponent } from './useField.js';
|
|
3
|
+
import './types.js';
|
|
4
|
+
|
|
5
|
+
type FormFactory<TFormData> = {
|
|
6
|
+
useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>;
|
|
7
|
+
useField: UseField<TFormData>;
|
|
8
|
+
Field: FieldComponent<TFormData, TFormData>;
|
|
9
|
+
};
|
|
10
|
+
declare function createFormFactory<TFormData>(defaultOpts?: FormOptions<TFormData>): FormFactory<TFormData>;
|
|
11
|
+
|
|
12
|
+
export { FormFactory, createFormFactory };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/createFormFactory.ts
|
|
2
|
+
import { Field, useField } from "./useField.js";
|
|
3
|
+
import { useForm } from "./useForm.js";
|
|
4
|
+
function createFormFactory(defaultOpts) {
|
|
5
|
+
return {
|
|
6
|
+
useForm: (opts) => {
|
|
7
|
+
const formOptions = Object.assign({}, defaultOpts, opts);
|
|
8
|
+
return useForm(formOptions);
|
|
9
|
+
},
|
|
10
|
+
useField,
|
|
11
|
+
Field
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
createFormFactory
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=createFormFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions } from '@tanstack/form-core'\n\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { useForm } from './useForm'\n\nexport type FormFactory<TFormData> = {\n useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>\n useField: UseField<TFormData>\n Field: FieldComponent<TFormData, TFormData>\n}\n\nexport function createFormFactory<TFormData>(\n defaultOpts?: FormOptions<TFormData>,\n): FormFactory<TFormData> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData>(formOptions)\n },\n useField: useField as any,\n Field: Field as any,\n }\n}\n"],"mappings":";AAEA,SAA6C,OAAO,gBAAgB;AACpE,SAAS,eAAe;AAQjB,SAAS,kBACd,aACwB;AACxB,SAAO;AAAA,IACL,SAAS,CAAC,SAAS;AACjB,YAAM,cAAc,OAAO,OAAO,CAAC,GAAG,aAAa,IAAI;AACvD,aAAO,QAAmB,WAAW;AAAA,IACvC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/formContext.ts
|
|
31
|
+
var formContext_exports = {};
|
|
32
|
+
__export(formContext_exports, {
|
|
33
|
+
formContext: () => formContext,
|
|
34
|
+
useFormContext: () => useFormContext
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(formContext_exports);
|
|
37
|
+
var React = __toESM(require("react"), 1);
|
|
38
|
+
var formContext = React.createContext(null);
|
|
39
|
+
function useFormContext() {
|
|
40
|
+
const formApi = React.useContext(formContext);
|
|
41
|
+
if (!formApi) {
|
|
42
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
|
43
|
+
}
|
|
44
|
+
return formApi;
|
|
45
|
+
}
|
|
46
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
47
|
+
0 && (module.exports = {
|
|
48
|
+
formContext,
|
|
49
|
+
useFormContext
|
|
50
|
+
});
|
|
51
|
+
//# sourceMappingURL=formContext.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/formContext.ts"],"sourcesContent":["import type { FormApi } from '@tanstack/form-core'\nimport * as React from 'react'\n\nexport const formContext = React.createContext<{\n formApi: FormApi<any>\n parentFieldName?: string\n} | null>(null!)\n\nexport function useFormContext() {\n const formApi = React.useContext(formContext)\n\n if (!formApi) {\n throw new Error(`You are trying to use the form API outside of a form!`)\n }\n\n return formApi\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,YAAuB;AAEhB,IAAM,cAAoB,oBAGvB,IAAK;AAER,SAAS,iBAAiB;AAC/B,QAAM,UAAgB,iBAAW,WAAW;AAE5C,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FormApi } from '@tanstack/form-core';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
|
|
4
|
+
declare const formContext: React.Context<{
|
|
5
|
+
formApi: FormApi<any>;
|
|
6
|
+
parentFieldName?: string | undefined;
|
|
7
|
+
} | null>;
|
|
8
|
+
declare function useFormContext(): {
|
|
9
|
+
formApi: FormApi<any>;
|
|
10
|
+
parentFieldName?: string | undefined;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { formContext, useFormContext };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FormApi } from '@tanstack/form-core';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
|
|
4
|
+
declare const formContext: React.Context<{
|
|
5
|
+
formApi: FormApi<any>;
|
|
6
|
+
parentFieldName?: string | undefined;
|
|
7
|
+
} | null>;
|
|
8
|
+
declare function useFormContext(): {
|
|
9
|
+
formApi: FormApi<any>;
|
|
10
|
+
parentFieldName?: string | undefined;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { formContext, useFormContext };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/formContext.ts
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
var formContext = React.createContext(null);
|
|
4
|
+
function useFormContext() {
|
|
5
|
+
const formApi = React.useContext(formContext);
|
|
6
|
+
if (!formApi) {
|
|
7
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
|
8
|
+
}
|
|
9
|
+
return formApi;
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
formContext,
|
|
13
|
+
useFormContext
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=formContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/formContext.ts"],"sourcesContent":["import type { FormApi } from '@tanstack/form-core'\nimport * as React from 'react'\n\nexport const formContext = React.createContext<{\n formApi: FormApi<any>\n parentFieldName?: string\n} | null>(null!)\n\nexport function useFormContext() {\n const formApi = React.useContext(formContext)\n\n if (!formApi) {\n throw new Error(`You are trying to use the form API outside of a form!`)\n }\n\n return formApi\n}\n"],"mappings":";AACA,YAAY,WAAW;AAEhB,IAAM,cAAoB,oBAGvB,IAAK;AAER,SAAS,iBAAiB;AAC/B,QAAM,UAAgB,iBAAW,WAAW;AAE5C,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Field: () => import_useField.Field,
|
|
24
|
+
FieldApi: () => import_form_core.FieldApi,
|
|
25
|
+
FormApi: () => import_form_core.FormApi,
|
|
26
|
+
createFormFactory: () => import_createFormFactory.createFormFactory,
|
|
27
|
+
functionalUpdate: () => import_form_core.functionalUpdate,
|
|
28
|
+
useField: () => import_useField.useField,
|
|
29
|
+
useForm: () => import_useForm.useForm
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(src_exports);
|
|
32
|
+
var import_form_core = require("@tanstack/form-core");
|
|
33
|
+
var import_useForm = require("./useForm.cjs");
|
|
34
|
+
var import_useField = require("./useField.cjs");
|
|
35
|
+
var import_createFormFactory = require("./createFormFactory.cjs");
|
|
36
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
37
|
+
0 && (module.exports = {
|
|
38
|
+
Field,
|
|
39
|
+
FieldApi,
|
|
40
|
+
FormApi,
|
|
41
|
+
createFormFactory,
|
|
42
|
+
functionalUpdate,
|
|
43
|
+
useField,
|
|
44
|
+
useForm
|
|
45
|
+
});
|
|
46
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type {\n DeepKeys,\n DeepValue,\n FieldApiOptions,\n FieldInfo,\n FieldMeta,\n FieldOptions,\n FieldState,\n FormOptions,\n FormState,\n RequiredByKey,\n Updater,\n UpdaterFn,\n ValidationCause,\n ValidationError,\n ValidationMeta,\n} from '@tanstack/form-core'\n\nexport { FormApi, FieldApi, functionalUpdate } from '@tanstack/form-core'\n\nexport { useForm } from './useForm'\n\nexport type { UseField, FieldComponent } from './useField'\nexport { useField, Field } from './useField'\n\nexport type { FormFactory } from './createFormFactory'\nexport { createFormFactory } from './createFormFactory'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBA,uBAAoD;AAEpD,qBAAwB;AAGxB,sBAAgC;AAGhC,+BAAkC;","names":[]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { DeepKeys, DeepValue, FieldApi, FieldApiOptions, FieldInfo, FieldMeta, FieldOptions, FieldState, FormApi, FormOptions, FormState, RequiredByKey, Updater, UpdaterFn, ValidationCause, ValidationError, ValidationMeta, functionalUpdate } from '@tanstack/form-core';
|
|
2
|
+
export { useForm } from './useForm.cjs';
|
|
3
|
+
export { Field, FieldComponent, UseField, useField } from './useField.cjs';
|
|
4
|
+
export { FormFactory, createFormFactory } from './createFormFactory.cjs';
|
|
5
|
+
import '@tanstack/react-store';
|
|
6
|
+
import 'react';
|
|
7
|
+
import './types.cjs';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { DeepKeys, DeepValue, FieldApi, FieldApiOptions, FieldInfo, FieldMeta, FieldOptions, FieldState, FormApi, FormOptions, FormState, RequiredByKey, Updater, UpdaterFn, ValidationCause, ValidationError, ValidationMeta, functionalUpdate } from '@tanstack/form-core';
|
|
2
|
+
export { useForm } from './useForm.js';
|
|
3
|
+
export { Field, FieldComponent, UseField, useField } from './useField.js';
|
|
4
|
+
export { FormFactory, createFormFactory } from './createFormFactory.js';
|
|
5
|
+
import '@tanstack/react-store';
|
|
6
|
+
import 'react';
|
|
7
|
+
import './types.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { FormApi, FieldApi, functionalUpdate } from "@tanstack/form-core";
|
|
3
|
+
import { useForm } from "./useForm.js";
|
|
4
|
+
import { useField, Field } from "./useField.js";
|
|
5
|
+
import { createFormFactory } from "./createFormFactory.js";
|
|
6
|
+
export {
|
|
7
|
+
Field,
|
|
8
|
+
FieldApi,
|
|
9
|
+
FormApi,
|
|
10
|
+
createFormFactory,
|
|
11
|
+
functionalUpdate,
|
|
12
|
+
useField,
|
|
13
|
+
useForm
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type {\n DeepKeys,\n DeepValue,\n FieldApiOptions,\n FieldInfo,\n FieldMeta,\n FieldOptions,\n FieldState,\n FormOptions,\n FormState,\n RequiredByKey,\n Updater,\n UpdaterFn,\n ValidationCause,\n ValidationError,\n ValidationMeta,\n} from '@tanstack/form-core'\n\nexport { FormApi, FieldApi, functionalUpdate } from '@tanstack/form-core'\n\nexport { useForm } from './useForm'\n\nexport type { UseField, FieldComponent } from './useField'\nexport { useField, Field } from './useField'\n\nexport type { FormFactory } from './createFormFactory'\nexport { createFormFactory } from './createFormFactory'\n"],"mappings":";AAkBA,SAAS,SAAS,UAAU,wBAAwB;AAEpD,SAAS,eAAe;AAGxB,SAAS,UAAU,aAAa;AAGhC,SAAS,yBAAyB;","names":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/types.ts
|
|
17
|
+
var types_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(types_exports);
|
|
19
|
+
//# sourceMappingURL=types.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/types.ts"],"sourcesContent":["import type { FieldOptions, DeepKeys } from '@tanstack/form-core'\n\nexport type UseFieldOptions<\n TData,\n TFormData,\n TName = unknown extends TFormData ? string : DeepKeys<TFormData>,\n> = FieldOptions<TData, TFormData, TName> & {\n mode?: 'value' | 'array'\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DeepKeys, FieldOptions } from '@tanstack/form-core';
|
|
2
|
+
|
|
3
|
+
type UseFieldOptions<TData, TFormData, TName = unknown extends TFormData ? string : DeepKeys<TFormData>> = FieldOptions<TData, TFormData, TName> & {
|
|
4
|
+
mode?: 'value' | 'array';
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export { UseFieldOptions };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DeepKeys, FieldOptions } from '@tanstack/form-core';
|
|
2
|
+
|
|
3
|
+
type UseFieldOptions<TData, TFormData, TName = unknown extends TFormData ? string : DeepKeys<TFormData>> = FieldOptions<TData, TFormData, TName> & {
|
|
4
|
+
mode?: 'value' | 'array';
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export { UseFieldOptions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/useField.tsx
|
|
31
|
+
var useField_exports = {};
|
|
32
|
+
__export(useField_exports, {
|
|
33
|
+
Field: () => Field,
|
|
34
|
+
useField: () => useField
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(useField_exports);
|
|
37
|
+
var import_react = __toESM(require("react"), 1);
|
|
38
|
+
var import_react_store = require("@tanstack/react-store");
|
|
39
|
+
var import_form_core = require("@tanstack/form-core");
|
|
40
|
+
var import_formContext = require("./formContext.cjs");
|
|
41
|
+
var import_use_isomorphic_layout_effect = __toESM(require("use-isomorphic-layout-effect"), 1);
|
|
42
|
+
function useField(opts) {
|
|
43
|
+
const { formApi, parentFieldName } = (0, import_formContext.useFormContext)();
|
|
44
|
+
const [fieldApi] = (0, import_react.useState)(() => {
|
|
45
|
+
const name = (typeof opts.index === "number" ? [parentFieldName, opts.index, opts.name] : [parentFieldName, opts.name]).filter((d) => d !== void 0).join(".");
|
|
46
|
+
const api = new import_form_core.FieldApi({
|
|
47
|
+
...opts,
|
|
48
|
+
form: formApi,
|
|
49
|
+
name
|
|
50
|
+
});
|
|
51
|
+
api.Field = Field;
|
|
52
|
+
return api;
|
|
53
|
+
});
|
|
54
|
+
(0, import_use_isomorphic_layout_effect.default)(() => {
|
|
55
|
+
fieldApi.update({ ...opts, form: formApi });
|
|
56
|
+
});
|
|
57
|
+
(0, import_react_store.useStore)(
|
|
58
|
+
fieldApi.store,
|
|
59
|
+
opts.mode === "array" ? (state) => {
|
|
60
|
+
return [state.meta, Object.keys(state.value || []).length];
|
|
61
|
+
} : void 0
|
|
62
|
+
);
|
|
63
|
+
(0, import_use_isomorphic_layout_effect.default)(() => fieldApi.mount(), [fieldApi]);
|
|
64
|
+
return fieldApi;
|
|
65
|
+
}
|
|
66
|
+
function Field({
|
|
67
|
+
children,
|
|
68
|
+
...fieldOptions
|
|
69
|
+
}) {
|
|
70
|
+
const fieldApi = useField(fieldOptions);
|
|
71
|
+
return /* @__PURE__ */ import_react.default.createElement(
|
|
72
|
+
import_formContext.formContext.Provider,
|
|
73
|
+
{
|
|
74
|
+
value: { formApi: fieldApi.form, parentFieldName: fieldApi.name },
|
|
75
|
+
children: (0, import_form_core.functionalUpdate)(children, fieldApi)
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
+
0 && (module.exports = {
|
|
81
|
+
Field,
|
|
82
|
+
useField
|
|
83
|
+
});
|
|
84
|
+
//# sourceMappingURL=useField.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport type {\n DeepKeys,\n DeepValue,\n FieldApiOptions,\n Narrow,\n} from '@tanstack/form-core'\nimport { FieldApi, type FormApi, functionalUpdate } from '@tanstack/form-core'\nimport { useFormContext, formContext } from './formContext'\nimport useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect'\nimport type { UseFieldOptions } from './types'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FieldApi<_TData, TFormData, Opts, TData> {\n Field: FieldComponent<TData, TFormData>\n }\n}\n\nexport type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(\n opts?: { name: Narrow<TField> } & UseFieldOptions<\n DeepValue<TFormData, TField>,\n TFormData\n >,\n) => FieldApi<DeepValue<TFormData, TField>, TFormData>\n\nexport function useField<\n TData,\n TFormData,\n TName extends unknown extends TFormData\n ? string\n : DeepKeys<TFormData> = unknown extends TFormData\n ? string\n : DeepKeys<TFormData>,\n>(\n opts: UseFieldOptions<TData, TFormData, TName>,\n): FieldApi<\n TData,\n TFormData,\n Omit<typeof opts, 'onMount'> & {\n form: FormApi<TFormData>\n }\n> {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const [fieldApi] = useState(() => {\n const name = (\n typeof opts.index === 'number'\n ? [parentFieldName, opts.index, opts.name]\n : [parentFieldName, opts.name]\n )\n .filter((d) => d !== undefined)\n .join('.')\n\n const api = new FieldApi({\n ...opts,\n form: formApi,\n name: name,\n } as never)\n\n api.Field = Field as never\n\n return api\n })\n\n /**\n * fieldApi.update should not have any side effects. Think of it like a `useRef`\n * that we need to keep updated every render with the most up-to-date information.\n */\n useIsomorphicLayoutEffect(() => {\n fieldApi.update({ ...opts, form: formApi } as never)\n })\n\n useStore(\n fieldApi.store,\n opts.mode === 'array'\n ? (state: any) => {\n return [state.meta, Object.keys(state.value || []).length]\n }\n : undefined,\n )\n // Instantiates field meta and removes it when unrendered\n useIsomorphicLayoutEffect(() => fieldApi.mount(), [fieldApi])\n\n return fieldApi as never\n}\n\ntype FieldComponentProps<\n TParentData,\n TFormData,\n TField,\n TName extends unknown extends TFormData ? string : DeepKeys<TFormData>,\n> = {\n children: (\n fieldApi: FieldApi<\n TField,\n TFormData,\n FieldApiOptions<TField, TFormData, TName>\n >,\n ) => any\n} & (TParentData extends any[]\n ? {\n name?: TName\n index: number\n }\n : {\n name: TName\n index?: never\n }) &\n Omit<UseFieldOptions<TField, TFormData, TName>, 'name' | 'index'>\n\nexport type FieldComponent<TParentData, TFormData> = <\n // Type of the field\n TField,\n // Name of the field\n TName extends unknown extends TFormData ? string : DeepKeys<TFormData>,\n>({\n children,\n ...fieldOptions\n}: FieldComponentProps<TParentData, TFormData, TField, TName>) => any\n\nexport function Field<TData, TFormData>({\n children,\n ...fieldOptions\n}: {\n children: (fieldApi: FieldApi<TData, TFormData>) => any\n} & UseFieldOptions<TData, TFormData>) {\n const fieldApi = useField(fieldOptions as any)\n\n return (\n <formContext.Provider\n value={{ formApi: fieldApi.form, parentFieldName: fieldApi.name }}\n children={functionalUpdate(children, fieldApi as any)}\n />\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAgC;AAChC,yBAAyB;AAOzB,uBAAyD;AACzD,yBAA4C;AAC5C,0CAAsC;AAiB/B,SAAS,SASd,MAOA;AAEA,QAAM,EAAE,SAAS,gBAAgB,QAAI,mCAAe;AAEpD,QAAM,CAAC,QAAQ,QAAI,uBAAS,MAAM;AAChC,UAAM,QACJ,OAAO,KAAK,UAAU,WAClB,CAAC,iBAAiB,KAAK,OAAO,KAAK,IAAI,IACvC,CAAC,iBAAiB,KAAK,IAAI,GAE9B,OAAO,CAAC,MAAM,MAAM,MAAS,EAC7B,KAAK,GAAG;AAEX,UAAM,MAAM,IAAI,0BAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM;AAAA,MACN;AAAA,IACF,CAAU;AAEV,QAAI,QAAQ;AAEZ,WAAO;AAAA,EACT,CAAC;AAMD,0CAAAA,SAA0B,MAAM;AAC9B,aAAS,OAAO,EAAE,GAAG,MAAM,MAAM,QAAQ,CAAU;AAAA,EACrD,CAAC;AAED;AAAA,IACE,SAAS;AAAA,IACT,KAAK,SAAS,UACV,CAAC,UAAe;AACd,aAAO,CAAC,MAAM,MAAM,OAAO,KAAK,MAAM,SAAS,CAAC,CAAC,EAAE,MAAM;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,0CAAAA,SAA0B,MAAM,SAAS,MAAM,GAAG,CAAC,QAAQ,CAAC;AAE5D,SAAO;AACT;AAoCO,SAAS,MAAwB;AAAA,EACtC;AAAA,EACA,GAAG;AACL,GAEuC;AACrC,QAAM,WAAW,SAAS,YAAmB;AAE7C,SACE,6BAAAC,QAAA;AAAA,IAAC,+BAAY;AAAA,IAAZ;AAAA,MACC,OAAO,EAAE,SAAS,SAAS,MAAM,iBAAiB,SAAS,KAAK;AAAA,MAChE,cAAU,mCAAiB,UAAU,QAAe;AAAA;AAAA,EACtD;AAEJ;","names":["useIsomorphicLayoutEffect","React"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DeepKeys, Narrow, DeepValue, FieldApi, FormApi, FieldApiOptions } from '@tanstack/form-core';
|
|
2
|
+
import { UseFieldOptions } from './types.cjs';
|
|
3
|
+
|
|
4
|
+
declare module '@tanstack/form-core' {
|
|
5
|
+
interface FieldApi<_TData, TFormData, Opts, TData> {
|
|
6
|
+
Field: FieldComponent<TData, TFormData>;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(opts?: {
|
|
10
|
+
name: Narrow<TField>;
|
|
11
|
+
} & UseFieldOptions<DeepValue<TFormData, TField>, TFormData>) => FieldApi<DeepValue<TFormData, TField>, TFormData>;
|
|
12
|
+
declare function useField<TData, TFormData, TName extends unknown extends TFormData ? string : DeepKeys<TFormData> = unknown extends TFormData ? string : DeepKeys<TFormData>>(opts: UseFieldOptions<TData, TFormData, TName>): FieldApi<TData, TFormData, Omit<typeof opts, 'onMount'> & {
|
|
13
|
+
form: FormApi<TFormData>;
|
|
14
|
+
}>;
|
|
15
|
+
type FieldComponentProps<TParentData, TFormData, TField, TName extends unknown extends TFormData ? string : DeepKeys<TFormData>> = {
|
|
16
|
+
children: (fieldApi: FieldApi<TField, TFormData, FieldApiOptions<TField, TFormData, TName>>) => any;
|
|
17
|
+
} & (TParentData extends any[] ? {
|
|
18
|
+
name?: TName;
|
|
19
|
+
index: number;
|
|
20
|
+
} : {
|
|
21
|
+
name: TName;
|
|
22
|
+
index?: never;
|
|
23
|
+
}) & Omit<UseFieldOptions<TField, TFormData, TName>, 'name' | 'index'>;
|
|
24
|
+
type FieldComponent<TParentData, TFormData> = <TField, TName extends unknown extends TFormData ? string : DeepKeys<TFormData>>({ children, ...fieldOptions }: FieldComponentProps<TParentData, TFormData, TField, TName>) => any;
|
|
25
|
+
declare function Field<TData, TFormData>({ children, ...fieldOptions }: {
|
|
26
|
+
children: (fieldApi: FieldApi<TData, TFormData>) => any;
|
|
27
|
+
} & UseFieldOptions<TData, TFormData>): JSX.Element;
|
|
28
|
+
|
|
29
|
+
export { Field, FieldComponent, UseField, useField };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DeepKeys, Narrow, DeepValue, FieldApi, FormApi, FieldApiOptions } from '@tanstack/form-core';
|
|
2
|
+
import { UseFieldOptions } from './types.js';
|
|
3
|
+
|
|
4
|
+
declare module '@tanstack/form-core' {
|
|
5
|
+
interface FieldApi<_TData, TFormData, Opts, TData> {
|
|
6
|
+
Field: FieldComponent<TData, TFormData>;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(opts?: {
|
|
10
|
+
name: Narrow<TField>;
|
|
11
|
+
} & UseFieldOptions<DeepValue<TFormData, TField>, TFormData>) => FieldApi<DeepValue<TFormData, TField>, TFormData>;
|
|
12
|
+
declare function useField<TData, TFormData, TName extends unknown extends TFormData ? string : DeepKeys<TFormData> = unknown extends TFormData ? string : DeepKeys<TFormData>>(opts: UseFieldOptions<TData, TFormData, TName>): FieldApi<TData, TFormData, Omit<typeof opts, 'onMount'> & {
|
|
13
|
+
form: FormApi<TFormData>;
|
|
14
|
+
}>;
|
|
15
|
+
type FieldComponentProps<TParentData, TFormData, TField, TName extends unknown extends TFormData ? string : DeepKeys<TFormData>> = {
|
|
16
|
+
children: (fieldApi: FieldApi<TField, TFormData, FieldApiOptions<TField, TFormData, TName>>) => any;
|
|
17
|
+
} & (TParentData extends any[] ? {
|
|
18
|
+
name?: TName;
|
|
19
|
+
index: number;
|
|
20
|
+
} : {
|
|
21
|
+
name: TName;
|
|
22
|
+
index?: never;
|
|
23
|
+
}) & Omit<UseFieldOptions<TField, TFormData, TName>, 'name' | 'index'>;
|
|
24
|
+
type FieldComponent<TParentData, TFormData> = <TField, TName extends unknown extends TFormData ? string : DeepKeys<TFormData>>({ children, ...fieldOptions }: FieldComponentProps<TParentData, TFormData, TField, TName>) => any;
|
|
25
|
+
declare function Field<TData, TFormData>({ children, ...fieldOptions }: {
|
|
26
|
+
children: (fieldApi: FieldApi<TData, TFormData>) => any;
|
|
27
|
+
} & UseFieldOptions<TData, TFormData>): JSX.Element;
|
|
28
|
+
|
|
29
|
+
export { Field, FieldComponent, UseField, useField };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// src/useField.tsx
|
|
2
|
+
import React, { useState } from "react";
|
|
3
|
+
import { useStore } from "@tanstack/react-store";
|
|
4
|
+
import { FieldApi, functionalUpdate } from "@tanstack/form-core";
|
|
5
|
+
import { useFormContext, formContext } from "./formContext.js";
|
|
6
|
+
import useIsomorphicLayoutEffect from "use-isomorphic-layout-effect";
|
|
7
|
+
function useField(opts) {
|
|
8
|
+
const { formApi, parentFieldName } = useFormContext();
|
|
9
|
+
const [fieldApi] = useState(() => {
|
|
10
|
+
const name = (typeof opts.index === "number" ? [parentFieldName, opts.index, opts.name] : [parentFieldName, opts.name]).filter((d) => d !== void 0).join(".");
|
|
11
|
+
const api = new FieldApi({
|
|
12
|
+
...opts,
|
|
13
|
+
form: formApi,
|
|
14
|
+
name
|
|
15
|
+
});
|
|
16
|
+
api.Field = Field;
|
|
17
|
+
return api;
|
|
18
|
+
});
|
|
19
|
+
useIsomorphicLayoutEffect(() => {
|
|
20
|
+
fieldApi.update({ ...opts, form: formApi });
|
|
21
|
+
});
|
|
22
|
+
useStore(
|
|
23
|
+
fieldApi.store,
|
|
24
|
+
opts.mode === "array" ? (state) => {
|
|
25
|
+
return [state.meta, Object.keys(state.value || []).length];
|
|
26
|
+
} : void 0
|
|
27
|
+
);
|
|
28
|
+
useIsomorphicLayoutEffect(() => fieldApi.mount(), [fieldApi]);
|
|
29
|
+
return fieldApi;
|
|
30
|
+
}
|
|
31
|
+
function Field({
|
|
32
|
+
children,
|
|
33
|
+
...fieldOptions
|
|
34
|
+
}) {
|
|
35
|
+
const fieldApi = useField(fieldOptions);
|
|
36
|
+
return /* @__PURE__ */ React.createElement(
|
|
37
|
+
formContext.Provider,
|
|
38
|
+
{
|
|
39
|
+
value: { formApi: fieldApi.form, parentFieldName: fieldApi.name },
|
|
40
|
+
children: functionalUpdate(children, fieldApi)
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
Field,
|
|
46
|
+
useField
|
|
47
|
+
};
|
|
48
|
+
//# sourceMappingURL=useField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport type {\n DeepKeys,\n DeepValue,\n FieldApiOptions,\n Narrow,\n} from '@tanstack/form-core'\nimport { FieldApi, type FormApi, functionalUpdate } from '@tanstack/form-core'\nimport { useFormContext, formContext } from './formContext'\nimport useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect'\nimport type { UseFieldOptions } from './types'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FieldApi<_TData, TFormData, Opts, TData> {\n Field: FieldComponent<TData, TFormData>\n }\n}\n\nexport type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(\n opts?: { name: Narrow<TField> } & UseFieldOptions<\n DeepValue<TFormData, TField>,\n TFormData\n >,\n) => FieldApi<DeepValue<TFormData, TField>, TFormData>\n\nexport function useField<\n TData,\n TFormData,\n TName extends unknown extends TFormData\n ? string\n : DeepKeys<TFormData> = unknown extends TFormData\n ? string\n : DeepKeys<TFormData>,\n>(\n opts: UseFieldOptions<TData, TFormData, TName>,\n): FieldApi<\n TData,\n TFormData,\n Omit<typeof opts, 'onMount'> & {\n form: FormApi<TFormData>\n }\n> {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const [fieldApi] = useState(() => {\n const name = (\n typeof opts.index === 'number'\n ? [parentFieldName, opts.index, opts.name]\n : [parentFieldName, opts.name]\n )\n .filter((d) => d !== undefined)\n .join('.')\n\n const api = new FieldApi({\n ...opts,\n form: formApi,\n name: name,\n } as never)\n\n api.Field = Field as never\n\n return api\n })\n\n /**\n * fieldApi.update should not have any side effects. Think of it like a `useRef`\n * that we need to keep updated every render with the most up-to-date information.\n */\n useIsomorphicLayoutEffect(() => {\n fieldApi.update({ ...opts, form: formApi } as never)\n })\n\n useStore(\n fieldApi.store,\n opts.mode === 'array'\n ? (state: any) => {\n return [state.meta, Object.keys(state.value || []).length]\n }\n : undefined,\n )\n // Instantiates field meta and removes it when unrendered\n useIsomorphicLayoutEffect(() => fieldApi.mount(), [fieldApi])\n\n return fieldApi as never\n}\n\ntype FieldComponentProps<\n TParentData,\n TFormData,\n TField,\n TName extends unknown extends TFormData ? string : DeepKeys<TFormData>,\n> = {\n children: (\n fieldApi: FieldApi<\n TField,\n TFormData,\n FieldApiOptions<TField, TFormData, TName>\n >,\n ) => any\n} & (TParentData extends any[]\n ? {\n name?: TName\n index: number\n }\n : {\n name: TName\n index?: never\n }) &\n Omit<UseFieldOptions<TField, TFormData, TName>, 'name' | 'index'>\n\nexport type FieldComponent<TParentData, TFormData> = <\n // Type of the field\n TField,\n // Name of the field\n TName extends unknown extends TFormData ? string : DeepKeys<TFormData>,\n>({\n children,\n ...fieldOptions\n}: FieldComponentProps<TParentData, TFormData, TField, TName>) => any\n\nexport function Field<TData, TFormData>({\n children,\n ...fieldOptions\n}: {\n children: (fieldApi: FieldApi<TData, TFormData>) => any\n} & UseFieldOptions<TData, TFormData>) {\n const fieldApi = useField(fieldOptions as any)\n\n return (\n <formContext.Provider\n value={{ formApi: fieldApi.form, parentFieldName: fieldApi.name }}\n children={functionalUpdate(children, fieldApi as any)}\n />\n )\n}\n"],"mappings":";AAAA,OAAO,SAAS,gBAAgB;AAChC,SAAS,gBAAgB;AAOzB,SAAS,UAAwB,wBAAwB;AACzD,SAAS,gBAAgB,mBAAmB;AAC5C,OAAO,+BAA+B;AAiB/B,SAAS,SASd,MAOA;AAEA,QAAM,EAAE,SAAS,gBAAgB,IAAI,eAAe;AAEpD,QAAM,CAAC,QAAQ,IAAI,SAAS,MAAM;AAChC,UAAM,QACJ,OAAO,KAAK,UAAU,WAClB,CAAC,iBAAiB,KAAK,OAAO,KAAK,IAAI,IACvC,CAAC,iBAAiB,KAAK,IAAI,GAE9B,OAAO,CAAC,MAAM,MAAM,MAAS,EAC7B,KAAK,GAAG;AAEX,UAAM,MAAM,IAAI,SAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM;AAAA,MACN;AAAA,IACF,CAAU;AAEV,QAAI,QAAQ;AAEZ,WAAO;AAAA,EACT,CAAC;AAMD,4BAA0B,MAAM;AAC9B,aAAS,OAAO,EAAE,GAAG,MAAM,MAAM,QAAQ,CAAU;AAAA,EACrD,CAAC;AAED;AAAA,IACE,SAAS;AAAA,IACT,KAAK,SAAS,UACV,CAAC,UAAe;AACd,aAAO,CAAC,MAAM,MAAM,OAAO,KAAK,MAAM,SAAS,CAAC,CAAC,EAAE,MAAM;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,4BAA0B,MAAM,SAAS,MAAM,GAAG,CAAC,QAAQ,CAAC;AAE5D,SAAO;AACT;AAoCO,SAAS,MAAwB;AAAA,EACtC;AAAA,EACA,GAAG;AACL,GAEuC;AACrC,QAAM,WAAW,SAAS,YAAmB;AAE7C,SACE;AAAA,IAAC,YAAY;AAAA,IAAZ;AAAA,MACC,OAAO,EAAE,SAAS,SAAS,MAAM,iBAAiB,SAAS,KAAK;AAAA,MAChE,UAAU,iBAAiB,UAAU,QAAe;AAAA;AAAA,EACtD;AAEJ;","names":[]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/useForm.tsx
|
|
31
|
+
var useForm_exports = {};
|
|
32
|
+
__export(useForm_exports, {
|
|
33
|
+
useForm: () => useForm
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(useForm_exports);
|
|
36
|
+
var import_form_core = require("@tanstack/form-core");
|
|
37
|
+
var import_react_store = require("@tanstack/react-store");
|
|
38
|
+
var import_react = __toESM(require("react"), 1);
|
|
39
|
+
var import_useField = require("./useField.cjs");
|
|
40
|
+
var import_formContext = require("./formContext.cjs");
|
|
41
|
+
var import_use_isomorphic_layout_effect = __toESM(require("use-isomorphic-layout-effect"), 1);
|
|
42
|
+
function useForm(opts) {
|
|
43
|
+
const [formApi] = (0, import_react.useState)(() => {
|
|
44
|
+
const api = new import_form_core.FormApi(opts);
|
|
45
|
+
api.Provider = function Provider(props) {
|
|
46
|
+
return /* @__PURE__ */ import_react.default.createElement(import_formContext.formContext.Provider, { ...props, value: { formApi: api } });
|
|
47
|
+
};
|
|
48
|
+
api.Field = import_useField.Field;
|
|
49
|
+
api.useField = import_useField.useField;
|
|
50
|
+
api.useStore = (selector) => {
|
|
51
|
+
return (0, import_react_store.useStore)(api.store, selector);
|
|
52
|
+
};
|
|
53
|
+
api.Subscribe = (props) => {
|
|
54
|
+
return (0, import_form_core.functionalUpdate)(
|
|
55
|
+
props.children,
|
|
56
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
57
|
+
(0, import_react_store.useStore)(api.store, props.selector)
|
|
58
|
+
);
|
|
59
|
+
};
|
|
60
|
+
return api;
|
|
61
|
+
});
|
|
62
|
+
formApi.useStore((state) => state.isSubmitting);
|
|
63
|
+
(0, import_use_isomorphic_layout_effect.default)(() => {
|
|
64
|
+
formApi.update(opts);
|
|
65
|
+
});
|
|
66
|
+
return formApi;
|
|
67
|
+
}
|
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
+
0 && (module.exports = {
|
|
70
|
+
useForm
|
|
71
|
+
});
|
|
72
|
+
//# sourceMappingURL=useForm.cjs.map
|