如何在 Sanity Studio 中设置单选按钮的默认值?

问题描述 投票:0回答:2

我正在尝试在 Sanity Studio 中设置给定单选项目列表的默认值。

代码:

export default {
  name: 'banner',
  title: 'Banner',
  type: 'document',
  fields: [
    {
      name: "category",
      title: "Category",
      description: "Choose a category",
      type: "string",
      options: {
        layout: "radio",
        list: [
          { title: "Documentary", value: "documentary" },
          { title: "Fiction", value: "fiction" },
        ],
      },
      // set initial value here ?
    },
  ]
}

有一个名为

initialValue
的属性可以轻松设置字符串的默认值,但我不知道如何对单选项目执行此操作。

如何在页面加载时选择单选项目

Fiction

radio-button default sanity
2个回答
0
投票
  name: 'banner',
  title: 'Banner',
  type: 'document',
  fields: [
    {
      name: "category",
      title: "Category",
      description: "Choose a category",
      type: "string",
      options: {
        layout: "radio",
        list: [
          { title: "Documentary", value: "documentary" },
          { title: "Fiction", value: "fiction" },
        ],
      },
      initialValue: "documentary", // set initialValue's value to one of the `value`s in your list of radio items
    },
  ]
}```

0
投票

尝试直接在文档对象而不是字段上使用

initialValue

export default {
  name: 'banner',
  title: 'Banner',
  type: 'document',
  fields: [
    {
      name: "category",
      title: "Category",
      description: "Choose a category",
      type: "string",
      options: {
        layout: "radio",
        list: [
          { title: "Documentary", value: "documentary" },
          { title: "Fiction", value: "fiction" },
        ],
      },
    },
  ],
  initialValue: {
    category: 'fiction'
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.