Phone.vue 2.1 KB

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