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

在 getDefaultFormState 的初始化逻辑中,期望 merge array类型的字段时,如果找不到json scheme中定义的default值,能直接使用原本的formData,而不是覆盖成一个空值 #358

Open
koalayuan0607 opened this issue May 21, 2024 · 3 comments

Comments

@koalayuan0607
Copy link

vue和ui框架

vue2、ui框架element 、vjsf 使用版本说明:1.19.0

问题描述

json object会丢失array类型的字段值:merge逻辑判断有问题

如何复现

我有以下业务场景:在下面列举的 rawForm 和 schema中,其中simpleName 展示默认的 input, jsonEditorData 是个复杂的object,并且里面的key是不固定的,schema中我也未定义下一层级的properties内容,在我的业务场景中 以下情况(类型为复杂 object,但却没有下层的properties)我会使用自定义的 ui:field 直接展示一个jsoneditor的编辑器(如下图1),但实际上 vue-json-schema-form 会在初始化 执行 getDefaultFormState 逻辑的时候,把下面 array类型字段 subs的值给覆盖掉(如下图2)

const rawForm = {
  simpleName: 'simple name',
  jsonEditorData: {
    name: 'mock name',
    description: 'mock description',
    subs: [
      {
        name: 'mock name',
        description: 'mock description'
      },
      {
        name: 'mock name',
        description: 'mock description'
      }
    ]
  }
}

const schema = {
  type: 'object',
  properties: {
    simpleName: {
      type: 'string'
    },
    jsonEditorData: {
      type: 'object'
    }
  }
}

image

image

期望的结果

期望数据能正常展示上图1的效果。看了下代码,修改以下逻辑即可:
code path:packages/lib/utils/schema/getDefaultFormState.js

function mergeDefaultsWithFormData(defaults, formData) {
    if (Array.isArray(formData)) {
        if (!Array.isArray(defaults)) {
            console.warn('无效的formData,已覆盖数据', formData);
            return defaults;
        }
        ...
    }
   ...
}

把上述逻辑中的 return defaults; 修改为 return formData; 即可满足期望

vu3 版本(https://www.npmjs.com/package/@lljj/vue3-form-element)中有同样问题,如果采纳了上述意见,希望也能同步到vue3的版本 🙏

@lljj-x
Copy link
Owner

lljj-x commented May 23, 2024

看起来是你subs定义的schema 是object 而不是 array 吧?

@lljj-x
Copy link
Owner

lljj-x commented May 23, 2024

明白你的意思了,要考虑下整体怎么合并更合适

@YamMangoTea
Copy link

我也在@lljj/vue3-form-element 遇到了同样的问题

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

No branches or pull requests

3 participants