Skip to content

Commit

Permalink
fix: user input now works
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Jul 3, 2023
1 parent 4a70b82 commit 2f7ec1d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
11 changes: 10 additions & 1 deletion packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { DeepKeys, DeepValue, RequiredByKey, Updater } from './utils'
import type { FormApi, ValidationError } from './FormApi'
import { Store } from '@tanstack/store'
import { setBy } from './utils'

export type ValidationCause = 'change' | 'blur' | 'submit'

Expand Down Expand Up @@ -187,7 +188,15 @@ export class FieldApi<TData, TFormData> {
setValue = (
updater: Updater<TData>,
options?: { touch?: boolean; notify?: boolean },
) => this.form.setFieldValue(this.name, updater as any, options)
) => {
this.form.setFieldValue(this.name, updater as any, options)
this.store.setState((prev) => {
return {
...prev,
value: updater as any,
}
})
}

getMeta = (): FieldMeta => this.form.getFieldMeta(this.name)
setMeta = (updater: Updater<FieldMeta>) =>
Expand Down
39 changes: 39 additions & 0 deletions packages/react-form/src/tests/createFormFactory.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { render } from '@testing-library/react'
import '@testing-library/jest-dom'
import * as React from 'react'
import { createFormFactory } from '..'

describe('createFormFactory', () => {
it('should allow default values to be set', async () => {
type Person = {
firstName: string
lastName: string
}

const formFactory = createFormFactory<Person>({
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
},
})

function Comp() {
const form = formFactory.useForm({})

return (
<form.Provider>
<form.Field
name="firstName"
children={(field) => {
return <p>{field.getInputProps().value}</p>
}}
/>
</form.Provider>
)
}

const { findByText, queryByText } = render(<Comp />)
expect(await findByText('FirstName')).toBeInTheDocument()
expect(queryByText('LastName')).not.toBeInTheDocument()
})
})
5 changes: 0 additions & 5 deletions packages/react-form/src/tests/test.test.tsx

This file was deleted.

40 changes: 40 additions & 0 deletions packages/react-form/src/tests/useForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import '@testing-library/jest-dom'
import * as React from 'react'
import { createFormFactory } from '..'

const user = userEvent.setup()

describe('useForm', () => {
it('preserves field state', async () => {
type Person = {
firstName: string
lastName: string
}

const formFactory = createFormFactory<Person>()

function Comp() {
const form = formFactory.useForm()

return (
<form.Provider>
<form.Field
name="firstName"
defaultValue={""}
children={(field) => {
return <input data-testid="fieldinput" {...field.getInputProps()} />
}}
/>
</form.Provider>
)
}

const { getByTestId, queryByText } = render(<Comp />)
const input = getByTestId("fieldinput");
expect(queryByText('FirstName')).not.toBeInTheDocument()
await user.type(input, "FirstName")
expect(input).toHaveValue("FirstName")
})
})

0 comments on commit 2f7ec1d

Please sign in to comment.