@tanstack/react-form 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +35 -0
- package/build/cjs/Field.js +57 -0
- package/build/cjs/Field.js.map +1 -0
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +33 -0
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
- package/build/cjs/formContext.js +54 -0
- package/build/cjs/formContext.js.map +1 -0
- package/build/cjs/index.js +27 -0
- package/build/cjs/index.js.map +1 -0
- package/build/cjs/useField.js +71 -0
- package/build/cjs/useField.js.map +1 -0
- package/build/cjs/useForm.js +96 -0
- package/build/cjs/useForm.js.map +1 -0
- package/build/esm/index.js +160 -0
- package/build/esm/index.js.map +1 -0
- package/build/stats-html.html +2689 -0
- package/build/stats-react.json +367 -0
- package/build/types/form-core/src/FieldApi.d.ts +69 -0
- package/build/types/form-core/src/FormApi.d.ts +74 -0
- package/build/types/form-core/src/index.d.ts +3 -0
- package/build/types/form-core/src/utils.d.ts +16 -0
- package/build/types/index.d.ts +50 -0
- package/build/types/react-form/src/Field.d.ts +9 -0
- package/build/types/react-form/src/formContext.d.ts +4 -0
- package/build/types/react-form/src/index.d.ts +3 -0
- package/build/types/react-form/src/tests/test.test.d.ts +0 -0
- package/build/types/react-form/src/useField.d.ts +7 -0
- package/build/types/react-form/src/useForm.d.ts +25 -0
- package/build/umd/index.development.js +907 -0
- package/build/umd/index.development.js.map +1 -0
- package/build/umd/index.production.js +42 -0
- package/build/umd/index.production.js.map +1 -0
- package/package.json +55 -0
- package/src/Field.tsx +37 -0
- package/src/formContext.ts +18 -0
- package/src/index.ts +3 -0
- package/src/tests/test.test.tsx +5 -0
- package/src/useField.ts +54 -0
- package/src/useForm.tsx +125 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-present Tanner Linsley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<img src="https://static.scarf.sh/a.png?x-pxid=be2d8a11-9712-4c1d-9963-580b2d4fb133" />
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Hooks for managing form state in React
|
|
6
|
+
|
|
7
|
+
<a href="https://twitter.com/intent/tweet?button_hashtag=TanStack" target="\_parent">
|
|
8
|
+
<img alt="#TanStack" src="https://img.shields.io/twitter/url?color=%2308a0e9&label=%23TanStack&style=social&url=https%3A%2F%2Ftwitter.com%2Fintent%2Ftweet%3Fbutton_hashtag%3DTanStack">
|
|
9
|
+
</a><a href="https://discord.com/invite/WrRKjPJ" target="\_parent">
|
|
10
|
+
<img alt="" src="https://img.shields.io/badge/Discord-TanStack-%235865F2" />
|
|
11
|
+
</a><a href="https://github.com/TanStack/form/actions?query=workflow%3A%22react-form+tests%22">
|
|
12
|
+
<img src="https://github.com/TanStack/form/workflows/react-form%20tests/badge.svg" />
|
|
13
|
+
</a><a href="https://www.npmjs.com/package/@tanstack/form-core" target="\_parent">
|
|
14
|
+
<img alt="" src="https://img.shields.io/npm/dm/@tanstack/form-core.svg" />
|
|
15
|
+
</a><a href="https://bundlephobia.com/package/@tanstack/react-form@latest" target="\_parent">
|
|
16
|
+
<img alt="" src="https://badgen.net/bundlephobia/minzip/@tanstack/react-form" />
|
|
17
|
+
</a><a href="#badge">
|
|
18
|
+
<img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
|
|
19
|
+
</a><a href="https://github.com/TanStack/form/discussions">
|
|
20
|
+
<img alt="Join the discussion on Github" src="https://img.shields.io/badge/Github%20Discussions%20%26%20Support-Chat%20now!-blue" />
|
|
21
|
+
</a><a href="https://bestofjs.org/projects/tanstack-form"><img alt="Best of JS" src="https://img.shields.io/endpoint?url=https://bestofjs-serverless.now.sh/api/project-badge?fullName=TanStack%form%26since=daily" /></a><a href="https://github.com/TanStack/form/" target="\_parent">
|
|
22
|
+
<img alt="" src="https://img.shields.io/github/stars/TanStack/form.svg?style=social&label=Star" />
|
|
23
|
+
</a><a href="https://twitter.com/tannerlinsley" target="\_parent">
|
|
24
|
+
<img alt="" src="https://img.shields.io/twitter/follow/tannerlinsley.svg?style=social&label=Follow" />
|
|
25
|
+
</a> <a href="https://gitpod.io/from-referrer/">
|
|
26
|
+
<img src="https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod" alt="Gitpod Ready-to-Code"/>
|
|
27
|
+
</a>
|
|
28
|
+
|
|
29
|
+
Enjoy this library? Try the entire [TanStack](https://tanstack.com)! [TanStack Table](https://github.com/TanStack/table), [TanStack Router](https://github.com/tanstack/router), [TanStack Virtual](https://github.com/tanstack/virtual), [React Charts](https://github.com/TanStack/react-charts), [React Ranger](https://github.com/TanStack/ranger)
|
|
30
|
+
|
|
31
|
+
## Visit [tanstack.com/form](https://tanstack.com/form) for docs, guides, API and more!
|
|
32
|
+
|
|
33
|
+
### [Become a Sponsor!](https://github.com/sponsors/tannerlinsley/)
|
|
34
|
+
|
|
35
|
+
<!-- Use the force, Luke -->
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* react-form
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
|
|
16
|
+
var React = require('react');
|
|
17
|
+
var formCore = require('@tanstack/form-core');
|
|
18
|
+
var useField = require('./useField.js');
|
|
19
|
+
|
|
20
|
+
function _interopNamespace(e) {
|
|
21
|
+
if (e && e.__esModule) return e;
|
|
22
|
+
var n = Object.create(null);
|
|
23
|
+
if (e) {
|
|
24
|
+
Object.keys(e).forEach(function (k) {
|
|
25
|
+
if (k !== 'default') {
|
|
26
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
27
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () { return e[k]; }
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
n["default"] = e;
|
|
35
|
+
return Object.freeze(n);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
39
|
+
|
|
40
|
+
function createFieldComponent(formApi) {
|
|
41
|
+
const ConnectedField = props => /*#__PURE__*/React__namespace.createElement(Field, _rollupPluginBabelHelpers["extends"]({}, props, {
|
|
42
|
+
form: formApi
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
return ConnectedField;
|
|
46
|
+
}
|
|
47
|
+
function Field({
|
|
48
|
+
children,
|
|
49
|
+
...fieldOptions
|
|
50
|
+
}) {
|
|
51
|
+
const fieldApi = useField.useField(fieldOptions);
|
|
52
|
+
return formCore.functionalUpdate(children, fieldApi);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
exports.Field = Field;
|
|
56
|
+
exports.createFieldComponent = createFieldComponent;
|
|
57
|
+
//# sourceMappingURL=Field.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Field.js","sources":["../../src/Field.tsx"],"sourcesContent":["import * as React from 'react'\nimport {\n functionalUpdate,\n type DeepKeys,\n type DeepValue,\n type FieldApi,\n type FieldOptions,\n type FormApi,\n} from '@tanstack/form-core'\nimport { useField } from './useField'\n\n//\n\nexport type FieldComponent<TFormData> = <TField extends DeepKeys<TFormData>>({\n children,\n ...fieldOptions\n}: {\n children: (fieldApi: FieldApi<DeepValue<TFormData, TField>, TFormData>) => any\n name: TField\n} & Omit<FieldOptions<DeepValue<TFormData, TField>, TFormData>, 'name'>) => any\n\nexport function createFieldComponent<TFormData>(formApi: FormApi<TFormData>) {\n const ConnectedField: FieldComponent<TFormData> = (props) => (\n <Field {...(props as any)} form={formApi} />\n )\n return ConnectedField\n}\n\nexport function Field<TData, TFormData>({\n children,\n ...fieldOptions\n}: {\n children: (fieldApi: FieldApi<TData, TFormData>) => any\n} & FieldOptions<TData, TFormData>) {\n const fieldApi = useField(fieldOptions as any)\n return functionalUpdate(children, fieldApi as any)\n}\n"],"names":["createFieldComponent","formApi","ConnectedField","props","React","Field","children","fieldOptions","fieldApi","useField","functionalUpdate"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBO,SAASA,oBAAT,CAAyCC,OAAzC,EAAsE;AAC3E,EAAA,MAAMC,cAAyC,GAAIC,KAAD,iBAChDC,gBAAC,CAAA,aAAA,CAAA,KAAD,2CAAYD,KAAZ,EAAA;AAA2B,IAAA,IAAI,EAAEF,OAAAA;GADnC,CAAA,CAAA,CAAA;;AAGA,EAAA,OAAOC,cAAP,CAAA;AACD,CAAA;AAEM,SAASG,KAAT,CAAiC;EACtCC,QADsC;EAEtC,GAAGC,YAAAA;AAFmC,CAAjC,EAK6B;AAClC,EAAA,MAAMC,QAAQ,GAAGC,iBAAQ,CAACF,YAAD,CAAzB,CAAA;AACA,EAAA,OAAOG,yBAAgB,CAACJ,QAAD,EAAWE,QAAX,CAAvB,CAAA;AACD;;;;;"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* react-form
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
function _extends() {
|
|
16
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
17
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
18
|
+
var source = arguments[i];
|
|
19
|
+
|
|
20
|
+
for (var key in source) {
|
|
21
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
22
|
+
target[key] = source[key];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return target;
|
|
28
|
+
};
|
|
29
|
+
return _extends.apply(this, arguments);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
exports["extends"] = _extends;
|
|
33
|
+
//# sourceMappingURL=_rollupPluginBabelHelpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_rollupPluginBabelHelpers.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* react-form
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var React = require('react');
|
|
16
|
+
|
|
17
|
+
function _interopNamespace(e) {
|
|
18
|
+
if (e && e.__esModule) return e;
|
|
19
|
+
var n = Object.create(null);
|
|
20
|
+
if (e) {
|
|
21
|
+
Object.keys(e).forEach(function (k) {
|
|
22
|
+
if (k !== 'default') {
|
|
23
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
24
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () { return e[k]; }
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
n["default"] = e;
|
|
32
|
+
return Object.freeze(n);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
36
|
+
|
|
37
|
+
const formContext = /*#__PURE__*/React__namespace.createContext(null);
|
|
38
|
+
function useFormContext(customFormApi) {
|
|
39
|
+
const formApi = React__namespace.useContext(formContext);
|
|
40
|
+
|
|
41
|
+
if (customFormApi) {
|
|
42
|
+
return customFormApi;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!formApi) {
|
|
46
|
+
throw new Error("You are trying to use the form API outside of a form!");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return formApi;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
exports.formContext = formContext;
|
|
53
|
+
exports.useFormContext = useFormContext;
|
|
54
|
+
//# sourceMappingURL=formContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formContext.js","sources":["../../src/formContext.ts"],"sourcesContent":["import { FormApi } from '@tanstack/form-core'\nimport * as React from 'react'\n\nexport const formContext = React.createContext<FormApi<any>>(null!)\n\nexport function useFormContext(customFormApi?: FormApi<any>) {\n const formApi = React.useContext(formContext)\n\n if (customFormApi) {\n return customFormApi\n }\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"],"names":["formContext","React","createContext","useFormContext","customFormApi","formApi","useContext","Error"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGO,MAAMA,WAAW,gBAAGC,gBAAK,CAACC,aAAN,CAAkC,IAAlC,EAApB;AAEA,SAASC,cAAT,CAAwBC,aAAxB,EAAsD;AAC3D,EAAA,MAAMC,OAAO,GAAGJ,gBAAK,CAACK,UAAN,CAAiBN,WAAjB,CAAhB,CAAA;;AAEA,EAAA,IAAII,aAAJ,EAAmB;AACjB,IAAA,OAAOA,aAAP,CAAA;AACD,GAAA;;EAED,IAAI,CAACC,OAAL,EAAc;IACZ,MAAM,IAAIE,KAAJ,CAAN,uDAAA,CAAA,CAAA;AACD,GAAA;;AAED,EAAA,OAAOF,OAAP,CAAA;AACD;;;;;"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* react-form
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var useForm = require('./useForm.js');
|
|
16
|
+
var Field = require('./Field.js');
|
|
17
|
+
var useField = require('./useField.js');
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
exports.createFormComponent = useForm.createFormComponent;
|
|
22
|
+
exports.useForm = useForm.useForm;
|
|
23
|
+
exports.Field = Field.Field;
|
|
24
|
+
exports.createFieldComponent = Field.createFieldComponent;
|
|
25
|
+
exports.createUseField = useField.createUseField;
|
|
26
|
+
exports.useField = useField.useField;
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* react-form
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var React = require('react');
|
|
16
|
+
var reactStore = require('@tanstack/react-store');
|
|
17
|
+
var formCore = require('@tanstack/form-core');
|
|
18
|
+
var formContext = require('./formContext.js');
|
|
19
|
+
|
|
20
|
+
function _interopNamespace(e) {
|
|
21
|
+
if (e && e.__esModule) return e;
|
|
22
|
+
var n = Object.create(null);
|
|
23
|
+
if (e) {
|
|
24
|
+
Object.keys(e).forEach(function (k) {
|
|
25
|
+
if (k !== 'default') {
|
|
26
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
27
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () { return e[k]; }
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
n["default"] = e;
|
|
35
|
+
return Object.freeze(n);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
39
|
+
|
|
40
|
+
function createUseField(formApi) {
|
|
41
|
+
const useFormField = opts => {
|
|
42
|
+
return useField({ ...opts,
|
|
43
|
+
form: formApi
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
return useFormField;
|
|
48
|
+
}
|
|
49
|
+
function useField(opts) {
|
|
50
|
+
// invariant( // TODO:
|
|
51
|
+
// opts.name,
|
|
52
|
+
// `useField: A field is required to use this hook. eg, useField('myField', options)`
|
|
53
|
+
// )
|
|
54
|
+
// Get the form API either manually or from context
|
|
55
|
+
const formApi = formContext.useFormContext(opts.form);
|
|
56
|
+
const [fieldApi] = React__namespace.useState(() => new formCore.FieldApi({ ...opts,
|
|
57
|
+
form: formApi
|
|
58
|
+
})); // Keep options up to date as they are rendered
|
|
59
|
+
|
|
60
|
+
fieldApi.update({ ...opts,
|
|
61
|
+
form: formApi
|
|
62
|
+
});
|
|
63
|
+
reactStore.useStore(fieldApi.store); // Instantiates field meta and removes it when unrendered
|
|
64
|
+
|
|
65
|
+
React__namespace.useEffect(() => fieldApi.mount(), [fieldApi]);
|
|
66
|
+
return fieldApi;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
exports.createUseField = createUseField;
|
|
70
|
+
exports.useField = useField;
|
|
71
|
+
//# sourceMappingURL=useField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useField.js","sources":["../../src/useField.ts"],"sourcesContent":["import * as React from 'react'\n//\nimport { useStore } from '@tanstack/react-store'\nimport type {\n DeepKeys,\n DeepValue,\n FieldOptions,\n FormApi,\n} from '@tanstack/form-core'\nimport { FieldApi } from '@tanstack/form-core'\nimport { useFormContext } from './formContext'\n\nexport type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(\n opts?: { name: TField } & FieldOptions<\n DeepValue<TFormData, TField>,\n TFormData\n >,\n) => FieldApi<DeepValue<TFormData, TField>, TFormData>\n\nexport function createUseField<TFormData>(formApi: FormApi<TFormData>) {\n const useFormField: UseField<TFormData> = (opts) => {\n return useField({ ...opts, form: formApi } as any)\n }\n\n return useFormField\n}\n\nexport function useField<TData, TFormData>(\n opts: FieldOptions<TData, TFormData> & {\n // selector: (state: FieldApi<TData, TFormData>) => TSelected\n },\n): FieldApi<TData, TFormData> {\n // invariant( // TODO:\n // opts.name,\n // `useField: A field is required to use this hook. eg, useField('myField', options)`\n // )\n\n // Get the form API either manually or from context\n const formApi = useFormContext(opts.form)\n\n const [fieldApi] = React.useState<FieldApi<TData, TFormData>>(\n () => new FieldApi({ ...opts, form: formApi }),\n )\n\n // Keep options up to date as they are rendered\n fieldApi.update({ ...opts, form: formApi })\n\n useStore(fieldApi.store)\n\n // Instantiates field meta and removes it when unrendered\n React.useEffect(() => fieldApi.mount(), [fieldApi])\n\n return fieldApi\n}\n"],"names":["createUseField","formApi","useFormField","opts","useField","form","useFormContext","fieldApi","React","useState","FieldApi","update","useStore","store","useEffect","mount"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBO,SAASA,cAAT,CAAmCC,OAAnC,EAAgE;EACrE,MAAMC,YAAiC,GAAIC,IAAD,IAAU;AAClD,IAAA,OAAOC,QAAQ,CAAC,EAAE,GAAGD,IAAL;AAAWE,MAAAA,IAAI,EAAEJ,OAAAA;AAAjB,KAAD,CAAf,CAAA;GADF,CAAA;;AAIA,EAAA,OAAOC,YAAP,CAAA;AACD,CAAA;AAEM,SAASE,QAAT,CACLD,IADK,EAIuB;AAC5B;AACA;AACA;AACA;AAEA;AACA,EAAA,MAAMF,OAAO,GAAGK,0BAAc,CAACH,IAAI,CAACE,IAAN,CAA9B,CAAA;AAEA,EAAA,MAAM,CAACE,QAAD,CAAaC,GAAAA,gBAAK,CAACC,QAAN,CACjB,MAAM,IAAIC,iBAAJ,CAAa,EAAE,GAAGP,IAAL;AAAWE,IAAAA,IAAI,EAAEJ,OAAAA;GAA9B,CADW,CAAnB,CAT4B;;AAc5BM,EAAAA,QAAQ,CAACI,MAAT,CAAgB,EAAE,GAAGR,IAAL;AAAWE,IAAAA,IAAI,EAAEJ,OAAAA;GAAjC,CAAA,CAAA;AAEAW,EAAAA,mBAAQ,CAACL,QAAQ,CAACM,KAAV,CAAR,CAhB4B;;EAmB5BL,gBAAK,CAACM,SAAN,CAAgB,MAAMP,QAAQ,CAACQ,KAAT,EAAtB,EAAwC,CAACR,QAAD,CAAxC,CAAA,CAAA;AAEA,EAAA,OAAOA,QAAP,CAAA;AACD;;;;;"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* react-form
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
|
|
16
|
+
var formCore = require('@tanstack/form-core');
|
|
17
|
+
var reactStore = require('@tanstack/react-store');
|
|
18
|
+
var React = require('react');
|
|
19
|
+
var Field = require('./Field.js');
|
|
20
|
+
var useField = require('./useField.js');
|
|
21
|
+
var formContext = require('./formContext.js');
|
|
22
|
+
|
|
23
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
24
|
+
|
|
25
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
26
|
+
|
|
27
|
+
//
|
|
28
|
+
function useForm(opts) {
|
|
29
|
+
const [formApi] = React__default["default"].useState(() => {
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
const api = new formCore.FormApi(opts || {});
|
|
32
|
+
api.Form = createFormComponent(api);
|
|
33
|
+
api.Field = Field.createFieldComponent(api);
|
|
34
|
+
api.useField = useField.createUseField(api);
|
|
35
|
+
|
|
36
|
+
api.useStore = ( // @ts-ignore
|
|
37
|
+
selector) => {
|
|
38
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
39
|
+
return reactStore.useStore(api.store, selector);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
api.Subscribe = ( // @ts-ignore
|
|
43
|
+
props) => {
|
|
44
|
+
return formCore.functionalUpdate(props.children, // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
45
|
+
reactStore.useStore(api.store, props.selector));
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return api;
|
|
49
|
+
}); // React.useEffect(() => formApi.mount(), [])
|
|
50
|
+
|
|
51
|
+
return formApi;
|
|
52
|
+
}
|
|
53
|
+
function createFormComponent(formApi) {
|
|
54
|
+
const Form = ({
|
|
55
|
+
children,
|
|
56
|
+
noFormElement,
|
|
57
|
+
...rest
|
|
58
|
+
}) => {
|
|
59
|
+
const isSubmitting = formApi.useStore(state => state.isSubmitting);
|
|
60
|
+
return /*#__PURE__*/React__default["default"].createElement(formContext.formContext.Provider, {
|
|
61
|
+
value: formApi
|
|
62
|
+
}, noFormElement ? children : /*#__PURE__*/React__default["default"].createElement("form", _rollupPluginBabelHelpers["extends"]({
|
|
63
|
+
onSubmit: formApi.handleSubmit,
|
|
64
|
+
disabled: isSubmitting
|
|
65
|
+
}, rest), formApi.options.debugForm ? /*#__PURE__*/React__default["default"].createElement("div", {
|
|
66
|
+
style: {
|
|
67
|
+
margin: '2rem 0'
|
|
68
|
+
}
|
|
69
|
+
}, /*#__PURE__*/React__default["default"].createElement("div", {
|
|
70
|
+
style: {
|
|
71
|
+
fontWeight: 'bolder'
|
|
72
|
+
}
|
|
73
|
+
}, "Form State"), /*#__PURE__*/React__default["default"].createElement("pre", null, /*#__PURE__*/React__default["default"].createElement("code", null, JSON.stringify(formApi, safeStringifyReplace(), 2)))) : null, children));
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return Form;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function safeStringifyReplace() {
|
|
80
|
+
const set = new Set();
|
|
81
|
+
return (_key, value) => {
|
|
82
|
+
if (typeof value === 'object' || Array.isArray(value)) {
|
|
83
|
+
if (set.has(value)) {
|
|
84
|
+
return '(circular value)';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
set.add(value);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return typeof value === 'function' ? undefined : value;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
exports.createFormComponent = createFormComponent;
|
|
95
|
+
exports.useForm = useForm;
|
|
96
|
+
//# sourceMappingURL=useForm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useForm.js","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 from 'react'\nimport { createFieldComponent, type FieldComponent } from './Field'\nimport { createUseField, type UseField } from './useField'\nimport { formContext } from './formContext'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData> {\n Form: FormComponent\n Field: FieldComponent<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:\n | ((state: NoInfer<TSelected>) => React.ReactNode)\n | React.ReactNode\n }) => any\n }\n}\n//\n\nexport function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData> {\n const [formApi] = React.useState(() => {\n // @ts-ignore\n const api = new FormApi<TData>(opts || {})\n\n api.Form = createFormComponent(api)\n api.Field = createFieldComponent(api)\n api.useField = createUseField(api)\n api.useStore = (\n // @ts-ignore\n selector,\n ) => {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useStore(api.store, selector) 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, props.selector),\n ) as any\n }\n\n return api\n })\n\n // React.useEffect(() => formApi.mount(), [])\n\n return formApi\n}\n\nexport type FormProps = React.HTMLProps<HTMLFormElement> & {\n children: React.ReactNode\n noFormElement?: boolean\n}\n\nexport type FormComponent = (props: FormProps) => any\n\nexport function createFormComponent(formApi: FormApi<any>) {\n const Form: FormComponent = ({ children, noFormElement, ...rest }) => {\n const isSubmitting = formApi.useStore((state) => state.isSubmitting)\n\n return (\n <formContext.Provider value={formApi}>\n {noFormElement ? (\n children\n ) : (\n <form\n onSubmit={formApi.handleSubmit}\n disabled={isSubmitting}\n {...rest}\n >\n {formApi.options.debugForm ? (\n <div\n style={{\n margin: '2rem 0',\n }}\n >\n <div\n style={{\n fontWeight: 'bolder',\n }}\n >\n Form State\n </div>\n <pre>\n <code>\n {JSON.stringify(formApi, safeStringifyReplace(), 2)}\n </code>\n </pre>\n </div>\n ) : null}\n {children}\n </form>\n )}\n </formContext.Provider>\n )\n }\n\n return Form\n}\n\nfunction safeStringifyReplace() {\n const set = new Set()\n return (_key: string, value: any) => {\n if (typeof value === 'object' || Array.isArray(value)) {\n if (set.has(value)) {\n return '(circular value)'\n }\n set.add(value)\n }\n return typeof value === 'function' ? undefined : value\n }\n}\n"],"names":["useForm","opts","formApi","React","useState","api","FormApi","Form","createFormComponent","Field","createFieldComponent","useField","createUseField","useStore","selector","store","Subscribe","props","functionalUpdate","children","noFormElement","rest","isSubmitting","state","formContext","_extends","handleSubmit","options","debugForm","margin","fontWeight","JSON","stringify","safeStringifyReplace","set","Set","_key","value","Array","isArray","has","add","undefined"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA;AAEO,SAASA,OAAT,CAAwBC,IAAxB,EAAmE;AACxE,EAAA,MAAM,CAACC,OAAD,CAAA,GAAYC,yBAAK,CAACC,QAAN,CAAe,MAAM;AACrC;IACA,MAAMC,GAAG,GAAG,IAAIC,gBAAJ,CAAmBL,IAAI,IAAI,EAA3B,CAAZ,CAAA;AAEAI,IAAAA,GAAG,CAACE,IAAJ,GAAWC,mBAAmB,CAACH,GAAD,CAA9B,CAAA;AACAA,IAAAA,GAAG,CAACI,KAAJ,GAAYC,0BAAoB,CAACL,GAAD,CAAhC,CAAA;AACAA,IAAAA,GAAG,CAACM,QAAJ,GAAeC,uBAAc,CAACP,GAAD,CAA7B,CAAA;;IACAA,GAAG,CAACQ,QAAJ,GAAe;AAEbC,IAAAA,QAFa,KAGV;AACH;AACA,MAAA,OAAOD,mBAAQ,CAACR,GAAG,CAACU,KAAL,EAAYD,QAAZ,CAAf,CAAA;KALF,CAAA;;IAOAT,GAAG,CAACW,SAAJ,GAAgB;AAEdC,IAAAA,KAFc,KAGX;AACH,MAAA,OAAOC,yBAAgB,CACrBD,KAAK,CAACE,QADe;MAGrBN,mBAAQ,CAACR,GAAG,CAACU,KAAL,EAAYE,KAAK,CAACH,QAAlB,CAHa,CAAvB,CAAA;KAJF,CAAA;;AAWA,IAAA,OAAOT,GAAP,CAAA;GAzBgB,CAAlB,CADwE;;AA+BxE,EAAA,OAAOH,OAAP,CAAA;AACD,CAAA;AASM,SAASM,mBAAT,CAA6BN,OAA7B,EAAoD;EACzD,MAAMK,IAAmB,GAAG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6BC,KAAK,CAACC,OAAN,CAAcF,KAAd,CAAjC,EAAuD;AACrD,MAAA,IAAIH,GAAG,CAACM,GAAJ,CAAQH,KAAR,CAAJ,EAAoB;AAClB,QAAA,OAAO,kBAAP,CAAA;AACD,OAAA;;MACDH,GAAG,CAACO,GAAJ,CAAQJ,KAAR,CAAA,CAAA;AACD,KAAA;;AACD,IAAA,OAAO,OAAOA,KAAP,KAAiB,UAAjB,GAA8BK,SAA9B,GAA0CL,KAAjD,CAAA;GAPF,CAAA;AASD;;;;;"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* react-form
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
import { FieldApi, functionalUpdate, FormApi } from '@tanstack/form-core';
|
|
12
|
+
import { useStore } from '@tanstack/react-store';
|
|
13
|
+
import * as React from 'react';
|
|
14
|
+
import React__default from 'react';
|
|
15
|
+
|
|
16
|
+
function _extends() {
|
|
17
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
18
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
19
|
+
var source = arguments[i];
|
|
20
|
+
|
|
21
|
+
for (var key in source) {
|
|
22
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
23
|
+
target[key] = source[key];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return target;
|
|
29
|
+
};
|
|
30
|
+
return _extends.apply(this, arguments);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const formContext = /*#__PURE__*/React.createContext(null);
|
|
34
|
+
function useFormContext(customFormApi) {
|
|
35
|
+
const formApi = React.useContext(formContext);
|
|
36
|
+
|
|
37
|
+
if (customFormApi) {
|
|
38
|
+
return customFormApi;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!formApi) {
|
|
42
|
+
throw new Error("You are trying to use the form API outside of a form!");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return formApi;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function createUseField(formApi) {
|
|
49
|
+
const useFormField = opts => {
|
|
50
|
+
return useField({ ...opts,
|
|
51
|
+
form: formApi
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return useFormField;
|
|
56
|
+
}
|
|
57
|
+
function useField(opts) {
|
|
58
|
+
// invariant( // TODO:
|
|
59
|
+
// opts.name,
|
|
60
|
+
// `useField: A field is required to use this hook. eg, useField('myField', options)`
|
|
61
|
+
// )
|
|
62
|
+
// Get the form API either manually or from context
|
|
63
|
+
const formApi = useFormContext(opts.form);
|
|
64
|
+
const [fieldApi] = React.useState(() => new FieldApi({ ...opts,
|
|
65
|
+
form: formApi
|
|
66
|
+
})); // Keep options up to date as they are rendered
|
|
67
|
+
|
|
68
|
+
fieldApi.update({ ...opts,
|
|
69
|
+
form: formApi
|
|
70
|
+
});
|
|
71
|
+
useStore(fieldApi.store); // Instantiates field meta and removes it when unrendered
|
|
72
|
+
|
|
73
|
+
React.useEffect(() => fieldApi.mount(), [fieldApi]);
|
|
74
|
+
return fieldApi;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function createFieldComponent(formApi) {
|
|
78
|
+
const ConnectedField = props => /*#__PURE__*/React.createElement(Field, _extends({}, props, {
|
|
79
|
+
form: formApi
|
|
80
|
+
}));
|
|
81
|
+
|
|
82
|
+
return ConnectedField;
|
|
83
|
+
}
|
|
84
|
+
function Field({
|
|
85
|
+
children,
|
|
86
|
+
...fieldOptions
|
|
87
|
+
}) {
|
|
88
|
+
const fieldApi = useField(fieldOptions);
|
|
89
|
+
return functionalUpdate(children, fieldApi);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
//
|
|
93
|
+
function useForm(opts) {
|
|
94
|
+
const [formApi] = React__default.useState(() => {
|
|
95
|
+
// @ts-ignore
|
|
96
|
+
const api = new FormApi(opts || {});
|
|
97
|
+
api.Form = createFormComponent(api);
|
|
98
|
+
api.Field = createFieldComponent(api);
|
|
99
|
+
api.useField = createUseField(api);
|
|
100
|
+
|
|
101
|
+
api.useStore = ( // @ts-ignore
|
|
102
|
+
selector) => {
|
|
103
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
104
|
+
return useStore(api.store, selector);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
api.Subscribe = ( // @ts-ignore
|
|
108
|
+
props) => {
|
|
109
|
+
return functionalUpdate(props.children, // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
110
|
+
useStore(api.store, props.selector));
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return api;
|
|
114
|
+
}); // React.useEffect(() => formApi.mount(), [])
|
|
115
|
+
|
|
116
|
+
return formApi;
|
|
117
|
+
}
|
|
118
|
+
function createFormComponent(formApi) {
|
|
119
|
+
const Form = ({
|
|
120
|
+
children,
|
|
121
|
+
noFormElement,
|
|
122
|
+
...rest
|
|
123
|
+
}) => {
|
|
124
|
+
const isSubmitting = formApi.useStore(state => state.isSubmitting);
|
|
125
|
+
return /*#__PURE__*/React__default.createElement(formContext.Provider, {
|
|
126
|
+
value: formApi
|
|
127
|
+
}, noFormElement ? children : /*#__PURE__*/React__default.createElement("form", _extends({
|
|
128
|
+
onSubmit: formApi.handleSubmit,
|
|
129
|
+
disabled: isSubmitting
|
|
130
|
+
}, rest), formApi.options.debugForm ? /*#__PURE__*/React__default.createElement("div", {
|
|
131
|
+
style: {
|
|
132
|
+
margin: '2rem 0'
|
|
133
|
+
}
|
|
134
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
135
|
+
style: {
|
|
136
|
+
fontWeight: 'bolder'
|
|
137
|
+
}
|
|
138
|
+
}, "Form State"), /*#__PURE__*/React__default.createElement("pre", null, /*#__PURE__*/React__default.createElement("code", null, JSON.stringify(formApi, safeStringifyReplace(), 2)))) : null, children));
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
return Form;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function safeStringifyReplace() {
|
|
145
|
+
const set = new Set();
|
|
146
|
+
return (_key, value) => {
|
|
147
|
+
if (typeof value === 'object' || Array.isArray(value)) {
|
|
148
|
+
if (set.has(value)) {
|
|
149
|
+
return '(circular value)';
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
set.add(value);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return typeof value === 'function' ? undefined : value;
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export { Field, createFieldComponent, createFormComponent, createUseField, useField, useForm };
|
|
160
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/formContext.ts","../../src/useField.ts","../../src/Field.tsx","../../src/useForm.tsx"],"sourcesContent":["import { FormApi } from '@tanstack/form-core'\nimport * as React from 'react'\n\nexport const formContext = React.createContext<FormApi<any>>(null!)\n\nexport function useFormContext(customFormApi?: FormApi<any>) {\n const formApi = React.useContext(formContext)\n\n if (customFormApi) {\n return customFormApi\n }\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","import * as React from 'react'\n//\nimport { useStore } from '@tanstack/react-store'\nimport type {\n DeepKeys,\n DeepValue,\n FieldOptions,\n FormApi,\n} from '@tanstack/form-core'\nimport { FieldApi } from '@tanstack/form-core'\nimport { useFormContext } from './formContext'\n\nexport type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(\n opts?: { name: TField } & FieldOptions<\n DeepValue<TFormData, TField>,\n TFormData\n >,\n) => FieldApi<DeepValue<TFormData, TField>, TFormData>\n\nexport function createUseField<TFormData>(formApi: FormApi<TFormData>) {\n const useFormField: UseField<TFormData> = (opts) => {\n return useField({ ...opts, form: formApi } as any)\n }\n\n return useFormField\n}\n\nexport function useField<TData, TFormData>(\n opts: FieldOptions<TData, TFormData> & {\n // selector: (state: FieldApi<TData, TFormData>) => TSelected\n },\n): FieldApi<TData, TFormData> {\n // invariant( // TODO:\n // opts.name,\n // `useField: A field is required to use this hook. eg, useField('myField', options)`\n // )\n\n // Get the form API either manually or from context\n const formApi = useFormContext(opts.form)\n\n const [fieldApi] = React.useState<FieldApi<TData, TFormData>>(\n () => new FieldApi({ ...opts, form: formApi }),\n )\n\n // Keep options up to date as they are rendered\n fieldApi.update({ ...opts, form: formApi })\n\n useStore(fieldApi.store)\n\n // Instantiates field meta and removes it when unrendered\n React.useEffect(() => fieldApi.mount(), [fieldApi])\n\n return fieldApi\n}\n","import * as React from 'react'\nimport {\n functionalUpdate,\n type DeepKeys,\n type DeepValue,\n type FieldApi,\n type FieldOptions,\n type FormApi,\n} from '@tanstack/form-core'\nimport { useField } from './useField'\n\n//\n\nexport type FieldComponent<TFormData> = <TField extends DeepKeys<TFormData>>({\n children,\n ...fieldOptions\n}: {\n children: (fieldApi: FieldApi<DeepValue<TFormData, TField>, TFormData>) => any\n name: TField\n} & Omit<FieldOptions<DeepValue<TFormData, TField>, TFormData>, 'name'>) => any\n\nexport function createFieldComponent<TFormData>(formApi: FormApi<TFormData>) {\n const ConnectedField: FieldComponent<TFormData> = (props) => (\n <Field {...(props as any)} form={formApi} />\n )\n return ConnectedField\n}\n\nexport function Field<TData, TFormData>({\n children,\n ...fieldOptions\n}: {\n children: (fieldApi: FieldApi<TData, TFormData>) => any\n} & FieldOptions<TData, TFormData>) {\n const fieldApi = useField(fieldOptions as any)\n return functionalUpdate(children, fieldApi as any)\n}\n","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 from 'react'\nimport { createFieldComponent, type FieldComponent } from './Field'\nimport { createUseField, type UseField } from './useField'\nimport { formContext } from './formContext'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData> {\n Form: FormComponent\n Field: FieldComponent<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:\n | ((state: NoInfer<TSelected>) => React.ReactNode)\n | React.ReactNode\n }) => any\n }\n}\n//\n\nexport function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData> {\n const [formApi] = React.useState(() => {\n // @ts-ignore\n const api = new FormApi<TData>(opts || {})\n\n api.Form = createFormComponent(api)\n api.Field = createFieldComponent(api)\n api.useField = createUseField(api)\n api.useStore = (\n // @ts-ignore\n selector,\n ) => {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useStore(api.store, selector) 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, props.selector),\n ) as any\n }\n\n return api\n })\n\n // React.useEffect(() => formApi.mount(), [])\n\n return formApi\n}\n\nexport type FormProps = React.HTMLProps<HTMLFormElement> & {\n children: React.ReactNode\n noFormElement?: boolean\n}\n\nexport type FormComponent = (props: FormProps) => any\n\nexport function createFormComponent(formApi: FormApi<any>) {\n const Form: FormComponent = ({ children, noFormElement, ...rest }) => {\n const isSubmitting = formApi.useStore((state) => state.isSubmitting)\n\n return (\n <formContext.Provider value={formApi}>\n {noFormElement ? (\n children\n ) : (\n <form\n onSubmit={formApi.handleSubmit}\n disabled={isSubmitting}\n {...rest}\n >\n {formApi.options.debugForm ? (\n <div\n style={{\n margin: '2rem 0',\n }}\n >\n <div\n style={{\n fontWeight: 'bolder',\n }}\n >\n Form State\n </div>\n <pre>\n <code>\n {JSON.stringify(formApi, safeStringifyReplace(), 2)}\n </code>\n </pre>\n </div>\n ) : null}\n {children}\n </form>\n )}\n </formContext.Provider>\n )\n }\n\n return Form\n}\n\nfunction safeStringifyReplace() {\n const set = new Set()\n return (_key: string, value: any) => {\n if (typeof value === 'object' || Array.isArray(value)) {\n if (set.has(value)) {\n return '(circular value)'\n }\n set.add(value)\n }\n return typeof value === 'function' ? undefined : value\n }\n}\n"],"names":["formContext","React","createContext","useFormContext","customFormApi","formApi","useContext","Error","createUseField","useFormField","opts","useField","form","fieldApi","useState","FieldApi","update","useStore","store","useEffect","mount","createFieldComponent","ConnectedField","props","Field","children","fieldOptions","functionalUpdate","useForm","api","FormApi","Form","createFormComponent","selector","Subscribe","noFormElement","rest","isSubmitting","state","handleSubmit","options","debugForm","margin","fontWeight","JSON","stringify","safeStringifyReplace","set","Set","_key","value","Array","isArray","has","add","undefined"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGO,MAAMA,WAAW,gBAAGC,KAAK,CAACC,aAAN,CAAkC,IAAlC,CAApB,CAAA;AAEA,SAASC,cAAT,CAAwBC,aAAxB,EAAsD;AAC3D,EAAA,MAAMC,OAAO,GAAGJ,KAAK,CAACK,UAAN,CAAiBN,WAAjB,CAAhB,CAAA;;AAEA,EAAA,IAAII,aAAJ,EAAmB;AACjB,IAAA,OAAOA,aAAP,CAAA;AACD,GAAA;;EAED,IAAI,CAACC,OAAL,EAAc;IACZ,MAAM,IAAIE,KAAJ,CAAN,uDAAA,CAAA,CAAA;AACD,GAAA;;AAED,EAAA,OAAOF,OAAP,CAAA;AACD;;ACEM,SAASG,cAAT,CAAmCH,OAAnC,EAAgE;EACrE,MAAMI,YAAiC,GAAIC,IAAD,IAAU;AAClD,IAAA,OAAOC,QAAQ,CAAC,EAAE,GAAGD,IAAL;AAAWE,MAAAA,IAAI,EAAEP,OAAAA;AAAjB,KAAD,CAAf,CAAA;GADF,CAAA;;AAIA,EAAA,O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