mirror of
https://github.com/LukeHagar/form.git
synced 2025-12-10 12:27:45 +00:00
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:
@@ -101,6 +101,10 @@ export class FieldApi<
|
|||||||
|
|
||||||
this.name = opts.name as any
|
this.name = opts.name as any
|
||||||
|
|
||||||
|
if (opts.defaultValue !== undefined) {
|
||||||
|
this.form.setFieldValue(this.name, opts.defaultValue as never)
|
||||||
|
}
|
||||||
|
|
||||||
this.store = new Store<FieldState<TData>>(
|
this.store = new Store<FieldState<TData>>(
|
||||||
{
|
{
|
||||||
value: this.getValue(),
|
value: this.getValue(),
|
||||||
|
|||||||
@@ -20,6 +20,22 @@ describe('field api', () => {
|
|||||||
expect(field.getValue()).toBe('test')
|
expect(field.getValue()).toBe('test')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should use field default value first', () => {
|
||||||
|
const form = new FormApi({
|
||||||
|
defaultValues: {
|
||||||
|
name: 'test',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const field = new FieldApi({
|
||||||
|
form,
|
||||||
|
defaultValue: 'other',
|
||||||
|
name: 'name',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(field.getValue()).toBe('other')
|
||||||
|
})
|
||||||
|
|
||||||
it('should get default meta', () => {
|
it('should get default meta', () => {
|
||||||
const form = new FormApi()
|
const form = new FormApi()
|
||||||
const field = new FieldApi({
|
const field = new FieldApi({
|
||||||
|
|||||||
@@ -18,13 +18,17 @@ describe('useField', () => {
|
|||||||
const formFactory = createFormFactory<Person>()
|
const formFactory = createFormFactory<Person>()
|
||||||
|
|
||||||
function Comp() {
|
function Comp() {
|
||||||
const form = formFactory.useForm()
|
const form = formFactory.useForm({
|
||||||
|
defaultValues: {
|
||||||
|
firstName: 'FirstName',
|
||||||
|
lastName: 'LastName',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form.Provider>
|
<form.Provider>
|
||||||
<form.Field
|
<form.Field
|
||||||
name="firstName"
|
name="firstName"
|
||||||
defaultValue="FirstName"
|
|
||||||
children={(field) => {
|
children={(field) => {
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
@@ -45,6 +49,47 @@ describe('useField', () => {
|
|||||||
expect(input).toHaveValue('FirstName')
|
expect(input).toHaveValue('FirstName')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should use field default value first', async () => {
|
||||||
|
type Person = {
|
||||||
|
firstName: string
|
||||||
|
lastName: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const formFactory = createFormFactory<Person>()
|
||||||
|
|
||||||
|
function Comp() {
|
||||||
|
const form = formFactory.useForm({
|
||||||
|
defaultValues: {
|
||||||
|
firstName: 'FirstName',
|
||||||
|
lastName: 'LastName',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
return (
|
||||||
|
<form.Provider>
|
||||||
|
<form.Field
|
||||||
|
name="firstName"
|
||||||
|
defaultValue="otherName"
|
||||||
|
children={(field) => {
|
||||||
|
return (
|
||||||
|
<input
|
||||||
|
data-testid="fieldinput"
|
||||||
|
value={field.state.value}
|
||||||
|
onBlur={field.handleBlur}
|
||||||
|
onChange={(e) => field.handleChange(e.target.value)}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</form.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const { getByTestId } = render(<Comp />)
|
||||||
|
const input = getByTestId('fieldinput')
|
||||||
|
expect(input).toHaveValue('otherName')
|
||||||
|
})
|
||||||
|
|
||||||
it('should not validate on change if isTouched is false', async () => {
|
it('should not validate on change if isTouched is false', async () => {
|
||||||
type Person = {
|
type Person = {
|
||||||
firstName: string
|
firstName: string
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ describe('useForm', () => {
|
|||||||
form.provideFormContext()
|
form.provideFormContext()
|
||||||
|
|
||||||
return () => (
|
return () => (
|
||||||
<form.Field name="firstName" defaultValue="">
|
<form.Field name="firstName">
|
||||||
{({ field }: { field: FieldApi<Person, 'firstName'> }) => (
|
{({ field }: { field: FieldApi<Person, 'firstName'> }) => (
|
||||||
<p>{field.state.value}</p>
|
<p>{field.state.value}</p>
|
||||||
)}
|
)}
|
||||||
@@ -81,6 +81,37 @@ describe('useForm', () => {
|
|||||||
expect(queryByText('LastName')).not.toBeInTheDocument()
|
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 () => {
|
it('should handle submitting properly', async () => {
|
||||||
const Comp = defineComponent(() => {
|
const Comp = defineComponent(() => {
|
||||||
const submittedData = ref<{ firstName: string }>()
|
const submittedData = ref<{ firstName: string }>()
|
||||||
|
|||||||
Reference in New Issue
Block a user