Country/Region Selector Input Field for Sanity
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-selectThen 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 thread7 replies
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.