RSSAmplifier

Gulla.net · Oct 22, 2024

Sorting reference field dropdown in Sanity

0
Sign in to vote or save

Tomas Hensrud Gulla · Tomas Hensrud Gulla

Sanity is a highly customizable headless CMS, where schemas are defined using code.

A field for referencing other documents can be defined like this:

fields: [
  defineField({
    title: 'Fruit',
    name: 'fruit',
    type: 'reference',
    to: [{type: 'fruit'}],
  }),
],

And looks like this:

A Sanity reference field, listing fruit. Not sorted.

...pretty nice. But it would look better if we could sort the fruit alphabetically. According to the documentation, it does not seem like that is possible, but way down in a github issue, there lies the solution.

Sorting is undocumented, but as easy as this.

fields: [
  defineField({
    title: 'Fruit',
    name: 'fruit',
    type: 'reference',
    to: [{type: 'fruit'}],
    options: {
      sort: [{field: 'name', direction: 'asc'}],
    },
  }),
],

Finally sorted:

A Sanity reference field, listing fruit. Sorted.

That's it!

Read the original on gulla.net

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.