Skip to Content

Country Selector

A dropdown for selecting countries, with flag icons and ISO-3166 codes. Works in conjunction with StateSelector and CitySelector for cascading location selection.


Import

import { CountrySelector } from "@phpcreation/frontend-components-react-nextjs-bundle/components/CountrySelector";

Props

PropTypeDefaultDescription
valuestring""Controlled ISO country code
onChange(code: string) => voidChange handler
placeholderstring"Select country..."Placeholder text
disabledbooleanfalseDisable the control
classNamestringAdditional CSS classes

Usage

import { CountrySelector } from "..."; import { StateSelector } from "..."; const [country, setCountry] = useState(""); const [state, setState] = useState(""); // Reset state when country changes const handleCountryChange = (code: string) => { setCountry(code); setState(""); }; <CountrySelector value={country} onChange={handleCountryChange} placeholder="Select country..." /> <StateSelector country={country} value={state} onChange={setState} />

With React Hook Form

<FormField control={form.control} name="country" render={({ field }) => ( <FormItem> <FormLabel>Country</FormLabel> <FormControl> <CountrySelector value={field.value} onChange={field.onChange} /> </FormControl> <FormMessage /> </FormItem> )} />

Cascading Location

For complete address selection, chain the three location selectors:

<CountrySelector value={country} onChange={handleCountryChange} /> <StateSelector country={country} value={state} onChange={handleStateChange} /> <CitySelector country={country} state={state} value={city} onChange={setCity} />

Last updated on