An automated and intuitive international and national phone input field for React with TypeScript support.
libphonenumber-jsnpm install react-phonenr-input
or
yarn add react-phonenr-input
import { 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.
# Install dependencies
yarn install
# Start Storybook for development
yarn start
# Run tests
yarn test
# Run linting
yarn lint
# Build the library
yarn build
yarn 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.
MIT © Kai Hotz
Contributions are welcome! Please feel free to submit a Pull Request.
$ claude mcp add React-PhoneNr-Input \
-- python -m otcore.mcp_server <graph>