Skip to content

Conversation

@MadCcc
Copy link
Member

@MadCcc MadCcc commented Mar 2, 2022

@vercel
Copy link

vercel bot commented Mar 2, 2022

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

🔍 Inspect: https://vercel.com/react-component/field-form/D38F1S4YPSn8dBunnv839NvPMogU
✅ Preview: https://field-form-git-fix-initialvalues-react-component.vercel.app

@codecov
Copy link

codecov bot commented Mar 2, 2022

Codecov Report

Merging #377 (8e185ab) into master (6170663) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 8e185ab differs from pull request most recent head bf18a1e. Consider uploading reports for the commit bf18a1e to get more accurate results

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #377   +/-   ##
=======================================
  Coverage   99.90%   99.90%           
=======================================
  Files          14       15    +1     
  Lines        1035     1051   +16     
  Branches      221      224    +3     
=======================================
+ Hits         1034     1050   +16     
  Misses          1        1           
Impacted Files Coverage Δ
src/useForm.ts 99.74% <100.00%> (+<0.01%) ⬆️
src/utils/cloneDeep.ts 100.00% <100.00%> (ø)
src/utils/valueUtil.ts 100.00% <100.00%> (ø)

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 6170663...bf18a1e. Read the comment docs.

const namePath = entity.getNamePath();

const defaultValue = isListField ? undefined : getValue(this.initialValues, namePath);
const defaultValue = isListField ? undefined : this.getInitialValue(namePath);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里使用有深拷贝的方法获取 defaultValue,因为 this.initialValues 里存的是浅拷贝,直接浅拷贝过来会被赋值行为修改原值。

const recursive = isObject(prevValue) && isObject(value);
newStore[key] = recursive ? internalSetValues(prevValue, value || {}) : value;

newStore[key] = recursive ? internalSetValues(prevValue, value || {}) : cloneDeep(value); // Clone deep for arrays
Copy link
Member Author

@MadCcc MadCcc Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的深拷贝是针对 array 的。之前的做法是把 array 看作 object 处理,但是实际上处理逻辑并不一样,数组应该被直接替换,而不是比对每一个元素进行赋值

this.initialValues = initialValues || {};
if (init) {
this.store = setValues({}, initialValues, this.store);
this.store = setValues({}, this.store, initialValues);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修改了一下顺序,防止 initialValues 被覆盖。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是其实 this.store 是没用的?初始化的时候 this.store 应该是空的。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

理论上是的,但我不太确定。
这个方法确实是组件第一次渲染时 init 才会传 true

@MadCcc MadCcc merged commit 084e293 into master Mar 3, 2022
@delete-merged-branch delete-merged-branch bot deleted the fix/initialValues branch March 3, 2022 03:15
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.

3 participants