GithubHelp home page GithubHelp logo

lbgm / phone-number-input Goto Github PK

View Code? Open in Web Editor NEW
29.0 2.0 6.0 445 KB

Phone Input library for Vue 3

License: GNU General Public License v3.0

JavaScript 2.65% HTML 0.58% Vue 25.57% TypeScript 38.58% SCSS 32.62%
phone-number-input vue vuejs-components vuejs-library vuejs3 input phone vue-phone-number-input

phone-number-input's Introduction

Phone number input

Simple Phone Number Input for VueJs

Screenshot

install

npm i @lbgm/phone-number-input

Props & Types

interface PhoneDATA {
  country?: string;
  dialCode?: string | number;
  nationalNumber?: string | number;
  number?: string | number;
  isValid?: boolean;
}

interface Props {
  value?: string;
  label?: string;
  hasError?: boolean;
  hasSuccess?: boolean;
  successMessage?: string;
  errorMessage?: string;
  placeholder?: string;
  name?: string;
  required?: boolean;
  defaultCountry?: string;
  arrow?: boolean;
  listHeight?: number;
  allowed?: string[];
}

// default props values

{
  value: "", // like '22997000000',
  label: "",
  hasError: false,
  hasSuccess: false,
  successMessage: "",
  errorMessage: "",
  placeholder: "",
  name: "",
  required: false,
  defaultCountry: "BJ",
  arrow: true, // show or hide arrow
  listHeight: 150,
  allowed: () => [],
}
  • pass value on this format: ${dialCode}${nationalNumber}
  • allowed is an array of country iso2 (string).

Slots

arrow

<!-- to change arrow icon-->
<phone-input>
 <template #arrow><icon /><template>
</phone-input>

use global slot to append content at the end of the component.

<phone-input>
  <div>Hello</div>
</phone-input>

Use

main.ts :

 import { PhoneInput } from '@lbgm/phone-number-input';

 // register as global component
 app.component('PhoneInput', PhoneInput);

App.vue :

// import component style
import '@lbgm/phone-number-input/style';

use component:

   <phone-input
     @phone="phone = $event"
     @country="country = $event"
     @phoneData="phoneData = $event"
     name="phone-number-input"
     label="Enter your phone"
     required
     :value="'22997788842'"
   />
  • phone is string
  • country is string
  • phoneData is type PhoneDATA

Use it with Vee-validate

Sample wrapper code:

<template>
 <phone-input
   :has-error="hasError"
   :errorMessage="errorMessage"
   @phoneData="validatePhone"
   ref="phoneInput"
 />
</template>
<script setup lang="ts">
import { useField } from 'vee-validate';
import { computed, onMounted, useAttrs, getCurrentInstance, type ComponentInternalInstance } from 'vue';
import { PhoneInput, type PhoneDATA } from '@lbgm/phone-number-input';

type T_PhoneInput = typeof PhoneInput;

const that: ComponentInternalInstance | null = getCurrentInstance();
const attrs = useAttrs();
const emit = defineEmits(['inputData']);

const {
  value: inputValue,
  errorMessage,
  handleBlur,
  handleChange,
  meta,
} = useField(attrs.name, undefined, {
  initialValue: attrs.value ? attrs.value : '',
  validateOnValueUpdate: false,
});

// compute error from vee-validate
const hasError = computed((): boolean => {
  return errorMessage.value !== undefined;
});

const validatePhone = (data: PhoneDATA) => {
  handleChange(data.nationalNumber, false);
  emit('inputData', data);
};

onMounted(() => {
  if ((that?.refs?.phoneInput as T_PhoneInput).phone) {
    handleChange((that?.refs.phoneInput as T_PhoneInput).phone);
  }
});
</script>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.