@tanstack/react-form 0.0.13 → 0.0.15
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/lib/createFormFactory.cjs +2 -4
- package/build/lib/createFormFactory.cjs.map +1 -1
- package/build/lib/createFormFactory.d.ts.map +1 -1
- package/build/lib/createFormFactory.js +2 -4
- package/build/lib/createFormFactory.js.map +1 -1
- package/build/lib/createFormFactory.legacy.cjs +2 -4
- package/build/lib/createFormFactory.legacy.cjs.map +1 -1
- package/build/lib/createFormFactory.legacy.js +2 -4
- package/build/lib/createFormFactory.legacy.js.map +1 -1
- package/build/lib/tests/useField.test.d.ts +2 -0
- package/build/lib/tests/useField.test.d.ts.map +1 -0
- package/build/lib/tests/utils.d.ts +2 -0
- package/build/lib/tests/utils.d.ts.map +1 -0
- package/package.json +4 -3
- package/src/createFormFactory.ts +3 -1
- package/src/tests/useField.test.tsx +210 -0
- package/src/tests/useForm.test.tsx +1 -1
- package/src/tests/utils.ts +5 -0
|
@@ -6,10 +6,8 @@ var useForm = require('./useForm.cjs');
|
|
|
6
6
|
function createFormFactory(defaultOpts) {
|
|
7
7
|
return {
|
|
8
8
|
useForm: opts => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
...opts
|
|
12
|
-
});
|
|
9
|
+
const formOptions = Object.assign({}, defaultOpts, opts);
|
|
10
|
+
return useForm.useForm(formOptions);
|
|
13
11
|
},
|
|
14
12
|
useField: useField.useField,
|
|
15
13
|
Field: useField.Field
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createFormFactory.cjs","sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions } from '@tanstack/form-core'\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
|
|
1
|
+
{"version":3,"file":"createFormFactory.cjs","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"],"names":["createFormFactory","defaultOpts","useForm","opts","formOptions","Object","assign","useField","Field"],"mappings":";;;;;AAWO,SAASA,iBAAiBA,CAC/BC,WAAoC,EACZ;EACxB,OAAO;IACLC,OAAO,EAAGC,IAAI,IAAK;AACjB,MAAA,MAAMC,WAAW,GAAGC,MAAM,CAACC,MAAM,CAAC,EAAE,EAAEL,WAAW,EAAEE,IAAI,CAAC,CAAA;MACxD,OAAOD,eAAO,CAAYE,WAAW,CAAC,CAAA;KACvC;AACDG,IAAAA,QAAQ,EAAEA,iBAAe;AACzBC,IAAAA,KAAK,EAAEA,cAAAA;GACR,CAAA;AACH;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createFormFactory.d.ts","sourceRoot":"","sources":["../../src/createFormFactory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"createFormFactory.d.ts","sourceRoot":"","sources":["../../src/createFormFactory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAE/D,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,cAAc,EAAmB,MAAM,YAAY,CAAA;AAGhF,MAAM,MAAM,WAAW,CAAC,SAAS,IAAI;IACnC,OAAO,EAAE,CAAC,IAAI,CAAC,EAAE,WAAW,CAAC,SAAS,CAAC,KAAK,OAAO,CAAC,SAAS,CAAC,CAAA;IAC9D,QAAQ,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAA;IAC7B,KAAK,EAAE,cAAc,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;CAC5C,CAAA;AAED,wBAAgB,iBAAiB,CAAC,SAAS,EACzC,WAAW,CAAC,EAAE,WAAW,CAAC,SAAS,CAAC,GACnC,WAAW,CAAC,SAAS,CAAC,CASxB"}
|
|
@@ -4,10 +4,8 @@ import { useForm } from './useForm.js';
|
|
|
4
4
|
function createFormFactory(defaultOpts) {
|
|
5
5
|
return {
|
|
6
6
|
useForm: opts => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
...opts
|
|
10
|
-
});
|
|
7
|
+
const formOptions = Object.assign({}, defaultOpts, opts);
|
|
8
|
+
return useForm(formOptions);
|
|
11
9
|
},
|
|
12
10
|
useField: useField,
|
|
13
11
|
Field: Field
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createFormFactory.js","sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions } from '@tanstack/form-core'\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
|
|
1
|
+
{"version":3,"file":"createFormFactory.js","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"],"names":["createFormFactory","defaultOpts","useForm","opts","formOptions","Object","assign","useField","Field"],"mappings":";;;AAWO,SAASA,iBAAiBA,CAC/BC,WAAoC,EACZ;EACxB,OAAO;IACLC,OAAO,EAAGC,IAAI,IAAK;AACjB,MAAA,MAAMC,WAAW,GAAGC,MAAM,CAACC,MAAM,CAAC,EAAE,EAAEL,WAAW,EAAEE,IAAI,CAAC,CAAA;MACxD,OAAOD,OAAO,CAAYE,WAAW,CAAC,CAAA;KACvC;AACDG,IAAAA,QAAQ,EAAEA,QAAe;AACzBC,IAAAA,KAAK,EAAEA,KAAAA;GACR,CAAA;AACH;;;;"}
|
|
@@ -6,10 +6,8 @@ var useForm = require('./useForm.legacy.cjs');
|
|
|
6
6
|
function createFormFactory(defaultOpts) {
|
|
7
7
|
return {
|
|
8
8
|
useForm: opts => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
...opts
|
|
12
|
-
});
|
|
9
|
+
const formOptions = Object.assign({}, defaultOpts, opts);
|
|
10
|
+
return useForm.useForm(formOptions);
|
|
13
11
|
},
|
|
14
12
|
useField: useField.useField,
|
|
15
13
|
Field: useField.Field
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createFormFactory.legacy.cjs","sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions } from '@tanstack/form-core'\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
|
|
1
|
+
{"version":3,"file":"createFormFactory.legacy.cjs","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"],"names":["createFormFactory","defaultOpts","useForm","opts","formOptions","Object","assign","useField","Field"],"mappings":";;;;;AAWO,SAASA,iBAAiBA,CAC/BC,WAAoC,EACZ;EACxB,OAAO;IACLC,OAAO,EAAGC,IAAI,IAAK;AACjB,MAAA,MAAMC,WAAW,GAAGC,MAAM,CAACC,MAAM,CAAC,EAAE,EAAEL,WAAW,EAAEE,IAAI,CAAC,CAAA;MACxD,OAAOD,eAAO,CAAYE,WAAW,CAAC,CAAA;KACvC;AACDG,IAAAA,QAAQ,EAAEA,iBAAe;AACzBC,IAAAA,KAAK,EAAEA,cAAAA;GACR,CAAA;AACH;;;;"}
|
|
@@ -4,10 +4,8 @@ import { useForm } from './useForm.legacy.js';
|
|
|
4
4
|
function createFormFactory(defaultOpts) {
|
|
5
5
|
return {
|
|
6
6
|
useForm: opts => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
...opts
|
|
10
|
-
});
|
|
7
|
+
const formOptions = Object.assign({}, defaultOpts, opts);
|
|
8
|
+
return useForm(formOptions);
|
|
11
9
|
},
|
|
12
10
|
useField: useField,
|
|
13
11
|
Field: Field
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createFormFactory.legacy.js","sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions } from '@tanstack/form-core'\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
|
|
1
|
+
{"version":3,"file":"createFormFactory.legacy.js","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"],"names":["createFormFactory","defaultOpts","useForm","opts","formOptions","Object","assign","useField","Field"],"mappings":";;;AAWO,SAASA,iBAAiBA,CAC/BC,WAAoC,EACZ;EACxB,OAAO;IACLC,OAAO,EAAGC,IAAI,IAAK;AACjB,MAAA,MAAMC,WAAW,GAAGC,MAAM,CAACC,MAAM,CAAC,EAAE,EAAEL,WAAW,EAAEE,IAAI,CAAC,CAAA;MACxD,OAAOD,OAAO,CAAYE,WAAW,CAAC,CAAA;KACvC;AACDG,IAAAA,QAAQ,EAAEA,QAAe;AACzBC,IAAAA,KAAK,EAAEA,KAAAA;GACR,CAAA;AACH;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useField.test.d.ts","sourceRoot":"","sources":["../../../src/tests/useField.test.tsx"],"names":[],"mappings":"AAGA,OAAO,2BAA2B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/tests/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAIpD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-form",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"description": "Powerful, type-safe forms for React.",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,8 +42,9 @@
|
|
|
42
42
|
"react-error-boundary": "^3.1.4"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@tanstack/react-store": "0.
|
|
46
|
-
"@tanstack/
|
|
45
|
+
"@tanstack/react-store": "0.1.3",
|
|
46
|
+
"@tanstack/store": "0.1.3",
|
|
47
|
+
"@tanstack/form-core": "0.0.15"
|
|
47
48
|
},
|
|
48
49
|
"peerDependencies": {
|
|
49
50
|
"react": "^17.0.0 || ^18.0.0",
|
package/src/createFormFactory.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { FormApi, FormOptions } from '@tanstack/form-core'
|
|
2
|
+
|
|
2
3
|
import { type UseField, type FieldComponent, Field, useField } from './useField'
|
|
3
4
|
import { useForm } from './useForm'
|
|
4
5
|
|
|
@@ -13,7 +14,8 @@ export function createFormFactory<TFormData>(
|
|
|
13
14
|
): FormFactory<TFormData> {
|
|
14
15
|
return {
|
|
15
16
|
useForm: (opts) => {
|
|
16
|
-
|
|
17
|
+
const formOptions = Object.assign({}, defaultOpts, opts)
|
|
18
|
+
return useForm<TFormData>(formOptions)
|
|
17
19
|
},
|
|
18
20
|
useField: useField as any,
|
|
19
21
|
Field: Field as any,
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { render, waitFor } from '@testing-library/react'
|
|
3
|
+
import userEvent from '@testing-library/user-event'
|
|
4
|
+
import '@testing-library/jest-dom'
|
|
5
|
+
import { createFormFactory } from '..'
|
|
6
|
+
import { sleep } from './utils'
|
|
7
|
+
|
|
8
|
+
const user = userEvent.setup()
|
|
9
|
+
|
|
10
|
+
describe('useField', () => {
|
|
11
|
+
it('should allow to set default value', () => {
|
|
12
|
+
type Person = {
|
|
13
|
+
firstName: string
|
|
14
|
+
lastName: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const formFactory = createFormFactory<Person>()
|
|
18
|
+
|
|
19
|
+
function Comp() {
|
|
20
|
+
const form = formFactory.useForm()
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<form.Provider>
|
|
24
|
+
<form.Field
|
|
25
|
+
name="firstName"
|
|
26
|
+
defaultValue="FirstName"
|
|
27
|
+
children={(field) => {
|
|
28
|
+
return (
|
|
29
|
+
<input data-testid="fieldinput" {...field.getInputProps()} />
|
|
30
|
+
)
|
|
31
|
+
}}
|
|
32
|
+
/>
|
|
33
|
+
</form.Provider>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const { getByTestId } = render(<Comp />)
|
|
38
|
+
const input = getByTestId('fieldinput')
|
|
39
|
+
expect(input).toHaveValue('FirstName')
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('should not validate on change if isTouched is false', async () => {
|
|
43
|
+
type Person = {
|
|
44
|
+
firstName: string
|
|
45
|
+
lastName: string
|
|
46
|
+
}
|
|
47
|
+
const error = 'Please enter a different value'
|
|
48
|
+
|
|
49
|
+
const formFactory = createFormFactory<Person>()
|
|
50
|
+
|
|
51
|
+
function Comp() {
|
|
52
|
+
const form = formFactory.useForm()
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<form.Provider>
|
|
56
|
+
<form.Field
|
|
57
|
+
name="firstName"
|
|
58
|
+
onChange={(value) => (value === 'other' ? error : undefined)}
|
|
59
|
+
children={(field) => (
|
|
60
|
+
<div>
|
|
61
|
+
<input
|
|
62
|
+
data-testid="fieldinput"
|
|
63
|
+
name={field.name}
|
|
64
|
+
{...field.getInputProps()}
|
|
65
|
+
/>
|
|
66
|
+
<p>{field.getMeta().error}</p>
|
|
67
|
+
</div>
|
|
68
|
+
)}
|
|
69
|
+
/>
|
|
70
|
+
</form.Provider>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const { getByTestId, queryByText } = render(<Comp />)
|
|
75
|
+
const input = getByTestId('fieldinput')
|
|
76
|
+
await user.type(input, 'other')
|
|
77
|
+
expect(queryByText(error)).not.toBeInTheDocument()
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('should validate on change if isTouched is true', async () => {
|
|
81
|
+
type Person = {
|
|
82
|
+
firstName: string
|
|
83
|
+
lastName: string
|
|
84
|
+
}
|
|
85
|
+
const error = 'Please enter a different value'
|
|
86
|
+
|
|
87
|
+
const formFactory = createFormFactory<Person>()
|
|
88
|
+
|
|
89
|
+
function Comp() {
|
|
90
|
+
const form = formFactory.useForm()
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<form.Provider>
|
|
94
|
+
<form.Field
|
|
95
|
+
name="firstName"
|
|
96
|
+
defaultMeta={{ isTouched: true }}
|
|
97
|
+
onChange={(value) => (value === 'other' ? error : undefined)}
|
|
98
|
+
children={(field) => (
|
|
99
|
+
<div>
|
|
100
|
+
<input
|
|
101
|
+
data-testid="fieldinput"
|
|
102
|
+
name={field.name}
|
|
103
|
+
{...field.getInputProps()}
|
|
104
|
+
/>
|
|
105
|
+
<p>{field.getMeta().error}</p>
|
|
106
|
+
</div>
|
|
107
|
+
)}
|
|
108
|
+
/>
|
|
109
|
+
</form.Provider>
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const { getByTestId, getByText, queryByText } = render(<Comp />)
|
|
114
|
+
const input = getByTestId('fieldinput')
|
|
115
|
+
expect(queryByText(error)).not.toBeInTheDocument()
|
|
116
|
+
await user.type(input, 'other')
|
|
117
|
+
expect(getByText(error)).toBeInTheDocument()
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('should validate async on change', async () => {
|
|
121
|
+
type Person = {
|
|
122
|
+
firstName: string
|
|
123
|
+
lastName: string
|
|
124
|
+
}
|
|
125
|
+
const error = 'Please enter a different value'
|
|
126
|
+
|
|
127
|
+
const formFactory = createFormFactory<Person>()
|
|
128
|
+
|
|
129
|
+
function Comp() {
|
|
130
|
+
const form = formFactory.useForm()
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<form.Provider>
|
|
134
|
+
<form.Field
|
|
135
|
+
name="firstName"
|
|
136
|
+
defaultMeta={{ isTouched: true }}
|
|
137
|
+
onChangeAsync={async () => {
|
|
138
|
+
await sleep(10)
|
|
139
|
+
return error
|
|
140
|
+
}}
|
|
141
|
+
children={(field) => (
|
|
142
|
+
<div>
|
|
143
|
+
<input
|
|
144
|
+
data-testid="fieldinput"
|
|
145
|
+
name={field.name}
|
|
146
|
+
{...field.getInputProps()}
|
|
147
|
+
/>
|
|
148
|
+
<p>{field.getMeta().error}</p>
|
|
149
|
+
</div>
|
|
150
|
+
)}
|
|
151
|
+
/>
|
|
152
|
+
</form.Provider>
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const { getByTestId, getByText, queryByText } = render(<Comp />)
|
|
157
|
+
const input = getByTestId('fieldinput')
|
|
158
|
+
expect(queryByText(error)).not.toBeInTheDocument()
|
|
159
|
+
await user.type(input, 'other')
|
|
160
|
+
await waitFor(() => getByText(error))
|
|
161
|
+
expect(getByText(error)).toBeInTheDocument()
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
it('should validate async on change with debounce', async () => {
|
|
165
|
+
type Person = {
|
|
166
|
+
firstName: string
|
|
167
|
+
lastName: string
|
|
168
|
+
}
|
|
169
|
+
const mockFn = vi.fn()
|
|
170
|
+
const error = 'Please enter a different value'
|
|
171
|
+
const formFactory = createFormFactory<Person>()
|
|
172
|
+
|
|
173
|
+
function Comp() {
|
|
174
|
+
const form = formFactory.useForm()
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<form.Provider>
|
|
178
|
+
<form.Field
|
|
179
|
+
name="firstName"
|
|
180
|
+
defaultMeta={{ isTouched: true }}
|
|
181
|
+
onChangeAsyncDebounceMs={100}
|
|
182
|
+
onChangeAsync={async () => {
|
|
183
|
+
mockFn()
|
|
184
|
+
await sleep(10)
|
|
185
|
+
return error
|
|
186
|
+
}}
|
|
187
|
+
children={(field) => (
|
|
188
|
+
<div>
|
|
189
|
+
<input
|
|
190
|
+
data-testid="fieldinput"
|
|
191
|
+
name={field.name}
|
|
192
|
+
{...field.getInputProps()}
|
|
193
|
+
/>
|
|
194
|
+
<p>{field.getMeta().error}</p>
|
|
195
|
+
</div>
|
|
196
|
+
)}
|
|
197
|
+
/>
|
|
198
|
+
</form.Provider>
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const { getByTestId, getByText } = render(<Comp />)
|
|
203
|
+
const input = getByTestId('fieldinput')
|
|
204
|
+
await user.type(input, 'other')
|
|
205
|
+
// mockFn will have been called 5 times without onChangeAsyncDebounceMs
|
|
206
|
+
expect(mockFn).toHaveBeenCalledTimes(0)
|
|
207
|
+
await waitFor(() => getByText(error))
|
|
208
|
+
expect(getByText(error)).toBeInTheDocument()
|
|
209
|
+
})
|
|
210
|
+
})
|