<!--
Champs de saisie de texte

@see https://vuetifyjs.com/en/components/text-fields/
-->

<template>
    <v-text-field
      autocomplete="off"
      :value="data"
      :label="$t(label_field)"
      :rules="rules"
      :disabled="readonly"
      :type="type"
      :error="error || violations"
      :error-messages="errorMessage"
      @change="onChange($event)"
      v-mask="mask"
    />
</template>

<script lang="ts">
import { defineComponent } from '@nuxtjs/composition-api'
import {$useError} from "~/composables/form/useError";
import {mask} from 'vue-the-mask';

export default defineComponent({
  props: {
    label: {
      type: String,
      required: false,
      default: null
    },
    field: {
      type: String,
      required: false,
      default: null
    },
    type: {
      type: String,
      required: false,
      default: null
    },
    data: {
      type: [String, Number],
      required: false,
      default: null
    },
    readonly: {
      type: Boolean,
      required: false
    },
    rules: {
      type: Array,
      required: false,
      default: () => []
    },
    error: {
      type: Boolean,
      required: false
    },
    errorMessage: {
      type: String,
      required: false,
      default: null
    },
    mask: {
      type: [Array, Boolean],
      required: false,
      default: false
    }
  },
  setup (props, {emit}) {
    const {error: violations, onChange} = $useError(props.field, emit)

    return {
      label_field: props.label ?? props.field,
      violations,
      onChange
    }
  },
  directives: {
    mask: (el, binding, vnode, oldVnode) => {
      if (!binding.value) return;
      mask(el, binding, vnode, oldVnode);
    }
  }
})
</script>

<style>
  input:read-only{
    color: #666 !important;
  }
</style>
