Phone.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <!--
  2. Champs de saisie d'un numéro de téléphone
  3. @see https://github.com/yogakurniawan/vue-tel-input-vuetify
  4. -->
  5. <template>
  6. <client-only>
  7. <vue-tel-input-vuetify
  8. :field="field"
  9. :label="label"
  10. :value="data"
  11. :readonly="readonly"
  12. clearable
  13. valid-characters-only
  14. validate-on-blur
  15. :rules="rules"
  16. :active-country="{iso2: 'FR'}"
  17. @input="onInput"
  18. @change="onChange"
  19. />
  20. </client-only>
  21. </template>
  22. <script lang="ts">
  23. import { defineComponent, Ref, ref, useContext } from '@nuxtjs/composition-api'
  24. export default defineComponent({
  25. props: {
  26. label: {
  27. type: String,
  28. required: false,
  29. default: ''
  30. },
  31. field: {
  32. type: String,
  33. required: false,
  34. default: null
  35. },
  36. data: {
  37. type: [String, Number],
  38. required: false,
  39. default: null
  40. },
  41. readonly: {
  42. type: Boolean,
  43. required: false
  44. },
  45. error: {
  46. type: Boolean,
  47. required: false
  48. },
  49. errorMessage: {
  50. type: String,
  51. required: false,
  52. default: null
  53. }
  54. },
  55. setup () {
  56. const { app: { i18n } } = useContext()
  57. const nationalNumber: Ref<string | number> = ref('')
  58. const internationalNumber: Ref<string | number> = ref('')
  59. const isValid: Ref<boolean> = ref(false)
  60. const country: Ref<string> = ref('')
  61. return {
  62. nationalNumber,
  63. internationalNumber,
  64. isValid,
  65. country,
  66. rules: [
  67. () => isValid.value || i18n.t('phone_error')
  68. ]
  69. }
  70. },
  71. methods: {
  72. onInput (_: any, { number, valid, country }: { number: any, valid: boolean, country: any }) {
  73. this.isValid = valid
  74. this.nationalNumber = number.national
  75. this.internationalNumber = number.international
  76. this.country = country && country.name
  77. // console.log(this.field, this.isValid, this.nationalNumber, this.internationalNumber, this.country)
  78. },
  79. onChange () {
  80. if (this.isValid) {
  81. this.$emit('update', this.internationalNumber, this.field)
  82. }
  83. }
  84. }
  85. })
  86. </script>
  87. <style>
  88. input:read-only{
  89. color: #666 !important;
  90. }
  91. </style>