Dynamic list values for string field options in Sanity

2 replies
Last updated: May 27, 2020
Hello!Have anybody got any experience with returning dynamic
list
values for a string field using a function?Is this possible somehow?


{
  name: 'mySelectField',
  type: 'string',
  options: {
    list: (doc, options) => { return MY_VALUES; }   //  <-- Like this!?
  }
}
TLDR; I'd like to refer to inlined objects in array in the same document using their
_key
property, and populating the string dropdown with the existing values.
AI Update

Yes, this is definitely possible! While Sanity doesn't natively support passing a function directly to options.list in the schema, there's a well-established pattern using custom input components that gives you exactly what you need.

The approach involves creating a custom input component that dynamically generates the list based on the document's current values. Here's the pattern:

Solution: Custom Input Component

import { StringInputProps } from 'sanity'
import { useFormValue } from 'sanity'

function DynamicListInput(props: StringInputProps) {
  const document = useFormValue([]) // Gets the entire document
  
  // Generate your list dynamically based on document values
  const dynamicList = generateListFromDocument(document)
  
  // Override the options.list with your dynamic values
  props.schemaType.options.list = dynamicList
  
  // Render the default string input with the new list
  return props.renderDefault(props)
}

function generateListFromDocument(document: any) {
  // Your logic here - for example, referencing array items by _key:
  const items = document.myArrayField || []
  return items.map(item => ({
    title: item.title,
    value: item._key
  }))
}

Then in your schema:

defineField({
  name: 'mySelectField',
  type: 'string',
  components: {
    input: DynamicListInput
  },
  options: {
    layout: 'dropdown' // or 'radio'
  }
})

How It Works

The useFormValue hook gives you access to the current document state, and props.renderDefault(props) renders the standard string input with your modified options. This pattern works great for:

  • Populating dropdowns based on other field values (like your country → region use case)
  • Referencing array items by their _key properties
  • Building dependent field relationships

More Advanced Pattern

For cases where you need async data (like GROQ queries), check out this community schema example that shows how to fetch data within a custom component using useClient and React state.

Why Not Built-In?

There's an open feature request (#4095) for native function support in options.list, which would make this more straightforward. The custom component approach is currently the recommended solution and works well in production, though it requires a bit more setup than a simple function would.

Show original thread
2 replies
I can see how this could be useful, but it isn't possible with the built in options. That means you'd have to build a custom component for it.
I can see how this could be useful, but it isn't possible with the built in options. That means you'd have to build a custom component for it.

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?