我需要ui模式中的某些字段在模式部分

时间:2018-10-28 08:58:52

标签: json reactjs react-jsonschema-forms

我曾经在项目中使用jsonform,但是现在我迁移以做出反应,所以我使用了这个库(react-jsonschema-form)。 在jsonform中,我可以在form部分中有一些不在模式中的字段集。像这样:

{
  "schema":
    {
      "firstName": {
      "type": "string",
      "title": "Is JSON Form useful?",
      }
    },
  "form": [
     {
      "key": "firstName",
      "type": "text",
     }, 
     {
      "title" : "this is non-schema",
      "type": "fieldset"
     }
   ]
}

您可以在jsonschema playground进行测试。 请帮助我执行react-jsonschema-form中的上述代码。 如何拥有不在架构中但我想在uiSchema中显示的某些字段?

react-jsonschema-form也有一个playground。您可以找到一个名为“日期”的字段。它在uiSchema中添加,但在模式部分中不存在。结果中该字段也没有任何显示。我不知道他们为什么不能使用它!

谢谢。

1 个答案:

答案 0 :(得分:1)

日期只是uiSchema的一个示例,操场这次不使用它。在这里我创建了一个示例以帮助理解。

JSONSchema

{
  "title": "An example form",
  "description": "A simple form example",
  "type": "object",
  "required": [
    "firstName",
    "lastName"
  ],
  "properties": {
    "firstName": {
      "type": "string",
      "title": "First name"
    },
    "age": {
      "type": "integer",
      "title": "Age"
    },
    "telephone": {
      "type": "string",
      "title": "Telephone",
      "minLength": 10
    },
    "date": {
      "type": "string",
      "title": "Date"
    }
  }
}

UISchema

{
  "firstName": {
    "ui:autofocus": true,
    "ui:emptyValue": ""
  },
  "age": {
    "ui:widget": "updown",
    "ui:title": "Age of person",
    "ui:description": "(earthian year)"
  },
  "date": {
    "ui:widget": "alt-datetime"
  },
  "telephone": {
    "ui:options": {
      "inputType": "tel"
    }
  }
}

JSONSchema中有四个属性:名字,年龄,电话,日期和UISchema中的四个属性:名字,年龄,电话,日期,它们是相同的.JSONSchema中的每个属性在UISchema中具有一个或更少。在JSONSchema之类的字符串中,在字符串中有一些子选项,例如“ updown”。我们在ui:ui:widget(UISchema)中进行设置,这是我的结果。底部元素是您提到的日期。 enter image description here