How to create nested sub-categories in Sanity documents?

1 replies
Last updated: May 20, 2023
Hey everyone! πŸ‘‹
I am trying to make a sub-category in a document but I cant figure it out. Tried searching Google and this Slack channel but I feel like I might be searching for the wrong terms?

I mocked up an image of what I want to get basically:

Any help would be great thanks!
AI Update

Based on your mockup, it sounds like you're looking for fieldsets! This is exactly the feature that lets you create those visual sub-categories/groupings within your document schema.

Fieldsets allow you to group related fields together under a collapsible section without changing your data structure. Here's how to set them up:

export default {
  name: 'myDocument',
  type: 'document',
  title: 'My Document',
  fieldsets: [
    {
      name: 'seoGroup',
      title: 'SEO Settings',
      options: {
        collapsible: true,  // Makes it collapsible
        collapsed: false    // Set to true if you want it collapsed by default
      }
    },
    {
      name: 'mediaGroup', 
      title: 'Media',
      options: {
        collapsible: true,
        collapsed: true
      }
    }
  ],
  fields: [
    {
      name: 'title',
      type: 'string',
      title: 'Title'
    },
    {
      name: 'metaTitle',
      type: 'string',
      title: 'Meta Title',
      fieldset: 'seoGroup'  // Assigns this field to the SEO fieldset
    },
    {
      name: 'metaDescription',
      type: 'text',
      title: 'Meta Description',
      fieldset: 'seoGroup'
    },
    {
      name: 'featuredImage',
      type: 'image',
      title: 'Featured Image',
      fieldset: 'mediaGroup'
    }
  ]
}

The key parts are:

  1. Define your fieldsets in the fieldsets array at the document level
  2. Reference them by name in each field using fieldset: 'yourFieldsetName'
  3. Use the options object to control collapsibility and default state

You can also arrange fields in columns within a fieldset by adding columns: 2 to the options. Check out the guide on creating effective editor experiences for more tips on organizing your schemas!

Show original thread
1 reply
Just a follow up to this, I ended up making a reference in the Category which referenced the Continent. This works fine :)

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.

Was this answer helpful?