AppInputText.vue 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <script setup>
  2. import { computed } from 'vue'
  3. const props = defineProps({
  4. label: {
  5. type: String,
  6. required: true,
  7. },
  8. disabled: {
  9. type: Boolean,
  10. default: false,
  11. },
  12. placeholder: {
  13. type: String,
  14. required: true,
  15. },
  16. type: {
  17. type: String,
  18. default: 'text',
  19. },
  20. error: {
  21. type: String,
  22. default: null,
  23. },
  24. modelValue: null,
  25. })
  26. defineEmits(['update:modelValue'])
  27. const isError = computed(() => (props.error ? true : false))
  28. const forLabel = computed(() => props.label.toLowerCase().replace(/\s+/g, '-'))
  29. const ariaDescribedbyLabel = computed(
  30. () => props.label.toLowerCase().replace(/\s+/g, '-') + '-error'
  31. )
  32. </script>
  33. <template>
  34. <div class="field">
  35. <label :for="forLabel">{{ label }}</label>
  36. <InputText
  37. class="w-full"
  38. :type="type"
  39. :class="{ 'p-invalid': isError }"
  40. :id="forLabel"
  41. :model-value="modelValue"
  42. :placeholder="placeholder"
  43. :value="modelValue"
  44. :disabled="disabled"
  45. @input="$emit('update:modelValue', $event.target.value)"
  46. />
  47. <small
  48. v-if="error"
  49. :id="ariaDescribedbyLabel"
  50. :class="{ 'p-error': isError }"
  51. >
  52. {{ error }}
  53. </small>
  54. </div>
  55. </template>