vue-tel-input

repository·main·Indexed 21 days ago

https://github.com/iamstevendao/vue-tel-input

An international telephone input component for Vue applications that provides a user-friendly interface for selecting country codes and entering phone numbers. It supports Vue 3, Vue 2 (via @legacy), and CDN installation, featuring v-model binding, IP-based country detection, and customizable props for country filtering and input formatting.

Tokens
7.3K
Snippets
23
Records
36
Agent score
72%

What's inside vue-tel-input

  1. Customize the arrow icon using the arrow-icon slot

    main

    You can replace the default arrow icon located next to the country flag by using the arrow-icon slot. This allows you to provide your own Vue component or HTML element to customize the appearance of the dropdown trigger.

    <vue-tel-input>
      <template #arrow-icon>
        <!-- Your custom component or icon here -->
        <i class="custom-arrow-class"></i>
      </template>
    </vue-tel-input>
  2. Install vue-tel-input via npm

    main

    Install the package using npm:

    npm install vue-tel-input

    Global Installation

    To install the plugin globally with optional default settings, use Vue.use():

    import Vue from 'vue'
    import VueTelInput from 'vue-tel-input'
    
    // Define default global options here (optional)
    Vue.use(VueTelInput, [globalOptions = {}])

    Local Component Usage

    To use the component directly in a specific Vue component:

    <!-- your-component.vue-->
    <template>
      <vue-tel-input v-model="value"></vue-tel-input>
    </template>
    
    <script>
    import { VueTelInput } from 'vue-tel-input'
    
    export default {
      components: {
        VueTelInput,
      },
    };
    </script>
  3. Register the vue-tel-input plugin in your Vue app

    main

    After installation, import the VueTelInput component and its required CSS file, then register it as a plugin in your Vue application instance.

    import Vue from 'vue';
    import VueTelInput from 'vue-tel-input';
    import 'vue-tel-input/vue-tel-input.css';
    
    const app = createApp(App);
    app.use(VueTelInput);
    app.mount('#app');
  4. Use vue-tel-input as a custom field in vue-form-generator

    main

    You can integrate vue-tel-input into vue-form-generator by creating a custom field component that uses the abstractField mixin provided by vue-form-generator.

    Follow these three steps:

    1. Create the custom component: Define a new Vue component (e.g., tel-input.vue) that wraps <vue-tel-input> and includes the abstractField mixin.
    2. Register the component: Register this component globally in your Vue application using the naming convention field-<name>. For a component named TelInput, register it as field-tel-input.
    3. Update your schema: Use the name used in the registration (without the field- prefix) as the type in your vue-form-generator schema.

    An interactive example is available on CodeSandbox.

    <!-- 1. tel-input.vue -->
    <template>
      <vue-tel-input v-model="value"></vue-tel-input>
    </template>
    
    <script>
      import { abstractField } from 'vue-form-generator';
    
      export default {
        name: 'TelephoneInput',
        mixins: [abstractField],
      };
    </script>
    
    // 2. Register globally
    import Vue from 'vue';
    import TelInput from './tel-input.vue';
    
    Vue.component('field-tel-input', TelInput);
    
    // 3. Use in schema
    var schema = {
      fields: [
        {
          type: 'tel-input',
          label: 'Awesome (tel input)',
          model: 'telephone',
        },
      ],
    };
  5. Lazy load the vue-tel-input component

    main

    To reduce initial bundle size, you can asynchronously import the component and its CSS. This is useful for large libraries (approx. 200kb JS and 100kb CSS) that are only needed on specific routes. Use a dynamic import that returns a Promise resolving to the component.

    <!-- your-component.vue-->
    <template>
      <vue-tel-input v-model="value"></vue-tel-input>
    </template>
    
    <script>
      const VueTelInput = () =>
        Promise.all([
          import(/* webpackChunkName: "chunk-vue-tel-input" */ 'vue-tel-input'),
          import(/* webpackChunkName: "chunk-vue-tel-input" */ 'vue-tel-input/vue-tel-input.css'),
        ]).then(([{ VueTelInput }]) => VueTelInput);
    
      export default {
        components: {
          VueTelInput,
        },
      };
    </script>
  6. Setup and basic usage for vue-tel-input v2.x.x

    main

    To use vue-tel-input in a Vue 2 project, you must first import the default CSS and then register the component in your Vue instance. Use v-model to bind the phone number value.

    1. Import the CSS: import 'vue-tel-input/dist/vue-tel-input.css';

    2. Import and register the component in your Vue component.

    <template>
      <vue-tel-input v-model="phone"></vue-tel-input>
    </template>
    
    <script>
    import VueTelInput from 'vue-tel-input';
    
    export default {
      components: {
        VueTelInput,
      },
      data() {
        return {
          phone: '',
        };
      },
    }
    </script>
  7. Add a right-side icon using the icon-right slot

    main

    The icon-right slot allows you to add an icon or element to the right side of the input field. This is commonly used for displaying validation status icons (e.g., a checkmark or error icon) or other decorative elements.

    <vue-tel-input>
      <template #icon-right>
        <!-- Your validation icon or other element -->
        <span class="validation-icon">✔</span>
      </template>
    </vue-tel-input>
  8. Register vue-tel-input as a global plugin in Vue 3

    main

    To use the component globally, import VueTelInput and its associated CSS, then use app.use() in your main entry file. You can optionally pass a globalOptions object to define default settings for all instances of the component.

    import { createApp } from 'vue';
    import App from './App.vue';
    import VueTelInput from 'vue-tel-input';
    import 'vue-tel-input/vue-tel-input.css';
    
    const globalOptions = {
      mode: 'auto',
    };
    
    const app = createApp(App);
    app.use(VueTelInput, globalOptions); // Define default global options here (optional)
    app.mount('#app');
  9. Register vue-tel-input as a Vue plugin

    main

    To use the plugin globally with default options, import VueTelInput and its CSS, then use app.use(). You can pass a globalOptions object to app.use() to define default settings for all instances of the component.

    import { createApp } from 'vue';
    import App from './App.vue';
    import VueTelInput from 'vue-tel-input';
    import 'vue-tel-input/vue-tel-input.css';
    
    const globalOptions = {
      mode: 'auto',
    };
    
    const app = createApp(App);
    app.use(VueTelInput, globalOptions); // Define default global options here (optional)
    app.mount('#app');
  10. Install vue-tel-input via Browser (CDN)

    main

    For projects using Vue via a script tag in the browser, include the following files:

    <script src="https://unpkg.com/vue-tel-input@5"></script>
    <link rel="stylesheet" href="https://unpkg.com/vue-tel-input/dist/vue-tel-input.css" />

    If Vue is already detected on the page, the plugin is installed automatically. Otherwise, manually install it using:

    Vue.use(window['vue-tel-input']);