|
| 1 | +# vue-tel-input v2.x.x |
| 2 | +International Telephone Input with Vue. |
| 3 | + |
| 4 | +[](https://www.npmjs.com/package/vue-tel-input) [](https://github.com/EducationLink/vue-tel-input) |
| 5 | + |
| 6 | +Checkout [Demo](https://educationlink.github.io/vue-tel-input/) or [Playground](https://codesandbox.io/s/0yyyk45q7w?fontsize=14&module=%2Fsrc%2FApp.vue&moduleview=1). |
| 7 | + |
| 8 | +<p align="center"> |
| 9 | +<img width="600px" alt="In-action GIF" src="https://thumbs.gfycat.com/EducatedPoliteBluefintuna-size_restricted.gif"/> |
| 10 | +</p> |
| 11 | + |
| 12 | +## Installation |
| 13 | +- **yarn**: |
| 14 | + ```bash |
| 15 | + yarn add vue-tel-input |
| 16 | + ``` |
| 17 | +- **npm**: |
| 18 | + ```bash |
| 19 | + npm i --save vue-tel-input |
| 20 | + ``` |
| 21 | + |
| 22 | +## Usage |
| 23 | +- Import default `CSS` to your project: |
| 24 | + ```js |
| 25 | + import 'vue-tel-input/dist/vue-tel-input.css'; |
| 26 | + ``` |
| 27 | + |
| 28 | +- In your component: |
| 29 | + ```html |
| 30 | + <template> |
| 31 | + ... |
| 32 | + <vue-tel-input v-model="phone"></vue-tel-input> |
| 33 | + ... |
| 34 | + <template> |
| 35 | + <script> |
| 36 | + import VueTelInput from 'vue-tel-input'; |
| 37 | +
|
| 38 | + export default { |
| 39 | + components: { |
| 40 | + VueTelInput, |
| 41 | + }, |
| 42 | + data() { |
| 43 | + return { |
| 44 | + phone: '', |
| 45 | + }; |
| 46 | + }, |
| 47 | + } |
| 48 | + </script> |
| 49 | + ``` |
| 50 | + |
| 51 | +### Use as a custom field of [vue-form-generator](https://github.com/vue-generators/vue-form-generator) |
| 52 | +- Add a component using `vue-form-generator`'s `abstractField` mixin |
| 53 | + ```js |
| 54 | + // tel-input.vue |
| 55 | + <template> |
| 56 | + <vue-tel-input v-model="value"></vue-tel-input> |
| 57 | + </template> |
| 58 | +
|
| 59 | + <script> |
| 60 | + import VueTelInput from 'vue-tel-input' |
| 61 | + import { abstractField } from 'vue-form-generator'; |
| 62 | +
|
| 63 | + export default { |
| 64 | + name: 'TelephoneInput', |
| 65 | + mixins: [abstractField], |
| 66 | + components: { |
| 67 | + VueTelInput, |
| 68 | + }, |
| 69 | + }; |
| 70 | + </script> |
| 71 | + ``` |
| 72 | +
|
| 73 | +- Register the new field as a global component |
| 74 | + ```js |
| 75 | + import Vue from 'vue'; |
| 76 | + import TelInput from '<path>/tel-input.vue'; |
| 77 | +
|
| 78 | + import 'vue-tel-input/dist/vue-tel-input.css'; |
| 79 | +
|
| 80 | + Vue.component('field-tel-input', TelInput); |
| 81 | + ``` |
| 82 | +
|
| 83 | +- Now it can be used as `tel-input` in schema of `vue-form-generator` |
| 84 | + ```js |
| 85 | + var schema: { |
| 86 | + fields: [{ |
| 87 | + type: "tel-input", |
| 88 | + label: "Awesome (tel input)", |
| 89 | + model: "telephone" |
| 90 | + }] |
| 91 | + }; |
| 92 | + ``` |
| 93 | +Read more on `vue-form-generator`'s [instruction page](https://icebob.gitbooks.io/vueformgenerator/content/fields/custom_fields.html) |
| 94 | + |
| 95 | +### Props |
| 96 | + |
| 97 | + Test all props on [CodeSandbox](https://codesandbox.io/s/0yyyk45q7w?fontsize=14&module=%2Fsrc%2FApp.vue&moduleview=1). |
| 98 | + |
| 99 | + | Property | Type | Default value | Description | |
| 100 | + | -------- | ---- | ------------- | ----------- | |
| 101 | + | `defaultCountry` | `String` | `''` | Default country, will override the country fetched from IP address of user | |
| 102 | + | `disabledFetchingCountry` | `Boolean` | `false` | Disable fetching current country based on IP address of user | |
| 103 | + | `disabled` | `Boolean` | `false` | Disable input field | |
| 104 | + | `disabledFormatting` | `Boolean` | `false` | Disable formatting the phone number in the input, the formatted result still be accessible by `formattedNumber` returned from `onInput` event | |
| 105 | + | `placeholder` | `String` | Enter a phone number | Placeholder for the input | |
| 106 | + | `required` | `Boolean` | `false` | Required property for HTML5 required attribute | |
| 107 | + | `enabledCountryCode` | `Boolean` | `false` | Enable country code in the input | |
| 108 | + | `enabledFlags` | `Boolean` | `true` | Enable flags in the input | |
| 109 | + | `preferredCountries` | `Array` | `[]` | Preferred countries list, will be on top of the dropdown. ie `['AU', 'BR']` | |
| 110 | + | `onlyCountries` | `Array` | `[]` | List of countries will be shown on the dropdown. ie `['AU', 'BR']` | |
| 111 | + | `ignoredCountries` | `Array` | `[]` | List of countries will NOT be shown on the dropdown. ie `['AU', 'BR']` | |
| 112 | + | `autocomplete` | `String` | `'on'` | Native input 'autocomplete' attribute | |
| 113 | + | `name` | `String` | `'telephone'` | Native input 'name' attribute | |
| 114 | + | `maxLen` | `Number` | `25` | Native input 'maxlength' attribute | |
| 115 | + | `wrapperClasses` | `String` | `Array` | `Object` | `''` | Custom classes for the wrapper | |
| 116 | + | `inputClasses` | `String` | `Array` | `Object` | `''` | Custom classes for the `input` | |
| 117 | + | `inputId` | `String` | `''` | Custom 'id' for the `input` | |
| 118 | + | `dropdownOptions` | `Object` | `{ disabledDialCode: false, tabindex: 0 }` | Options for dropdown, supporting `disabledDialCode` and `tabindex`| |
| 119 | + | `inputOptions` | `Object` | `{ showDialCode: false, tabindex: 0 }` | Options for input, supporting `showDialCode` (always show dial code in the input) and `tabindex`| |
| 120 | + | `validCharactersOnly` | `Boolean` | `false` | Only allow valid characters in a phone number (will also verify in `mounted`, so phone number with invalid characters will be shown as an empty string) | |
| 121 | + |
| 122 | +### Events |
| 123 | + |
| 124 | + | Event | Arguments | Description | Notes | |
| 125 | + | ----- | --------- | ----------- | ----- | |
| 126 | + | `input` | `String`, `Object` | Fires when the input changes with the argument is the object includes `{ number, isValid, country }` | `onInput` deprecated | |
| 127 | + | `validate` | `Object` | Fires when the correctness of the phone number changes (from `true` to `false` or vice-versa) and when the component is mounted `{ number, isValid, country }` | `onValidate` deprecated | |
| 128 | + | `blur` | | Fires on blur event | `onBlur` deprecated | |
| 129 | + | `space` | | Fires on keyup.space event | `onSpace` deprecated | |
| 130 | + | `enter` | | Fires on keyup.enter event | `onEnter` deprecated | |
| 131 | + | `open` | | Fires when the flags dropdown opens | | |
| 132 | + | `close` | | Fires when the flags dropdown closes | | |
| 133 | + | `country-changed` | `Object` | Fires when country changed (even for the first time) | Available from `v2.4.2` | |
| 134 | + |
| 135 | +### Slots |
| 136 | + | Slot | Description | Notes | |
| 137 | + | ---- | ----------- | ----- | |
| 138 | + | `arrow-icon` | Replace the arrow next to the flag with a component of your choice | Available from [v2.4.3](https://github.com/EducationLink/vue-tel-input/releases/tag/v2.4.3) | |
| 139 | + |
| 140 | +## Highlights & Credits |
| 141 | +- Telephone Number parsing, validation by [libphonenumber-js](https://catamphetamine.github.io/libphonenumber-js/). |
| 142 | +- Country Codes data from [intl-tel-input](https://github.com/jackocnr/intl-tel-input/blob/master/src/js/data.js). |
| 143 | +- User's country by [ip2c.org](https://ip2c.org/s), request using [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). |
| 144 | +
|
| 145 | +## Demo Usage |
| 146 | +
|
| 147 | +```bash |
| 148 | +
|
| 149 | +# install dependencies |
| 150 | +$ yarn/npm install |
| 151 | +
|
| 152 | +# compile demo for development |
| 153 | +$ yarn/npm dev |
| 154 | +
|
| 155 | +# open Browser and start serving in demo |
| 156 | +$ yarn/npm demo:open |
| 157 | +
|
| 158 | +# compile dist demo |
| 159 | +$ yarn/npm dist:demo |
| 160 | +
|
| 161 | +# compile dist |
| 162 | +$ yarn/npm dist |
| 163 | +
|
| 164 | +``` |
| 165 | +
|
| 166 | +## Typescript Support |
| 167 | +
|
| 168 | +If you working with typescript will need declaration requirements. |
| 169 | +
|
| 170 | +```bash |
| 171 | +npm install --save-dev @types/vue-tel-input |
| 172 | +``` |
| 173 | +
|
| 174 | +## Contributors |
| 175 | +- [mikob](https://github.com/mikob) for super awesome work to [remove the bootstrap dependency](https://github.com/EducationLink/vue-tel-input/pull/13). |
| 176 | +- [kalcifield](https://github.com/kalcifield) for helping make the input [preload with a phone number](https://github.com/EducationLink/vue-tel-input/pull/8). |
| 177 | +- [serbemas](https://github.com/serbemas) for [adding web components support](https://github.com/EducationLink/vue-tel-input/pull/92). |
| 178 | +
|
| 179 | +...[more](https://github.com/EducationLink/vue-tel-input/graphs/contributors) |
| 180 | +
|
| 181 | +made with ❤ by [Steven](https://github.com/iamstevendao). |
0 commit comments