3

I want to preview a reference name in the studio I have and icon type, for example one which has the title 'facebook'

export default {
name: 'icon',
title: 'Icon',
type: 'document',
fields: [
    {
        name: 'name',
        title: 'Name',
        type: 'string'
    },
]

}

I reference this in a menu elsewhere like this

{
        name: 'icon',
        title: 'Icon',
        type: 'reference',
        to: [{ type: 'icon' }]
    },

and then try to preview like this

preview: {
    select: {
        title: 'icon',
    },
    prepare(selection) {
        const { title } = selection;

        return {
            title: title.name,
        }
    }
}

but my selection returns the reference object, with _ref etc. not the object itself. Is there a way to preview this reference?

1 Answer 1

7

You can dot your way into the property on the reference that you'd like to use in the preview like this:

preview: {
    select: {
        title: 'icon.name',
    },
    prepare(selection) {
        const { title } = selection;

        return {
            title: title.name,
        }
    }
}

Side note: Since the prepare function now just passes through its input, you can remove it altogether. This would be sufficient:

preview: {
    select: {
        title: 'icon.name'
    }
}
1
  • Remarkably you can also do arrays! author0: "authors.0.name"
    – doublejosh
    Sep 2, 2022 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.