基于react的电话号码输入框

本文介绍了基于React的电话号码输入框组件react-phonenumber-text-input的使用,包括在线示例、安装步骤和基本、高级用法。组件支持自定义样式,并提供了国家编码选择器的定制选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

react-phonenumber-text-input

基于 React 的电话号码输入组件

在线示例

请添加图片描述

Install

  npm install react-phonenumber-text-input --save
  yarn add react-phonenumber-text-input
  pnpm add react-phonenumber-text-input
使用
  1. 基本用法

    import PhonenumberInput from 'react-phonenumber-input'
    import { useState } from 'react'
    
    function App() {
      const [value, setValue] = useState('')
      const [valid, setValid] = useState(false)
      const [formatedPhonenumber, setFormatedPhonenumber] = useState('')
      const [fullValue, setFullValue] = useState('')
    
      return (
        <div
          className="App"
          style={{
            margin: '100px',
            textAlign: 'left',
          }}
        >
          <div
            style={{
              height: '60px',
            }}
          ></div>
          <div>
            <label
              style={{
                lineHeight: '30px',
                fontSize: '12px',
              }}
            >
              <span>Phone Number Input:</span>
            </label>
            <PhonenumberInput
              value={value}
              onChange={(v, metadata) => {
                setValue(v)
                setValid(metadata.valid)
                setFormatedPhonenumber(metadata.formated)
                setFullValue(metadata.fullValue)
              }}
              style={{
                width: '200px',
              }}
            />
            <p>
              value: {value} <br />
              valid: {valid ? 'true' : 'false'} <br />
              formated Phone number: {formatedPhonenumber} <br />
              full value: {fullValue} <br />
            </p>
          </div>
        </div>
      )
    }
    
  2. 使用原生 select 标签的国家编码选择器

    import { PhoneNumberInputWithNativeSelect } from 'react-phonenumber-input'
    import { useState } from 'react'
    
    function App() {
      const [value, setValue] = useState('')
      const [valid, setValid] = useState(false)
      const [formatedPhonenumber, setFormatedPhonenumber] = useState('')
      const [fullValue, setFullValue] = useState('')
    
      return (
        <div
          className="App"
          style={{
            margin: '100px',
            textAlign: 'left',
          }}
        >
          <div
            style={{
              height: '60px',
            }}
          ></div>
          <div>
            <label
              style={{
                lineHeight: '30px',
                fontSize: '12px',
              }}
            >
              <span>Phone Number Input:</span>
            </label>
            <PhoneNumberInputWithNativeSelect
              value={value}
              onChange={(v, metadata) => {
                setValue(v)
                setValid(metadata.valid)
                setFormatedPhonenumber(metadata.formated)
                setFullValue(metadata.fullValue)
              }}
              style={{
                width: '200px',
              }}
            />
            <p>
              value: {value} <br />
              valid: {valid ? 'true' : 'false'} <br />
              formated Phone number: {formatedPhonenumber} <br />
              full value: {fullValue} <br />
            </p>
          </div>
        </div>
      )
    }
    
  3. 组件属性说明

    名称类型默认值描述
    valuestring‘’输入框的值
    onChange(value: string, metadata: { formated: string; fullValue: string; valid: boolean }) => void-输入框值改变时的回调函数;metadata.formated 是格式化后的电话号码,metadata.fullValue 是完整的电话号码,metadata.valid 是电话号码是否有效
    countriesCountryCode[]所有的国家国家编码列表
    defaultCountryCountryCodefirst country of props.countries默认的国家编码
    placeholderstring‘’输入框的占位符
    showCallingCodebooleantrue是否显示国家 callingCode 码
    onCountryCodeChange(countryCode: CountryCode) => void-国家编码改变时的回调函数
    classNamestringundfined输入框包裹容器的 className
    styleReact.CSSPropertiesundfined输入框包裹容器的 style
    inputClassNamestringundfined输入框的 className
    inputStyleReact.CSSPropertiesundfined输入框的 style
    selectClassNamestringundfinedslect 框的 className
    selectStyleReact.CSSPropertiesundfinedslect 框的 style
    optionClassNamestringundfinedoption 的 className (对使用原生 select 的 option 标签无效)
    optionStyleReact.CSSPropertiesundfinedoption 的 style (对使用原生 select 的 option 标签无效)
  4. 自定义样式

    • 基础用法
      • 使用classNamestyle属性自定义输入框的样式
      • 使用inputClassNameinputStyle属性自定义输入框的样式
      • 使用selectClassNameselectStyle属性自定义国家编码选择器的样式
      • 使用optionClassNameoptionStyle属性自定义国家编码选择器的选项的样式
    • 高级用法
      使用 BaseReactPhonenumberInput 组件 (具体用法可以看看 online demo)
      BaseReactPhonenumberInput 有四个额外的属性:
      • SelectComponent
      • CallingCodeComponent
      • InputComponent
      • WrapperComponent
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值