国际电话组件封装使用以及vuephonenumberinput属性解读

18 篇文章 1 订阅

phone-number-input

组件描述:国际电话输入组件。
适用场景:电话号码输入框。
特点:

  1. 包含各个国家的区号;
  2. 输入一串号码,可给出是否是正确号码的校验结果;
  3. 灵活性、可变性强;可满足绝大多数场景下的业务需要。

Image text

props

propstypedefaultexplain
valueString‘’组件绑定的值,默认为空
errorBooleanfalse值为true时展示校验失败的外观
no-exampleBooleanfalse是否展示输入示例
disabledBooleanfalse是否禁用
clearableBooleanfalse是否可清空
show-code-on-listBooleanfalse是否在国家选择器内展示国家区号
no-flagsBooleanfalse是否在国家选择器内展示国家的国旗
border-radiusNumber0边框圆角大小
colorStringdodgerblue默认外观
valid-colorStringyellowgreen校验通过的边框颜色
translationsObject{自定义文案
countrySelectorLabel: ‘国家’,有4个属性可配置不同地方的显示文案;
countrySelectorError: ‘请选择国家’,详见左侧单元格内
phoneNumberLabel: ‘号码’,
example: ‘示例 :’
}
no-country-selectorBooleanfalse是否不展示国家选择框,默认展示
preferred-countriesArray[]将国家选择框中指定countryCode的国家前置,如中国:[‘CN’]

event

eventreturnexplain
updateallValue组件值更改时触发;返回参数:国际电话组件的全部数据,包括组件绑定的值、各种号码格式以及校验结果等;
inputvalue组件值更改时触发;返回参数:国际电话组件绑定的值
PhoneNumberBlur输入框失去焦点时触发

usage

  1. 引用
 import PhoneNumberInput from 'fe-coms/components/business/phone-number-input'
  1. 注册
  components: {
    PhoneNumberInput
  }
  1. 使用
  <template>
    <PhoneNumberInput value="yourValue" />
  </template>

源码

以下代码对VuePhoneNumberInput的大部分属性做了封装,满足绝大多数使用场景;

<template>
  <div
  >
    <VuePhoneNumberInput
      v-model="value"
      @update="update"
      @input="input"
      @phone-number-blur="PhoneNumberBlur"
      :error="error"
      :valid-color="ValidColor"
      :color="color"
      :no-country-selector="NoCountrySelector"
      :preferred-countries="PreferredCountries"
      :disabled="disabled"
      :clearable="clearable"
      :show-code-on-list="showCodeOnList"
      :no-flags="NoFlags"
      :border-radius="BorderRadius"
      :no-example='NoExample'
      :translations="translations"
    >
    </VuePhoneNumberInput>
  </div>
</template>

<script>
import VuePhoneNumberInput from 'vue-phone-number-input'
import 'vue-phone-number-input/dist/vue-phone-number-input.css'
export default {
  name: 'PhoneNumberInput',
  components: {
    VuePhoneNumberInput
  },
  data () {
    return {
    }
  },
  props: {
    value: { //组件绑定的值
      type: String,
      default: ''
    },
    error: { //校验失败
      type: Boolean,
      default: false
    },
    disabled: { //禁用
      type: Boolean,
      default: false
    },
    clearable: { //是否可清空
      type: Boolean,
      default: false
    },
    ValidColor: { //验证通过的颜色
      type: String,
      default: 'yellowgreen'
    },
    color: { //默认外观
      type: String,
      default: 'dodgerblue'
    },
    translations: { //自定义文案
      type: Object,
      default: {
        countrySelectorLabel: '国家',
        countrySelectorError: '请选择国家',
        phoneNumberLabel: '号码',
        example: '示例 :'
      }
    },
    NoCountrySelector: { //是否不使用国家选择器,默认使用
      type: Boolean,
      default: false
    },
    PreferredCountries: { //默认展示在前面的国家,数组格式,值是国家代码的集合,如中国“CN”
      type: Array,
      default: []
    },
    BorderRadius: { 
      type: Number,
      default: 0
    },
    ShowCodeOnList: { //是否在国家选择器内展示国际号
      type: Boolean,
      default: false
    },
    NoFlags: { //是否在国家选择器内展示国家的国旗
      type: Boolean,
      default: false
    },
    NoExample: { //是否展示输入示例
      type: Boolean,
      default: false
    }
  },
  methods: {
    input (value) {
      console.log(value)
      this.$emit('input', value)
    },
    update (allValue) {
      this.$emit('update', allValue)
    },
    PhoneNumberBlur () {
      this.$emit('PhoneNumberBlur')
    }
  }
}
</script>

<style lang="scss" scoped>
   /deep/ .country-selector__label,
   /deep/ .input-tel__label {
     line-height: 1;
   }
</style>

关于VuePhoneNumberInput

官方地址:https://github.com/LouisMazel/vue-phone-number-input

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值