Formatting the display value of a string field in Studio

4 replies
Last updated: Mar 13, 2021
Hi, quick Studio question:Is it possible to format the display value of a string field?

I have a string field (select) which gets its values populated from an external API call.
However, I would like to show the concatenated value+title to the Studio editor in case they need to manually adjust. Is it possible to define a “display” value that concatenates {value} and {title}?


export default {
    name:"euronorm",
    type:"object",
    fields:[
       {title:"Euronorm", name:"euro", type:"string",
        options: {
            list: [
                {value:"U", title:"Ikke Euro (før euro)"},
                {value:"1L", title:"Euro 1 - 70/220/EØF"},
                {value:"2L", title:"Euro 2 - 70/220/EØF"},
                {value:"3L", title:"Euro 3 - 70/220/EØF"},
                {value:"4L", title:"Euro 4 - 70/220/EØF"},
                {value:"5L", title:"Euro 5 - 692/2008 (med bokstavene A til M)"},
                {value:"6L", title:"Euro 6 - 692/2008 (med bokstavene N til Y)"},
                {value:"1T", title:"Euro 1 - 88/77/EØF"},
                {value:"2T", title:"Euro 2 - 88/77/EØF"},
                {value:"3T", title:"Euro 3 - 88/77/EØF eller 2005/55/EF"},
                {value:"4T", title:"Euro 4 - 88/77/EØF eller 2005/55/EF"},
                {value:"5T", title:"Euro 5 - 88/77/EØF eller 2005/55/EF"},
                {value:"6T", title:"Euro 6 – 595/2009"},
                {value:"ET", title:"EEV – 88/77/EØF eller 2005/55/EF"}
            ] }
    },     
    ]}

Mar 13, 2021, 12:02 AM
It may be possible in the schema, but in my experience any time I’ve needed to use a value from the document, I had to make a custom component and use the document hook.
Mar 13, 2021, 12:20 AM
Yes, that is perhaps the best solution anyway, as I am hoping to keep the schema as clean as possible for the future.I have seen the
doc on custom input components, so I’ll have to study up on that to see if I can build a custom string input field that displays both. Thanks for the help :)
Mar 13, 2021, 12:24 AM
title
is the display value, though, and that can be whatever you want. I presume you’re already setting the title and value programmatically somewhere based on the API response, so you could concat the two when you’re setting that:`{ value: value, title:
${value} ${title}
}`If you need the value of
title
saved on its own as well, you can save that as a third parameter:`{ value: value, title:
${value} ${title}
, originalTitle: title }`
Mar 13, 2021, 12:24 AM
ah, nice. good point. That makes for an easy solution. Yes, I can control this from the API side of things, so that would solve it nicely.
Mar 13, 2021, 12:26 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?