@tanstack/react-form 0.4.1 → 0.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-form",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "Powerful, type-safe forms for React.",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -54,7 +54,7 @@
54
54
  "@tanstack/react-store": "0.1.3",
55
55
  "@tanstack/store": "0.1.3",
56
56
  "use-isomorphic-layout-effect": "^1.1.2",
57
- "@tanstack/form-core": "0.4.1"
57
+ "@tanstack/form-core": "0.5.0"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "react": "^17.0.0 || ^18.0.0",
@@ -3,7 +3,7 @@ import * as React from 'react'
3
3
  import { render, waitFor } from '@testing-library/react'
4
4
  import userEvent from '@testing-library/user-event'
5
5
  import '@testing-library/jest-dom'
6
- import { createFormFactory } from '..'
6
+ import { type FormApi, createFormFactory } from '..'
7
7
  import { sleep } from './utils'
8
8
 
9
9
  const user = userEvent.setup()
@@ -373,4 +373,80 @@ describe('useField', () => {
373
373
  await waitFor(() => getByText(error))
374
374
  expect(getByText(error)).toBeInTheDocument()
375
375
  })
376
+
377
+ it('should preserve value when preserve value property is true', async () => {
378
+ type Person = {
379
+ firstName: string
380
+ lastName: string
381
+ }
382
+ const formFactory = createFormFactory<Person, unknown>()
383
+ let form: FormApi<Person, unknown> | null = null
384
+ function Comp() {
385
+ form = formFactory.useForm()
386
+ return (
387
+ <form.Provider>
388
+ <form.Field
389
+ name="firstName"
390
+ defaultValue="hello"
391
+ preserveValue={true}
392
+ children={(field) => {
393
+ return (
394
+ <input
395
+ data-testid="fieldinput"
396
+ value={field.state.value}
397
+ onBlur={field.handleBlur}
398
+ onChange={(e) => field.handleChange(e.target.value)}
399
+ />
400
+ )
401
+ }}
402
+ />
403
+ </form.Provider>
404
+ )
405
+ }
406
+
407
+ const { getByTestId, unmount, rerender } = render(<Comp />)
408
+ const input = getByTestId('fieldinput')
409
+ expect(input).toHaveValue('hello')
410
+ await user.type(input, 'world')
411
+ unmount()
412
+ expect(form!.fieldInfo['firstName']).toBeDefined()
413
+ })
414
+
415
+ it('should not preserve value when preserve value property is false', async () => {
416
+ type Person = {
417
+ firstName: string
418
+ lastName: string
419
+ }
420
+ const formFactory = createFormFactory<Person, unknown>()
421
+ let form: FormApi<Person, unknown> | null = null
422
+ function Comp() {
423
+ form = formFactory.useForm()
424
+ return (
425
+ <form.Provider>
426
+ <form.Field
427
+ name="firstName"
428
+ defaultValue="hello"
429
+ preserveValue={false}
430
+ children={(field) => {
431
+ return (
432
+ <input
433
+ data-testid="fieldinput"
434
+ value={field.state.value}
435
+ onBlur={field.handleBlur}
436
+ onChange={(e) => field.handleChange(e.target.value)}
437
+ />
438
+ )
439
+ }}
440
+ />
441
+ </form.Provider>
442
+ )
443
+ }
444
+
445
+ const { getByTestId, unmount } = render(<Comp />)
446
+ const input = getByTestId('fieldinput')
447
+ expect(input).toHaveValue('hello')
448
+ unmount()
449
+ const info = form!.fieldInfo
450
+ expect(Object.keys(info)).toHaveLength(0)
451
+ })
376
452
  })