@tanstack/form-core 0.20.1 → 0.20.3
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/cjs/FieldApi.cjs +1 -3
- package/dist/cjs/FieldApi.cjs.map +1 -1
- package/dist/cjs/FieldApi.d.cts +1 -1
- package/dist/cjs/FormApi.cjs +44 -3
- package/dist/cjs/FormApi.cjs.map +1 -1
- package/dist/cjs/FormApi.d.cts +3 -1
- package/dist/cjs/utils.cjs +3 -0
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/esm/FieldApi.d.ts +1 -1
- package/dist/esm/FieldApi.js +1 -3
- package/dist/esm/FieldApi.js.map +1 -1
- package/dist/esm/FormApi.d.ts +3 -1
- package/dist/esm/FormApi.js +44 -3
- package/dist/esm/FormApi.js.map +1 -1
- package/dist/esm/utils.js +3 -0
- package/dist/esm/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/FieldApi.ts +2 -3
- package/src/FormApi.ts +75 -3
- package/src/tests/FieldApi.spec.ts +157 -0
- package/src/tests/FormApi.spec.ts +362 -0
- package/src/utils.ts +3 -0
|
@@ -324,6 +324,25 @@ describe('form api', () => {
|
|
|
324
324
|
expect(form.getFieldValue('names')).toStrictEqual(['test', 'other'])
|
|
325
325
|
})
|
|
326
326
|
|
|
327
|
+
it("should run onChange validation when pushing an array field's value", () => {
|
|
328
|
+
const form = new FormApi({
|
|
329
|
+
defaultValues: {
|
|
330
|
+
names: ['test'],
|
|
331
|
+
},
|
|
332
|
+
validators: {
|
|
333
|
+
onChange: ({ value }) =>
|
|
334
|
+
value.names.length > 3 ? undefined : 'At least 3 names are required',
|
|
335
|
+
},
|
|
336
|
+
})
|
|
337
|
+
form.mount()
|
|
338
|
+
// Since validation runs through the field, a field must be mounted for that array
|
|
339
|
+
new FieldApi({ form, name: 'names' }).mount()
|
|
340
|
+
|
|
341
|
+
form.pushFieldValue('names', 'other')
|
|
342
|
+
|
|
343
|
+
expect(form.state.errors).toStrictEqual(['At least 3 names are required'])
|
|
344
|
+
})
|
|
345
|
+
|
|
327
346
|
it("should insert an array field's value", () => {
|
|
328
347
|
const form = new FormApi({
|
|
329
348
|
defaultValues: {
|
|
@@ -336,6 +355,56 @@ describe('form api', () => {
|
|
|
336
355
|
expect(form.getFieldValue('names')).toStrictEqual(['one', 'other', 'three'])
|
|
337
356
|
})
|
|
338
357
|
|
|
358
|
+
it("should run onChange validation when inserting an array field's value", () => {
|
|
359
|
+
const form = new FormApi({
|
|
360
|
+
defaultValues: {
|
|
361
|
+
names: ['test'],
|
|
362
|
+
},
|
|
363
|
+
validators: {
|
|
364
|
+
onChange: ({ value }) =>
|
|
365
|
+
value.names.length > 3 ? undefined : 'At least 3 names are required',
|
|
366
|
+
},
|
|
367
|
+
})
|
|
368
|
+
form.mount()
|
|
369
|
+
// Since validation runs through the field, a field must be mounted for that array
|
|
370
|
+
new FieldApi({ form, name: 'names' }).mount()
|
|
371
|
+
|
|
372
|
+
form.insertFieldValue('names', 1, 'other')
|
|
373
|
+
|
|
374
|
+
expect(form.state.errors).toStrictEqual(['At least 3 names are required'])
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
it("should validate all shifted fields when inserting an array field's value", async () => {
|
|
378
|
+
const form = new FormApi({
|
|
379
|
+
defaultValues: {
|
|
380
|
+
names: [{ first: 'test' }, { first: 'test2' }],
|
|
381
|
+
},
|
|
382
|
+
validators: {
|
|
383
|
+
onChange: ({ value }) =>
|
|
384
|
+
value.names.length > 3 ? undefined : 'At least 3 names are required',
|
|
385
|
+
},
|
|
386
|
+
})
|
|
387
|
+
form.mount()
|
|
388
|
+
// Since validation runs through the field, a field must be mounted for that array
|
|
389
|
+
new FieldApi({ form, name: 'names' }).mount()
|
|
390
|
+
|
|
391
|
+
const field1 = new FieldApi({
|
|
392
|
+
form,
|
|
393
|
+
name: 'names[0].first',
|
|
394
|
+
defaultValue: 'test',
|
|
395
|
+
validators: {
|
|
396
|
+
onChange: ({ value }) => value !== 'test' && 'Invalid value',
|
|
397
|
+
},
|
|
398
|
+
})
|
|
399
|
+
field1.mount()
|
|
400
|
+
|
|
401
|
+
expect(field1.state.meta.errors).toStrictEqual([])
|
|
402
|
+
|
|
403
|
+
await form.insertFieldValue('names', 0, { first: 'other' })
|
|
404
|
+
|
|
405
|
+
expect(field1.state.meta.errors).toStrictEqual(['Invalid value'])
|
|
406
|
+
})
|
|
407
|
+
|
|
339
408
|
it("should remove an array field's value", () => {
|
|
340
409
|
const form = new FormApi({
|
|
341
410
|
defaultValues: {
|
|
@@ -348,6 +417,79 @@ describe('form api', () => {
|
|
|
348
417
|
expect(form.getFieldValue('names')).toStrictEqual(['one', 'three'])
|
|
349
418
|
})
|
|
350
419
|
|
|
420
|
+
it("should run onChange validation when removing an array field's value", () => {
|
|
421
|
+
const form = new FormApi({
|
|
422
|
+
defaultValues: {
|
|
423
|
+
names: ['test'],
|
|
424
|
+
},
|
|
425
|
+
validators: {
|
|
426
|
+
onChange: ({ value }) =>
|
|
427
|
+
value.names.length > 1 ? undefined : 'At least 1 name is required',
|
|
428
|
+
},
|
|
429
|
+
})
|
|
430
|
+
form.mount()
|
|
431
|
+
// Since validation runs through the field, a field must be mounted for that array
|
|
432
|
+
new FieldApi({ form, name: 'names' }).mount()
|
|
433
|
+
|
|
434
|
+
form.removeFieldValue('names', 0)
|
|
435
|
+
|
|
436
|
+
expect(form.state.errors).toStrictEqual(['At least 1 name is required'])
|
|
437
|
+
})
|
|
438
|
+
|
|
439
|
+
it("should validate following fields when removing an array field's value", async () => {
|
|
440
|
+
const form = new FormApi({
|
|
441
|
+
defaultValues: {
|
|
442
|
+
names: ['test', 'test2', 'test3'],
|
|
443
|
+
},
|
|
444
|
+
validators: {
|
|
445
|
+
onChange: ({ value }) =>
|
|
446
|
+
value.names.length > 1 ? undefined : 'At least 1 name is required',
|
|
447
|
+
},
|
|
448
|
+
})
|
|
449
|
+
form.mount()
|
|
450
|
+
// Since validation runs through the field, a field must be mounted for that array
|
|
451
|
+
new FieldApi({ form, name: 'names' }).mount()
|
|
452
|
+
|
|
453
|
+
const field1 = new FieldApi({
|
|
454
|
+
form,
|
|
455
|
+
name: 'names[0]',
|
|
456
|
+
defaultValue: 'test',
|
|
457
|
+
validators: {
|
|
458
|
+
onChange: ({ value }) => value !== 'test' && 'Invalid value',
|
|
459
|
+
},
|
|
460
|
+
})
|
|
461
|
+
field1.mount()
|
|
462
|
+
const field2 = new FieldApi({
|
|
463
|
+
form,
|
|
464
|
+
name: 'names[1]',
|
|
465
|
+
defaultValue: 'test2',
|
|
466
|
+
validators: {
|
|
467
|
+
onChange: ({ value }) => value !== 'test2' && 'Invalid value',
|
|
468
|
+
},
|
|
469
|
+
})
|
|
470
|
+
field2.mount()
|
|
471
|
+
const field3 = new FieldApi({
|
|
472
|
+
form,
|
|
473
|
+
name: 'names[2]',
|
|
474
|
+
defaultValue: 'test3',
|
|
475
|
+
validators: {
|
|
476
|
+
onChange: ({ value }) => value !== 'test3' && 'Invalid value',
|
|
477
|
+
},
|
|
478
|
+
})
|
|
479
|
+
field3.mount()
|
|
480
|
+
|
|
481
|
+
expect(field1.state.meta.errors).toStrictEqual([])
|
|
482
|
+
expect(field2.state.meta.errors).toStrictEqual([])
|
|
483
|
+
expect(field3.state.meta.errors).toStrictEqual([])
|
|
484
|
+
|
|
485
|
+
await form.removeFieldValue('names', 1)
|
|
486
|
+
|
|
487
|
+
expect(field1.state.meta.errors).toStrictEqual([])
|
|
488
|
+
expect(field2.state.meta.errors).toStrictEqual(['Invalid value'])
|
|
489
|
+
// This field does not exist anymore. Therefore, its validation should also not run
|
|
490
|
+
expect(field3.state.meta.errors).toStrictEqual([])
|
|
491
|
+
})
|
|
492
|
+
|
|
351
493
|
it("should swap an array field's value", () => {
|
|
352
494
|
const form = new FormApi({
|
|
353
495
|
defaultValues: {
|
|
@@ -355,11 +497,146 @@ describe('form api', () => {
|
|
|
355
497
|
},
|
|
356
498
|
})
|
|
357
499
|
form.mount()
|
|
500
|
+
// Since validation runs through the field, a field must be mounted for that array
|
|
501
|
+
new FieldApi({ form, name: 'names' }).mount()
|
|
502
|
+
|
|
358
503
|
form.swapFieldValues('names', 1, 2)
|
|
359
504
|
|
|
360
505
|
expect(form.getFieldValue('names')).toStrictEqual(['one', 'three', 'two'])
|
|
361
506
|
})
|
|
362
507
|
|
|
508
|
+
it("should run onChange validation when swapping an array field's value", () => {
|
|
509
|
+
const form = new FormApi({
|
|
510
|
+
defaultValues: {
|
|
511
|
+
names: ['test', 'test2'],
|
|
512
|
+
},
|
|
513
|
+
validators: {
|
|
514
|
+
onChange: ({ value }) =>
|
|
515
|
+
value.names.length > 3 ? undefined : 'At least 3 names are required',
|
|
516
|
+
},
|
|
517
|
+
})
|
|
518
|
+
form.mount()
|
|
519
|
+
// Since validation runs through the field, a field must be mounted for that array
|
|
520
|
+
new FieldApi({ form, name: 'names' }).mount()
|
|
521
|
+
expect(form.state.errors).toStrictEqual([])
|
|
522
|
+
|
|
523
|
+
form.swapFieldValues('names', 1, 2)
|
|
524
|
+
|
|
525
|
+
expect(form.state.errors).toStrictEqual(['At least 3 names are required'])
|
|
526
|
+
})
|
|
527
|
+
|
|
528
|
+
it('should run validation on swapped fields', () => {
|
|
529
|
+
const form = new FormApi({
|
|
530
|
+
defaultValues: {
|
|
531
|
+
names: ['test', 'test2'],
|
|
532
|
+
},
|
|
533
|
+
validators: {
|
|
534
|
+
onChange: ({ value }) =>
|
|
535
|
+
value.names.length > 3 ? undefined : 'At least 3 names are required',
|
|
536
|
+
},
|
|
537
|
+
})
|
|
538
|
+
form.mount()
|
|
539
|
+
// Since validation runs through the field, a field must be mounted for that array
|
|
540
|
+
new FieldApi({ form, name: 'names' }).mount()
|
|
541
|
+
|
|
542
|
+
const field1 = new FieldApi({
|
|
543
|
+
form,
|
|
544
|
+
name: 'names[0]',
|
|
545
|
+
defaultValue: 'test',
|
|
546
|
+
validators: {
|
|
547
|
+
onChange: ({ value }) => value !== 'test' && 'Invalid value',
|
|
548
|
+
},
|
|
549
|
+
})
|
|
550
|
+
field1.mount()
|
|
551
|
+
|
|
552
|
+
const field2 = new FieldApi({
|
|
553
|
+
form,
|
|
554
|
+
name: 'names[1]',
|
|
555
|
+
defaultValue: 'test2',
|
|
556
|
+
})
|
|
557
|
+
field2.mount()
|
|
558
|
+
|
|
559
|
+
expect(field1.state.meta.errors).toStrictEqual([])
|
|
560
|
+
expect(field2.state.meta.errors).toStrictEqual([])
|
|
561
|
+
|
|
562
|
+
form.swapFieldValues('names', 0, 1)
|
|
563
|
+
|
|
564
|
+
expect(field1.state.meta.errors).toStrictEqual(['Invalid value'])
|
|
565
|
+
expect(field2.state.meta.errors).toStrictEqual([])
|
|
566
|
+
})
|
|
567
|
+
|
|
568
|
+
it("should move an array field's value", () => {
|
|
569
|
+
const form = new FormApi({
|
|
570
|
+
defaultValues: {
|
|
571
|
+
names: ['one', 'two', 'three'],
|
|
572
|
+
},
|
|
573
|
+
})
|
|
574
|
+
form.mount()
|
|
575
|
+
form.moveFieldValues('names', 1, 2)
|
|
576
|
+
|
|
577
|
+
expect(form.getFieldValue('names')).toStrictEqual(['one', 'three', 'two'])
|
|
578
|
+
})
|
|
579
|
+
|
|
580
|
+
it("should run onChange validation when moving an array field's value", () => {
|
|
581
|
+
const form = new FormApi({
|
|
582
|
+
defaultValues: {
|
|
583
|
+
names: ['test', 'test2'],
|
|
584
|
+
},
|
|
585
|
+
validators: {
|
|
586
|
+
onChange: ({ value }) =>
|
|
587
|
+
value.names.length > 3 ? undefined : 'At least 3 names are required',
|
|
588
|
+
},
|
|
589
|
+
})
|
|
590
|
+
form.mount()
|
|
591
|
+
// Since validation runs through the field, a field must be mounted for that array
|
|
592
|
+
new FieldApi({ form, name: 'names' }).mount()
|
|
593
|
+
|
|
594
|
+
expect(form.state.errors).toStrictEqual([])
|
|
595
|
+
form.moveFieldValues('names', 0, 1)
|
|
596
|
+
|
|
597
|
+
expect(form.state.errors).toStrictEqual(['At least 3 names are required'])
|
|
598
|
+
})
|
|
599
|
+
|
|
600
|
+
it('should run validation on moved fields', () => {
|
|
601
|
+
const form = new FormApi({
|
|
602
|
+
defaultValues: {
|
|
603
|
+
names: ['test', 'test2'],
|
|
604
|
+
},
|
|
605
|
+
validators: {
|
|
606
|
+
onChange: ({ value }) =>
|
|
607
|
+
value.names.length > 3 ? undefined : 'At least 3 names are required',
|
|
608
|
+
},
|
|
609
|
+
})
|
|
610
|
+
form.mount()
|
|
611
|
+
// Since validation runs through the field, a field must be mounted for that array
|
|
612
|
+
new FieldApi({ form, name: 'names' }).mount()
|
|
613
|
+
|
|
614
|
+
const field1 = new FieldApi({
|
|
615
|
+
form,
|
|
616
|
+
name: 'names[0]',
|
|
617
|
+
defaultValue: 'test',
|
|
618
|
+
validators: {
|
|
619
|
+
onChange: ({ value }) => value !== 'test' && 'Invalid value',
|
|
620
|
+
},
|
|
621
|
+
})
|
|
622
|
+
field1.mount()
|
|
623
|
+
|
|
624
|
+
const field2 = new FieldApi({
|
|
625
|
+
form,
|
|
626
|
+
name: 'names[1]',
|
|
627
|
+
defaultValue: 'test2',
|
|
628
|
+
})
|
|
629
|
+
field2.mount()
|
|
630
|
+
|
|
631
|
+
expect(field1.state.meta.errors).toStrictEqual([])
|
|
632
|
+
expect(field2.state.meta.errors).toStrictEqual([])
|
|
633
|
+
|
|
634
|
+
form.swapFieldValues('names', 0, 1)
|
|
635
|
+
|
|
636
|
+
expect(field1.state.meta.errors).toStrictEqual(['Invalid value'])
|
|
637
|
+
expect(field2.state.meta.errors).toStrictEqual([])
|
|
638
|
+
})
|
|
639
|
+
|
|
363
640
|
it('should handle fields inside an array', async () => {
|
|
364
641
|
interface Employee {
|
|
365
642
|
firstName: string
|
|
@@ -1052,6 +1329,35 @@ describe('form api', () => {
|
|
|
1052
1329
|
expect(field.getMeta().errorMap.onChange).toEqual('first name is required')
|
|
1053
1330
|
})
|
|
1054
1331
|
|
|
1332
|
+
it('should validate a single field consistently if touched', async () => {
|
|
1333
|
+
const form = new FormApi({
|
|
1334
|
+
defaultValues: {
|
|
1335
|
+
firstName: '',
|
|
1336
|
+
lastName: '',
|
|
1337
|
+
},
|
|
1338
|
+
})
|
|
1339
|
+
|
|
1340
|
+
const field = new FieldApi({
|
|
1341
|
+
form,
|
|
1342
|
+
name: 'firstName',
|
|
1343
|
+
validators: {
|
|
1344
|
+
onChange: ({ value }) =>
|
|
1345
|
+
value.length > 0 ? undefined : 'first name is required',
|
|
1346
|
+
},
|
|
1347
|
+
defaultMeta: {
|
|
1348
|
+
isTouched: true,
|
|
1349
|
+
},
|
|
1350
|
+
})
|
|
1351
|
+
|
|
1352
|
+
field.mount()
|
|
1353
|
+
form.mount()
|
|
1354
|
+
|
|
1355
|
+
await form.validateField('firstName', 'change')
|
|
1356
|
+
expect(field.getMeta().errorMap.onChange).toEqual('first name is required')
|
|
1357
|
+
await form.validateField('firstName', 'change')
|
|
1358
|
+
expect(field.getMeta().errorMap.onChange).toEqual('first name is required')
|
|
1359
|
+
})
|
|
1360
|
+
|
|
1055
1361
|
it('should show onSubmit errors', async () => {
|
|
1056
1362
|
const form = new FormApi({
|
|
1057
1363
|
defaultValues: {
|
|
@@ -1095,4 +1401,60 @@ describe('form api', () => {
|
|
|
1095
1401
|
await form.handleSubmit()
|
|
1096
1402
|
expect(form.state.errors).toStrictEqual(['first name is required'])
|
|
1097
1403
|
})
|
|
1404
|
+
|
|
1405
|
+
it('should update a nullable object', async () => {
|
|
1406
|
+
const form = new FormApi({
|
|
1407
|
+
defaultValues: {
|
|
1408
|
+
person: null,
|
|
1409
|
+
} as { person: { firstName: string } | null },
|
|
1410
|
+
})
|
|
1411
|
+
|
|
1412
|
+
const field = new FieldApi({
|
|
1413
|
+
form,
|
|
1414
|
+
name: 'person.firstName',
|
|
1415
|
+
})
|
|
1416
|
+
|
|
1417
|
+
field.mount()
|
|
1418
|
+
|
|
1419
|
+
field.setValue('firstName')
|
|
1420
|
+
expect(form.state.values.person?.firstName).toStrictEqual('firstName')
|
|
1421
|
+
})
|
|
1422
|
+
|
|
1423
|
+
it('should update a deep nullable object', async () => {
|
|
1424
|
+
const form = new FormApi({
|
|
1425
|
+
defaultValues: {
|
|
1426
|
+
person: null,
|
|
1427
|
+
} as { person: { nameInfo: { first: string } | null } | null },
|
|
1428
|
+
})
|
|
1429
|
+
|
|
1430
|
+
const field = new FieldApi({
|
|
1431
|
+
form,
|
|
1432
|
+
name: 'person.nameInfo.first',
|
|
1433
|
+
})
|
|
1434
|
+
|
|
1435
|
+
field.mount()
|
|
1436
|
+
|
|
1437
|
+
field.setValue('firstName')
|
|
1438
|
+
expect(form.state.values.person?.nameInfo?.first).toStrictEqual('firstName')
|
|
1439
|
+
})
|
|
1440
|
+
|
|
1441
|
+
it('should update a nullable array', async () => {
|
|
1442
|
+
const form = new FormApi({
|
|
1443
|
+
defaultValues: {
|
|
1444
|
+
persons: null,
|
|
1445
|
+
} as { persons: Array<{ nameInfo: { first: string } }> | null },
|
|
1446
|
+
})
|
|
1447
|
+
|
|
1448
|
+
const field = new FieldApi({
|
|
1449
|
+
form,
|
|
1450
|
+
name: 'persons',
|
|
1451
|
+
})
|
|
1452
|
+
|
|
1453
|
+
field.mount()
|
|
1454
|
+
|
|
1455
|
+
field.pushValue({ nameInfo: { first: 'firstName' } })
|
|
1456
|
+
expect(form.state.values.persons).toStrictEqual([
|
|
1457
|
+
{ nameInfo: { first: 'firstName' } },
|
|
1458
|
+
])
|
|
1459
|
+
})
|
|
1098
1460
|
})
|
package/src/utils.ts
CHANGED