Skip to content

Commit 5b74e0d

Browse files
authored
fix: field defaultValue should precedence over form defaultValues (#463)
* fix: field `defaultValue` should precedence over form `defaultValues` * chore: fix type * chore: format
1 parent 119ce68 commit 5b74e0d

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

packages/form-core/src/FieldApi.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ export class FieldApi<
101101

102102
this.name = opts.name as any
103103

104+
if (opts.defaultValue !== undefined) {
105+
this.form.setFieldValue(this.name, opts.defaultValue as never)
106+
}
107+
104108
this.store = new Store<FieldState<TData>>(
105109
{
106110
value: this.getValue(),

packages/form-core/src/tests/FieldApi.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ describe('field api', () => {
2020
expect(field.getValue()).toBe('test')
2121
})
2222

23+
it('should use field default value first', () => {
24+
const form = new FormApi({
25+
defaultValues: {
26+
name: 'test',
27+
},
28+
})
29+
30+
const field = new FieldApi({
31+
form,
32+
defaultValue: 'other',
33+
name: 'name',
34+
})
35+
36+
expect(field.getValue()).toBe('other')
37+
})
38+
2339
it('should get default meta', () => {
2440
const form = new FormApi()
2541
const field = new FieldApi({

packages/react-form/src/tests/useField.test.tsx

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ describe('useField', () => {
1818
const formFactory = createFormFactory<Person>()
1919

2020
function Comp() {
21-
const form = formFactory.useForm()
21+
const form = formFactory.useForm({
22+
defaultValues: {
23+
firstName: 'FirstName',
24+
lastName: 'LastName',
25+
},
26+
})
2227

2328
return (
2429
<form.Provider>
2530
<form.Field
2631
name="firstName"
27-
defaultValue="FirstName"
2832
children={(field) => {
2933
return (
3034
<input
@@ -45,6 +49,47 @@ describe('useField', () => {
4549
expect(input).toHaveValue('FirstName')
4650
})
4751

52+
it('should use field default value first', async () => {
53+
type Person = {
54+
firstName: string
55+
lastName: string
56+
}
57+
58+
const formFactory = createFormFactory<Person>()
59+
60+
function Comp() {
61+
const form = formFactory.useForm({
62+
defaultValues: {
63+
firstName: 'FirstName',
64+
lastName: 'LastName',
65+
},
66+
})
67+
68+
return (
69+
<form.Provider>
70+
<form.Field
71+
name="firstName"
72+
defaultValue="otherName"
73+
children={(field) => {
74+
return (
75+
<input
76+
data-testid="fieldinput"
77+
value={field.state.value}
78+
onBlur={field.handleBlur}
79+
onChange={(e) => field.handleChange(e.target.value)}
80+
/>
81+
)
82+
}}
83+
/>
84+
</form.Provider>
85+
)
86+
}
87+
88+
const { getByTestId } = render(<Comp />)
89+
const input = getByTestId('fieldinput')
90+
expect(input).toHaveValue('otherName')
91+
})
92+
4893
it('should not validate on change if isTouched is false', async () => {
4994
type Person = {
5095
firstName: string

packages/vue-form/src/tests/useForm.test.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('useForm', () => {
6868
form.provideFormContext()
6969

7070
return () => (
71-
<form.Field name="firstName" defaultValue="">
71+
<form.Field name="firstName">
7272
{({ field }: { field: FieldApi<Person, 'firstName'> }) => (
7373
<p>{field.state.value}</p>
7474
)}
@@ -81,6 +81,37 @@ describe('useForm', () => {
8181
expect(queryByText('LastName')).not.toBeInTheDocument()
8282
})
8383

84+
it('should use field default value first', async () => {
85+
type Person = {
86+
firstName: string
87+
lastName: string
88+
}
89+
90+
const formFactory = createFormFactory<Person>()
91+
92+
const Comp = defineComponent(() => {
93+
const form = formFactory.useForm({
94+
defaultValues: {
95+
firstName: 'FirstName',
96+
lastName: 'LastName',
97+
},
98+
})
99+
form.provideFormContext()
100+
101+
return () => (
102+
<form.Field name="firstName" defaultValue="otherName">
103+
{({ field }: { field: FieldApi<Person, 'firstName'> }) => (
104+
<p>{field.state.value}</p>
105+
)}
106+
</form.Field>
107+
)
108+
})
109+
110+
const { findByText, queryByText } = render(Comp)
111+
expect(await findByText('otherName')).toBeInTheDocument()
112+
expect(queryByText('LastName')).not.toBeInTheDocument()
113+
})
114+
84115
it('should handle submitting properly', async () => {
85116
const Comp = defineComponent(() => {
86117
const submittedData = ref<{ firstName: string }>()

0 commit comments

Comments
 (0)