Use field value as title for object field in Sanity document
Yes, you can definitely use a field value as the title for an object field! This is exactly what the preview configuration is designed for.
For your case where you have a content object with a nested title field, you'd add a preview configuration to your object schema like this:
{
name: 'content',
type: 'object',
inputComponent: Tabs,
// Add this preview configuration
preview: {
select: {
title: 'title' // This references the nested title field
},
prepare({title}) {
return {
title: title || 'Untitled' // Fallback if no title yet
}
}
},
fieldsets: [
{ name: 'details', title: 'Details' },
{ name: 'infos', title: 'Informations' },
{ name: 'content', title: 'Content' },
],
fields: [
{
name: 'title',
title: 'Project title',
type: 'string',
fieldset: 'details',
validation: Rule => Rule.required(),
},
// ... other fields
]
}The select object uses the field name directly since title is a direct child of the content object. When the object is collapsed in the Studio, it will show "Project title" value as the header instead of just "Content".
If you ever need to access deeper nested fields or referenced documents, you can use dot notation like select: { title: 'some.nested.field' } or even reference fields like authorName: 'author.name'.
This makes working with complex objects much more user-friendly since editors can see what's inside without having to expand every object!
Show original thread4 replies
Sanity ā Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.