如何在Sanity Studio中设置无线电按钮的默认值?

发布于 2025-02-10 22:04:40 字数 671 浏览 2 评论 0原文

我正在尝试在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在加载页面时已选择?

I'm trying to set the default value given a list of radio items in Sanity Studio.

Code:

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 ?
    },
  ]
}

There is a property called initialValue that can set the default value of a string easily, but I can't figure out how to do this with radio items.

How can I have the radio item Fiction already selected when the page is loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

默嘫て 2025-02-17 22:04:40
  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
    },
  ]
}```
  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
    },
  ]
}```
小兔几 2025-02-17 22:04:40

尝试直接在文档对象上而不是在字段上使用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'
  }
}

Try using initialValue directly on the document object rather than on the field:

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'
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文