0

我正在使用保存formData到服务器的 rjsf 表单。稍后,用户可以打开相同的表单,它应该会填充之前保存的formData. 但是,在重新填充安装时,我无法选择正确的oneOf/选项。anyOfForm

例如,使用以下代码,我希望second anyOf在安装时预先选择该选项Form

import Form from '@rjsf/core'

function Test() {
  return <Form formData={formData} schema={schema} />
}

const formData = {
  item: 'second',
  secondProp: 'abc',
}

const schema = {
  $id: "https://gitlab.com/feedyou/feedbot-schema/schema/steps/action.json",
  type: "object",

  anyOf: [
    {
      type: "object",
      title: "first",
      properties: {
        // the item prop will be hidden with uiSchema, see below
        item: { type: "string", enum: ["first"], default: ["first"] }, 
        firstProp: { type: "string" }
      }
    },

    {
      type: "object",
      title: "second",
      properties: {
        item: { type: "string", enum: ["first"], default: ["second"] },
        secondProp: { type: "string" }
      }
    }
  ]
};

Codesandbox 演示在这里

疯狂的是,上面提到的代码实际上在我们的项目(CRA 应用程序)中按我想要的方式工作,但我无法在其他任何地方复制这种行为!也就是说,在我的项目中,该second选项确实被预填充,但是当我在新的 CRA 应用程序或 Codesandbox 中尝试它时,第二个选项没有被预选。

所以我有两个问题:
  1. 它怎么可能在我的项目(CRA 应用程序)中工作,但似乎没有其他地方?什么可能导致这种绝望?和的匹配版本rjsfReact不能消除它。
  2. 达到我想要的效果的正确方法是什么?

关于item物业

它是一种用于明确区分不同项目的属性(例如,当两个项目否则将具有相同的属性或根本没有属性时)。它应该用 uiSchema 隐藏(但我在示例中没有这样做以使其更简单)。

我在我正在处理的项目中发现了这种模式,其他开发人员也在使用它。但即使删除它,结果也是一样的(在这个特定示例中):预选在我的项目中有效,但在沙盒或干净的 CRA 项目中无效。

4

0 回答 0