fix: field defaultValue should precedence over form defaultValues (#463)

* fix: field `defaultValue` should precedence over form `defaultValues`

* chore: fix type

* chore: format
This commit is contained in:
☕️ mino
2023-10-17 06:55:30 -05:00
committed by GitHub
parent 119ce68903
commit 5b74e0d29c
4 changed files with 99 additions and 3 deletions

View File

@@ -68,7 +68,7 @@ describe('useForm', () => {
form.provideFormContext()
return () => (
<form.Field name="firstName" defaultValue="">
<form.Field name="firstName">
{({ field }: { field: FieldApi<Person, 'firstName'> }) => (
<p>{field.state.value}</p>
)}
@@ -81,6 +81,37 @@ describe('useForm', () => {
expect(queryByText('LastName')).not.toBeInTheDocument()
})
it('should use field default value first', async () => {
type Person = {
firstName: string
lastName: string
}
const formFactory = createFormFactory<Person>()
const Comp = defineComponent(() => {
const form = formFactory.useForm({
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
},
})
form.provideFormContext()
return () => (
<form.Field name="firstName" defaultValue="otherName">
{({ field }: { field: FieldApi<Person, 'firstName'> }) => (
<p>{field.state.value}</p>
)}
</form.Field>
)
})
const { findByText, queryByText } = render(Comp)
expect(await findByText('otherName')).toBeInTheDocument()
expect(queryByText('LastName')).not.toBeInTheDocument()
})
it('should handle submitting properly', async () => {
const Comp = defineComponent(() => {
const submittedData = ref<{ firstName: string }>()