react ~7 KB 0 deps v1.1.1 β†— GitHub β†—

@mshafiqyajid/react-phone-input

Headless phone input hook and styled component. Country selector with flag emoji, dial code, 30+ countries, E.164 output.

Playground #

Props
TSX
import { PhoneInputStyled } from "@mshafiqyajid/react-phone-input/styled";
import "@mshafiqyajid/react-phone-input/styles.css";

<PhoneInputStyled
  value={value}
  onChange={setValue}
/>

Install #

npm install @mshafiqyajid/react-phone-input

Quick start #

import { PhoneInputStyled } from "@mshafiqyajid/react-phone-input/styled";
import "@mshafiqyajid/react-phone-input/styles.css";

const [phone, setPhone] = useState("");
<PhoneInputStyled
  value={phone}
  onChange={setPhone}
  defaultCountry="US"
  label="Phone number"
  tone="primary"
/>

Headless #

import { usePhoneInput } from "@mshafiqyajid/react-phone-input";

const { inputProps, country, setCountry, dialCode, isValid } = usePhoneInput({
  defaultCountry: "US",
});

return (
  <div className="phone">
    <button onClick={() => setCountry("GB")}>{country} (+{dialCode})</button>
    <input {...inputProps} aria-invalid={!isValid} />
  </div>
);

API #

PropTypeDefaultDescription
valuestringβ€”Controlled phone number value
defaultValuestring""Uncontrolled initial value
onChange(v) => voidβ€”Called on change
defaultCountrystring"US"ISO-2 country code default
showFlagbooleantrueShow flag emoji in selector
labelstringβ€”Visible label
hintstringβ€”Helper text below input
errorstringβ€”Error message (overrides hint)
size"sm" | "md" | "lg""md"Input size
tone"neutral" | "primary" | "success" | "danger""neutral"Color tone
disabledbooleanfalseDisable interaction
countriesstring[]β€”Whitelist of ISO-2 country codes
onCountryChange(country) => voidβ€”Country change callback
outputFormat"national" | "international" | "e164""national"Output format for onChange value
readOnlybooleanfalseRead-only input
preferredCountriesstring[]β€”ISO-2 codes to pin to the top of the dropdown (rendered above a divider).
searchablebooleantrueShow a search field at the top of the country dropdown.
searchPlaceholderstring"Search..."Placeholder for the country search input.
disableCountrySelectorbooleanfalseLock the country to defaultCountry β€” hides the dropdown trigger.
Edit this page on GitHub