123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. error: {
  17. type: String,
  18. default: null,
  19. },
  20. optionLabel: {
  21. type: String,
  22. default: 'label',
  23. },
  24. optionValue: {
  25. type: String,
  26. default: 'value',
  27. },
  28. optionDisabled: {
  29. type: String,
  30. default: 'disabled',
  31. },
  32. optionGroupChildren: {
  33. type: String,
  34. default: null,
  35. },
  36. optionGroupLabel: {
  37. type: String,
  38. default: null,
  39. },
  40. options: {
  41. type: Array,
  42. required: true,
  43. },
  44. modelValue: null,
  45. })
  46. const isError = computed(() => (props.error ? true : false))
  47. const forLabel = computed(() =>
  48. props.label ? props.label.toLowerCase().replace(/\s+/g, '-') : null
  49. )
  50. const ariaDescribedbyLabel = computed(
  51. () => props.label.toLowerCase().replace(/\s+/g, '-') + '-error'
  52. )
  53. const selectedDropdownLabel = (value) => {
  54. const result = props.options.find(
  55. (option) => option[props.optionValue] == value
  56. )
  57. if (result) {
  58. return result[props.optionLabel]
  59. }
  60. }
  61. </script>
  62. <template>
  63. <div class="field">
  64. <label v-if="label" :for="forLabel">{{ label }}</label>
  65. <Dropdown
  66. class="w-full"
  67. :class="{ 'p-invalid': isError }"
  68. :id="forLabel"
  69. :option-disabled="optionDisabled"
  70. :option-group-children="optionGroupChildren"
  71. :option-group-label="optionGroupLabel"
  72. :option-label="optionLabel"
  73. :option-value="optionValue"
  74. :placeholder="placeholder"
  75. :options="options"
  76. :model-value="modelValue"
  77. :disabled="disabled"
  78. @change="$emit('update:modelValue', $event.value)"
  79. >
  80. <template #value="slotProps">
  81. <div v-if="slotProps.value">
  82. {{ selectedDropdownLabel(slotProps.value) }}
  83. </div>
  84. <div v-else>
  85. {{ slotProps.placeholder }}
  86. </div>
  87. </template>
  88. <template #option="{ option, index }">
  89. <slot name="option" :option="option" :index="index" />
  90. </template>
  91. </Dropdown>
  92. <small
  93. v-if="error"
  94. :id="ariaDescribedbyLabel"
  95. :class="{ 'p-error': isError }"
  96. >
  97. {{ error }}
  98. </small>
  99. </div>
  100. </template>