Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): nest array remove initial-value logic in destory #4052

Closed
wants to merge 1 commit into from

Conversation

charlzyx
Copy link
Contributor

@charlzyx charlzyx commented Dec 28, 2023

Before submitting a pull request, please make sure the following is done...

  • Ensure the pull request title and commit message follow the Commit Specific in English.
  • Fork the repo and create your branch from master or formily_next.
  • If you've added code that should be tested, add tests!
  • If you've changed APIs, update the documentation.
  • Ensure the test suite passes (npm test).
  • Make sure your code lints (npm run lint) - we've done our best to make sure these rules match our internal linting guidelines.

Please do not delete the above content


try fix #4024

What have you changed?

@charlzyx charlzyx changed the title fix(core): patchFieldStates#destory should forceClear by initialValue fix(core): nest array remove initial-value logic in destory Dec 28, 2023
Copy link
Contributor Author

@charlzyx charlzyx left a comment

Choose a reason for hiding this comment

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

那么问题来了,是否应该在组件销毁的时候重置 schema 上所声明的 initialValue呢? 在我的部分使用场景中,其实有跟 #4024 楼主类似的疑惑, 我使用 form 上的 initialValues 是期望 reset 的时候能重置回去整个表单的数据; 如果 schema 上配置的 default 即 initialValue 对表单的 initialValues 产生了影响,还是是非常费解的。 所以我给出一下总结和建议

  1. 使用原始方案,对form.initialValues 进行更新
    2.按照我的方案,如果有删除,那么对有 schema 上进行 initalValue(default)标注的 path 所对应的 form.initialValues 中的字段进行删除
  2. 添加一个类似 IFormMergeStrategy 的属性来让用户决定如何判断
  3. 重新添加一个 freeze 初始值,这将是一个不会变的 form 初始值,以方便用户在重置的时候直接 reset 到这个最初的,不会被改变的初始值

@@ -514,6 +514,7 @@ test('array field remove memo leak', async () => {
expect(initialValuesChange).toBeCalledTimes(0)
})

// add sandbox https://codesandbox.io/p/devbox/lingering-violet-jwr565
test('nest array remove', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

场景一:嵌套数组删除逻辑测试代码, 其中深层的数组的 initialValue 属性会改变 form.initialValues 值,针对这个测试, 我补充了一个 sandbox

expect(
form.initialValues.metrics?.[1]?.content?.[0]?.attr
).not.toBeUndefined()
})


test('nest array remove for #4024', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

情况二:针对 #4024 中反馈的问题, 我补充了一个测试用例

if (forceClear) {
form.deleteValuesIn(path)
}
// 在 schema 上有定义 initialValue (JSX prop name: default)
Copy link
Contributor Author

@charlzyx charlzyx Dec 28, 2023

Choose a reason for hiding this comment

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

当前方案: 根据 schema 上的initialValues,在销毁时对 form.initialValues 进行现场恢复

@@ -588,11 +589,84 @@ test('nest array remove', async () => {
expect(form.fields['metrics.0.content.0.attr']).not.toBeUndefined()
await metrics.remove(1)
expect(form.fields['metrics.0.content.0.attr']).not.toBeUndefined()
// TODO!! 测试不通过
Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里是按照我当前的修复方案,form.initialValues 会被重置, 所以这个测试语句不通过

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@charlzyx
Copy link
Contributor Author

charlzyx commented Dec 29, 2023

rebase 玩坏了, 这里重开 #4053

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.

[Bug Report] array 类型下子字段也为 array类型时,某些场景会导致添加item 异常
2 participants