A phone number with its country, grouped as it's typed and given in international form.
$ pnpm dlx shadcn@latest add @dashral/phone-inputInstallation
$ pnpm dlx shadcn@latest add @dashral/phone-inputAdds the component, the components it builds on, its npm packages and the theme tokens it uses. Or by its full URL: https://dashral.com/r/phone-input.json
Usage
import { PhoneInput } from "@/components/dashral/phone-input"
<PhoneInput label="Phone" name="phone" defaultCountry="ID" onValueChange={(phone) => setPhone(phone)} />Using the npm package? Import from @dashral/ui/components/phone-input instead, here and in the examples; the rest is the same.
Examples
API reference
PhoneInput
Also takes every prop of Input(opens in a new tab).
| Prop | Type | Default |
|---|---|---|
label | ReactNodeShown above, linked to the control. | — |
description | ReactNodeA hint under the control. | — |
error | boolean | ReactNodetrue shows the error style; a message also shows under the control. | — |
labelAction | ReactNodeAt the end of the label's row, e.g. a “Forgot password?” link or the current value. | — |
withAsterisk | booleanMarks the label with an asterisk, e.g. alongside required. | false |
value / defaultValue | stringThe number in international form (E.164): "+6281234567890". | "" |
onValueChange | (value, { country, national }) => voidThe international number ("" when empty), the country and the digits typed. | — |
country / defaultCountry / onCountryChange | stringThe country picked (ISO 3166: "ID"), controlled or first. Without one, it follows the number given. | "US" |
countries | string[]Only these in the list. | every country |
preferredCountries | string[]Listed first, above the rest. | — |
name | stringThe form field's name; the international number is sent. | — |
placeholder / disabled / required | As on Input. | — |
classNames | { root, label, wrapper, input, description, error, trigger, content }Classes for each part; trigger is the country button, content its list. | — |
className | stringExtra classes, merged with cn() so yours win. | — |
Keyboard
| Keys | Action |
|---|---|
| EnterorSpace | Open the countries (on the flag) |
| Type | Search the countries by name or code |
| ↓or↑ and Enter | Move through them and pick one |
Accessibility
- With label, description or error, the id, aria-describedby and aria-invalid are wired up for you.
- The country button is named "Country" and the country picked; country names are in the page's language (Intl).
- type="tel" with autocomplete="tel", so phones show the number pad and browsers fill it in.