Skip to content

Commit c173351

Browse files
author
Nabarun Gogoi
committed
modified from MitrophD : v0.1
1 parent 2d9b5e1 commit c173351

File tree

6 files changed

+4336
-12
lines changed

6 files changed

+4336
-12
lines changed

lib/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/styles.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@
5858
},
5959
"peerDependencies": {
6060
"vue": "^2.4.4"
61+
},
62+
"dependencies": {
63+
"libphonenumber-js": "^0.4.48"
6164
}
6265
}

src/VuePhoneInput/index.vue

+39-9
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@
3737
</li>
3838
</ul>
3939
</div>
40-
<input type="text"
41-
autocomplete="off"
40+
<input type="tel"
41+
autocomplete="off"
42+
class="input"
43+
:class="[error ? errorClass : '']"
44+
@focus="$emit('focus')"
4245
v-bind:name="name"
4346
v-on:input="handleChangePhoneNumber"
4447
v-bind:value="phoneNumber"
@@ -49,6 +52,8 @@
4952

5053
<script>
5154
/* eslint-disable no-unused-vars */
55+
/* eslint-disable camelcase */
56+
import { is_valid_number, format } from 'libphonenumber-js';
5257
5358
const has = Object.hasOwnProperty;
5459
const isObject = maybeObj => Object.prototype.toString.call(maybeObj) === '[object Object]';
@@ -304,7 +309,6 @@
304309
ax: { code: 'ax', name: 'Åland Islands', dialCode: 358, example: '041 2345678' },
305310
};
306311
307-
/* eslint-enable object-curly-newline, no-unused-vars */
308312
309313
let counter = 0;
310314
@@ -346,10 +350,23 @@
346350
type: Function,
347351
default: null,
348352
},
353+
error: {
354+
type: Boolean,
355+
default: false,
356+
},
357+
errorClass: {
358+
type: String,
359+
default: null,
360+
},
361+
dialCode: {
362+
type: Number,
363+
default: -1,
364+
},
349365
},
350366
351367
data() {
352-
const { code, availableOnly, value } = this;
368+
const { code, availableOnly, value, dialCode } = this;
369+
/* eslint-enable object-curly-newline, no-unused-vars */
353370
let tmpPhonesData = Object.assign({}, phonesData);
354371
355372
if (isArray(availableOnly)) {
@@ -362,8 +379,16 @@
362379
}
363380
364381
this.availableData = tmpPhonesData;
365-
const needCode = has.call(this.availableData, code) ? code : Object.keys(this.availableData)[0];
366-
382+
let needCode = has.call(this.availableData, code) ? code : Object.keys(this.availableData)[0];
383+
if (dialCode > 0) {
384+
/* eslint-disable no-restricted-syntax */
385+
for (const key in this.availableData) {
386+
if (this.availableData[key].dialCode === dialCode) {
387+
needCode = key;
388+
}
389+
}
390+
}
391+
/* eslint-enable no-restricted-syntax */
367392
if (!needCode) {
368393
throw new Error('Available data is empty.Please set correct "availableOnly" attribute [vue-phone-input]');
369394
}
@@ -372,6 +397,8 @@
372397
phoneNumber: value,
373398
countryCode: needCode,
374399
isVisiblePanel: false,
400+
valid: false,
401+
// formatter: new asYouType()
375402
};
376403
},
377404
@@ -407,8 +434,9 @@
407434
408435
methods: {
409436
handleChangePhoneNumber(event) {
410-
this.phoneNumber = event.target.value;
411-
437+
// this.phoneNumber = new asYouType(this.countryCode.toUpperCase()).input(event.target.value);
438+
this.phoneNumber = format({ country: this.countryCode.toUpperCase(), phone: event.target.value }, 'National');
439+
this.valid = is_valid_number(this.phoneNumber, this.countryCode.toUpperCase());
412440
if (isFunction(this.onChange)) {
413441
this.onChange(this);
414442
}
@@ -433,9 +461,11 @@
433461
},
434462
435463
mounted() {
464+
this.valid = is_valid_number(this.phoneNumber, this.countryCode.toUpperCase());
465+
/* eslint-enable camelcase */
436466
if (isFunction(this.onInit)) {
437467
this.onInit(this);
438468
}
439469
},
440470
};
441-
</script>
471+
</script>

src/style.less

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.intl-phone-input {
22
position:relative;
3-
display:inline-block;
3+
display:block;
44
font-family: Arial, Helvetica, sans-serif;
55

66
input[type="text"] {

0 commit comments

Comments
 (0)