37
37
</li >
38
38
</ul >
39
39
</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')"
42
45
v-bind:name =" name"
43
46
v-on:input =" handleChangePhoneNumber"
44
47
v-bind:value =" phoneNumber"
49
52
50
53
<script >
51
54
/* eslint-disable no-unused-vars */
55
+ /* eslint-disable camelcase */
56
+ import { is_valid_number , format } from ' libphonenumber-js' ;
52
57
53
58
const has = Object .hasOwnProperty ;
54
59
const isObject = maybeObj => Object .prototype .toString .call (maybeObj) === ' [object Object]' ;
304
309
ax: { code: ' ax' , name: ' Åland Islands' , dialCode: 358 , example: ' 041 2345678' },
305
310
};
306
311
307
- /* eslint-enable object-curly-newline, no-unused-vars */
308
312
309
313
let counter = 0 ;
310
314
346
350
type: Function ,
347
351
default: null ,
348
352
},
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
+ },
349
365
},
350
366
351
367
data () {
352
- const { code , availableOnly , value } = this ;
368
+ const { code , availableOnly , value , dialCode } = this ;
369
+ /* eslint-enable object-curly-newline, no-unused-vars */
353
370
let tmpPhonesData = Object .assign ({}, phonesData);
354
371
355
372
if (isArray (availableOnly)) {
362
379
}
363
380
364
381
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 */
367
392
if (! needCode) {
368
393
throw new Error (' Available data is empty.Please set correct "availableOnly" attribute [vue-phone-input]' );
369
394
}
372
397
phoneNumber: value,
373
398
countryCode: needCode,
374
399
isVisiblePanel: false ,
400
+ valid: false ,
401
+ // formatter: new asYouType()
375
402
};
376
403
},
377
404
407
434
408
435
methods: {
409
436
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 ());
412
440
if (isFunction (this .onChange )) {
413
441
this .onChange (this );
414
442
}
433
461
},
434
462
435
463
mounted () {
464
+ this .valid = is_valid_number (this .phoneNumber , this .countryCode .toUpperCase ());
465
+ /* eslint-enable camelcase */
436
466
if (isFunction (this .onInit )) {
437
467
this .onInit (this );
438
468
}
439
469
},
440
470
};
441
- </script >
471
+ </script >
0 commit comments