Telephone number input React component with country selection, validation, and formatting capabilities
—
Streamlined phone number input components without country selection dropdowns. These components focus purely on phone number input with automatic formatting and validation, ideal when country context is predetermined or handled separately.
Phone number input component without country selection dropdown.
/**
* Phone number input component without country selection
* Provides formatting and validation for a specific country or international format
*/
interface Props<InputComponentProps> {
/** Phone number value in E.164 format (e.g., "+12133734253") */
value?: string;
/** Called when phone number changes, receives E.164 formatted value or undefined */
onChange(value?: string): void;
/** Specific country for national formatting (two-letter country code) */
country?: Country;
/** Force international format input */
international?: boolean;
/** Include country calling code in input field when international=true */
withCountryCallingCode?: boolean;
/** Default country for ambiguous numbers */
defaultCountry?: Country;
/** Use national format for default country initial value */
useNationalFormatForDefaultCountryValue?: boolean;
/** Custom input component */
inputComponent?: React.ElementType;
/** Enable smart caret positioning */
smartCaret?: boolean;
/** Standard input properties */
placeholder?: string;
disabled?: boolean;
readOnly?: boolean;
autoComplete?: string;
className?: string;
style?: object;
onFocus?(event: React.FocusEvent<HTMLElement>): void;
onBlur?(event: React.FocusEvent<HTMLElement>): void;
}
declare const PhoneInput: React.ForwardRefExoticComponent<Props<DefaultInputComponentProps>>;Usage Examples:
import React, { useState } from "react";
import PhoneInput from "react-phone-number-input/input";
// National format for specific country
function NationalFormatExample() {
const [value, setValue] = useState();
return (
<PhoneInput
country="US"
value={value}
onChange={setValue}
placeholder="Enter US phone number"
/>
);
}
// International format
function InternationalFormatExample() {
const [value, setValue] = useState();
return (
<PhoneInput
international
value={value}
onChange={setValue}
placeholder="Enter international phone number"
/>
);
}
// With country calling code visible
function WithCallingCodeExample() {
const [value, setValue] = useState();
return (
<PhoneInput
country="US"
international
withCountryCallingCode
value={value}
onChange={setValue}
placeholder="+1"
/>
);
}
// With default country fallback
function DefaultCountryExample() {
const [value, setValue] = useState();
return (
<PhoneInput
defaultCountry="US"
value={value}
onChange={setValue}
placeholder="Enter phone number"
/>
);
}Core input component requiring manual metadata parameter.
/**
* Core input component requiring manual metadata
* Use for custom metadata or smaller bundle sizes
*/
interface InputCoreProps<InputComponentProps> extends Props<InputComponentProps> {
/** libphonenumber-js metadata object (required) */
metadata: Metadata;
}
declare const PhoneInput: React.ForwardRefExoticComponent<InputCoreProps<DefaultInputComponentProps>>;Usage Example:
import PhoneInput from "react-phone-number-input/input-core";
import metadata from "libphonenumber-js/metadata.json";
function InputCoreExample() {
const [value, setValue] = useState();
return (
<PhoneInput
country="US"
value={value}
onChange={setValue}
metadata={metadata}
/>
);
}Different bundle size variants with the same API.
// Minimal bundle size
import PhoneInput from "react-phone-number-input/input-min";
// Maximum features
import PhoneInput from "react-phone-number-input/input-max";
// Mobile-optimized
import PhoneInput from "react-phone-number-input/input-mobile";Input components behave differently based on props configuration:
When country prop is provided without international:
When international prop is true:
When defaultCountry prop is provided:
Input components support custom input component integration.
/**
* Custom input component interface
* Must use React.forwardRef() to forward ref to underlying input
*/
interface CustomInputProps {
value: string;
onChange(event: React.ChangeEvent<HTMLInputElement>): void;
onKeyDown?(event: React.KeyboardEvent<HTMLInputElement>): void;
onPaste?(event: React.ClipboardEvent<HTMLInputElement>): void;
// Any other standard input props
}
type InputComponent<InputComponentProps> =
| ((props: InputComponentProps) => JSX.Element)
| React.ComponentClass<InputComponentProps>
| React.ForwardRefExoticComponent<InputComponentProps>;Custom Input Example:
import React, { forwardRef } from "react";
import PhoneInput from "react-phone-number-input/input";
const CustomTextInput = forwardRef<HTMLInputElement, any>((props, ref) => (
<input
{...props}
ref={ref}
className="custom-input-style"
style={{ border: '2px solid blue', borderRadius: '8px' }}
/>
));
function CustomInputExample() {
const [value, setValue] = useState();
return (
<PhoneInput
inputComponent={CustomTextInput}
country="US"
value={value}
onChange={setValue}
/>
);
}Input components support intelligent caret positioning during editing.
interface SmartCaretProps {
/** Enable/disable smart caret positioning (default: true) */
smartCaret?: boolean;
}Smart caret behavior:
Input components handle various value formats consistently.
// Input value types
type InputValue = string | undefined;
// onChange signature
onChange(value?: string): void;Value handling rules:
undefined, null, or empty string are treated as "empty"undefined for empty values (not null or "")undefinedundefined onChange callsValue Handling Examples:
function ValueHandlingExample() {
const [value, setValue] = useState<string | undefined>();
const handleChange = (newValue: string | undefined) => {
setValue(newValue);
if (newValue === undefined) {
console.log('Phone number cleared or invalid');
} else {
console.log('Valid phone number:', newValue); // E.164 format
}
};
return (
<PhoneInput
country="US"
value={value}
onChange={handleChange}
placeholder="(555) 123-4567"
/>
);
}Input components handle various error conditions:
undefined onChange callsInstall with Tessl CLI
npx tessl i tessl/npm-react-phone-number-input