0

我在这里有一个模式,我想在其中有一个下拉菜单,以选择一个选项,然后从那里 - 根据选择不同的选项出现;所有都嵌套在一个数组中以具有多个。

我注意到,当我填写虚拟数据时,输出 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"
              }
            }
          }
        ]
      }
    }
  }
}

如果有助于理解我的问题,还可以链接到在线模式编辑器

4

1 回答 1

1

Why not just add a name-like property to each object? You can then hide/disable it if you want:

schema:

"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 then should look like this:

{
    "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"
        }
    ]
}
于 2019-05-18T19:59:02.273 回答