0

如何根据 React Json Schema 中另一个字段的更改来更新一个字段?下面是我的模式对象。

为了简化用例,假设我有一个国家下拉字段默认设置为加拿大,货币文本字段为空。如果国家/地区发生变化,货币文本字段应显示新元。

{
    type: 'object',
    properties: {
        country: {
            title: 'Country',
            type: 'string',
            enum: ['Canada', 'Singapore', ..., 'United States'],
            default: 'Canada',
        },
        currency: {
            type: 'string',
            default: '',
        },
    },
    dependencies: {
        country: {
            properties: {
                currency: {
                    const: 'SG',
                },
            },
        },
    },
}
4

1 回答 1

0

在伪代码中,“如果财产国家存在且其值为加拿大,则货币为加元;如果财产国家存在且其值为 SG……”

https://json-schema.org/understanding-json-schema/reference/conditionals.html#if-then-else

因此:

{
  "type": "object",
  "required": [ "countries", "currency" ],
  "allOf": [
    {
      "if": { "properties": { "countries": { "const": "Canada" } },
      "else": { "properties": { "currency": { "const": "CAD" } } },
    },
    {
      "if": { "properties": { "countries": { "const": "Singapore" } },
      "else": { "properties": { "currency": { "const": "SG" } } },
    },
    ...
  ]
}
于 2021-08-10T05:39:28.697 回答