Skip to content

Conversation

zombieJ
Copy link
Member

@zombieJ zombieJ commented Apr 7, 2020

resolve ant-design/ant-design#21816

在动态 Field,Form level 的 initialValues 的确比较乏力。Field 添加回来 initialValue,遵循以下规则:

  1. Field 的 initialValue 同 Form 的 initialValues 冲突时
    • Field 的 initialValue 不生效并 warning
  2. 多个 Field 同时设置 initialValue
    • initialValue 不生效并 warning
  3. Field 的 initialValue 只 Field 第一次 mount 时才生效
    • 非受控,只能通过 resetFields 重置
      • 如果满足 1,则以 Form initialValues 为准
      • 如果满足 2,则不生效
    • 当值已经存在于 store 中,mount Field 不应该重置值

@vercel
Copy link

vercel bot commented Apr 7, 2020

This pull request is being automatically deployed with ZEIT Now (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://zeit.co/react-component/field-form/3xte27j8x
✅ Preview: https://field-form-git-initialvalues.react-component.now.sh

@codecov
Copy link

codecov bot commented Apr 7, 2020

Codecov Report

Merging #111 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #111   +/-   ##
=======================================
  Coverage   99.87%   99.88%           
=======================================
  Files          13       13           
  Lines         818      862   +44     
  Branches      177      180    +3     
=======================================
+ Hits          817      861   +44     
  Misses          1        1           
Impacted Files Coverage Δ
src/Field.tsx 100.00% <ø> (ø)
src/FieldContext.ts 100.00% <ø> (ø)
src/List.tsx 100.00% <ø> (ø)
src/utils/validateUtil.ts 100.00% <ø> (ø)
src/useForm.ts 99.69% <100.00%> (+0.04%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5b97316...43e22b2. Read the comment docs.

@afc163
Copy link
Member

afc163 commented Apr 7, 2020

多个 Field 同时设置 initialValue 时 ,initialValue 不生效并 warning

这个是啥意思?

@zombieJ
Copy link
Member Author

zombieJ commented Apr 7, 2020

这个是啥意思?

相同 name 设置不同的 initialValue

<Form>
  <Field name="test" initialValue="light">
    <Input />
  </Field>
  <Field name="test" initialValue="bamboo">
    <Input />
  </Field>
</Form>

@afc163
Copy link
Member

afc163 commented Apr 7, 2020

<Form>
  {this.state.condition ? <Form.Item name="test" initialValue="111" /> : <Form.Item name="test" initialValue="222" />}
</Form>

这样的场景会是如何表现,condition 变化的情况下。

@zombieJ
Copy link
Member Author

zombieJ commented Apr 7, 2020

这样的场景会是如何表现,condition 变化的情况下。

第一次 mount 的 Field 的 initialValue 为准,之后切换不变。重置时以当时存在的 initialValue 为准,如果存在多个则不生效且 warning。

@afc163
Copy link
Member

afc163 commented Apr 7, 2020

还有一种情况。

<Form initialValues={{ name: 'text' }}>
  {this.state.condition ? <Form.Item name="test" /> : <Form.Item name="test" />}
</Form>

condition 从 true 变成 false 后,控件显示空还是 text?期望应该是 text

@zombieJ
Copy link
Member Author

zombieJ commented Apr 7, 2020

condition 从 true 变成 false 后,控件显示空还是 text?期望应该是 text

Form 的 initialValues 优先级最高,显示的是 text

  1. Field 的 initialValue 同 Form 的 initialValues 冲突时
    • Field 的 initialValue 不生效并 warning

这里 Field 的不生效,但是 Form 的是生效的。Form 的 initialValues 永远是 single source of truth。

@zombieJ zombieJ merged commit 09b570a into master Apr 7, 2020
@delete-merged-branch delete-merged-branch bot deleted the initialValues branch April 7, 2020 09:43
@yoyo837
Copy link
Member

yoyo837 commented Apr 7, 2020

就等这个啦 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

antd v4 add initialValue support on Form.Item like v3
3 participants