An automated and intuitive international and national phone input field for React with TypeScript support.
- π International phone number formatting with country code detection
- π― Automatic phone number validation using
libphonenumber-js - π³οΈ Country flags with dropdown selector
- π± Mobile-friendly with optimized UX
- βοΈ Flexible configuration - set default country, preferred countries, or filter by regions
- π¨ Customizable styling with SCSS support
- π¦ TypeScript first with full type definitions
- βΏ Accessible with proper ARIA attributes
- π Supports both INTERNATIONAL and NATIONAL formats
npm install react-phonenr-inputor
yarn add react-phonenr-inputimport { useState } from 'react';
import { PhoneInput } from 'react-phonenr-input';
import 'react-phonenr-input/styles.css';
function App() {
const [phoneNumber, setPhoneNumber] = useState('');
return (
<PhoneInput
onChange={setPhoneNumber}
placeholder="+1 702 123 4567"
/>
);
}| Prop | Type | Default | Description |
|---|---|---|---|
onChange |
(value: PhoneNumber) => void |
required | Callback fired when the phone number changes |
defaultCountry |
CountryCode |
- | Default country code (e.g., 'US', 'GB') |
initialValue |
string |
- | Initial phone number value with country code |
preferredCountries |
CountryCode[] |
- | List of countries to show at the top of the dropdown |
regions |
Region[] |
- | Filter countries by region(s) |
format |
'INTERNATIONAL' | 'NATIONAL' |
'INTERNATIONAL' |
Phone number format |
withCountryMeta |
boolean |
false |
Include country metadata in the returned value |
maxLength |
number |
21 |
Maximum input length |
disabled |
boolean |
false |
Disable the input |
placeholder |
string |
- | Placeholder text |
className |
string |
- | Custom CSS class name |
onFocus |
FocusEventHandler |
- | Focus event handler |
onBlur |
FocusEventHandler |
- | Blur event handler |
Additional HTML input attributes are also supported via the spread operator.
<PhoneInput
onChange={setPhoneNumber}
initialValue="+491761234112"
/><PhoneInput
onChange={setPhoneNumber}
defaultCountry="US"
placeholder="Enter phone number"
/><PhoneInput
onChange={setPhoneNumber}
preferredCountries={['US', 'GB', 'DE', 'FR']}
/><PhoneInput
onChange={setPhoneNumber}
regions={['europe', 'north-america']}
/><PhoneInput
onChange={setPhoneNumber}
withCountryMeta
/>When withCountryMeta is enabled, the onChange callback receives an object with the phone number and country information:
{
phoneNumber: "+491761234112",
country: {
name: "Germany",
iso2: "DE",
dialCode: "49",
// ... additional metadata
}
}<PhoneInput
onChange={setPhoneNumber}
format="NATIONAL"
defaultCountry="US"
/>type PhoneNumber = string | {
phoneNumber: string;
country: ICountry;
}type Region =
| "asia"
| "europe"
| "africa"
| "north-africa"
| "oceania"
| "america"
| "carribean"
| "south-america"
| "ex-ussr"
| "european-union"
| "middle-east"
| "central-america"
| "north-america"Import the default styles:
import 'react-phonenr-input/styles.css';Or create your own custom styles by targeting the component classes. The component uses BEM methodology for CSS class names.
- Node.js >= 22
- Yarn or npm
# Install dependencies
yarn install
# Start Storybook for development
yarn start
# Run tests
yarn test
# Run linting
yarn lint
# Build the library
yarn buildyarn start- Start Storybook development serveryarn build- Build the library for productionyarn test- Run tests with Vitestyarn lint- Run ESLint and Stylelintyarn lint:fix- Fix linting issues automaticallyyarn check-types- Run TypeScript type checkingyarn ci- Run all checks (lint + tests)
This library supports all modern browsers. For older browsers, you may need to include appropriate polyfills.
- React 19.2.x
- libphonenumber-js - Phone number parsing and validation
- react-country-flag - Country flag components
MIT Β© Kai Hotz
Contributions are welcome! Please feel free to submit a Pull Request.