@tanstack/react-form 0.3.0 → 0.3.2
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 +80 -0
- package/build/legacy/useField.cjs.map +1 -0
- package/build/legacy/useField.d.cts +27 -0
- package/build/legacy/useField.d.ts +27 -0
- package/build/legacy/useField.js +44 -0
- package/build/legacy/useField.js.map +1 -0
- package/build/legacy/useForm.cjs +70 -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 +35 -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 +80 -0
- package/build/modern/useField.cjs.map +1 -0
- package/build/modern/useField.d.cts +27 -0
- package/build/modern/useField.d.ts +27 -0
- package/build/modern/useField.js +44 -0
- package/build/modern/useField.js.map +1 -0
- package/build/modern/useForm.cjs +70 -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 +35 -0
- package/build/modern/useForm.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { FormState, FormOptions, FormApi } from '@tanstack/form-core';
|
|
2
|
+
import { NoInfer } from '@tanstack/react-store';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
import { FieldComponent, UseField } from './useField.cjs';
|
|
5
|
+
import './types.cjs';
|
|
6
|
+
|
|
7
|
+
declare module '@tanstack/form-core' {
|
|
8
|
+
interface FormApi<TFormData> {
|
|
9
|
+
Provider: (props: {
|
|
10
|
+
children: any;
|
|
11
|
+
}) => any;
|
|
12
|
+
Field: FieldComponent<TFormData, TFormData>;
|
|
13
|
+
useField: UseField<TFormData>;
|
|
14
|
+
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => TSelected;
|
|
15
|
+
Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
|
|
16
|
+
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
|
|
17
|
+
children: ((state: NoInfer<TSelected>) => ReactNode) | ReactNode;
|
|
18
|
+
}) => any;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
declare function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData>;
|
|
22
|
+
|
|
23
|
+
export { useForm };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { FormState, FormOptions, FormApi } from '@tanstack/form-core';
|
|
2
|
+
import { NoInfer } from '@tanstack/react-store';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
import { FieldComponent, UseField } from './useField.js';
|
|
5
|
+
import './types.js';
|
|
6
|
+
|
|
7
|
+
declare module '@tanstack/form-core' {
|
|
8
|
+
interface FormApi<TFormData> {
|
|
9
|
+
Provider: (props: {
|
|
10
|
+
children: any;
|
|
11
|
+
}) => any;
|
|
12
|
+
Field: FieldComponent<TFormData, TFormData>;
|
|
13
|
+
useField: UseField<TFormData>;
|
|
14
|
+
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => TSelected;
|
|
15
|
+
Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
|
|
16
|
+
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
|
|
17
|
+
children: ((state: NoInfer<TSelected>) => ReactNode) | ReactNode;
|
|
18
|
+
}) => any;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
declare function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData>;
|
|
22
|
+
|
|
23
|
+
export { useForm };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/useForm.tsx
|
|
2
|
+
import { FormApi, functionalUpdate } from "@tanstack/form-core";
|
|
3
|
+
import { useStore } from "@tanstack/react-store";
|
|
4
|
+
import React, { useState } from "react";
|
|
5
|
+
import { Field, useField } from "./useField.js";
|
|
6
|
+
import { formContext } from "./formContext.js";
|
|
7
|
+
import useIsomorphicLayoutEffect from "use-isomorphic-layout-effect";
|
|
8
|
+
function useForm(opts) {
|
|
9
|
+
const [formApi] = useState(() => {
|
|
10
|
+
const api = new FormApi(opts);
|
|
11
|
+
api.Provider = (props) => /* @__PURE__ */ React.createElement(formContext.Provider, { ...props, value: { formApi: api } });
|
|
12
|
+
api.Field = Field;
|
|
13
|
+
api.useField = useField;
|
|
14
|
+
api.useStore = (selector) => {
|
|
15
|
+
return useStore(api.store, selector);
|
|
16
|
+
};
|
|
17
|
+
api.Subscribe = (props) => {
|
|
18
|
+
return functionalUpdate(
|
|
19
|
+
props.children,
|
|
20
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
21
|
+
useStore(api.store, props.selector)
|
|
22
|
+
);
|
|
23
|
+
};
|
|
24
|
+
return api;
|
|
25
|
+
});
|
|
26
|
+
formApi.useStore((state) => state.isSubmitting);
|
|
27
|
+
useIsomorphicLayoutEffect(() => {
|
|
28
|
+
formApi.update(opts);
|
|
29
|
+
});
|
|
30
|
+
return formApi;
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
useForm
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=useForm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/useForm.tsx"],"sourcesContent":["import type { FormState, FormOptions } from '@tanstack/form-core'\nimport { FormApi, functionalUpdate } from '@tanstack/form-core'\nimport type { NoInfer } from '@tanstack/react-store'\nimport { useStore } from '@tanstack/react-store'\nimport React, { type ReactNode, useState } from 'react'\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { formContext } from './formContext'\nimport useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData> {\n Provider: (props: { children: any }) => any\n Field: FieldComponent<TFormData, TFormData>\n useField: UseField<TFormData>\n useStore: <TSelected = NoInfer<FormState<TFormData>>>(\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,\n ) => TSelected\n Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected\n children: ((state: NoInfer<TSelected>) => ReactNode) | ReactNode\n }) => any\n }\n}\n\nexport function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData> {\n const [formApi] = useState(() => {\n // @ts-ignore\n const api = new FormApi<TData>(opts)\n\n // eslint-disable-next-line react/display-name\n api.Provider = (props) => (\n <formContext.Provider {...props} value={{ formApi: api }} />\n )\n api.Field = Field as any\n api.useField = useField as any\n api.useStore = (\n // @ts-ignore\n selector,\n ) => {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useStore(api.store as any, selector as any) as any\n }\n api.Subscribe = (\n // @ts-ignore\n props,\n ) => {\n return functionalUpdate(\n props.children,\n // eslint-disable-next-line react-hooks/rules-of-hooks\n useStore(api.store as any, props.selector as any),\n ) as any\n }\n\n return api\n })\n\n formApi.useStore((state) => state.isSubmitting)\n\n /**\n * formApi.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 formApi.update(opts)\n })\n\n return formApi as any\n}\n"],"mappings":";AACA,SAAS,SAAS,wBAAwB;AAE1C,SAAS,gBAAgB;AACzB,OAAO,SAAyB,gBAAgB;AAChD,SAA6C,OAAO,gBAAgB;AACpE,SAAS,mBAAmB;AAC5B,OAAO,+BAA+B;AAkB/B,SAAS,QAAe,MAA2C;AACxE,QAAM,CAAC,OAAO,IAAI,SAAS,MAAM;AAE/B,UAAM,MAAM,IAAI,QAAe,IAAI;AAGnC,QAAI,WAAW,CAAC,UACd,oCAAC,YAAY,UAAZ,EAAsB,GAAG,OAAO,OAAO,EAAE,SAAS,IAAI,GAAG;AAE5D,QAAI,QAAQ;AACZ,QAAI,WAAW;AACf,QAAI,WAAW,CAEb,aACG;AAEH,aAAO,SAAS,IAAI,OAAc,QAAe;AAAA,IACnD;AACA,QAAI,YAAY,CAEd,UACG;AACH,aAAO;AAAA,QACL,MAAM;AAAA;AAAA,QAEN,SAAS,IAAI,OAAc,MAAM,QAAe;AAAA,MAClD;AAAA,IACF;AAEA,WAAO;AAAA,EACT,CAAC;AAED,UAAQ,SAAS,CAAC,UAAU,MAAM,YAAY;AAM9C,4BAA0B,MAAM;AAC9B,YAAQ,OAAO,IAAI;AAAA,EACrB,CAAC;AAED,SAAO;AACT;","names":[]}
|
|
@@ -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 } from '@tanstack/form-core'\n\nexport type UseFieldOptions<TData, TFormData> = FieldOptions<\n TData,\n TFormData\n> & {\n mode?: 'value' | 'array'\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,80 @@
|
|
|
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({ ...opts, form: formApi, name });
|
|
47
|
+
api.Field = Field;
|
|
48
|
+
return api;
|
|
49
|
+
});
|
|
50
|
+
(0, import_use_isomorphic_layout_effect.default)(() => {
|
|
51
|
+
fieldApi.update({ ...opts, form: formApi });
|
|
52
|
+
});
|
|
53
|
+
(0, import_react_store.useStore)(
|
|
54
|
+
fieldApi.store,
|
|
55
|
+
opts.mode === "array" ? (state) => {
|
|
56
|
+
return [state.meta, Object.keys(state.value || []).length];
|
|
57
|
+
} : void 0
|
|
58
|
+
);
|
|
59
|
+
(0, import_use_isomorphic_layout_effect.default)(() => fieldApi.mount(), [fieldApi]);
|
|
60
|
+
return fieldApi;
|
|
61
|
+
}
|
|
62
|
+
function Field({
|
|
63
|
+
children,
|
|
64
|
+
...fieldOptions
|
|
65
|
+
}) {
|
|
66
|
+
const fieldApi = useField(fieldOptions);
|
|
67
|
+
return /* @__PURE__ */ import_react.default.createElement(
|
|
68
|
+
import_formContext.formContext.Provider,
|
|
69
|
+
{
|
|
70
|
+
value: { formApi: fieldApi.form, parentFieldName: fieldApi.name },
|
|
71
|
+
children: (0, import_form_core.functionalUpdate)(children, fieldApi)
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
76
|
+
0 && (module.exports = {
|
|
77
|
+
Field,
|
|
78
|
+
useField
|
|
79
|
+
});
|
|
80
|
+
//# 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 FieldOptions,\n Narrow,\n} from '@tanstack/form-core'\nimport { FieldApi, 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> {\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<TData, TFormData>(\n opts: UseFieldOptions<TData, TFormData>,\n): FieldApi<TData, TFormData> {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const [fieldApi] = useState<FieldApi<TData, TFormData>>(() => {\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({ ...opts, form: formApi, name: name as any })\n\n api.Field = Field as any\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 as any,\n opts.mode === 'array'\n ? (state: any) => {\n return [state.meta, Object.keys(state.value || []).length]\n }\n : undefined,\n )\n\n // Instantiates field meta and removes it when unrendered\n useIsomorphicLayoutEffect(() => fieldApi.mount(), [fieldApi])\n\n return fieldApi\n}\n\n// export type FieldValue<TFormData, TField> = TFormData extends any[]\n// ? TField extends `[${infer TIndex extends number | 'i'}].${infer TRest}`\n// ? DeepValue<TFormData[TIndex extends 'i' ? number : TIndex], TRest>\n// : TField extends `[${infer TIndex extends number | 'i'}]`\n// ? TFormData[TIndex extends 'i' ? number : TIndex]\n// : never\n// : TField extends `${infer TPrefix}[${infer TIndex extends\n// | number\n// | 'i'}].${infer TRest}`\n// ? DeepValue<\n// DeepValue<TFormData, TPrefix>[TIndex extends 'i' ? number : TIndex],\n// TRest\n// >\n// : TField extends `${infer TPrefix}[${infer TIndex extends number | 'i'}]`\n// ? DeepValue<TFormData, TPrefix>[TIndex extends 'i' ? number : TIndex]\n// : DeepValue<TFormData, TField>\n\nexport type FieldValue<TFormData, TField> = TFormData extends any[]\n ? unknown extends TField\n ? TFormData[number]\n : DeepValue<TFormData[number], TField>\n : DeepValue<TFormData, TField>\n\n// type Test1 = FieldValue<{ foo: { bar: string }[] }, 'foo'>\n// // ^?\n// type Test2 = FieldValue<{ foo: { bar: string }[] }, 'foo[i]'>\n// // ^?\n// type Test3 = FieldValue<{ foo: { bar: string }[] }, 'foo[2].bar'>\n// // ^?\n\nexport type FieldComponent<TParentData, TFormData> = <TField>({\n children,\n ...fieldOptions\n}: {\n children: (\n fieldApi: FieldApi<FieldValue<TParentData, TField>, TFormData>,\n ) => any\n} & Omit<\n UseFieldOptions<FieldValue<TParentData, TField>, TFormData>,\n 'name' | 'index'\n> &\n (TParentData extends any[]\n ? {\n name?: TField extends undefined ? TField : DeepKeys<TParentData>\n index: number\n }\n : {\n name: TField extends undefined ? TField : DeepKeys<TParentData>\n index?: never\n })) => 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,uBAA2C;AAC3C,yBAA4C;AAC5C,0CAAsC;AAiB/B,SAAS,SACd,MAC4B;AAE5B,QAAM,EAAE,SAAS,gBAAgB,QAAI,mCAAe;AAEpD,QAAM,CAAC,QAAQ,QAAI,uBAAqC,MAAM;AAC5D,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,EAAE,GAAG,MAAM,MAAM,SAAS,KAAkB,CAAC;AAEtE,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;AAGA,0CAAAA,SAA0B,MAAM,SAAS,MAAM,GAAG,CAAC,QAAQ,CAAC;AAE5D,SAAO;AACT;AAqDO,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,27 @@
|
|
|
1
|
+
import { FieldApi, DeepKeys, Narrow, DeepValue } from '@tanstack/form-core';
|
|
2
|
+
import { UseFieldOptions } from './types.cjs';
|
|
3
|
+
|
|
4
|
+
declare module '@tanstack/form-core' {
|
|
5
|
+
interface FieldApi<TData, TFormData> {
|
|
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>(opts: UseFieldOptions<TData, TFormData>): FieldApi<TData, TFormData>;
|
|
13
|
+
type FieldValue<TFormData, TField> = TFormData extends any[] ? unknown extends TField ? TFormData[number] : DeepValue<TFormData[number], TField> : DeepValue<TFormData, TField>;
|
|
14
|
+
type FieldComponent<TParentData, TFormData> = <TField>({ children, ...fieldOptions }: {
|
|
15
|
+
children: (fieldApi: FieldApi<FieldValue<TParentData, TField>, TFormData>) => any;
|
|
16
|
+
} & Omit<UseFieldOptions<FieldValue<TParentData, TField>, TFormData>, 'name' | 'index'> & (TParentData extends any[] ? {
|
|
17
|
+
name?: TField extends undefined ? TField : DeepKeys<TParentData>;
|
|
18
|
+
index: number;
|
|
19
|
+
} : {
|
|
20
|
+
name: TField extends undefined ? TField : DeepKeys<TParentData>;
|
|
21
|
+
index?: never;
|
|
22
|
+
})) => any;
|
|
23
|
+
declare function Field<TData, TFormData>({ children, ...fieldOptions }: {
|
|
24
|
+
children: (fieldApi: FieldApi<TData, TFormData>) => any;
|
|
25
|
+
} & UseFieldOptions<TData, TFormData>): JSX.Element;
|
|
26
|
+
|
|
27
|
+
export { Field, FieldComponent, FieldValue, UseField, useField };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { FieldApi, DeepKeys, Narrow, DeepValue } from '@tanstack/form-core';
|
|
2
|
+
import { UseFieldOptions } from './types.js';
|
|
3
|
+
|
|
4
|
+
declare module '@tanstack/form-core' {
|
|
5
|
+
interface FieldApi<TData, TFormData> {
|
|
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>(opts: UseFieldOptions<TData, TFormData>): FieldApi<TData, TFormData>;
|
|
13
|
+
type FieldValue<TFormData, TField> = TFormData extends any[] ? unknown extends TField ? TFormData[number] : DeepValue<TFormData[number], TField> : DeepValue<TFormData, TField>;
|
|
14
|
+
type FieldComponent<TParentData, TFormData> = <TField>({ children, ...fieldOptions }: {
|
|
15
|
+
children: (fieldApi: FieldApi<FieldValue<TParentData, TField>, TFormData>) => any;
|
|
16
|
+
} & Omit<UseFieldOptions<FieldValue<TParentData, TField>, TFormData>, 'name' | 'index'> & (TParentData extends any[] ? {
|
|
17
|
+
name?: TField extends undefined ? TField : DeepKeys<TParentData>;
|
|
18
|
+
index: number;
|
|
19
|
+
} : {
|
|
20
|
+
name: TField extends undefined ? TField : DeepKeys<TParentData>;
|
|
21
|
+
index?: never;
|
|
22
|
+
})) => any;
|
|
23
|
+
declare function Field<TData, TFormData>({ children, ...fieldOptions }: {
|
|
24
|
+
children: (fieldApi: FieldApi<TData, TFormData>) => any;
|
|
25
|
+
} & UseFieldOptions<TData, TFormData>): JSX.Element;
|
|
26
|
+
|
|
27
|
+
export { Field, FieldComponent, FieldValue, UseField, useField };
|
|
@@ -0,0 +1,44 @@
|
|
|
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({ ...opts, form: formApi, name });
|
|
12
|
+
api.Field = Field;
|
|
13
|
+
return api;
|
|
14
|
+
});
|
|
15
|
+
useIsomorphicLayoutEffect(() => {
|
|
16
|
+
fieldApi.update({ ...opts, form: formApi });
|
|
17
|
+
});
|
|
18
|
+
useStore(
|
|
19
|
+
fieldApi.store,
|
|
20
|
+
opts.mode === "array" ? (state) => {
|
|
21
|
+
return [state.meta, Object.keys(state.value || []).length];
|
|
22
|
+
} : void 0
|
|
23
|
+
);
|
|
24
|
+
useIsomorphicLayoutEffect(() => fieldApi.mount(), [fieldApi]);
|
|
25
|
+
return fieldApi;
|
|
26
|
+
}
|
|
27
|
+
function Field({
|
|
28
|
+
children,
|
|
29
|
+
...fieldOptions
|
|
30
|
+
}) {
|
|
31
|
+
const fieldApi = useField(fieldOptions);
|
|
32
|
+
return /* @__PURE__ */ React.createElement(
|
|
33
|
+
formContext.Provider,
|
|
34
|
+
{
|
|
35
|
+
value: { formApi: fieldApi.form, parentFieldName: fieldApi.name },
|
|
36
|
+
children: functionalUpdate(children, fieldApi)
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
export {
|
|
41
|
+
Field,
|
|
42
|
+
useField
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=useField.js.map
|