Phone Input

Note
In Windows, flag emojis may not be visible due to a lack of native support from the OS. For a solution, consider checking out the Coutry Flag Emoji Polyfill.

Examples

Basic usage

+1

FlagContainer overridden with Tag

+1

Custom label using mapIsoToLabel

+1

Nested input overrides

+1

AutoFocus

PhoneInput accepts an autoFocus prop. For information on its behaviour, check Input's autoFocus example.

Stateful (uncontrolled) usage

+1

As with many of our components, there is also an uncontrolled version, StatefulPhoneInput, which manages its own state.

With Selective Countries

+374

Note that, default selected country will be the first country passed in countries.

Supported Countries

The following countries are available in the country selector dropdown. You can import these as an object COUNTRIES from spaceweb/phone-input. Note, the country objects are formatted for ease of use with our Select component.

COUNTRIES: {
id: string, // Country ISO
label: string, // Country Name
dialCode: string // Country Dial Code
}

API

Phone Input props

NameTypeDefaultDescription
autoFocus
string
Takes "start" | "end" | number | boolean values and sets the position of the cursor on mount.
clearable
boolean
falseAdds clear icon to the input.
countries
array
By default all countries will be visible in dropdown. Provide Country[] to selectively display countries.
disabled
boolean
falseRenders component in disabled state.
id
string
Id attribute value to be added to the input element and as a label's for attribute value.
intent
'default'| 'success'| 'warning'| 'error'
Defines the intent of the Input.
mapIsoToLabel
function
Function for mapping ISO codes to country names. Useful for localization of the country select dropdown.
maxDropdownHeight
string
Sets the max height of the country select dropdown.
maxDropdownWidth
string
Sets the max width of the country select dropdown.
onChange
function
Called when value is changed.
onInputChange
function
Called when input value is changed.
overrides
custom
Lets you customize all aspects of the component.
placeholder
string
Input's placeholder.
size
'xs'| 'sm'| 'md'
Input's size. SPACIOUS | COMFORTABLE -> md, COMPACT -> xxxs, DEFAULT -> md
value
object
Object which comprises the country set and phoneNumber entered.
variant
'default'| 'line'
Input's variant - default, line

Phone Input exports

You can import this module like so:

import {COUNTRIES} from 'spaceweb/phone-input'

It exports the following components or utility functions:

  • COUNTRIES
  • COUNTRY_SELECT_OPTIONS
  • DEFAULT_MAX_DROPDOWN_HEIGHT
  • DEFAULT_MAX_DROPDOWN_WIDTH
  • EMPTY_LIST_HEIGHT
  • ISO_REGEX
  • ISO_TO_FLAG_OFFSET
  • LIST_ITEM_HEIGHT
  • OVERSCAN_COUNT
  • PhoneInput
  • STATE_CHANGE_TYPE
  • StatefulPhoneInput
  • getPhoneNumberWithCountryDialCode