0.1.13 • Published 11 months ago

base-vue-phone-input v0.1.13

Weekly downloads
-
License
-
Repository
github
Last release
11 months ago

Base vue phone input

Basic phone input deeply customizable with slots

Installation

using npm

  npm install basic-vue-phone-input

Examples

Demo with shadcn/vue

Usage

<script setup lang="ts">
import { ref } from 'vue'
import PhoneInput from './index'

const tel = ref('')
const res = ref()
</script>

<template>
    <PhoneInput
        v-model="tel"// for phone number
        @update="res = $event"// for phone number object />
    {{ res }}
    {{ tel }}
</template>

But this input is meant to be customized

Features

  • You can set preferred-countries, ignored-countries or have only-countries
  • Multi options to getting country code : By default the component get the country code via the browser (disable it with no-use-browser-locale) or you can use fetch-country to get the country code via https://ip2c.org/s (network needed) - you can use default-country-code option instead to set one
  • Phone number formatting while typing
  • You can search your country in list (open countries list & type your country name)
  • Keyboard accessibility (Arrow down, Arrow up: Countries list navigation - Escape: Close countries list)
  • Phone number example for each country in placeholder
  • Fully cusstomizable with slots

Props API

Prop nameDescriptionTypeValuesDefault
modelValue@model Country calling code + telephone number in international formatstring-undefined
countryCode@model Country code selected - Ex: "FR"CountryCode-undefined
placeholderPlaceholder of the inputstring-undefined
labellabel of the inputstring-undefined
preferredCountriesList of country codes to place first in the select list - Ex: ['FR', 'BE', 'GE']Array-undefined
ignoredCountriesList of country codes to be removed from the select list - Ex: ['FR', 'BE', 'GE']Array-undefined
onlyCountriesList of country codes to only have the countries selected in the select list - Ex: ['FR', 'BE', 'GE']Array-undefined
translationsLocale strings of the componentPartial-undefined
noUseBrowserLocaleBy default the component use the browser locale to set the default country code if not country code is providedboolean-
fetchCountryThe component will make a request (https://ipwho.is) to get the location of the user and use it to set the default country codeboolean-
customCountriesListReplace country namesRecord-undefined
autoFormatDisabled auto-format when phone is valid @default trueboolean-true
noFormattingAsYouTypeDisabled auto-format as you type @default falseboolean-false
countryLocalelocale of country list - Ex: "fr-FR" @default {string} browser localestring-undefined
excludeSelectorsExclude selectors to close country selector list - usefull when you using custom flagArray-undefined

Events API

EventReturn
inputAsYouType value (emit when new value is enter on phone number input && when a country is choosed)
updateAll values (Result type)

Keyboard accessibility

PropsAction
ArrowDownNavigation down in countries list
ArrowUpNavigation up in countries list
EscapeClose countries list

Named slots

NameDescriptionBindings
selectorChange the country selector componentinput-value String - current selected country code - Ex: "FR" updateInputValue - action for changing country code by passing country code countries - array of all countries
inputChange the input componentinput-value String - phone number value updateInputValue - action for updating phone value placeholder - placeholder

Translations

Labels & placeholders

<PhoneInput
  :translations="{
    phoneInput: {
      placeholder: 'Phone number',
      example: 'Example:',
    },
  }"
/>

Country list

Two ways to translate the country list:

First solution - set country locale

<PhoneInput country-locale='fr-FR' />

Second solution - custom list

<PhoneInput
  :custom-countries-list="{
    FR: 'France',
    BE: 'Belgique',
    DE: 'Allemagne',
    US: 'Etats-Unis',
  }"
/>

Types

Results emitted by @update or @data event

export type Results = {
  isValid: boolean
  isPossible?: boolean
  countryCode?: CountryCode
  countryCallingCode?: CountryCallingCode
  nationalNumber?: NationalNumber
  type?: NumberType
  formatInternational?: string
  formatNational?: string
  uri?: string
  e164?: string
  rfc3966?: string
}

Country type

export interface Country {
    iso2: CountryCode
    dialCode: CountryCallingCode
    name: string
}
0.1.10

11 months ago

0.1.11

11 months ago

0.1.12

11 months ago

0.1.13

11 months ago

0.1.9

11 months ago

0.1.8

11 months ago

0.1.7

11 months ago

0.1.4

11 months ago

0.1.3

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago