@tanstack/solid-form 0.22.1 → 0.23.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/dist/index.d.ts CHANGED
@@ -1,7 +1,4 @@
1
- export type { DeepKeys, DeepValue, FieldApiOptions, FieldInfo, FieldMeta, FieldOptions, FieldState, FormOptions, FormState, RequiredByKey, Updater, UpdaterFn, ValidationCause, ValidationError, ValidationMeta, } from '@tanstack/form-core';
2
- export { FormApi, FieldApi, functionalUpdate } from '@tanstack/form-core';
1
+ export * from '@tanstack/form-core';
3
2
  export { createForm } from './createForm';
4
3
  export type { CreateField, FieldComponent } from './createField';
5
4
  export { createField, Field } from './createField';
6
- export type { FormFactory } from './createFormFactory';
7
- export { createFormFactory } from './createFormFactory';
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
- export { FormApi, FieldApi, functionalUpdate } from '@tanstack/form-core';
1
+ export * from '@tanstack/form-core';
2
2
  export { createForm } from './createForm';
3
3
  export { createField, Field } from './createField';
4
- export { createFormFactory } from './createFormFactory';
5
4
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAEzE,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAGzC,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA;AAGlD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AAEnC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAGzC,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/solid-form",
3
- "version": "0.22.1",
3
+ "version": "0.23.1",
4
4
  "description": "Powerful, type-safe forms for Solid.",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -17,6 +17,7 @@
17
17
  ],
18
18
  "type": "module",
19
19
  "types": "dist/index.d.ts",
20
+ "main": "dist/index.js",
20
21
  "module": "dist/index.js",
21
22
  "exports": {
22
23
  ".": {
@@ -38,7 +39,7 @@
38
39
  },
39
40
  "dependencies": {
40
41
  "@tanstack/solid-store": "^0.4.1",
41
- "@tanstack/form-core": "0.22.0"
42
+ "@tanstack/form-core": "0.23.1"
42
43
  },
43
44
  "peerDependencies": {
44
45
  "solid-js": "^1.6.0"
package/src/index.ts CHANGED
@@ -1,27 +1,6 @@
1
- export type {
2
- DeepKeys,
3
- DeepValue,
4
- FieldApiOptions,
5
- FieldInfo,
6
- FieldMeta,
7
- FieldOptions,
8
- FieldState,
9
- FormOptions,
10
- FormState,
11
- RequiredByKey,
12
- Updater,
13
- UpdaterFn,
14
- ValidationCause,
15
- ValidationError,
16
- ValidationMeta,
17
- } from '@tanstack/form-core'
18
-
19
- export { FormApi, FieldApi, functionalUpdate } from '@tanstack/form-core'
1
+ export * from '@tanstack/form-core'
20
2
 
21
3
  export { createForm } from './createForm'
22
4
 
23
5
  export type { CreateField, FieldComponent } from './createField'
24
6
  export { createField, Field } from './createField'
25
-
26
- export type { FormFactory } from './createFormFactory'
27
- export { createFormFactory } from './createFormFactory'
@@ -1,8 +0,0 @@
1
- import { Field, createField } from './createField';
2
- import type { FormApi, FormOptions, Validator } from '@tanstack/form-core';
3
- export type FormFactory<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = {
4
- createForm: (opts?: () => FormOptions<TFormData, TFormValidator>) => FormApi<TFormData, TFormValidator>;
5
- createField: typeof createField;
6
- Field: typeof Field;
7
- };
8
- export declare function createFormFactory<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts?: () => FormOptions<TFormData, TFormValidator>): FormFactory<TFormData, TFormValidator>;
@@ -1,11 +0,0 @@
1
- import { mergeProps } from 'solid-js';
2
- import { Field, createField, } from './createField';
3
- import { createForm } from './createForm';
4
- export function createFormFactory(defaultOpts) {
5
- return {
6
- createForm: (opts) => createForm(() => mergeProps(defaultOpts?.() ?? {}, opts?.() ?? {})),
7
- createField,
8
- Field: Field,
9
- };
10
- }
11
- //# sourceMappingURL=createFormFactory.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createFormFactory.js","sourceRoot":"","sources":["../src/createFormFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AACrC,OAAO,EAEL,KAAK,EAEL,WAAW,GACZ,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAczC,MAAM,UAAU,iBAAiB,CAI/B,WAA0D;IAE1D,OAAO;QACL,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE,CACnB,UAAU,CAA4B,GAAG,EAAE,CACzC,UAAU,CAAC,WAAW,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,CAClD;QACH,WAAW;QACX,KAAK,EAAE,KAAK;KACb,CAAA;AACH,CAAC"}
@@ -1,36 +0,0 @@
1
- import { mergeProps } from 'solid-js'
2
- import {
3
- type CreateField,
4
- Field,
5
- type FieldComponent,
6
- createField,
7
- } from './createField'
8
- import { createForm } from './createForm'
9
- import type { FormApi, FormOptions, Validator } from '@tanstack/form-core'
10
-
11
- export type FormFactory<
12
- TFormData,
13
- TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
14
- > = {
15
- createForm: (
16
- opts?: () => FormOptions<TFormData, TFormValidator>,
17
- ) => FormApi<TFormData, TFormValidator>
18
- createField: typeof createField
19
- Field: typeof Field
20
- }
21
-
22
- export function createFormFactory<
23
- TFormData,
24
- TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
25
- >(
26
- defaultOpts?: () => FormOptions<TFormData, TFormValidator>,
27
- ): FormFactory<TFormData, TFormValidator> {
28
- return {
29
- createForm: (opts) =>
30
- createForm<TFormData, TFormValidator>(() =>
31
- mergeProps(defaultOpts?.() ?? {}, opts?.() ?? {}),
32
- ),
33
- createField,
34
- Field: Field,
35
- }
36
- }
@@ -1,74 +0,0 @@
1
- import { assertType, it } from 'vitest'
2
- import { createForm } from '../createForm'
3
-
4
- it('should type state.value properly', () => {
5
- function Comp() {
6
- const form = createForm(
7
- () =>
8
- ({
9
- defaultValues: {
10
- firstName: 'test',
11
- age: 84,
12
- },
13
- }) as const,
14
- )
15
-
16
- return (
17
- <>
18
- <form.Field
19
- name="firstName"
20
- children={(field) => {
21
- assertType<'test'>(field().state.value)
22
- return null
23
- }}
24
- />
25
- <form.Field
26
- name="age"
27
- children={(field) => {
28
- assertType<84>(field().state.value)
29
- return null
30
- }}
31
- />
32
- </>
33
- )
34
- }
35
- })
36
-
37
- it('should type onChange properly', () => {
38
- function Comp() {
39
- const form = createForm(
40
- () =>
41
- ({
42
- defaultValues: {
43
- firstName: 'test',
44
- age: 84,
45
- },
46
- }) as const,
47
- )
48
-
49
- return (
50
- <>
51
- <form.Field
52
- name="firstName"
53
- validators={{
54
- onChange: ({ value }) => {
55
- assertType<'test'>(value)
56
- return null
57
- },
58
- }}
59
- children={() => null}
60
- />
61
- <form.Field
62
- name="age"
63
- validators={{
64
- onChange: ({ value }) => {
65
- assertType<84>(value)
66
- return null
67
- },
68
- }}
69
- children={() => null}
70
- />
71
- </>
72
- )
73
- }
74
- })
@@ -1,578 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest'
2
- import { render, waitFor } from '@solidjs/testing-library'
3
- import userEvent from '@testing-library/user-event'
4
- import '@testing-library/jest-dom/vitest'
5
- import { Index, Show, createEffect } from 'solid-js'
6
- import { createForm, createFormFactory } from '../index'
7
- import { sleep } from './utils'
8
-
9
- const user = userEvent.setup()
10
-
11
- describe('createField', () => {
12
- it('should allow to set default value', () => {
13
- type Person = {
14
- firstName: string
15
- lastName: string
16
- }
17
-
18
- const formFactory = createFormFactory<Person>()
19
-
20
- function Comp() {
21
- const form = formFactory.createForm()
22
-
23
- return (
24
- <>
25
- <form.Field
26
- name="firstName"
27
- defaultValue="FirstName"
28
- children={(field) => {
29
- return (
30
- <input
31
- data-testid="fieldinput"
32
- value={field().state.value}
33
- onBlur={field().handleBlur}
34
- onInput={(e) => field().handleChange(e.currentTarget.value)}
35
- />
36
- )
37
- }}
38
- />
39
- </>
40
- )
41
- }
42
-
43
- const { getByTestId } = render(() => <Comp />)
44
- const input = getByTestId('fieldinput')
45
- expect(input).toHaveValue('FirstName')
46
- })
47
-
48
- it('should use field default value first', async () => {
49
- type Person = {
50
- firstName: string
51
- lastName: string
52
- }
53
-
54
- const formFactory = createFormFactory<Person>()
55
-
56
- function Comp() {
57
- const form = formFactory.createForm(() => ({
58
- defaultValues: {
59
- firstName: 'FirstName',
60
- lastName: 'LastName',
61
- },
62
- }))
63
-
64
- return (
65
- <>
66
- <form.Field
67
- name="firstName"
68
- defaultValue="otherName"
69
- children={(field) => {
70
- return (
71
- <input
72
- data-testid="fieldinput"
73
- value={field().state.value}
74
- onBlur={field().handleBlur}
75
- onChange={(e) => field().handleChange(e.target.value)}
76
- />
77
- )
78
- }}
79
- />
80
- </>
81
- )
82
- }
83
-
84
- const { getByTestId } = render(() => <Comp />)
85
- const input = getByTestId('fieldinput')
86
- expect(input).toHaveValue('otherName')
87
- })
88
-
89
- it('should not validate on change if isTouched is false', async () => {
90
- type Person = {
91
- firstName: string
92
- lastName: string
93
- }
94
- const error = 'Please enter a different value'
95
-
96
- const formFactory = createFormFactory<Person>()
97
-
98
- function Comp() {
99
- const form = formFactory.createForm()
100
-
101
- return (
102
- <>
103
- <form.Field
104
- name="firstName"
105
- validators={{
106
- onChange: ({ value }) =>
107
- value.includes('other') ? error : undefined,
108
- }}
109
- children={(field) => (
110
- <div>
111
- <input
112
- data-testid="fieldinput"
113
- name={field().name}
114
- value={field().state.value}
115
- onBlur={field().handleBlur}
116
- onInput={(e) => field().setValue(e.currentTarget.value)}
117
- />
118
- <p>{field().getMeta().errors}</p>
119
- </div>
120
- )}
121
- />
122
- </>
123
- )
124
- }
125
-
126
- const { getByTestId, queryByText } = render(() => <Comp />)
127
- const input = getByTestId('fieldinput')
128
- await user.type(input, 'other')
129
- expect(queryByText(error)).not.toBeInTheDocument()
130
- })
131
-
132
- it('should validate on change if isTouched is true', async () => {
133
- type Person = {
134
- firstName: string
135
- lastName: string
136
- }
137
- const error = 'Please enter a different value'
138
-
139
- const formFactory = createFormFactory<Person>()
140
-
141
- function Comp() {
142
- const form = formFactory.createForm()
143
-
144
- return (
145
- <>
146
- <form.Field
147
- name="firstName"
148
- defaultMeta={{ isTouched: true }}
149
- validators={{
150
- onChange: ({ value }) =>
151
- value.includes('other') ? error : undefined,
152
- }}
153
- children={(field) => {
154
- return (
155
- <div>
156
- <input
157
- data-testid="fieldinput"
158
- name={field().name}
159
- value={field().state.value}
160
- onBlur={field().handleBlur}
161
- onInput={(e) => field().setValue(e.currentTarget.value)}
162
- />
163
- <p>{field().getMeta().errorMap.onChange}</p>
164
- </div>
165
- )
166
- }}
167
- />
168
- </>
169
- )
170
- }
171
-
172
- const { getByTestId, getByText, queryByText } = render(() => <Comp />)
173
- const input = getByTestId('fieldinput')
174
- expect(queryByText(error)).not.toBeInTheDocument()
175
- await user.type(input, 'other')
176
- expect(getByText(error)).toBeInTheDocument()
177
- })
178
-
179
- it('should validate on change and on blur', async () => {
180
- type Person = {
181
- firstName: string
182
- lastName: string
183
- }
184
- const onChangeError = 'Please enter a different value (onChangeError)'
185
- const onBlurError = 'Please enter a different value (onBlurError)'
186
-
187
- const formFactory = createFormFactory<Person>()
188
-
189
- function Comp() {
190
- const form = formFactory.createForm()
191
-
192
- return (
193
- <>
194
- <form.Field
195
- name="firstName"
196
- defaultMeta={{ isTouched: true }}
197
- validators={{
198
- onChange: ({ value }) =>
199
- value.includes('other') ? onChangeError : undefined,
200
- onBlur: ({ value }) =>
201
- value.includes('other') ? onBlurError : undefined,
202
- }}
203
- children={(field) => (
204
- <div>
205
- <input
206
- data-testid="fieldinput"
207
- name={field().name}
208
- value={field().state.value}
209
- onBlur={field().handleBlur}
210
- onInput={(e) => field().handleChange(e.currentTarget.value)}
211
- />
212
- <p>{field().getMeta().errorMap.onChange}</p>
213
- <p>{field().getMeta().errorMap.onBlur}</p>
214
- </div>
215
- )}
216
- />
217
- </>
218
- )
219
- }
220
-
221
- const { getByTestId, getByText, queryByText } = render(() => <Comp />)
222
- const input = getByTestId('fieldinput')
223
- expect(queryByText(onChangeError)).not.toBeInTheDocument()
224
- expect(queryByText(onBlurError)).not.toBeInTheDocument()
225
- await user.type(input, 'other')
226
- expect(getByText(onChangeError)).toBeInTheDocument()
227
- await user.click(document.body)
228
- expect(queryByText(onBlurError)).toBeInTheDocument()
229
- })
230
-
231
- it('should validate async on change', async () => {
232
- type Person = {
233
- firstName: string
234
- lastName: string
235
- }
236
- const error = 'Please enter a different value'
237
-
238
- const formFactory = createFormFactory<Person>()
239
-
240
- function Comp() {
241
- const form = formFactory.createForm()
242
-
243
- return (
244
- <>
245
- <form.Field
246
- name="firstName"
247
- defaultMeta={{ isTouched: true }}
248
- validators={{
249
- onChangeAsync: async () => {
250
- await sleep(10)
251
- return error
252
- },
253
- }}
254
- children={(field) => (
255
- <div>
256
- <input
257
- data-testid="fieldinput"
258
- name={field().name}
259
- value={field().state.value}
260
- onBlur={field().handleBlur}
261
- onInput={(e) => field().handleChange(e.currentTarget.value)}
262
- />
263
- <p>{field().getMeta().errorMap.onChange}</p>
264
- </div>
265
- )}
266
- />
267
- </>
268
- )
269
- }
270
-
271
- const { getByTestId, getByText, queryByText } = render(() => <Comp />)
272
- const input = getByTestId('fieldinput')
273
- expect(queryByText(error)).not.toBeInTheDocument()
274
- await user.type(input, 'other')
275
- await waitFor(() => getByText(error))
276
- expect(getByText(error)).toBeInTheDocument()
277
- })
278
-
279
- it('should validate async on change and async on blur', async () => {
280
- type Person = {
281
- firstName: string
282
- lastName: string
283
- }
284
- const onChangeError = 'Please enter a different value (onChangeError)'
285
- const onBlurError = 'Please enter a different value (onBlurError)'
286
-
287
- const formFactory = createFormFactory<Person>()
288
-
289
- function Comp() {
290
- const form = formFactory.createForm()
291
-
292
- return (
293
- <>
294
- <form.Field
295
- name="firstName"
296
- defaultMeta={{ isTouched: true }}
297
- validators={{
298
- onChangeAsync: async () => {
299
- await sleep(10)
300
- return onChangeError
301
- },
302
- onBlurAsync: async () => {
303
- await sleep(10)
304
- return onBlurError
305
- },
306
- }}
307
- children={(field) => (
308
- <div>
309
- <input
310
- data-testid="fieldinput"
311
- name={field().name}
312
- value={field().state.value}
313
- onBlur={field().handleBlur}
314
- onInput={(e) => field().handleChange(e.currentTarget.value)}
315
- />
316
- <p>{field().getMeta().errorMap?.onChange}</p>
317
- <p>{field().getMeta().errorMap?.onBlur}</p>
318
- </div>
319
- )}
320
- />
321
- </>
322
- )
323
- }
324
-
325
- const { getByTestId, getByText, queryByText } = render(() => <Comp />)
326
- const input = getByTestId('fieldinput')
327
-
328
- expect(queryByText(onChangeError)).not.toBeInTheDocument()
329
- expect(queryByText(onBlurError)).not.toBeInTheDocument()
330
- await user.type(input, 'other')
331
- await waitFor(() => getByText(onChangeError))
332
- expect(getByText(onChangeError)).toBeInTheDocument()
333
- await user.click(document.body)
334
- await waitFor(() => getByText(onBlurError))
335
- expect(getByText(onBlurError)).toBeInTheDocument()
336
- })
337
-
338
- it('should validate async on change with debounce', async () => {
339
- type Person = {
340
- firstName: string
341
- lastName: string
342
- }
343
- const mockFn = vi.fn()
344
- const error = 'Please enter a different value'
345
- const formFactory = createFormFactory<Person>()
346
-
347
- function Comp() {
348
- const form = formFactory.createForm()
349
-
350
- return (
351
- <>
352
- <form.Field
353
- name="firstName"
354
- defaultMeta={{ isTouched: true }}
355
- validators={{
356
- onChangeAsyncDebounceMs: 100,
357
- onChangeAsync: async () => {
358
- mockFn()
359
- await sleep(10)
360
- return error
361
- },
362
- }}
363
- children={(field) => (
364
- <div>
365
- <input
366
- data-testid="fieldinput"
367
- name={field().name}
368
- value={field().state.value}
369
- onBlur={field().handleBlur}
370
- onInput={(e) => field().handleChange(e.currentTarget.value)}
371
- />
372
- <p>{field().getMeta().errors}</p>
373
- </div>
374
- )}
375
- />
376
- </>
377
- )
378
- }
379
-
380
- const { getByTestId, getByText } = render(() => <Comp />)
381
- const input = getByTestId('fieldinput')
382
- await user.type(input, 'other')
383
- // mockFn will have been called 5 times without onChangeAsyncDebounceMs
384
- expect(mockFn).toHaveBeenCalledTimes(0)
385
- await waitFor(() => getByText(error))
386
- expect(getByText(error)).toBeInTheDocument()
387
- })
388
-
389
- it('should handle arrays with primitive values', async () => {
390
- const fn = vi.fn()
391
-
392
- function Comp() {
393
- const form = createForm(() => ({
394
- defaultValues: {
395
- people: [] as Array<string>,
396
- },
397
- onSubmit: ({ value }) => fn(value),
398
- }))
399
- return (
400
- <div>
401
- <form
402
- onSubmit={(e) => {
403
- e.preventDefault()
404
- e.stopPropagation()
405
- form.handleSubmit()
406
- }}
407
- >
408
- <form.Field name="people">
409
- {(field) => (
410
- <div>
411
- <Show when={field().state.value.length > 0}>
412
- {/* Do not change this to For or the test will fail */}
413
- <Index each={field().state.value}>
414
- {(_, i) => {
415
- return (
416
- <form.Field name={`people[${i}]`}>
417
- {(subField) => (
418
- <div>
419
- <label>
420
- <div>Name for person {i}</div>
421
- <input
422
- value={subField().state.value}
423
- onInput={(e) => {
424
- subField().handleChange(
425
- e.currentTarget.value,
426
- )
427
- }}
428
- />
429
- </label>
430
- <button
431
- onClick={() => field().removeValue(i)}
432
- type="button"
433
- >
434
- Remove person {i}
435
- </button>
436
- </div>
437
- )}
438
- </form.Field>
439
- )
440
- }}
441
- </Index>
442
- </Show>
443
-
444
- <button onClick={() => field().pushValue('')} type="button">
445
- Add person
446
- </button>
447
- </div>
448
- )}
449
- </form.Field>
450
- <button type="submit">Submit</button>
451
- </form>
452
- </div>
453
- )
454
- }
455
-
456
- const { getByText, findByLabelText, queryByText, findByText } = render(
457
- () => <Comp />,
458
- )
459
-
460
- expect(queryByText('Name for person 0')).not.toBeInTheDocument()
461
- expect(queryByText('Name for person 1')).not.toBeInTheDocument()
462
- await user.click(getByText('Add person'))
463
- const input = await findByLabelText('Name for person 0')
464
- expect(input).toBeInTheDocument()
465
- await user.type(input, 'John')
466
-
467
- await user.click(getByText('Add person'))
468
- const input2 = await findByLabelText('Name for person 1')
469
- expect(input).toBeInTheDocument()
470
- await user.type(input2, 'Jack')
471
-
472
- expect(queryByText('Name for person 0')).toBeInTheDocument()
473
- expect(queryByText('Name for person 1')).toBeInTheDocument()
474
- await user.click(getByText('Remove person 1'))
475
- expect(queryByText('Name for person 0')).toBeInTheDocument()
476
- expect(queryByText('Name for person 1')).not.toBeInTheDocument()
477
-
478
- await user.click(await findByText('Submit'))
479
- expect(fn).toHaveBeenCalledWith({ people: ['John'] })
480
- })
481
-
482
- it('should handle arrays with subvalues', async () => {
483
- const fn = vi.fn()
484
-
485
- function Comp() {
486
- const form = createForm(() => ({
487
- defaultValues: {
488
- people: [] as Array<{ age: number; name: string }>,
489
- },
490
- onSubmit: ({ value }) => fn(value),
491
- }))
492
-
493
- return (
494
- <div>
495
- <form
496
- onSubmit={(e) => {
497
- e.preventDefault()
498
- e.stopPropagation()
499
- form.handleSubmit()
500
- }}
501
- >
502
- <form.Field name="people">
503
- {(field) => (
504
- <div>
505
- <Show when={field().state.value.length > 0}>
506
- {/* Do not change this to For or the test will fail */}
507
- <Index each={field().state.value}>
508
- {(_, i) => {
509
- return (
510
- <form.Field name={`people[${i}].name`}>
511
- {(subField) => (
512
- <div>
513
- <label>
514
- <div>Name for person {i}</div>
515
- <input
516
- value={subField().state.value}
517
- onInput={(e) => {
518
- subField().handleChange(
519
- e.currentTarget.value,
520
- )
521
- }}
522
- />
523
- </label>
524
- <button
525
- onClick={() => field().removeValue(i)}
526
- type="button"
527
- >
528
- Remove person {i}
529
- </button>
530
- </div>
531
- )}
532
- </form.Field>
533
- )
534
- }}
535
- </Index>
536
- </Show>
537
-
538
- <button
539
- onClick={() => field().pushValue({ name: '', age: 0 })}
540
- type="button"
541
- >
542
- Add person
543
- </button>
544
- </div>
545
- )}
546
- </form.Field>
547
- <button type="submit">Submit</button>
548
- </form>
549
- </div>
550
- )
551
- }
552
-
553
- const { getByText, findByLabelText, queryByText, findByText } = render(
554
- () => <Comp />,
555
- )
556
-
557
- expect(queryByText('Name for person 0')).not.toBeInTheDocument()
558
- expect(queryByText('Name for person 1')).not.toBeInTheDocument()
559
- await user.click(getByText('Add person'))
560
- const input = await findByLabelText('Name for person 0')
561
- expect(input).toBeInTheDocument()
562
- await user.type(input, 'John')
563
-
564
- await user.click(getByText('Add person'))
565
- const input2 = await findByLabelText('Name for person 1')
566
- expect(input).toBeInTheDocument()
567
- await user.type(input2, 'Jack')
568
-
569
- expect(queryByText('Name for person 0')).toBeInTheDocument()
570
- expect(queryByText('Name for person 1')).toBeInTheDocument()
571
- await user.click(getByText('Remove person 1'))
572
- expect(queryByText('Name for person 0')).toBeInTheDocument()
573
- expect(queryByText('Name for person 1')).not.toBeInTheDocument()
574
-
575
- await user.click(await findByText('Submit'))
576
- expect(fn).toHaveBeenCalledWith({ people: [{ name: 'John', age: 0 }] })
577
- })
578
- })
@@ -1,481 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest'
2
- import { render, screen, waitFor } from '@solidjs/testing-library'
3
- import userEvent from '@testing-library/user-event'
4
- import { Show, createSignal, onCleanup } from 'solid-js'
5
- import { createForm, createFormFactory } from '../index'
6
- import { sleep } from './utils'
7
- import type { ValidationErrorMap } from '@tanstack/form-core'
8
-
9
- const user = userEvent.setup()
10
-
11
- describe('createForm', () => {
12
- it('preserves field state', async () => {
13
- type Person = {
14
- firstName: string
15
- lastName: string
16
- }
17
-
18
- const formFactory = createFormFactory<Person>()
19
-
20
- function Comp() {
21
- const form = formFactory.createForm()
22
- return (
23
- <>
24
- <form.Field
25
- name="firstName"
26
- defaultValue={''}
27
- children={(field) => (
28
- <input
29
- data-testid="fieldinput"
30
- value={field().state.value}
31
- onBlur={field().handleBlur}
32
- onChange={(e) => field().handleChange(e.currentTarget.value)}
33
- />
34
- )}
35
- />
36
- </>
37
- )
38
- }
39
-
40
- render(() => <Comp />)
41
- const input = screen.getByTestId('fieldinput')
42
- expect(screen.queryByText('FirstName')).not.toBeInTheDocument()
43
- await user.type(input, 'FirstName')
44
- expect(input).toHaveValue('FirstName')
45
- })
46
-
47
- it('should allow default values to be set', async () => {
48
- type Person = {
49
- firstName: string
50
- lastName: string
51
- }
52
-
53
- const formFactory = createFormFactory<Person>()
54
-
55
- function Comp() {
56
- const form = formFactory.createForm(() => ({
57
- defaultValues: {
58
- firstName: 'FirstName',
59
- lastName: 'LastName',
60
- },
61
- }))
62
-
63
- return (
64
- <>
65
- <form.Field
66
- name="firstName"
67
- children={(field) => {
68
- return <p>{field().state.value}</p>
69
- }}
70
- />
71
- </>
72
- )
73
- }
74
-
75
- const { findByText, queryByText } = render(() => <Comp />)
76
- expect(await findByText('FirstName')).toBeInTheDocument()
77
- expect(queryByText('LastName')).not.toBeInTheDocument()
78
- })
79
-
80
- it('should handle submitting properly', async () => {
81
- let submittedData = null as { firstName: string } | null
82
- function Comp() {
83
- const form = createForm(() => ({
84
- defaultValues: {
85
- firstName: 'FirstName',
86
- },
87
- onSubmit: ({ value }) => {
88
- submittedData = value
89
- },
90
- }))
91
-
92
- return (
93
- <>
94
- <form.Field
95
- name="firstName"
96
- children={(field) => {
97
- return (
98
- <input
99
- value={field().state.value}
100
- onBlur={field().handleBlur}
101
- onChange={(e) => field().handleChange(e.target.value)}
102
- placeholder={'First name'}
103
- />
104
- )
105
- }}
106
- />
107
- <button onClick={form.handleSubmit}>Submit</button>
108
- </>
109
- )
110
- }
111
-
112
- const { findByPlaceholderText, getByText } = render(() => <Comp />)
113
- const input = await findByPlaceholderText('First name')
114
- await user.clear(input)
115
- await user.type(input, 'OtherName')
116
- await user.click(getByText('Submit'))
117
- expect(submittedData?.firstName).toEqual('OtherName')
118
- })
119
-
120
- it('should run on form mount', async () => {
121
- const [formMounted, setFormMounted] = createSignal(false)
122
- const [mountForm, setMountForm] = createSignal(false)
123
- function Comp() {
124
- const form = createForm(() => ({
125
- defaultValues: {
126
- firstName: 'FirstName',
127
- },
128
- validators: {
129
- onMount: () => {
130
- setFormMounted(true)
131
- return undefined
132
- },
133
- },
134
- }))
135
-
136
- return (
137
- <Show
138
- when={mountForm()}
139
- fallback={
140
- <button onClick={() => setMountForm(true)}>Mount form</button>
141
- }
142
- >
143
- <>
144
- <h1>Form mounted</h1>
145
- </>
146
- </Show>
147
- )
148
- }
149
-
150
- const { getByText } = render(() => <Comp />)
151
- await user.click(getByText('Mount form'))
152
- expect(formMounted()).toBe(true)
153
- })
154
-
155
- it('should not validate on change if isTouched is false', async () => {
156
- type Person = {
157
- firstName: string
158
- lastName: string
159
- }
160
- const error = 'Please enter a different value'
161
-
162
- const formFactory = createFormFactory<Person>()
163
-
164
- function Comp() {
165
- const form = formFactory.createForm(() => ({
166
- validators: {
167
- onChange: ({ value }) =>
168
- value.firstName.includes('other') ? error : undefined,
169
- },
170
- }))
171
-
172
- const errors = form.useStore((s) => s.errors)
173
-
174
- return (
175
- <>
176
- <form.Field
177
- name="firstName"
178
- children={(field) => (
179
- <div>
180
- <input
181
- data-testid="fieldinput"
182
- name={field().name}
183
- value={field().state.value}
184
- onBlur={field().handleBlur}
185
- onInput={(e) => field().setValue(e.currentTarget.value)}
186
- />
187
- <p>{errors().join(',')}</p>
188
- </div>
189
- )}
190
- />
191
- </>
192
- )
193
- }
194
-
195
- const { getByTestId, queryByText } = render(() => <Comp />)
196
- const input = getByTestId('fieldinput')
197
- await user.type(input, 'other')
198
- expect(queryByText(error)).not.toBeInTheDocument()
199
- })
200
-
201
- it('should validate on change if isTouched is true', async () => {
202
- type Person = {
203
- firstName: string
204
- lastName: string
205
- }
206
- const error = 'Please enter a different value'
207
-
208
- const formFactory = createFormFactory<Person>()
209
-
210
- function Comp() {
211
- const form = formFactory.createForm(() => ({
212
- validators: {
213
- onChange: ({ value }) =>
214
- value.firstName.includes('other') ? error : undefined,
215
- },
216
- }))
217
-
218
- const [errors, setErrors] = createSignal<ValidationErrorMap>()
219
- onCleanup(form.store.subscribe(() => setErrors(form.state.errorMap)))
220
-
221
- return (
222
- <>
223
- <form.Field
224
- name="firstName"
225
- defaultMeta={{ isTouched: true }}
226
- children={(field) => {
227
- return (
228
- <div>
229
- <input
230
- data-testid="fieldinput"
231
- name={field().name}
232
- value={field().state.value}
233
- onBlur={field().handleBlur}
234
- onInput={(e) => field().setValue(e.currentTarget.value)}
235
- />
236
- <p>{errors()?.onChange}</p>
237
- </div>
238
- )
239
- }}
240
- />
241
- </>
242
- )
243
- }
244
-
245
- const { getByTestId, getByText, queryByText } = render(() => <Comp />)
246
- const input = getByTestId('fieldinput')
247
- expect(queryByText(error)).not.toBeInTheDocument()
248
- await user.type(input, 'other')
249
- expect(getByText(error)).toBeInTheDocument()
250
- })
251
-
252
- it('should validate on change and on blur', async () => {
253
- type Person = {
254
- firstName: string
255
- lastName: string
256
- }
257
- const onChangeError = 'Please enter a different value (onChangeError)'
258
- const onBlurError = 'Please enter a different value (onBlurError)'
259
-
260
- const formFactory = createFormFactory<Person>()
261
-
262
- function Comp() {
263
- const form = formFactory.createForm(() => ({
264
- validators: {
265
- onChange: ({ value }) =>
266
- value.firstName.includes('other') ? onChangeError : undefined,
267
- onBlur: ({ value }) =>
268
- value.firstName.includes('other') ? onBlurError : undefined,
269
- },
270
- }))
271
-
272
- const [errors, setErrors] = createSignal<ValidationErrorMap>()
273
- onCleanup(form.store.subscribe(() => setErrors(form.state.errorMap)))
274
-
275
- return (
276
- <>
277
- <form.Field
278
- name="firstName"
279
- defaultMeta={{ isTouched: true }}
280
- children={(field) => (
281
- <div>
282
- <input
283
- data-testid="fieldinput"
284
- name={field().name}
285
- value={field().state.value}
286
- onBlur={field().handleBlur}
287
- onInput={(e) => field().handleChange(e.currentTarget.value)}
288
- />
289
- <p>{errors()?.onChange}</p>
290
- <p>{errors()?.onBlur}</p>
291
- </div>
292
- )}
293
- />
294
- </>
295
- )
296
- }
297
-
298
- const { getByTestId, getByText, queryByText } = render(() => <Comp />)
299
- const input = getByTestId('fieldinput')
300
- expect(queryByText(onChangeError)).not.toBeInTheDocument()
301
- expect(queryByText(onBlurError)).not.toBeInTheDocument()
302
- await user.type(input, 'other')
303
- expect(getByText(onChangeError)).toBeInTheDocument()
304
- await user.click(document.body)
305
- expect(queryByText(onBlurError)).toBeInTheDocument()
306
- })
307
-
308
- it('should validate async on change', async () => {
309
- type Person = {
310
- firstName: string
311
- lastName: string
312
- }
313
- const error = 'Please enter a different value'
314
-
315
- const formFactory = createFormFactory<Person>()
316
-
317
- function Comp() {
318
- const form = formFactory.createForm(() => ({
319
- validators: {
320
- onChangeAsync: async () => {
321
- await sleep(10)
322
- return error
323
- },
324
- },
325
- }))
326
-
327
- const [errors, setErrors] = createSignal<ValidationErrorMap>()
328
- onCleanup(form.store.subscribe(() => setErrors(form.state.errorMap)))
329
-
330
- return (
331
- <>
332
- <form.Field
333
- name="firstName"
334
- defaultMeta={{ isTouched: true }}
335
- children={(field) => (
336
- <div>
337
- <input
338
- data-testid="fieldinput"
339
- name={field().name}
340
- value={field().state.value}
341
- onBlur={field().handleBlur}
342
- onInput={(e) => field().handleChange(e.currentTarget.value)}
343
- />
344
- <p>{errors()?.onChange}</p>
345
- </div>
346
- )}
347
- />
348
- </>
349
- )
350
- }
351
-
352
- const { getByTestId, getByText, queryByText } = render(() => <Comp />)
353
- const input = getByTestId('fieldinput')
354
- expect(queryByText(error)).not.toBeInTheDocument()
355
- await user.type(input, 'other')
356
- await waitFor(() => getByText(error))
357
- expect(getByText(error)).toBeInTheDocument()
358
- })
359
-
360
- it('should validate async on change and async on blur', async () => {
361
- type Person = {
362
- firstName: string
363
- lastName: string
364
- }
365
- const onChangeError = 'Please enter a different value (onChangeError)'
366
- const onBlurError = 'Please enter a different value (onBlurError)'
367
-
368
- const formFactory = createFormFactory<Person>()
369
-
370
- function Comp() {
371
- const form = formFactory.createForm(() => ({
372
- validators: {
373
- async onChangeAsync() {
374
- await sleep(10)
375
- return onChangeError
376
- },
377
- async onBlurAsync() {
378
- await sleep(10)
379
- return onBlurError
380
- },
381
- },
382
- }))
383
-
384
- const [errors, setErrors] = createSignal<ValidationErrorMap>()
385
- onCleanup(form.store.subscribe(() => setErrors(form.state.errorMap)))
386
-
387
- return (
388
- <>
389
- <form.Field
390
- name="firstName"
391
- defaultMeta={{ isTouched: true }}
392
- children={(field) => (
393
- <div>
394
- <input
395
- data-testid="fieldinput"
396
- name={field().name}
397
- value={field().state.value}
398
- onBlur={field().handleBlur}
399
- onInput={(e) => field().handleChange(e.currentTarget.value)}
400
- />
401
- <p>{errors()?.onChange}</p>
402
- <p>{errors()?.onBlur}</p>
403
- </div>
404
- )}
405
- />
406
- </>
407
- )
408
- }
409
-
410
- const { getByTestId, getByText, queryByText } = render(() => <Comp />)
411
- const input = getByTestId('fieldinput')
412
-
413
- expect(queryByText(onChangeError)).not.toBeInTheDocument()
414
- expect(queryByText(onBlurError)).not.toBeInTheDocument()
415
- await user.type(input, 'other')
416
- await waitFor(() => getByText(onChangeError))
417
- expect(getByText(onChangeError)).toBeInTheDocument()
418
- await user.click(document.body)
419
- await waitFor(() => getByText(onBlurError))
420
- expect(getByText(onBlurError)).toBeInTheDocument()
421
- })
422
-
423
- it('should validate async on change with debounce', async () => {
424
- type Person = {
425
- firstName: string
426
- lastName: string
427
- }
428
- const mockFn = vi.fn()
429
- const error = 'Please enter a different value'
430
- const formFactory = createFormFactory<Person>()
431
-
432
- function Comp() {
433
- const form = formFactory.createForm(() => ({
434
- validators: {
435
- onChangeAsyncDebounceMs: 100,
436
- onChangeAsync: async () => {
437
- mockFn()
438
- await sleep(10)
439
- return error
440
- },
441
- },
442
- }))
443
-
444
- const [errors, setErrors] = createSignal<string>()
445
- onCleanup(
446
- form.store.subscribe(() =>
447
- setErrors(form.state.errorMap.onChange || ''),
448
- ),
449
- )
450
-
451
- return (
452
- <>
453
- <form.Field
454
- name="firstName"
455
- defaultMeta={{ isTouched: true }}
456
- children={(field) => (
457
- <div>
458
- <input
459
- data-testid="fieldinput"
460
- name={field().name}
461
- value={field().state.value}
462
- onBlur={field().handleBlur}
463
- onInput={(e) => field().handleChange(e.currentTarget.value)}
464
- />
465
- <p>{errors()}</p>
466
- </div>
467
- )}
468
- />
469
- </>
470
- )
471
- }
472
-
473
- const { getByTestId, getByText } = render(() => <Comp />)
474
- const input = getByTestId('fieldinput')
475
- await user.type(input, 'other')
476
- // mockFn will have been called 5 times without onChangeAsyncDebounceMs
477
- expect(mockFn).toHaveBeenCalledTimes(0)
478
- await waitFor(() => getByText(error))
479
- expect(getByText(error)).toBeInTheDocument()
480
- })
481
- })
@@ -1,38 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { render } from '@solidjs/testing-library'
3
- import { createFormFactory } from '../index'
4
-
5
- describe('createFormFactory', () => {
6
- it('should allow default values to be set', async () => {
7
- type Person = {
8
- firstName: string
9
- lastName: string
10
- }
11
-
12
- const formFactory = createFormFactory<Person>(() => ({
13
- defaultValues: {
14
- firstName: 'FirstName',
15
- lastName: 'LastName',
16
- },
17
- }))
18
-
19
- function Comp() {
20
- const form = formFactory.createForm()
21
-
22
- return (
23
- <>
24
- <form.Field
25
- name="firstName"
26
- children={(field) => {
27
- return <p>{field().state.value}</p>
28
- }}
29
- />
30
- </>
31
- )
32
- }
33
-
34
- const { findByText, queryByText } = render(() => <Comp />)
35
- expect(await findByText('FirstName')).toBeInTheDocument()
36
- expect(queryByText('LastName')).not.toBeInTheDocument()
37
- })
38
- })
@@ -1,5 +0,0 @@
1
- export function sleep(timeout: number): Promise<void> {
2
- return new Promise((resolve, _reject) => {
3
- setTimeout(resolve, timeout)
4
- })
5
- }