import { PhoneInputDemos } from '@docs/demos';
import { Layout } from '@/layout';
import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.PhoneInput);
## Usage
`PhoneInput` provides an international phone number input with country selector
and formatting masks.
## Input features
## Loading state
Set `loading` prop to display a loading indicator in the input:
## Controlled
Use `value` and `onChange` props to control the input value. The `onChange` callback
receives the raw value and a details object containing the formatted value and country data.
## Phone extensions
Set `withExtension` to allow extensions separated by `x`, for example `+12025551234x1234`.
The extension is included in `value` and available separately as `details.extension` in `onChange`.
With this prop, `details.extension` is an empty string when no extension digits are entered:
## Preferred countries
Use `preferredCountries` prop to pin countries to the top of the dropdown.
## Filter countries
Use `onlyCountries` to show only specific countries or `excludeCountries` to hide specific countries.
## Custom flags
Use `renderFlag` prop to render custom flag elements, for example emoji flags.
The function receives country data and should return a React node.
## Searchable
Country dropdown search is enabled by default. Set `searchable={false}` to disable it.
## Country auto-detection
Set `withCountryAutoDetect` to automatically detect and switch the country when the user
types a dial code. For example, typing `+44` will switch to United Kingdom.
Without the prop, the country still changes when a number starting with `+` and another dial code is typed or pasted.
Area codes, for example `+1 204` for Canada, are detected only with the prop:
## Shared dial codes
Countries sharing a dial code are selected by area code and priority by default.
Use `resolveCountry` to choose one by ISO2 code, or return `undefined` to keep the default:
```tsx
value.startsWith('+77') && countries.some((country) => country.iso2 === 'kz') ? 'kz' : undefined
}
/>
```
## National prefixes
`PhoneInput` removes known trunk prefixes from pasted national numbers. For example, pasting
`8 999 123-45-67` with Russia selected produces `+79991234567`. Clearing the input emits `''`.
In an empty international input, a typed leading trunk prefix is also removed.
Typing an explicit dial code, such as `+7`, preserves the national digits that follow in both `"auto"` and `"strip"` modes.
Ambiguous digits typed after a prefilled dial code are preserved by default. Set `nationalPrefixBehavior="strip"`
when users should enter numbers in national form, or `"preserve"` to disable prefix removal.
With `"strip"`, unprefixed numbers use the selected country. Include `+` to enter another country's dial code:
```tsx
```
## Localization
Use `renderCountryOption` to customize how countries are displayed in the dropdown.
This can be used to translate country names to a different language.
`getCountryLabel` returns the translated name of a country. It is used as the
country button label for screen readers and in the dropdown search, so users
can search by translated names. `searchPlaceholder` and `selectCountryLabel`
translate the search input placeholder and the button label shown before a
country is selected:
## Dropdown position
## Use within Popover
To use `PhoneInput` within `Popover` or other overlay, set `comboboxProps={{ withinPortal: false }}`.
Pressing `Escape` while the country list is open closes both the list and the `Popover`.
Set `closeOnEscape={false}` on the `Popover` to keep it open. The country list still closes on `Escape`:
## Disabled state
## Read only
## Error state
## Success state