You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 26, 2021. It is now read-only.
Copy file name to clipboardexpand all lines: README.md
+148-34
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,7 @@ Hide or show a Sanity.io field based on a custom condition set by you.
4
4
5
5
---
6
6
7
-
🚨 **Warning:** I stopped working on this plugin before it was done as the Sanity team has voiced they're currently working on a native solution.
8
-
9
-
This can still be useful if you have basic use cases for conditionals, but it [doesn't work well on arrays](https://github.com/hdoro/sanity-plugin-conditional-field/issues/2), has [issues with validation markers](https://github.com/hdoro/sanity-plugin-conditional-field/issues/1) and is visually a bit buggy.
10
-
11
-
If in the meantime you _must_ rely on conditional fields, [reach me out in Sanity's community Slack](https://sanity-io-land.slack.com/team/UB1QTEXGC) and I'll try to help you out :)
7
+
🚨 **Warning:** the Sanity team has voiced they're currently working on a native solution. The goal of this plugin is to become obsolete.
// Simple conditional based on top-level document value
45
+
hide: ({ document }) =>document.internal, // hide if internal article
46
+
},
61
47
},
62
48
],
63
49
}
64
50
```
65
51
66
-
🚨 **Big red alert**: this plugin simply _hides_ fields if conditions aren't met. It doesn't interfere with validation, meaning that if you set a conditioned field as required, editors won't be able to publish documents when it's hidden.
Besides the current `document`, the `hide` function receives a `parents` array for accessing contextual data.
80
+
81
+
It's ordered from closest parents to furthest, meaning `parents[0]` will always be the object the field is in, and `parents[-1]` will always be the full document. If the field is at the top-level of the document, `parents[0] ===document`.
82
+
83
+
Here's an example of it in practice - notice how the `link` object is nested under an array, which means `parents[1]` will return the array with all the links:
84
+
85
+
```js
86
+
{
87
+
name:'links',
88
+
title:'Links',
89
+
type:'array',
90
+
of: [
91
+
{
92
+
name:'link',
93
+
title:'Link',
94
+
type:'object',
95
+
fields: [
96
+
{
97
+
name:'external',
98
+
title:"Links to external websites?",
99
+
type:'boolean',
100
+
},
101
+
{
102
+
name:'url',
103
+
title:'External URL',
104
+
type:'string',
105
+
inputComponent: ConditionalField,
106
+
options: {
107
+
hide: ({ parents }) => {
108
+
// Parents array exposes the closest parents in order
109
+
// Hence, parents[0] is the current object's value
110
+
return parents[0].external
111
+
},
112
+
},
113
+
},
114
+
{
115
+
name:'internalLink',
116
+
type:'reference',
117
+
to: [{ type:"page" }],
118
+
inputComponent: ConditionalField,
119
+
options: {
120
+
hide: ({ parents }) =>!parents[0].external
121
+
},
122
+
},
123
+
{
124
+
name:'flashyLooks',
125
+
type:'boolean',
126
+
inputComponent: ConditionalField,
127
+
options: {
128
+
// Prevent editors from making the link flashy if this link is not in the first position in the array
The `hide` function can also return an object to determine whether or not existing values should be cleared when the field is hidden. By default, this plugin won't clear values.
145
+
146
+
```js
147
+
{
148
+
name:'externalUrl',
149
+
type:'url',
150
+
inputComponent: ConditionalField,
151
+
options: {
152
+
hide: ({ document }) => {
153
+
hidden:!!document.internal,
154
+
// Clear field's value if hidden
155
+
clearOnHidden:true
156
+
},
157
+
},
158
+
},
159
+
```
160
+
161
+
### Typescript definitions
67
162
68
-
Take a look at the roadmap below for an idea on the plugin's shortcomings.
163
+
If you use Typescript in your schemas, here's how you type your `hide` functions:
🚨 **Big red alert**: this plugin simply _hides_ fields if conditions aren't met. It doesn't interfere with validation, meaning that if you set a conditioned field as required, editors won't be able to publish documents when it's hidden.
69
190
70
-
## Roadmap
191
+
Besides this, the following is true:
71
192
72
-
-[ ] Prevent the extra whitespace from hidden fields
73
-
-[ ] Find a way to facilitate validation
74
-
-[ ] Consider adding a `injectConditionals` helper to wrap the `fields` array & automatically use this inputComponent when options.condition is set
- Would require some debouncing in the execution of the condition function, else it'll fire off too many requests
78
-
- Maybe an array of dependencies similar to React.useEffect
79
-
-[ ] get merged into `@sanity/base`
80
-
- That's right! The goal of this plugin is to become obsolete. It'd be much better if the official type included in Sanity had this behavior from the get-go. Better for users and the platform :)
193
+
- Async conditions aren't debounced, meaning they'll be fired _a lot_
194
+
- There's no way of using this field with custom inputs
0 commit comments