Clean studio created with sanity init
with a simplest possible schema to showcase conditional fields.
-
Prepare the demo:
- Clone this repository.
- Run
npm install
. - Update
sanity.json
to point to your own Sanity project and data:
"api": { "projectId": "iitl24hy", "dataset": "example" }
-
Run
sanity start
and open the Sanity Studio on http://localhost:3333/. -
Try out the conditional field:
-
Now edit the product.
-
Can we remove the value of the conditional field in the stored document every time the field it's hidden? In other words, in our example, when a 'Paid' product is made 'Free' so that the price field becomes hidden, how can we also unset the price field in the stored document in Sanity.
-
Can we avoid having to specify
inputComponent: ConditionalField
on every single, conditional field, and instead implement an input resolver. This would be a much more elegent solution. An input resolver could simply check for the presence ofoptions.condition
in the field definition, like this:
import ConditionalField from './conditional-field/ConditionalField'
export default function resolveInput(type) {
// For any field that has 'options.condition' set
if (type.options && type.options.condition) {
return ConditionalField
}
}