Command palette

@yasinsensoy/web

Web · Components

CountryField

CountrySelect with its label, description and error message.

25 exports

Live example

Preview

If you paste +49…, the country changes by itself.

Code

import { PhoneField, CountryField } from "@yasinsensoy/web";

<PhoneField label="Telefon" name="phone" required
  priority={["TR", "DE", "NL", "AZ"]} />
// In the form "phone" = "+905321234567" (E.164)

<CountryField label="Country" name="country" defaultValue="TR" priority={["TR", "DE", "NL", "AZ"]} />

// On the server: isValidPhone(formData.get("phone")), formatPhone("+905321234567") → "+90 (532) 123 45 67"

Events

  1. Events appear here when you interact with the preview.

Import

import {
  CountryField,
  PhoneField,
  CountryFlag,
  CountrySelect,
  PhoneInput,
  countryFlag,
  countryName,
  getCountries,
  dialCodeOf,
  countryFromDialCode,
  phonePlaceholder,
  formatNationalNumber,
  nationalDigits,
  parsePhone,
  isValidNational,
  isValidPhone,
  formatPhone,
} from "@yasinsensoy/web";

CountryField

componentClient component

CountrySelect with its label, description and error message. For address forms; the value is the ISO code ("TR").

CountryField(input: CountryFieldProps): JSX.Element

Examples

<CountryField label="Country" name="address.country" defaultValue="TR" priority={["TR"]} required />

PhoneField

componentClient component

PhoneInput with a country code, label, description and error message. Submitted in the form as E.164 (+905321234567); for a number whose length doesn't fit the country, the error shows below the field.

PhoneField(input: PhoneFieldProps): JSX.Element

Examples

<PhoneField label="Phone" name="phone" defaultCountry="TR" required
  messages={{ valueMissing: "Phone is required" }} />

CountryFlag

componentClient component

Country flag. On systems that can't render flag emoji (Windows) it shows the country code as a small badge. Decorative; screen readers don't read it.

CountryFlag({ code, className }: CountryFlagProps): JSX.Element

Examples

<CountryFlag code="TR" />

CountrySelect

componentClient component

Country picker: with a flag, the country's name in the user's language and search. Takes all Select props; the value is an ISO 3166-1 alpha-2 code ("TR"). Country names come from the browser's own translations; no extra data is downloaded. Search looks at the name, code and dial code (+49); the long list is drawn with virtual scrolling and opens from the bottom on mobile.

Use CountryField in address forms and PhoneInput for phones.

CountrySelect({ countries, priority, locale, showDialCode, searchPlaceholder, search, ...props }: CountrySelectProps): JSX.Element

Examples

<CountrySelect name="country" defaultValue="TR" priority={["TR", "DE", "NL"]} />
<CountrySelect name="country" countries={["TR", "AZ", "CY"]} showDialCode />

PhoneInput

componentClient component

Phone input with a country code: a flag + dial code picker on the left, and the number grouped by the country's format on the right. Submitted in the form under name in E.164 format (+905321234567); no parsing needed on the server.

  • A leading 0 is dropped even if typed (0532… → +90532…).
  • Typing or pasting +49 … or 0049 … changes the country automatically.
  • If the length doesn't fit the country's rule the field is invalid; the message appears below the field.
  • The phone keyboard opens on mobile; browser autofill works.

For labeled use see PhoneField.

PhoneInput({ value, defaultValue, onValueChange, defaultCountry, onCountryChange, countries, priority, locale, countryName, validate, variant, effect, className, name, form, disabled, required, id, placeholder, onBlur, ...props }: PhoneInputProps): JSX.Element

Examples

<PhoneInput name="phone" defaultCountry="TR" required />
<PhoneInput name="phone" priority={["TR", "DE", "NL"]} countryName="phoneCountry" />

countryFlag

function

Flag emoji: countryFlag("TR") → "🇹🇷".

countryFlag(code: CountryCode): string

countryName

function

The country's name in the locale language; the code itself without a translation.

countryName(code: CountryCode, locale?: string): string

getCountries

function

All countries, sorted by name in the locale language.

getCountries(locale?: string): Country[]

Examples

getCountries("en-US").find((c) => c.code === "DE")?.name; // "Germany"

dialCodeOf

function

Dialing code of a country ("TR" → "90"); undefined when unknown.

dialCodeOf(code: CountryCode): string | undefined

Finds the country from the dialing code at the start of an international number (longest match). preferred is the country to prefer among those sharing a code (e.g. +1 US/Canada).

countryFromDialCode(digits: string, preferred?: CountryCode): {
    country: CountryCode

National number placeholder of a country ("(532) 123 45 67").

phonePlaceholder(country: CountryCode): string | undefined

Groups a national number with the country's pattern: "5321234567" → "(532) 123 45 67".

formatNationalNumber(digits: string, country: CountryCode): string

Reduces a national number typed by the user to digits; the leading 0 is dropped.

nationalDigits(input: string, country: CountryCode): string

parsePhone

function

Parses a phone number. When it starts with + or 00, the country comes from the dialing code; otherwise it counts as a national number of defaultCountry.

parsePhone(input: string, defaultCountry?: CountryCode): ParsedPhone

Examples

parsePhone("+90 532 123 45 67");      // { country: "TR", national: "5321234567", e164: "+905321234567", valid: true }
parsePhone("0532 123 45 67", "TR");   // same result

Does the national number's length follow the country's rule?

isValidNational(national: string, country: CountryCode): boolean

isValidPhone

function

Is the number valid (a length check; it does not verify the number is in use)? Works with a schema on the server: z.string().refine((v) => isValidPhone(v), "Invalid number").

isValidPhone(input: string, defaultCountry?: CountryCode): boolean

formatPhone

function

Turns an E.164 number into a readable form: "+905321234567" → "+90 (532) 123 45 67".

formatPhone(input: string, defaultCountry?: CountryCode): string

ISO 3166-1 alpha-2 country code ("TR", "DE"…).

type CountryCode = string

Country

interface

Info of a country.

NameTypeDescription
codeCountryCodeISO 3166-1 alpha-2 code ("TR").
namestringName in the selected language ("Türkiye").
dialCodestringInternational dialing code without + ("90").
flagstringFlag emoji ("🇹🇷").

ParsedPhone

interface

Result of parsePhone.

NameTypeDescription
countryCountryCodeCountry code.
dialCodestringCalling code ("90").
nationalstringNational number, digits only ("5321234567").
e164stringE.164 form ("+905321234567"); "" when the number is empty.
validbooleanDoes the length follow the country's rule?

Props of the CountryField component.

type CountryFieldProps = FieldOwnProps & Omit<CountrySelectProps, "className">

Extends: FieldOwnProps & Omit<Omit<SingleSelectProps, "items" | "loadItems" | "multiple">, "className">

NameTypeDefaultDescription
countries?readonly CountryCode[]Only these countries are listed (e.g. ["TR", "DE", "NL"]).
priority?readonly CountryCode[]Countries shown in a separate group at the top of the list (e.g. ["TR"]).
locale?string{@link UIProvider} `localeLanguage of country names.
showDialCode?booleanfalseShows the dial code (+90) below the name.

Props of the PhoneField component.

type PhoneFieldProps = FieldOwnProps & Omit<PhoneInputProps, "className">

Extends: FieldOwnProps & Omit<Omit<ComponentProps<"input">, "value" | "defaultValue" | "type" | "onChange" | "size" | "prefix">, "className">

NameTypeDefaultDescription
value?stringNumber in controlled use (E.164, e.g. "+905321234567").
defaultValue?stringInitial number in uncontrolled use (E.164 or national).
onValueChange?(value: string, phone: ParsedPhone) => voidCalled with the E.164 value and the parse result when the number changes.
defaultCountry?CountryCode"TR"Country selected while the number is empty.
onCountryChange?(country: CountryCode) => voidCalled when the country changes.
countries?readonly CountryCode[]Only these countries can be selected.
priority?readonly CountryCode[][defaultCountry]Countries shown at the top of the list.
locale?string{@link UIProvider} `localeLanguage of country names.
countryName?stringThe selected country code is also submitted in the form under this name (e.g. "phoneCountry"). The number is always submitted as E.164 under name.
validate?booleantrueA number that doesn't fit the country's length rule makes the field invalid ("Enter a valid phone number").
variant?FieldVariant | CustomVariant<"field">Appearance. See FieldVariant.
effect?EffectPropEffect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). Defaults to UIProvider appearance.defaults.field.effect. See BuiltinEffect.

Props of the CountryFlag component.

NameTypeDescription
codeCountryCodeISO 3166-1 alpha-2 code ("TR").
className?stringExtra classes.

Props of the CountrySelect component.

Extends: Omit<SingleSelectProps, "items" | "loadItems" | "multiple">

NameTypeDefaultDescription
countries?readonly CountryCode[]Only these countries are listed (e.g. ["TR", "DE", "NL"]).
priority?readonly CountryCode[]Countries shown in a separate group at the top of the list (e.g. ["TR"]).
locale?string{@link UIProvider} `localeLanguage of country names.
showDialCode?booleanfalseShows the dial code (+90) below the name.

Props of the PhoneInput component.

Extends: Omit<ComponentProps<"input">, "value" | "defaultValue" | "type" | "onChange" | "size" | "prefix">

NameTypeDefaultDescription
value?stringNumber in controlled use (E.164, e.g. "+905321234567").
defaultValue?stringInitial number in uncontrolled use (E.164 or national).
onValueChange?(value: string, phone: ParsedPhone) => voidCalled with the E.164 value and the parse result when the number changes.
defaultCountry?CountryCode"TR"Country selected while the number is empty.
onCountryChange?(country: CountryCode) => voidCalled when the country changes.
countries?readonly CountryCode[]Only these countries can be selected.
priority?readonly CountryCode[][defaultCountry]Countries shown at the top of the list.
locale?string{@link UIProvider} `localeLanguage of country names.
countryName?stringThe selected country code is also submitted in the form under this name (e.g. "phoneCountry"). The number is always submitted as E.164 under name.
validate?booleantrueA number that doesn't fit the country's length rule makes the field invalid ("Enter a valid phone number").
variant?FieldVariant | CustomVariant<"field">Appearance. See FieldVariant.
effect?EffectPropEffect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). Defaults to UIProvider appearance.defaults.field.effect. See BuiltinEffect.
className?stringClasses for the container wrapping the country picker and the number box.