dentil

Address Input

AddressInput starts with a country, then shows line 1, line 2, city, region and postal code. The US, Canada and Australia get a state or province select, the UK gets no region field, and other countries get a free-text region. Switch the country to the United Kingdom to see the region field go, or to France for a free-text one.

@dentil/address-input
On this page
{
  "line1": "1 Market St",
  "city": "San Francisco",
  "state": "CA",
  "postal_code": "94105",
  "country": "US"
}

Installation

pnpm dlx shadcn@latest add @dentil/address-input
npx shadcn@latest add @dentil/address-input
yarn dlx shadcn@latest add @dentil/address-input
bunx --bun shadcn@latest add @dentil/address-input

Files land in components/dentil/address-input/. Uses the stock shadcn input and native-select, and installs Country Select and Select Search.

Adding regions

Pass regions to add a list for any country. The select stores the region code. Use hideStateFor for countries whose addresses have no region.

import { AddressInput } from '@/components/dentil/address-input/address-input';

<AddressInput
  value={address}
  onChange={setAddress}
  regions={{
    IE: {
      label: 'County',
      regions: [
        { code: 'D', name: 'Dublin' },
        { code: 'CO', name: 'Cork' },
        // …
      ],
    },
  }}
  hideStateFor={['GB', 'FR', 'DE']}
/>;

API reference

AddressInput

PropTypeDefaultDescription
valueAddress | null | undefined—{ line1, line2, city, state, postal_code, country }, all optional strings.
onChange(value: Address | undefined) => void—Called with only the non-empty fields, or undefined when every field is empty. Changing the country clears the other fields.
errorstring—Shown under the fields, which get aria-invalid and aria-describedby pointing at it.
disabledbooleanfalseDisables every field.
allowCountryChangebooleantrueWhen false, the country select is disabled.
defaultCountrystring—Country used while value has none. The fields appear once there is a country.
regionsRecord<string, { label: string; regions: { code; name }[] }>—Region lists by country code, merged over the built-in US, CA and AU lists. A listed country gets a select whose value is the region code.
hideStateForstring[]['GB']Countries with no region field. Other countries without a list get a free-text field.
placeholdersPartial<Record<keyof Address, string>>—Placeholders and accessible labels for each field.
allowedCountriesstring[]—Passed to CountrySelect.
preferredCountriesstring[]—Passed to CountrySelect.
showFlagsbooleanfalsePassed to CountrySelect.
localestring'en'Passed to CountrySelect.