next.js 健全工作室:根据引用字段的值有条件地隐藏字段

svmlkihl  于 2023-03-02  发布在  其他
关注(0)|答案(1)|浏览(121)

是否有一种方法可以根据引用字段的值有条件地隐藏字段?
我有两种类型:DestinationDestinationType
以下是从Destination类型到DestinationType类型的参考:

defineField({
      name: "destinationType",
      title: "Destination Type",
      type: "reference",
      to: { type: "destinationType" },
    }),

只有四个destinationType文档:城市、州、国家、地区。在destinationType.name字段中设置相应的值。
然后在我的Destination类型有4个其他参考字段:x1米7英寸1x、x1米8英寸1x、x1米9英寸1x、regionRef
我希望根据destinationType.name的值有条件地隐藏这些字段
下面我尝试在www.example.com不等于City时隐藏cityRefdestinationType.name does not equal City

defineField({
      name: "cityRef",
      title: "Destination City Reference",
      type: "reference",
      to: { type: "destination" },
      hidden: ({ parent }) => parent?.destinationType?.name !== "City",
      options: {
        filter: ({ document }) => ({
          filter: 'destinationType->name == "City"',
        }),
      },
    }),

我好像没办法。我试了20种方法。
我寻求的结果是根据当前目标对destinationType.name的引用显示/隐藏4个{destinationType}引用字段
例如:如果我将destinationType设置为"区域",我想隐藏cityRef、stateRef和countryRed ...等等。
更新:看起来我只能访问引用上的有限数据,如console.log输出所示:

{
"_createdAt": "2023-02-19T17:26:48Z",
"_id": "8adb4138-d55d-48fb-9a2e-b6f78f1873a0",
"_rev": "VoO7lZqxqfzVYxXcTSOD38",
"_type": "destination",
"_updatedAt": "2023-02-19T17:26:48Z",
"destinationType": {
    "_ref": "8d818167-5ef4-4c99-bfe2-76463fb576fb",
    "_type": "reference"
},
"name": "New York City"

}

ddarikpa

ddarikpa1#

查看www.example.com文档中的条件字段示例Sanity.io。
基于当前文档中的值隐藏
仅当标题字段为真时才显示副标题字段:

{
  name: 'subtitle',
  type: 'string',
  title: 'Subtitle',
  hidden: ({document}) => !document?.title
}

因此,在您的情况下,以下两种方法中的 * 任一种 * 都应该有效:

hidden: ({ parent }) => parent?.name !== "cityRef",
hidden: ({ parent }) => parent?.title !== "Destination City Reference",

问题似乎是您使用的destinationType是不必要的。

相关问题