是否有一种方法可以根据引用字段的值有条件地隐藏字段?
我有两种类型:Destination
和DestinationType
以下是从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"
}
1条答案
按热度按时间ddarikpa1#
查看www.example.com文档中的条件字段示例Sanity.io。
基于当前文档中的值隐藏
仅当标题字段为真时才显示副标题字段:
因此,在您的情况下,以下两种方法中的 * 任一种 * 都应该有效:
问题似乎是您使用的
destinationType
是不必要的。