Country/Region Selector Input Field for Sanity

7 replies
Last updated: Aug 2, 2021
Does anyone know of an input field that implements something like this: country region/state selector>? I couldn’t find anything with a search, but it seems like a common enough UI convention that someone may have built it. I’m still getting started with Sanity (and React), and I’d like to avoid the time it would take to figure this out if something like it already exists. Thanks!
AI Update

Good news! There's actually a plugin specifically for this use case: sanity-country-state-select. This plugin provides exactly what you're looking for - a custom input field for countries and their corresponding states/regions following the ISO 3166 standard.

You can install it via npm:

npm install sanity-plugin-country-state-select

Then add it to your sanity.config.ts (or .js):

import {countryStateListPlugin} from 'sanity-country-state-select'

export default defineConfig({
  // ...
  plugins: [countryStateListPlugin()],
})

And use it in your schema:

import {defineType, defineField} from 'sanity'

defineType({
  fields: [
    defineField({
      title: 'Countries',
      name: 'countries',
      type: 'countrieslist', // the custom type from the plugin
      options: {
        showStates: true, // enables the state/region selector
        placeholder: 'Select country',
        showIcon: true,
      },
    }),
  ],
})

The plugin gives you a nice dropdown interface for selecting countries, and when showStates is enabled, it'll show a corresponding dropdown for states/regions based on the selected country. The data follows the ISO 3166 standard, so you're getting proper country and region codes.

Since you're still getting started with Sanity and React, using existing plugins like this is definitely the way to go - they're well-tested and save you a ton of time. You can browse more plugins at the Sanity plugin library to see what else is available for common UI patterns. Once you're more comfortable with the platform, you can always build custom input components if you need something more specific to your use case.

Show original thread
7 replies
Hey User! I haven’t seen an exact implementation of that package, but in the past people have spread an array of countries into the
list
option on a
string
type. I just published this snippet for you.
Oh that’s lovely. Thanks,
user M
! I’ve still got my eye on integrating this component somehow, but this is a welcome improvement while I learn more about Sanity and React.
Happy to help! You could probably integrate that package you mentioned by building a custom input component, but I find that when you’re first getting started that can be rough!
Yeah, I’ve started watching some of the custom input tutorial videos, but it’s a matter of balancing learning time with all the other bits I need to code. Some day!
If I use string fields for Country and Region for now, how challenging will it be to migrate data from those fields to (what I assume will be) string fields inside a custom input component in the future? Is there anything I should be doing now to make that process easier?
If you do go ahead and make a custom input component in the future it should be fairly straight forward to write a script that patches the string field’s values into the new custom component. It looks daunting at first but is surprisingly simple once you start tackling it.
Glad to hear it. I’m sure I’ll be back here with other questions before I even get that far, but it’s encouraging to know it’s simpler than it looks. 😀

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?