# Country input

SvCountryInput: a searchable checkout country picker (flag + name + dial code) emitting the ISO code, with label + required validation.

Uses `@svgrid/grid` (MIT). Category: Selection. Live demo: https://svgrid.com/demos/317-country-input/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvCountryInput in Svelte 5 as a checkout field: a searchable country picker showing flag, name and, with showDial, the dial code, emitting the ISO country code through onChange, with a label and required validation on the shared editor contract.

SvCountryInput - a production checkout field: a searchable country picker (flag + name + optional dial code) emitting the ISO code. Copy-paste ready.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### What value is stored?

The two-letter ISO code, for example DE, which is what payment and shipping APIs expect. The display shows the flag and localized name.

### How is the list searched?

Typing filters by country name, dial code and ISO code, so +49, DE and Germany all find the same entry.

### Can I translate the strings?

Yes. The messages prop overrides the placeholder, the search label and the empty-result text, and the country names come from the built-in reference list.

## Related documentation

- [SvCountryInput](https://svgrid.com/docs/help/ui-components/sv-country-input.md)

## Related articles

- [A Tri-State Select-All Checkbox in SvGrid](https://svgrid.com/blog/tri-state-select-all/) - How SvGrid's rowSelectionFeature drives the header checkbox through unchecked, indeterminate, and checked states - and the pagination edge case that catches everyone.
- [Stable Row Identity in SvGrid (and why it matters)](https://svgrid.com/blog/stable-row-identity-getrowid/) - How SvGrid tracks rows across mutations, why object reference stability is the key to correct selection and editing, and the exact patterns to use with live data feeds.
- [A Fill Handle (Drag to Fill) in SvGrid](https://svgrid.com/blog/fill-handle-drag-to-fill/) - Build a working spreadsheet-style fill handle on top of SvGrid's cell selection and editing - pointer tracking, range highlighting, series fill, and undo/redo integration all covered.

## Source code (317-country-input.svelte)

```svelte
<script lang="ts">
  /**
   * SvCountryInput - a production checkout field: a searchable country picker
   * (flag + name + optional dial code) emitting the ISO code. Copy-paste ready.
   */
  import { SvCountryInput } from '@svgrid/grid'

  let country = $state<string | null>('US')
  let billing = $state<string | null>(null)
  const billingError = $derived(billing == null ? 'Select a billing country' : undefined)
</script>

<div class="wrap">
  <header>
    <h2>Country input</h2>
    <p>A searchable country picker (search by name, dial code or ISO) emitting the alpha-2 code - checkout, profiles, phone defaults.</p>
  </header>

  <form class="form" onsubmit={(e) => e.preventDefault()}>
    <label class="f">Shipping country
      <SvCountryInput value={country} showDial onChange={(v) => (country = v)} />
      <span class="val">ISO: {country ?? '-'}</span>
    </label>

    <SvCountryInput
      value={billing}
      label="Billing country"
      required
      invalid={!!billingError}
      error={billingError}
      onChange={(v) => (billing = v)}
    />
  </form>
</div>

<style>
  .wrap { padding: 20px; max-width: 380px; display: flex; flex-direction: column; gap: 16px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
  .form { display: flex; flex-direction: column; gap: 18px; }
  .f { display: flex; flex-direction: column; gap: 6px; font-size: 12px; font-weight: 600; color: var(--sg-muted, #64748b); }
  .val { font-size: 12px; color: var(--sg-muted, #94a3b8); font-weight: 400; }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/317-country-input.svelte)
