PhoneInput

A phone number input with country code selector, built on react-phone-input-2.

Import

import { PhoneInput, isValidPhoneNumber } from '@tidbcloud/uikit/biz'

Basic Usage

import { useState } from 'react'
import { PhoneInput } from '@tidbcloud/uikit/biz'
 
function Demo() {
  const [phone, setPhone] = useState('')
 
  return <PhoneInput value={phone} onChange={setPhone} placeholder="Enter phone number" />
}

With Default Country

import { useState } from 'react'
import { PhoneInput } from '@tidbcloud/uikit/biz'
 
function Demo() {
  const [phone, setPhone] = useState('')
 
  return <PhoneInput value={phone} onChange={setPhone} country="us" placeholder="Enter phone number" />
}

Validation

import { useState } from 'react'
import { PhoneInput, isValidPhoneNumber } from '@tidbcloud/uikit/biz'
import { Button, Stack, Text } from '@tidbcloud/uikit'
 
function Demo() {
  const [phone, setPhone] = useState('')
  const [isValid, setIsValid] = useState(null)
 
  const handleValidate = () => {
    setIsValid(isValidPhoneNumber(phone))
  }
 
  return (
    <Stack>
      <PhoneInput value={phone} onChange={setPhone} />
      <Button onClick={handleValidate}>Validate</Button>
      {isValid !== null && (
        <Text c={isValid ? 'green' : 'red'}>{isValid ? 'Valid phone number' : 'Invalid phone number'}</Text>
      )}
    </Stack>
  )
}

With Form

import { Form, FormPhoneInput } from '@tidbcloud/uikit/biz'
 
function Demo() {
  return (
    <Form onSubmit={(data) => console.log(data)}>
      <FormPhoneInput name="phone" label="Phone Number" rules={{ required: 'Phone number is required' }} />
    </Form>
  )
}

Props

PropTypeDefaultDescription
valuestring-Phone number value
onChange(value: string) => void-Change handler
placeholderstring''Input placeholder
countrystring''Default country code (e.g., ‘us’)
enableAreaCodeStretchboolean-Show country code after first focus
rootPropsBoxProps-Root container props
inputClassstring-Input CSS class
buttonClassstring-Button CSS class
containerClassstring-Container CSS class
dropdownClassstring-Dropdown CSS class
searchClassstring-Search CSS class
onFocus(e) => void-Focus handler

Utilities

FunctionDescription
isValidPhoneNumberValidates a phone number string