@tanstack/solid-form 0.16.1 → 0.18.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/README.md +1 -1
- package/package.json +2 -2
- package/src/tests/createField.test.tsx +114 -4
package/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|

|
4
4
|
|
5
|
-
|
5
|
+
Signals for managing form state in Solid
|
6
6
|
|
7
7
|
<a href="https://twitter.com/intent/tweet?button_hashtag=TanStack" target="\_parent">
|
8
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">
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@tanstack/solid-form",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.18.0",
|
4
4
|
"description": "Powerful, type-safe forms for Solid.",
|
5
5
|
"author": "tannerlinsley",
|
6
6
|
"license": "MIT",
|
@@ -38,7 +38,7 @@
|
|
38
38
|
},
|
39
39
|
"dependencies": {
|
40
40
|
"@tanstack/solid-store": "^0.3.1",
|
41
|
-
"@tanstack/form-core": "0.
|
41
|
+
"@tanstack/form-core": "0.18.0"
|
42
42
|
},
|
43
43
|
"peerDependencies": {
|
44
44
|
"solid-js": "^1.6.0"
|
@@ -2,7 +2,7 @@ import { describe, expect, it, vi } from 'vitest'
|
|
2
2
|
import { render, waitFor } from '@solidjs/testing-library'
|
3
3
|
import userEvent from '@testing-library/user-event'
|
4
4
|
import '@testing-library/jest-dom/vitest'
|
5
|
-
import { Index, Show } from 'solid-js'
|
5
|
+
import { Index, Show, createEffect } from 'solid-js'
|
6
6
|
import { createForm, createFormFactory } from '../index'
|
7
7
|
import { sleep } from './utils'
|
8
8
|
|
@@ -386,6 +386,99 @@ describe('createField', () => {
|
|
386
386
|
expect(getByText(error)).toBeInTheDocument()
|
387
387
|
})
|
388
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
|
+
void 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
|
+
|
389
482
|
it('should handle arrays with subvalues', async () => {
|
390
483
|
const fn = vi.fn()
|
391
484
|
|
@@ -428,6 +521,12 @@ describe('createField', () => {
|
|
428
521
|
}}
|
429
522
|
/>
|
430
523
|
</label>
|
524
|
+
<button
|
525
|
+
onClick={() => field().removeValue(i)}
|
526
|
+
type="button"
|
527
|
+
>
|
528
|
+
Remove person {i}
|
529
|
+
</button>
|
431
530
|
</div>
|
432
531
|
)}
|
433
532
|
</form.Field>
|
@@ -456,13 +555,24 @@ describe('createField', () => {
|
|
456
555
|
)
|
457
556
|
|
458
557
|
expect(queryByText('Name for person 0')).not.toBeInTheDocument()
|
558
|
+
expect(queryByText('Name for person 1')).not.toBeInTheDocument()
|
459
559
|
await user.click(getByText('Add person'))
|
460
560
|
const input = await findByLabelText('Name for person 0')
|
461
561
|
expect(input).toBeInTheDocument()
|
462
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
|
+
|
463
575
|
await user.click(await findByText('Submit'))
|
464
|
-
expect(fn).toHaveBeenCalledWith({
|
465
|
-
people: [{ name: 'John', age: 0 }],
|
466
|
-
})
|
576
|
+
expect(fn).toHaveBeenCalledWith({ people: [{ name: 'John', age: 0 }] })
|
467
577
|
})
|
468
578
|
})
|