@tanstack/solid-form 0.6.0
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/dev.cjs +109 -0
- package/build/dev.js +93 -0
- package/build/index.cjs +109 -0
- package/build/index.d.cts +56 -0
- package/build/index.d.ts +56 -0
- package/build/index.js +93 -0
- package/package.json +72 -0
- package/src/createField.tsx +196 -0
- package/src/createForm.tsx +53 -0
- package/src/createFormFactory.ts +31 -0
- package/src/formContext.ts +20 -0
- package/src/index.ts +27 -0
- package/src/tests/createField.test-d.tsx +68 -0
- package/src/tests/createField.test.tsx +376 -0
- package/src/tests/createForm.test.tsx +116 -0
- package/src/tests/createFormFactory.test.tsx +38 -0
- package/src/tests/utils.ts +5 -0
- package/src/types.ts +11 -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 Solid
|
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%22solid-form+tests%22">
|
12
|
+
<img src="https://github.com/TanStack/form/workflows/solid-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/solid-form@latest" target="\_parent">
|
16
|
+
<img alt="" src="https://badgen.net/bundlephobia/minzip/@tanstack/solid-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 -->
|
package/build/dev.cjs
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var formCore = require('@tanstack/form-core');
|
4
|
+
var web = require('solid-js/web');
|
5
|
+
var solidJs = require('solid-js');
|
6
|
+
var solidStore = require('@tanstack/solid-store');
|
7
|
+
|
8
|
+
// src/index.ts
|
9
|
+
var formContext = solidJs.createContext(void 0);
|
10
|
+
function useFormContext() {
|
11
|
+
const formApi = solidJs.useContext(formContext);
|
12
|
+
if (!formApi)
|
13
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
14
|
+
return formApi;
|
15
|
+
}
|
16
|
+
|
17
|
+
// src/createField.tsx
|
18
|
+
function makeFieldReactive(fieldApi) {
|
19
|
+
const [flag, setFlag] = solidJs.createSignal(false);
|
20
|
+
const fieldApiMemo = solidJs.createMemo(() => [flag(), fieldApi]);
|
21
|
+
const unsubscribeStore = fieldApi.store.subscribe(() => setFlag((f) => !f));
|
22
|
+
solidJs.onCleanup(unsubscribeStore);
|
23
|
+
return () => fieldApiMemo()[1];
|
24
|
+
}
|
25
|
+
function createField(opts) {
|
26
|
+
const {
|
27
|
+
formApi,
|
28
|
+
parentFieldName
|
29
|
+
} = useFormContext();
|
30
|
+
const options = opts();
|
31
|
+
const name = (typeof options.index === "number" ? [parentFieldName, options.index, options.name] : [parentFieldName, options.name]).filter((d) => d !== void 0).join(".");
|
32
|
+
const fieldApi = new formCore.FieldApi({
|
33
|
+
...options,
|
34
|
+
form: formApi,
|
35
|
+
name
|
36
|
+
});
|
37
|
+
fieldApi.Field = Field;
|
38
|
+
solidJs.createComputed(() => fieldApi.update({
|
39
|
+
...opts(),
|
40
|
+
form: formApi
|
41
|
+
}));
|
42
|
+
solidJs.onMount(() => solidJs.onCleanup(fieldApi.mount()));
|
43
|
+
return makeFieldReactive(fieldApi);
|
44
|
+
}
|
45
|
+
function Field(props) {
|
46
|
+
const fieldApi = createField(() => {
|
47
|
+
const {
|
48
|
+
children,
|
49
|
+
...fieldOptions
|
50
|
+
} = props;
|
51
|
+
return fieldOptions;
|
52
|
+
});
|
53
|
+
return web.createComponent(formContext.Provider, {
|
54
|
+
get value() {
|
55
|
+
return {
|
56
|
+
formApi: fieldApi().form,
|
57
|
+
parentFieldName: String(fieldApi().name)
|
58
|
+
};
|
59
|
+
},
|
60
|
+
get children() {
|
61
|
+
return solidJs.createComponent(() => props.children(fieldApi), {});
|
62
|
+
}
|
63
|
+
});
|
64
|
+
}
|
65
|
+
|
66
|
+
// src/createForm.tsx
|
67
|
+
function createForm(opts) {
|
68
|
+
const options = opts?.();
|
69
|
+
const formApi = new formCore.FormApi(options);
|
70
|
+
formApi.Provider = function Provider(props) {
|
71
|
+
return web.createComponent(formContext.Provider, web.mergeProps(props, {
|
72
|
+
value: {
|
73
|
+
formApi
|
74
|
+
}
|
75
|
+
}));
|
76
|
+
};
|
77
|
+
formApi.Field = Field;
|
78
|
+
formApi.createField = createField;
|
79
|
+
formApi.useStore = (selector) => solidStore.useStore(formApi.store, selector);
|
80
|
+
formApi.Subscribe = (props) => formCore.functionalUpdate(props.children, solidStore.useStore(formApi.store, props.selector));
|
81
|
+
solidJs.createComputed(() => formApi.update(opts?.()));
|
82
|
+
return formApi;
|
83
|
+
}
|
84
|
+
function createFormFactory(defaultOpts) {
|
85
|
+
return {
|
86
|
+
createForm: (opts) => createForm(
|
87
|
+
() => solidJs.mergeProps(defaultOpts?.() ?? {}, opts?.() ?? {})
|
88
|
+
),
|
89
|
+
createField,
|
90
|
+
Field
|
91
|
+
};
|
92
|
+
}
|
93
|
+
|
94
|
+
Object.defineProperty(exports, 'FieldApi', {
|
95
|
+
enumerable: true,
|
96
|
+
get: function () { return formCore.FieldApi; }
|
97
|
+
});
|
98
|
+
Object.defineProperty(exports, 'FormApi', {
|
99
|
+
enumerable: true,
|
100
|
+
get: function () { return formCore.FormApi; }
|
101
|
+
});
|
102
|
+
Object.defineProperty(exports, 'functionalUpdate', {
|
103
|
+
enumerable: true,
|
104
|
+
get: function () { return formCore.functionalUpdate; }
|
105
|
+
});
|
106
|
+
exports.Field = Field;
|
107
|
+
exports.createField = createField;
|
108
|
+
exports.createForm = createForm;
|
109
|
+
exports.createFormFactory = createFormFactory;
|
package/build/dev.js
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
import { FieldApi, FormApi, functionalUpdate } from '@tanstack/form-core';
|
2
|
+
export { FieldApi, FormApi, functionalUpdate } from '@tanstack/form-core';
|
3
|
+
import { createComponent, mergeProps } from 'solid-js/web';
|
4
|
+
import { createContext, createComputed, onMount, onCleanup, createComponent as createComponent$1, mergeProps as mergeProps$1, useContext, createSignal, createMemo } from 'solid-js';
|
5
|
+
import { useStore } from '@tanstack/solid-store';
|
6
|
+
|
7
|
+
// src/index.ts
|
8
|
+
var formContext = createContext(void 0);
|
9
|
+
function useFormContext() {
|
10
|
+
const formApi = useContext(formContext);
|
11
|
+
if (!formApi)
|
12
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
13
|
+
return formApi;
|
14
|
+
}
|
15
|
+
|
16
|
+
// src/createField.tsx
|
17
|
+
function makeFieldReactive(fieldApi) {
|
18
|
+
const [flag, setFlag] = createSignal(false);
|
19
|
+
const fieldApiMemo = createMemo(() => [flag(), fieldApi]);
|
20
|
+
const unsubscribeStore = fieldApi.store.subscribe(() => setFlag((f) => !f));
|
21
|
+
onCleanup(unsubscribeStore);
|
22
|
+
return () => fieldApiMemo()[1];
|
23
|
+
}
|
24
|
+
function createField(opts) {
|
25
|
+
const {
|
26
|
+
formApi,
|
27
|
+
parentFieldName
|
28
|
+
} = useFormContext();
|
29
|
+
const options = opts();
|
30
|
+
const name = (typeof options.index === "number" ? [parentFieldName, options.index, options.name] : [parentFieldName, options.name]).filter((d) => d !== void 0).join(".");
|
31
|
+
const fieldApi = new FieldApi({
|
32
|
+
...options,
|
33
|
+
form: formApi,
|
34
|
+
name
|
35
|
+
});
|
36
|
+
fieldApi.Field = Field;
|
37
|
+
createComputed(() => fieldApi.update({
|
38
|
+
...opts(),
|
39
|
+
form: formApi
|
40
|
+
}));
|
41
|
+
onMount(() => onCleanup(fieldApi.mount()));
|
42
|
+
return makeFieldReactive(fieldApi);
|
43
|
+
}
|
44
|
+
function Field(props) {
|
45
|
+
const fieldApi = createField(() => {
|
46
|
+
const {
|
47
|
+
children,
|
48
|
+
...fieldOptions
|
49
|
+
} = props;
|
50
|
+
return fieldOptions;
|
51
|
+
});
|
52
|
+
return createComponent(formContext.Provider, {
|
53
|
+
get value() {
|
54
|
+
return {
|
55
|
+
formApi: fieldApi().form,
|
56
|
+
parentFieldName: String(fieldApi().name)
|
57
|
+
};
|
58
|
+
},
|
59
|
+
get children() {
|
60
|
+
return createComponent$1(() => props.children(fieldApi), {});
|
61
|
+
}
|
62
|
+
});
|
63
|
+
}
|
64
|
+
|
65
|
+
// src/createForm.tsx
|
66
|
+
function createForm(opts) {
|
67
|
+
const options = opts?.();
|
68
|
+
const formApi = new FormApi(options);
|
69
|
+
formApi.Provider = function Provider(props) {
|
70
|
+
return createComponent(formContext.Provider, mergeProps(props, {
|
71
|
+
value: {
|
72
|
+
formApi
|
73
|
+
}
|
74
|
+
}));
|
75
|
+
};
|
76
|
+
formApi.Field = Field;
|
77
|
+
formApi.createField = createField;
|
78
|
+
formApi.useStore = (selector) => useStore(formApi.store, selector);
|
79
|
+
formApi.Subscribe = (props) => functionalUpdate(props.children, useStore(formApi.store, props.selector));
|
80
|
+
createComputed(() => formApi.update(opts?.()));
|
81
|
+
return formApi;
|
82
|
+
}
|
83
|
+
function createFormFactory(defaultOpts) {
|
84
|
+
return {
|
85
|
+
createForm: (opts) => createForm(
|
86
|
+
() => mergeProps$1(defaultOpts?.() ?? {}, opts?.() ?? {})
|
87
|
+
),
|
88
|
+
createField,
|
89
|
+
Field
|
90
|
+
};
|
91
|
+
}
|
92
|
+
|
93
|
+
export { Field, createField, createForm, createFormFactory };
|
package/build/index.cjs
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var formCore = require('@tanstack/form-core');
|
4
|
+
var web = require('solid-js/web');
|
5
|
+
var solidJs = require('solid-js');
|
6
|
+
var solidStore = require('@tanstack/solid-store');
|
7
|
+
|
8
|
+
// src/index.ts
|
9
|
+
var formContext = solidJs.createContext(void 0);
|
10
|
+
function useFormContext() {
|
11
|
+
const formApi = solidJs.useContext(formContext);
|
12
|
+
if (!formApi)
|
13
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
14
|
+
return formApi;
|
15
|
+
}
|
16
|
+
|
17
|
+
// src/createField.tsx
|
18
|
+
function makeFieldReactive(fieldApi) {
|
19
|
+
const [flag, setFlag] = solidJs.createSignal(false);
|
20
|
+
const fieldApiMemo = solidJs.createMemo(() => [flag(), fieldApi]);
|
21
|
+
const unsubscribeStore = fieldApi.store.subscribe(() => setFlag((f) => !f));
|
22
|
+
solidJs.onCleanup(unsubscribeStore);
|
23
|
+
return () => fieldApiMemo()[1];
|
24
|
+
}
|
25
|
+
function createField(opts) {
|
26
|
+
const {
|
27
|
+
formApi,
|
28
|
+
parentFieldName
|
29
|
+
} = useFormContext();
|
30
|
+
const options = opts();
|
31
|
+
const name = (typeof options.index === "number" ? [parentFieldName, options.index, options.name] : [parentFieldName, options.name]).filter((d) => d !== void 0).join(".");
|
32
|
+
const fieldApi = new formCore.FieldApi({
|
33
|
+
...options,
|
34
|
+
form: formApi,
|
35
|
+
name
|
36
|
+
});
|
37
|
+
fieldApi.Field = Field;
|
38
|
+
solidJs.createComputed(() => fieldApi.update({
|
39
|
+
...opts(),
|
40
|
+
form: formApi
|
41
|
+
}));
|
42
|
+
solidJs.onMount(() => solidJs.onCleanup(fieldApi.mount()));
|
43
|
+
return makeFieldReactive(fieldApi);
|
44
|
+
}
|
45
|
+
function Field(props) {
|
46
|
+
const fieldApi = createField(() => {
|
47
|
+
const {
|
48
|
+
children,
|
49
|
+
...fieldOptions
|
50
|
+
} = props;
|
51
|
+
return fieldOptions;
|
52
|
+
});
|
53
|
+
return web.createComponent(formContext.Provider, {
|
54
|
+
get value() {
|
55
|
+
return {
|
56
|
+
formApi: fieldApi().form,
|
57
|
+
parentFieldName: String(fieldApi().name)
|
58
|
+
};
|
59
|
+
},
|
60
|
+
get children() {
|
61
|
+
return solidJs.createComponent(() => props.children(fieldApi), {});
|
62
|
+
}
|
63
|
+
});
|
64
|
+
}
|
65
|
+
|
66
|
+
// src/createForm.tsx
|
67
|
+
function createForm(opts) {
|
68
|
+
const options = opts?.();
|
69
|
+
const formApi = new formCore.FormApi(options);
|
70
|
+
formApi.Provider = function Provider(props) {
|
71
|
+
return web.createComponent(formContext.Provider, web.mergeProps(props, {
|
72
|
+
value: {
|
73
|
+
formApi
|
74
|
+
}
|
75
|
+
}));
|
76
|
+
};
|
77
|
+
formApi.Field = Field;
|
78
|
+
formApi.createField = createField;
|
79
|
+
formApi.useStore = (selector) => solidStore.useStore(formApi.store, selector);
|
80
|
+
formApi.Subscribe = (props) => formCore.functionalUpdate(props.children, solidStore.useStore(formApi.store, props.selector));
|
81
|
+
solidJs.createComputed(() => formApi.update(opts?.()));
|
82
|
+
return formApi;
|
83
|
+
}
|
84
|
+
function createFormFactory(defaultOpts) {
|
85
|
+
return {
|
86
|
+
createForm: (opts) => createForm(
|
87
|
+
() => solidJs.mergeProps(defaultOpts?.() ?? {}, opts?.() ?? {})
|
88
|
+
),
|
89
|
+
createField,
|
90
|
+
Field
|
91
|
+
};
|
92
|
+
}
|
93
|
+
|
94
|
+
Object.defineProperty(exports, 'FieldApi', {
|
95
|
+
enumerable: true,
|
96
|
+
get: function () { return formCore.FieldApi; }
|
97
|
+
});
|
98
|
+
Object.defineProperty(exports, 'FormApi', {
|
99
|
+
enumerable: true,
|
100
|
+
get: function () { return formCore.FormApi; }
|
101
|
+
});
|
102
|
+
Object.defineProperty(exports, 'functionalUpdate', {
|
103
|
+
enumerable: true,
|
104
|
+
get: function () { return formCore.functionalUpdate; }
|
105
|
+
});
|
106
|
+
exports.Field = Field;
|
107
|
+
exports.createField = createField;
|
108
|
+
exports.createForm = createForm;
|
109
|
+
exports.createFormFactory = createFormFactory;
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import { DeepKeys, DeepValue, FieldOptions, Narrow, FieldApi, FormState, FormOptions, FormApi } from '@tanstack/form-core';
|
2
|
+
export { DeepKeys, DeepValue, FieldApi, FieldApiOptions, FieldInfo, FieldMeta, FieldOptions, FieldState, FormApi, FormOptions, FormState, RequiredByKey, Updater, UpdaterFn, ValidationCause, ValidationError, ValidationMeta, functionalUpdate } from '@tanstack/form-core';
|
3
|
+
import { JSXElement } from 'solid-js';
|
4
|
+
|
5
|
+
type CreateFieldOptions<TParentData, TName extends DeepKeys<TParentData>, ValidatorType, FormValidator, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = FieldOptions<TParentData, TName, ValidatorType, FormValidator, TData> & {
|
6
|
+
mode?: 'value' | 'array';
|
7
|
+
};
|
8
|
+
|
9
|
+
declare module '@tanstack/form-core' {
|
10
|
+
interface FieldApi<TParentData, TName extends DeepKeys<TParentData>, ValidatorType, FormValidator, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> {
|
11
|
+
Field: FieldComponent<TData, FormValidator>;
|
12
|
+
}
|
13
|
+
}
|
14
|
+
type CreateField<TParentData> = <TName extends DeepKeys<TParentData>, ValidatorType, FormValidator>(opts: () => {
|
15
|
+
name: Narrow<TName>;
|
16
|
+
} & CreateFieldOptions<TParentData, TName, ValidatorType, FormValidator>) => () => FieldApi<TParentData, TName, ValidatorType, FormValidator, DeepValue<TParentData, TName>>;
|
17
|
+
declare function createField<TParentData, TName extends DeepKeys<TParentData>, ValidatorType, FormValidator>(opts: () => CreateFieldOptions<TParentData, TName, ValidatorType, FormValidator>): () => FieldApi<TParentData, TName, ValidatorType, FormValidator>;
|
18
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, ValidatorType, FormValidator, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = {
|
19
|
+
children: (fieldApi: () => FieldApi<TParentData, TName, ValidatorType, FormValidator, TData>) => JSXElement;
|
20
|
+
} & (TParentData extends any[] ? {
|
21
|
+
name?: TName;
|
22
|
+
index: number;
|
23
|
+
} : {
|
24
|
+
name: TName;
|
25
|
+
index?: never;
|
26
|
+
}) & Omit<CreateFieldOptions<TParentData, TName, ValidatorType, FormValidator>, 'name' | 'index'>;
|
27
|
+
type FieldComponent<TParentData, FormValidator> = <TName extends DeepKeys<TParentData>, ValidatorType, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>>({ children, ...fieldOptions }: FieldComponentProps<TParentData, TName, ValidatorType, FormValidator, TData>) => JSXElement;
|
28
|
+
declare function Field<TParentData, TName extends DeepKeys<TParentData>, ValidatorType, FormValidator>(props: {
|
29
|
+
children: (fieldApi: () => FieldApi<TParentData, TName, ValidatorType, FormValidator>) => JSXElement;
|
30
|
+
} & CreateFieldOptions<TParentData, TName, ValidatorType, FormValidator>): any;
|
31
|
+
|
32
|
+
type NoInfer<T> = [T][T extends any ? 0 : never];
|
33
|
+
declare module '@tanstack/form-core' {
|
34
|
+
interface FormApi<TFormData, ValidatorType> {
|
35
|
+
Provider: (props: {
|
36
|
+
children: any;
|
37
|
+
}) => any;
|
38
|
+
Field: FieldComponent<TFormData, ValidatorType>;
|
39
|
+
createField: CreateField<TFormData>;
|
40
|
+
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => () => TSelected;
|
41
|
+
Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
|
42
|
+
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
|
43
|
+
children: ((state: () => NoInfer<TSelected>) => JSXElement) | JSXElement;
|
44
|
+
}) => any;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
declare function createForm<TData, FormValidator>(opts?: () => FormOptions<TData, FormValidator>): FormApi<TData, FormValidator>;
|
48
|
+
|
49
|
+
type FormFactory<TFormData, FormValidator> = {
|
50
|
+
createForm: (opts?: () => FormOptions<TFormData, FormValidator>) => FormApi<TFormData, FormValidator>;
|
51
|
+
createField: CreateField<TFormData>;
|
52
|
+
Field: FieldComponent<TFormData, FormValidator>;
|
53
|
+
};
|
54
|
+
declare function createFormFactory<TFormData, FormValidator>(defaultOpts?: () => FormOptions<TFormData, FormValidator>): FormFactory<TFormData, FormValidator>;
|
55
|
+
|
56
|
+
export { CreateField, Field, FieldComponent, FormFactory, createField, createForm, createFormFactory };
|
package/build/index.d.ts
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
import { DeepKeys, DeepValue, FieldOptions, Narrow, FieldApi, FormState, FormOptions, FormApi } from '@tanstack/form-core';
|
2
|
+
export { DeepKeys, DeepValue, FieldApi, FieldApiOptions, FieldInfo, FieldMeta, FieldOptions, FieldState, FormApi, FormOptions, FormState, RequiredByKey, Updater, UpdaterFn, ValidationCause, ValidationError, ValidationMeta, functionalUpdate } from '@tanstack/form-core';
|
3
|
+
import { JSXElement } from 'solid-js';
|
4
|
+
|
5
|
+
type CreateFieldOptions<TParentData, TName extends DeepKeys<TParentData>, ValidatorType, FormValidator, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = FieldOptions<TParentData, TName, ValidatorType, FormValidator, TData> & {
|
6
|
+
mode?: 'value' | 'array';
|
7
|
+
};
|
8
|
+
|
9
|
+
declare module '@tanstack/form-core' {
|
10
|
+
interface FieldApi<TParentData, TName extends DeepKeys<TParentData>, ValidatorType, FormValidator, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> {
|
11
|
+
Field: FieldComponent<TData, FormValidator>;
|
12
|
+
}
|
13
|
+
}
|
14
|
+
type CreateField<TParentData> = <TName extends DeepKeys<TParentData>, ValidatorType, FormValidator>(opts: () => {
|
15
|
+
name: Narrow<TName>;
|
16
|
+
} & CreateFieldOptions<TParentData, TName, ValidatorType, FormValidator>) => () => FieldApi<TParentData, TName, ValidatorType, FormValidator, DeepValue<TParentData, TName>>;
|
17
|
+
declare function createField<TParentData, TName extends DeepKeys<TParentData>, ValidatorType, FormValidator>(opts: () => CreateFieldOptions<TParentData, TName, ValidatorType, FormValidator>): () => FieldApi<TParentData, TName, ValidatorType, FormValidator>;
|
18
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, ValidatorType, FormValidator, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = {
|
19
|
+
children: (fieldApi: () => FieldApi<TParentData, TName, ValidatorType, FormValidator, TData>) => JSXElement;
|
20
|
+
} & (TParentData extends any[] ? {
|
21
|
+
name?: TName;
|
22
|
+
index: number;
|
23
|
+
} : {
|
24
|
+
name: TName;
|
25
|
+
index?: never;
|
26
|
+
}) & Omit<CreateFieldOptions<TParentData, TName, ValidatorType, FormValidator>, 'name' | 'index'>;
|
27
|
+
type FieldComponent<TParentData, FormValidator> = <TName extends DeepKeys<TParentData>, ValidatorType, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>>({ children, ...fieldOptions }: FieldComponentProps<TParentData, TName, ValidatorType, FormValidator, TData>) => JSXElement;
|
28
|
+
declare function Field<TParentData, TName extends DeepKeys<TParentData>, ValidatorType, FormValidator>(props: {
|
29
|
+
children: (fieldApi: () => FieldApi<TParentData, TName, ValidatorType, FormValidator>) => JSXElement;
|
30
|
+
} & CreateFieldOptions<TParentData, TName, ValidatorType, FormValidator>): any;
|
31
|
+
|
32
|
+
type NoInfer<T> = [T][T extends any ? 0 : never];
|
33
|
+
declare module '@tanstack/form-core' {
|
34
|
+
interface FormApi<TFormData, ValidatorType> {
|
35
|
+
Provider: (props: {
|
36
|
+
children: any;
|
37
|
+
}) => any;
|
38
|
+
Field: FieldComponent<TFormData, ValidatorType>;
|
39
|
+
createField: CreateField<TFormData>;
|
40
|
+
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => () => TSelected;
|
41
|
+
Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
|
42
|
+
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
|
43
|
+
children: ((state: () => NoInfer<TSelected>) => JSXElement) | JSXElement;
|
44
|
+
}) => any;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
declare function createForm<TData, FormValidator>(opts?: () => FormOptions<TData, FormValidator>): FormApi<TData, FormValidator>;
|
48
|
+
|
49
|
+
type FormFactory<TFormData, FormValidator> = {
|
50
|
+
createForm: (opts?: () => FormOptions<TFormData, FormValidator>) => FormApi<TFormData, FormValidator>;
|
51
|
+
createField: CreateField<TFormData>;
|
52
|
+
Field: FieldComponent<TFormData, FormValidator>;
|
53
|
+
};
|
54
|
+
declare function createFormFactory<TFormData, FormValidator>(defaultOpts?: () => FormOptions<TFormData, FormValidator>): FormFactory<TFormData, FormValidator>;
|
55
|
+
|
56
|
+
export { CreateField, Field, FieldComponent, FormFactory, createField, createForm, createFormFactory };
|
package/build/index.js
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
import { FieldApi, FormApi, functionalUpdate } from '@tanstack/form-core';
|
2
|
+
export { FieldApi, FormApi, functionalUpdate } from '@tanstack/form-core';
|
3
|
+
import { createComponent, mergeProps } from 'solid-js/web';
|
4
|
+
import { createContext, createComputed, onMount, onCleanup, createComponent as createComponent$1, mergeProps as mergeProps$1, useContext, createSignal, createMemo } from 'solid-js';
|
5
|
+
import { useStore } from '@tanstack/solid-store';
|
6
|
+
|
7
|
+
// src/index.ts
|
8
|
+
var formContext = createContext(void 0);
|
9
|
+
function useFormContext() {
|
10
|
+
const formApi = useContext(formContext);
|
11
|
+
if (!formApi)
|
12
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
13
|
+
return formApi;
|
14
|
+
}
|
15
|
+
|
16
|
+
// src/createField.tsx
|
17
|
+
function makeFieldReactive(fieldApi) {
|
18
|
+
const [flag, setFlag] = createSignal(false);
|
19
|
+
const fieldApiMemo = createMemo(() => [flag(), fieldApi]);
|
20
|
+
const unsubscribeStore = fieldApi.store.subscribe(() => setFlag((f) => !f));
|
21
|
+
onCleanup(unsubscribeStore);
|
22
|
+
return () => fieldApiMemo()[1];
|
23
|
+
}
|
24
|
+
function createField(opts) {
|
25
|
+
const {
|
26
|
+
formApi,
|
27
|
+
parentFieldName
|
28
|
+
} = useFormContext();
|
29
|
+
const options = opts();
|
30
|
+
const name = (typeof options.index === "number" ? [parentFieldName, options.index, options.name] : [parentFieldName, options.name]).filter((d) => d !== void 0).join(".");
|
31
|
+
const fieldApi = new FieldApi({
|
32
|
+
...options,
|
33
|
+
form: formApi,
|
34
|
+
name
|
35
|
+
});
|
36
|
+
fieldApi.Field = Field;
|
37
|
+
createComputed(() => fieldApi.update({
|
38
|
+
...opts(),
|
39
|
+
form: formApi
|
40
|
+
}));
|
41
|
+
onMount(() => onCleanup(fieldApi.mount()));
|
42
|
+
return makeFieldReactive(fieldApi);
|
43
|
+
}
|
44
|
+
function Field(props) {
|
45
|
+
const fieldApi = createField(() => {
|
46
|
+
const {
|
47
|
+
children,
|
48
|
+
...fieldOptions
|
49
|
+
} = props;
|
50
|
+
return fieldOptions;
|
51
|
+
});
|
52
|
+
return createComponent(formContext.Provider, {
|
53
|
+
get value() {
|
54
|
+
return {
|
55
|
+
formApi: fieldApi().form,
|
56
|
+
parentFieldName: String(fieldApi().name)
|
57
|
+
};
|
58
|
+
},
|
59
|
+
get children() {
|
60
|
+
return createComponent$1(() => props.children(fieldApi), {});
|
61
|
+
}
|
62
|
+
});
|
63
|
+
}
|
64
|
+
|
65
|
+
// src/createForm.tsx
|
66
|
+
function createForm(opts) {
|
67
|
+
const options = opts?.();
|
68
|
+
const formApi = new FormApi(options);
|
69
|
+
formApi.Provider = function Provider(props) {
|
70
|
+
return createComponent(formContext.Provider, mergeProps(props, {
|
71
|
+
value: {
|
72
|
+
formApi
|
73
|
+
}
|
74
|
+
}));
|
75
|
+
};
|
76
|
+
formApi.Field = Field;
|
77
|
+
formApi.createField = createField;
|
78
|
+
formApi.useStore = (selector) => useStore(formApi.store, selector);
|
79
|
+
formApi.Subscribe = (props) => functionalUpdate(props.children, useStore(formApi.store, props.selector));
|
80
|
+
createComputed(() => formApi.update(opts?.()));
|
81
|
+
return formApi;
|
82
|
+
}
|
83
|
+
function createFormFactory(defaultOpts) {
|
84
|
+
return {
|
85
|
+
createForm: (opts) => createForm(
|
86
|
+
() => mergeProps$1(defaultOpts?.() ?? {}, opts?.() ?? {})
|
87
|
+
),
|
88
|
+
createField,
|
89
|
+
Field
|
90
|
+
};
|
91
|
+
}
|
92
|
+
|
93
|
+
export { Field, createField, createForm, createFormFactory };
|
package/package.json
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
{
|
2
|
+
"name": "@tanstack/solid-form",
|
3
|
+
"version": "0.6.0",
|
4
|
+
"description": "Powerful, type-safe forms for Solid.",
|
5
|
+
"author": "tannerlinsley",
|
6
|
+
"license": "MIT",
|
7
|
+
"repository": "tanstack/form",
|
8
|
+
"homepage": "https://tanstack.com/form",
|
9
|
+
"funding": {
|
10
|
+
"type": "github",
|
11
|
+
"url": "https://github.com/sponsors/tannerlinsley"
|
12
|
+
},
|
13
|
+
"sideEffects": false,
|
14
|
+
"files": [
|
15
|
+
"build",
|
16
|
+
"src"
|
17
|
+
],
|
18
|
+
"type": "module",
|
19
|
+
"main": "./build/index.cjs",
|
20
|
+
"module": "./build/index.js",
|
21
|
+
"types": "./build/index.d.ts",
|
22
|
+
"exports": {
|
23
|
+
"development": {
|
24
|
+
"import": {
|
25
|
+
"types": "./build/index.d.ts",
|
26
|
+
"default": "./build/dev.js"
|
27
|
+
},
|
28
|
+
"require": {
|
29
|
+
"types": "./build/index.d.cts",
|
30
|
+
"default": "./build/dev.cjs"
|
31
|
+
}
|
32
|
+
},
|
33
|
+
"import": {
|
34
|
+
"types": "./build/index.d.ts",
|
35
|
+
"default": "./build/index.js"
|
36
|
+
},
|
37
|
+
"require": {
|
38
|
+
"types": "./build/index.d.cts",
|
39
|
+
"default": "./build/index.cjs"
|
40
|
+
}
|
41
|
+
},
|
42
|
+
"nx": {
|
43
|
+
"targets": {
|
44
|
+
"test:build": {
|
45
|
+
"dependsOn": [
|
46
|
+
"build"
|
47
|
+
]
|
48
|
+
}
|
49
|
+
}
|
50
|
+
},
|
51
|
+
"devDependencies": {
|
52
|
+
"solid-js": "^1.7.8",
|
53
|
+
"tsup-preset-solid": "^2.1.0",
|
54
|
+
"vite-plugin-solid": "^2.7.0"
|
55
|
+
},
|
56
|
+
"dependencies": {
|
57
|
+
"@tanstack/solid-store": "^0.2.1",
|
58
|
+
"@tanstack/form-core": "0.6.0"
|
59
|
+
},
|
60
|
+
"peerDependencies": {
|
61
|
+
"solid-js": "^1.6.0"
|
62
|
+
},
|
63
|
+
"scripts": {
|
64
|
+
"clean": "rimraf ./build && rimraf ./coverage",
|
65
|
+
"test:eslint": "eslint --ext .ts,.tsx ./src",
|
66
|
+
"test:types": "tsc --noEmit",
|
67
|
+
"test:lib": "vitest run --coverage",
|
68
|
+
"test:lib:dev": "pnpm run test:lib --watch",
|
69
|
+
"test:build": "publint --strict",
|
70
|
+
"build": "tsup"
|
71
|
+
}
|
72
|
+
}
|