Command palette

@yasinsensoy/native

React Native · Components

CountrySelect

Country picker: with flags, the country's name in the user's language and search.

22 exports

Import

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

CountrySelect

component

Country picker: with flags, the country's name in the user's language and search. Same API as CountrySelect on web; takes all props of Select. The value is the ISO 3166-1 alpha-2 code ("TR"). Search looks at the name, the code and the dialing code (+49); on a phone it opens from the bottom.

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

Examples

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

CountryFlag

component

Country flag (a flag emoji; drawn by iOS and Android). Decorative; screen readers do not read it.

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

Examples

`<CountryFlag code="TR" />`

PhoneInput

component

Phone input with a country code: a flag + dialing code picker on the left, the number grouped in the country's format on the right. Same API as PhoneInput on web; the value is E.164 (+905321234567).

  • A leading 0 is dropped even if typed (0532… → +90532…).
  • Typing or pasting +49 … or 0049 … changes the country.
  • If the length does not fit the country's rule the field becomes invalid (on blur).
  • The phone keyboard opens; iOS/Android autofill works.
  • Binds inside a Form with name.
PhoneInput(props: PhoneInputProps): ReactElement<unknown, string | JSXElementConstructor<any>>

Examples

<PhoneInput name="phone" label="Phone" defaultCountry="TR" />

countryFlag

function

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

countryFlag(code: CountryCode): string

Registers a language's country names for environments without Intl.DisplayNames (React Native's Hermes). @yasinsensoy/native registers Turkish and English itself; for another language pass { TR: "Turquía", … }.

registerCountryNames(language: string, names: Readonly<Record<CountryCode, string>> | string): void

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

Props of CountryFlag.

NameTypeDefaultDescription
codeCountryCodeISO 3166-1 alpha-2 code ("TR").
size?number18Size (pt).

Props of CountrySelect.

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 the country names.
showDialCode?booleanfalseDialing code below the name (+90).

Props of PhoneInput.

Extends: FieldProps

NameTypeDefaultDescription
ref?Ref<TextInputRef>Ref of the underlying text input.
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) => voidWhen the number changes, with the E.164 value and the parse result.
defaultCountry?CountryCode"TR"Country selected while the number is empty.
onCountryChange?(country: CountryCode) => voidWhen 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 the country names.
validate?booleantrueA number that does not fit the country's length rule makes the field invalid ("Geçerli bir telefon numarası gir"; on blur).
placeholder?stringPlaceholder. Defaults to the country's example number.
variant?FieldVariant"outline"Field appearance.
onBlur?(event: TextInputFocusEvent) => voidOn blur.
style?StyleProp<ViewStyle>Container style.

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?