rosnovsky / sanity-plugin-autocomplete-tags

Creatable autocomplete tag dropdown input fields for Sanity Studio 🤓
MIT License
24 stars 7 forks source link

Dynamic data in Preset of tags #31

Open MartCube opened 2 years ago

MartCube commented 2 years ago

Hello there :wave: great work for creating this plugin :+1: This is more of a question then a feature. Instead of writing manually the Preset of tags , I want to add array of document reference ( exp: array of document slug ) I'm new in Sanity so I'm not sure is this possible to write it. If you can provide any demo code that would be great

gpoole commented 2 years ago

I think if preload could also be an async function that returns array, that would be flexible enough to cover a bunch of use cases. For example to solve the original question of getting document titles and slugs as tags with this change it would be:

import sanityClient from "part:@sanity/base/client"

// ...

{
  name: 'tags',
  type: 'tags',
  options: {
    preload() {
        return sanityClient.fetch('*[_type == "document"] { "label": title, "value": slug.current }')
    }
  }
}

It would also work for what I need which is let me load the tag list out of a global settings document.

gpoole commented 2 years ago

I know you asked this a while ago @MartCube and you've probably already figured it out, but just re-reading your question it sounds like maybe you wanted an array of references instead?