Phone.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. :error="error || !!violation"
  9. :error-messages="errorMessage || violation ? $t(violation) : ''"
  10. :field="field"
  11. :label="label"
  12. v-model="myPhone"
  13. :readonly="readonly"
  14. clearable
  15. valid-characters-only
  16. validate-on-blur
  17. :rules="rules"
  18. @input="onInput"
  19. @change="onChangeValue"
  20. />
  21. </client-only>
  22. </template>
  23. <script lang="ts">
  24. import { defineComponent, Ref, ref, useContext, computed } from '@nuxtjs/composition-api'
  25. import {useError} from "~/composables/form/useError";
  26. export default defineComponent({
  27. props: {
  28. label: {
  29. type: String,
  30. required: false,
  31. default: ''
  32. },
  33. field: {
  34. type: String,
  35. required: false,
  36. default: null
  37. },
  38. data: {
  39. type: [String, Number],
  40. required: false,
  41. default: null
  42. },
  43. readonly: {
  44. type: Boolean,
  45. required: false
  46. },
  47. error: {
  48. type: Boolean,
  49. required: false
  50. },
  51. errorMessage: {
  52. type: String,
  53. required: false,
  54. default: null
  55. }
  56. },
  57. setup (props, {emit}) {
  58. const { app: { i18n }, store } = useContext()
  59. const {violation, onChange} = useError(props.field, emit, store)
  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. return {
  86. myPhone,
  87. violation,
  88. onInput,
  89. onChangeValue,
  90. rules: [
  91. (phone: string) => (!phone || isValid.value) || i18n.t('phone_error')
  92. ]
  93. }
  94. }
  95. })
  96. </script>
  97. <style>
  98. input:read-only{
  99. color: #666 !important;
  100. }
  101. </style>