使用anyOf来查看对象名称?

时间:2019-05-16 22:10:38

标签: javascript reactjs react-jsonschema-forms

我在这里有一个架构,我想在其中进行下拉菜单,选择一个选项,然后从那里开始-根据选择,出现不同的选项;全部嵌套在数组中以具有多个。

我注意到,当我填充虚拟数据时,输出json不会存储所选选项的名称

所以data.json看起来像这样:

{
  "page1": [
    {
      "imageOptions": {
        "imageHeightType": "vh",
        "imageHeight": 50
      },
      "textboxArea": {
        "headerText": "Header for selection1",
        "headingTag": "h1",
        "textBoxOpacity": 15
      }
    },
    {
      "content": "This is a complety different selection, yet there is no name to tell the difference between these two difference objects"
    }
  ]
}

如您所见,没有对象可以将这两个不同的项目包装在page1数组中-理想情况是:

{
  "page1": [
    {
     // Title of object goes here from schema
      "imageOptions": {
        "imageHeightType": "vh",
        "imageHeight": 50
      },
      "textboxArea": {
        "headerText": "Header for selection1",
        "headingTag": "h1",
        "textBoxOpacity": 15
      }
    },
    {
      // Title of object goes here from schema
      "content": "This is a completely different selection, yet there is no name to tell the difference between these two difference objects"
    }
  ]
}

有没有办法做到这一点?我看了看AnyOf的文档,但是运气不高。对React-JsonSchema-Forms来说是相当新的。

以下是我当前的架构:

{
  "type": "object",
  "properties": {
    "page1": {
      "type": "array",
      "items": {
        "type": "object",
        "anyOf": [
          {
            "title": "Full Width Image",
            "type": "object",
            "properties": {
              "imageOptions": {
                "type": "object",
                "title": "Image",
                "properties": {
                  "image": {
                    "type": "string",
                    "title": "Image",
                    "format": "data-url"
                  },
                  "imageHeightType": {
                    "enum": [
                      "px",
                      "vh"
                    ]
                  },
                  "imageHeight": {
                    "type": "number",
                    "title": "Image Height"
                  }
                }
              },
              "textboxArea": {
                "type": "object",
                "title": "Textbox Area",
                "properties": {
                  "headerText": {
                    "type": "string",
                    "title": "Heading Text"
                  },
                  "headingTag": {
                    "enum": [
                      "h1",
                      "h2",
                      "h3"
                    ]
                  },
                  "imageText": {
                    "type": "string",
                    "title": "Body Text"
                  },
                  "textboxPosition": {
                    "title": "Textbox Position",
                    "enum": [
                      "Left",
                      "Center",
                      "Right"
                    ]
                  },
                  "textboxColor": {
                    "title": "Color of Textbox Area",
                    "type": "string"
                  },
                  "textBoxOpacity": {
                    "title": "Textbox Opacity %",
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 100,
                    "multipleOf": 5
                  }
                }
              }
            }
          },
          {
            "title": "Custom Block",
            "type": "object",
            "properties": {
              "content": {
                "type": "string"
              }
            }
          }
        ]
      }
    }
  }
}

如果可以帮助理解我的问题,还Link到在线模式编辑器

1 个答案:

答案 0 :(得分:1)

为什么不只是向每个对象添加类似名称的属性?然后,您可以根据需要隐藏/禁用它:

模式:

"anyOf": [
    {
        "title": "Full Width Image",
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "default": "fullWidthImage"
            },
            "imageOptions": {
                "type": "object",
                "title": "Image",
                "properties": {...}
                ...
            }
            ...
        }
    },
    {
        "title": "Custom Block",
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "default": "custom"
            },
            "content": {
                "type": "string"
            }
        }
    }
]

uiSchema:

{
    "page1": {
    "items": {
        "name": {
            "ui:widget": "hidden"
        },
        "imageOptions": {...},
        ...
    }
}

formData然后应如下所示:

{
    "page1": [
        {
            "name": "fullWidthImage",
            "imageOptions": {
                "imageHeightType": "vh",
                "imageHeight": 50
            },
            "textboxArea": {
                "headerText": "Header for selection1",
                "headingTag": "h1",
                "textBoxOpacity": 15
            }
        },
        {
            "name": "custom",
            "content": "This is a complety different selection, yet there is no name to tell the difference between these two difference objects"
        }
    ]
}