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

合并代码后,setFieldsValue 会重置部分其他字段 #3795

Closed
3 tasks done
C-racker opened this issue Apr 28, 2024 · 5 comments · Fixed by #3815
Closed
3 tasks done

合并代码后,setFieldsValue 会重置部分其他字段 #3795

C-racker opened this issue Apr 28, 2024 · 5 comments · Fixed by #3815

Comments

@C-racker
Copy link

C-racker commented Apr 28, 2024

⚠️ IMPORTANT ⚠️ Please check the following list before proceeding. If you ignore this issue template, your issue will be directly closed.

  • Read the docs.
  • Make sure the code is up to date. (Some bugs have been fixed in the latest code)
  • This is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.

Describe the bug

合并 #3775 这个代码后,setFieldsValue 会重置其他字段

Reproduction

System Info

  • Operating System: windows
  • Node version: 20
  • pnpm version: 0
@doraemonxxx
Copy link
Contributor

Hi, what do you mean other field is reset? can you please post the codes here? thanks

@C-racker
Copy link
Author

C-racker commented Apr 30, 2024

Hi, what do you mean other field is reset? can you please post the codes here? thanks

复现链接:https://codesandbox.io/p/github/vbenjs/vue-vben-admin/csb-zwhwkk/draft/mystifying-newton?file=%2Fsrc%2Fviews%2Fdemo%2Fform%2FRuleForm.vue%3A248%2C19

设置了 defaultValue 后,再次调用 setFieldsValue 会使用默认值覆盖,应该忽略已有值才对。

操作步骤,修改字段1,然后点击设置表单值会复现。

image

@electroluxcode
Copy link
Contributor

electroluxcode commented Apr 30, 2024

emm,怎么说呢。其实这是一个抉择问题。你可以看到 在这个pr修改前的源码,有着一个在 catch 中的判断。大意也是说取得 在setField中不存在的key然后赋予默认值

// key not exist
if (isDef(defaultValueRef.value[nestKey])) {
  unref(formModel)[nestKey] = cloneDeep(unref(defaultValueRef.value[nestKey]));
}

因此我当时提交这个pr的时候想着是保持 key not exist 的一致性然后提了这个pr。然后可以回答一下你为什么pr前后代码的行为不一致。原因是 在这个pr前 关于 key not exist 的逻辑处于catch的代码块中,然后永远不会被触发。。在我提了那个pr后,那个 key not exist 的逻辑能够走通了。。。。

然后关于你想要达到的效果,其实解决方法有两个。在 src\components\Form\src\hooks\useFormEvents.ts 文件中

  • // key not exist 之后的判断逻辑丢掉
  • 源码 78行 fields.forEach((key) => { 替换成 values.forEach((key) => {

@zwtvip
Copy link
Contributor

zwtvip commented May 7, 2024

@electroluxcode 我觉得设置哪个值就给哪个值赋值比较好点吧,看群里有人吐槽了,大佬要不要再提个PR

@electroluxcode
Copy link
Contributor

@electroluxcode 我觉得设置哪个值就给哪个值赋值比较好点吧,看群里有人吐槽了,大佬要不要再提个PR

ok,我觉得这段语义确实应该是设置哪个值就给哪个值赋值比较好。但是重置成默认值也是有需求的,目前的代码是将设置默认值和赋值耦合在一起了。我看看怎么把这两者分离出来,等一下提个pr吧

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