vue-vben-admin icon indicating copy to clipboard operation
vue-vben-admin copied to clipboard

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

Open C-racker opened this issue 9 months ago • 3 comments

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

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

Describe the bug

合并 https://github.com/vbenjs/vue-vben-admin/pull/3775 这个代码后,setFieldsValue 会重置其他字段

Reproduction

System Info

  • Operating System: windows
  • Node version: 20
  • pnpm version: 0

C-racker avatar Apr 28 '24 02:04 C-racker

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

doraemonxxx avatar Apr 28 '24 11:04 doraemonxxx

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

C-racker avatar Apr 30 '24 06:04 C-racker

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) => {

electroluxcode avatar Apr 30 '24 17:04 electroluxcode

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

zwtvip avatar May 07 '24 14:05 zwtvip

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

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

electroluxcode avatar May 08 '24 01:05 electroluxcode