TextArea.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!--
  2. Champs de saisie de bloc texte
  3. @see https://vuetifyjs.com/en/components/textareas/
  4. -->
  5. <template>
  6. <v-textarea
  7. :model-value="modelValue"
  8. :label="label || field ? $t(label ?? field) : undefined"
  9. :rules="rules"
  10. :disabled="readonly"
  11. :error="error || !!fieldViolations"
  12. :error-messages="
  13. errorMessage || (fieldViolations ? $t(fieldViolations) : '')
  14. "
  15. :variant="variant"
  16. @update:model-value="onUpdate($event)"
  17. @change="onChange($event)"
  18. />
  19. </template>
  20. <script setup lang="ts">
  21. import { useFieldViolation } from '~/composables/form/useFieldViolation'
  22. import type { PropType } from 'vue'
  23. type ValidationRule = (value: string | number | null) => boolean | string
  24. const props = defineProps({
  25. /**
  26. * v-model
  27. */
  28. modelValue: {
  29. type: [String, Number] as PropType<string | number | null>,
  30. required: false,
  31. default: null,
  32. },
  33. /**
  34. * Nom de la propriété d'une entité lorsque l'input concerne cette propriété
  35. * - Utilisé par la validation
  36. * - Laisser null si le champ ne s'applique pas à une entité
  37. */
  38. field: {
  39. type: String,
  40. required: false,
  41. default: null,
  42. },
  43. /**
  44. * Label du champ
  45. * Si non défini, c'est le nom de propriété qui est utilisé
  46. */
  47. label: {
  48. type: String,
  49. required: false,
  50. default: null,
  51. },
  52. /**
  53. * Définit si le champ est en lecture seule
  54. */
  55. readonly: {
  56. type: Boolean,
  57. required: false,
  58. default: false,
  59. },
  60. /**
  61. * Règles de validation
  62. * @see https://vuetify.cn/en/components/forms/#validation-with-submit-clear
  63. */
  64. rules: {
  65. type: Array as PropType<ValidationRule[]>,
  66. required: false,
  67. default: () => [],
  68. },
  69. /**
  70. * Le champ est-il actuellement en état d'erreur
  71. */
  72. error: {
  73. type: Boolean,
  74. required: false,
  75. default: false,
  76. },
  77. /**
  78. * Si le champ est en état d'erreur, quel est le message d'erreur?
  79. */
  80. errorMessage: {
  81. type: String,
  82. required: false,
  83. default: null,
  84. },
  85. /**
  86. * @see https://vuetifyjs.com/en/api/v-autocomplete/#props-variant
  87. */
  88. variant: {
  89. type: String as PropType<
  90. | 'filled'
  91. | 'outlined'
  92. | 'plain'
  93. | 'underlined'
  94. | 'solo'
  95. | 'solo-inverted'
  96. | 'solo-filled'
  97. | undefined
  98. >,
  99. required: false,
  100. default: 'outlined',
  101. },
  102. })
  103. const { fieldViolations, updateViolationState } = useFieldViolation(props.field)
  104. const emit = defineEmits(['update:model-value', 'change'])
  105. const onUpdate = (event: string) => {
  106. emit('update:model-value', event)
  107. }
  108. const onChange = (event: Event | undefined) => {
  109. updateViolationState()
  110. emit('change', event)
  111. }
  112. onBeforeUnmount(() => {
  113. updateViolationState()
  114. })
  115. </script>
  116. <style scoped lang="scss">
  117. input:read-only {
  118. color: rgb(var(--v-theme-on-neutral));
  119. }
  120. </style>