FormSelect.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <script setup>
  2. import { useIdGenerator, useValidationErrorIdGenerator } from '@/composables/helpers'
  3. const props = defineProps({
  4. modelValue: [String, Number, Boolean],
  5. label: {
  6. type: String,
  7. default: ''
  8. },
  9. fieldName: {
  10. type: String,
  11. default: '',
  12. required: true
  13. },
  14. fieldError: [String],
  15. options: {
  16. type: Array,
  17. required: true
  18. },
  19. help: {
  20. type: String,
  21. default: ''
  22. },
  23. isIndented: Boolean,
  24. isDisabled: Boolean,
  25. isLocked: Boolean,
  26. idSuffix: {
  27. type: String,
  28. default: ''
  29. },
  30. })
  31. const selected = ref(props.modelValue)
  32. const { inputId } = useIdGenerator('select', props.fieldName + props.idSuffix)
  33. const { valErrorId } = useValidationErrorIdGenerator(props.fieldName)
  34. const legendId = useIdGenerator('legend', props.fieldName + props.idSuffix).inputId
  35. </script>
  36. <template>
  37. <div class="field is-flex">
  38. <div v-if="isIndented" class="mx-2 pr-1" :class="{ 'is-opacity-5' : isDisabled || isLocked }">
  39. <FontAwesomeIcon class="has-text-grey" :icon="['fas', 'chevron-right']" transform="rotate-135"/>
  40. </div>
  41. <div>
  42. <label :for="inputId" class="label" :class="{ 'is-opacity-5' : isDisabled || isLocked }">
  43. {{ $t(label) }}<FontAwesomeIcon v-if="isLocked" :icon="['fas', 'lock']" class="ml-2" size="xs" />
  44. </label>
  45. <div class="control">
  46. <div class="select">
  47. <select
  48. :id="inputId"
  49. v-model="selected"
  50. v-on:change="$emit('update:modelValue', $event.target.value)"
  51. :disabled="isDisabled || isLocked"
  52. :aria-describedby="help ? legendId : undefined"
  53. :aria-invalid="fieldError != undefined"
  54. :aria-errormessage="fieldError != undefined ? valErrorId : undefined"
  55. >
  56. <option v-for="option in options" :value="option.value">{{ $t(option.text) }}</option>
  57. </select>
  58. </div>
  59. </div>
  60. <FieldError v-if="fieldError != undefined" :error="fieldError" :field="fieldName" />
  61. <p :id="legendId" class="help" v-html="$t(help)" v-if="help"></p>
  62. </div>
  63. </div>
  64. </template>