sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.33k stars 430 forks source link

Conditionally displayed reference field is broken when condition changes #7238

Open hrabiel opened 4 months ago

hrabiel commented 4 months ago

Describe the bug

I encountered an issue in Sanity Studio. When a field of Reference type is configured to be displayed conditionally, by adding a hidden property, it breaks after the condition changes. It becomes impossible to click on the reference in order to open a referenced document. It is fixed if I reload the page, but if the condition changed again, the issue appears again.

Below is a code example. Conditional logic was added as follows. The example repository is build upon a default Movie project Sanity template.

import {defineField, defineType} from 'sanity'

export default defineType({
  name: 'castMember',
  title: 'Cast Member',
  type: 'object',
  fields: [
    defineField({
      name: 'characterName',
      title: 'Character Name',
      type: 'string',
    }),
    // This field is used as a condition
    defineField({
      name: 'showActor',
      title: 'Show Actor',
      type: 'boolean',
    }),
    defineField({
      name: 'person',
      title: 'Actor',
      type: 'reference',
      to: [{type: 'person'}],
      hidden: ({parent}) => !parent?.showActor, // Display `person` field if only `showActor` is `true`
    }),
    defineField({
      name: 'externalId',
      title: 'External ID',
      type: 'number',
    }),
    defineField({
      name: 'externalCreditId',
      title: 'External Credit ID',
      type: 'string',
    }),
  ],
  preview: {
    select: {
      subtitle: 'characterName',
      title: 'person.name',
      media: 'person.image',
    },
  },
})

To Reproduce

Steps to reproduce the behavior:

  1. Clone and install the example project from https://github.com/hrabiel/sanity-issue-demo
  2. Run Sanity Studio locally.
  3. Go to Content -> Movie -> [any movie] -> Cast Members ->
  4. Click on Show Actor
  5. Make sure that Actor field appeared.
  6. Try to click on the Actor to open this referenced document.

Expected behavior

The referenced document should be opened successfully.

Screenshots

https://github.com/user-attachments/assets/9463c865-3db7-4663-a516-f87392fd244d

Which versions of Sanity are you using?

3.52.2

What operating system are you using?

MacOS Sonoma 14.5 (23F79)

Which versions of Node.js / npm are you running?

npm: 10.2.4 node: v20.11.1

runeb commented 4 months ago

This ticket got closed by a mistake, apologies @hrabiel. It is a duplicate of a known issue in our ticket tracking. We'll keep this ticket open and update status as we work on it