Skip to content

Commit 23e64e0

Browse files
Miles-hxyeiinu
andauthored
fix(FormItem): 修复initialValue属性初始化未被正确加入到Formstore中导致的问题 (#2247)
* fix(FormItem): 修复initialValue属性初始化未被正确加入到Formstore中导致的问题 * fix: formItem与form初始值同时设置情况下的处理 * chore: delete test code * chore: form增加测试用例覆盖 * fix: pr * chore: delete useless * fix: add unit test && submit bug --------- Co-authored-by: Eiinu <[email protected]>
1 parent f824831 commit 23e64e0

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/packages/form/__tests__/form.spec.tsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,62 @@ test('form set initialValues', () => {
2222
)
2323
})
2424

25+
test('formItem set initialValue', () => {
26+
const { container } = render(
27+
<Form>
28+
<Form.Item name="username" initialValue="NutUI-React">
29+
<Input />
30+
</Form.Item>
31+
</Form>
32+
)
33+
expect(container.querySelector('.nut-input-native')).toHaveValue(
34+
'NutUI-React'
35+
)
36+
})
37+
38+
test('Both form and formItem set initialValue(s)', () => {
39+
const { container } = render(
40+
<Form initialValues={{ username: 'NutUI-React-Form' }}>
41+
<Form.Item name="username" initialValue="NutUI-React-FormItem">
42+
<Input />
43+
</Form.Item>
44+
</Form>
45+
)
46+
expect(container.querySelector('.nut-input-native')).toHaveValue(
47+
'NutUI-React-Form'
48+
)
49+
})
50+
test('Both form and formItem set initialValue(s) to submit', async () => {
51+
const handleSubmit = vi.fn()
52+
const { container } = render(
53+
<Form
54+
initialValues={{ username: 'NutUI-React-Form', age: 18 }}
55+
onFinish={handleSubmit}
56+
>
57+
<Form.Item name="username" label="UserName" initialValue="NutUI-React">
58+
<Input />
59+
</Form.Item>
60+
<Form.Item name="age" label="Age" initialValue="30">
61+
<Input />
62+
</Form.Item>
63+
<Form.Item name="phone" label="Phone" initialValue="123456">
64+
<Input />
65+
</Form.Item>
66+
</Form>
67+
)
68+
const form = container.querySelector('form') as Element
69+
fireEvent.submit(form)
70+
await waitFor(() => {
71+
expect(handleSubmit).toBeCalled()
72+
expect(handleSubmit).toBeCalledWith(
73+
expect.objectContaining({
74+
username: 'NutUI-React-Form',
75+
age: 18,
76+
phone: '123456',
77+
})
78+
)
79+
})
80+
})
2581
test('form validateTrigger', async () => {
2682
const { container, rerender } = render(
2783
<Form>

src/packages/formitem/formitem.taro.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ export class FormItem extends React.Component<
7373
}
7474

7575
componentDidMount() {
76+
// Form设置initialValues时的处理
77+
const { store = {}, setInitialValues } = this.context.getInternal(SECRET)
78+
if (
79+
this.props.initialValue &&
80+
this.props.name &&
81+
!Object.keys(store).includes(this.props.name)
82+
) {
83+
setInitialValues(
84+
{ ...store, [this.props.name]: this.props.initialValue },
85+
true
86+
)
87+
}
7688
// 注册组件实例到FormStore
7789
const { registerField, registerUpdate } = this.context.getInternal(SECRET)
7890
this.cancelRegister = registerField(this)
@@ -98,6 +110,7 @@ export class FormItem extends React.Component<
98110
if (children?.props?.defaultValue) {
99111
console.warn('通过 initialValue 设置初始值')
100112
}
113+
101114
const fieldValue = getFieldValue(name)
102115
const controlled = {
103116
...children.props,

src/packages/formitem/formitem.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ export class FormItem extends React.Component<
7373
}
7474

7575
componentDidMount() {
76+
// Form设置initialValues时的处理
77+
const { store = {}, setInitialValues } = this.context.getInternal(SECRET)
78+
if (
79+
this.props.initialValue &&
80+
this.props.name &&
81+
!Object.keys(store).includes(this.props.name)
82+
) {
83+
setInitialValues(
84+
{ ...store, [this.props.name]: this.props.initialValue },
85+
true
86+
)
87+
}
7688
// 注册组件实例到FormStore
7789
const { registerField, registerUpdate } = this.context.getInternal(SECRET)
7890
this.cancelRegister = registerField(this)

0 commit comments

Comments
 (0)