| 1 |
<template> |
| 2 |
<div class="ivyforms-field__select"> |
| 3 |
<IvyFormItem |
| 4 |
:label="hideLabel ? '' : label" |
| 5 |
:required="required" |
| 6 |
:label-position="labelPosition" |
| 7 |
> |
| 8 |
<IvySelectInput |
| 9 |
:id="String(index)" |
| 10 |
v-model="selectedValue" |
| 11 |
class="ivyforms-field__select-dropdown" |
| 12 |
:multiple="isMultiSelect" |
| 13 |
:placeholder="placeholder" |
| 14 |
readonly |
| 15 |
> |
| 16 |
<IvySelectOption |
| 17 |
v-for="option in selectOptions" |
| 18 |
:key="option.id || option.value" |
| 19 |
:value="option.value" |
| 20 |
:label="option.label" |
| 21 |
> |
| 22 |
</IvySelectOption> |
| 23 |
</IvySelectInput> |
| 24 |
<div v-if="description" class="ivyforms-description-message regular-14"> |
| 25 |
{{ description }} |
| 26 |
</div> |
| 27 |
</IvyFormItem> |
| 28 |
</div> |
| 29 |
</template> |
| 30 |
|
| 31 |
<script setup lang="ts"> |
| 32 |
import { computed } from 'vue' |
| 33 |
import { useFormBuilder } from '@/stores/useFormBuilder' |
| 34 |
import type { Choice, Field } from '@/types/field' |
| 35 |
|
| 36 |
const props = defineProps<{ fieldIndex: number }>() |
| 37 |
const formBuilderStore = useFormBuilder() |
| 38 |
|
| 39 |
const field = computed<Field | undefined>(() => |
| 40 |
formBuilderStore.fields.find((f: Field) => f.fieldIndex === props.fieldIndex), |
| 41 |
) |
| 42 |
const label = computed(() => field.value?.label || '') |
| 43 |
const required = computed(() => field.value?.required || false) |
| 44 |
const hideLabel = computed(() => field.value?.hideLabel || false) |
| 45 |
const index = computed(() => field.value?.fieldIndex ?? props.fieldIndex) |
| 46 |
const description = computed(() => field.value?.description || '') |
| 47 |
|
| 48 |
const selectOptions = computed<Choice[]>(() => field.value?.fieldOptions || []) |
| 49 |
|
| 50 |
const isMultiSelect = computed(() => { |
| 51 |
const value = field.value |
| 52 |
return value && typeof value === 'object' && value.type === 'multi-select' |
| 53 |
}) |
| 54 |
|
| 55 |
const placeholder = computed(() => field.value?.placeholder || '') |
| 56 |
|
| 57 |
const selectedValue = computed({ |
| 58 |
get() { |
| 59 |
const opts = field.value?.fieldOptions || [] |
| 60 |
if (isMultiSelect.value) { |
| 61 |
// Return array of default values for multi-select |
| 62 |
return opts.filter((opt) => opt.isDefault).map((opt) => opt.value) |
| 63 |
} else { |
| 64 |
// Return single default value for select |
| 65 |
const def = opts.find((opt) => opt.isDefault) |
| 66 |
return def ? String(def.value) : '' |
| 67 |
} |
| 68 |
}, |
| 69 |
set(newValue) { |
| 70 |
// Update isDefault on options when value changes |
| 71 |
if (!field.value) return |
| 72 |
if (isMultiSelect.value && Array.isArray(newValue)) { |
| 73 |
field.value.fieldOptions.forEach((opt) => { |
| 74 |
opt.isDefault = newValue.includes(opt.value) |
| 75 |
}) |
| 76 |
} else { |
| 77 |
field.value.fieldOptions.forEach((opt) => { |
| 78 |
opt.isDefault = String(opt.value) === String(newValue) |
| 79 |
}) |
| 80 |
} |
| 81 |
}, |
| 82 |
}) |
| 83 |
|
| 84 |
const labelPosition = computed(() => |
| 85 |
field.value?.labelPosition === 'default' ? 'top' : field.value?.labelPosition || 'top', |
| 86 |
) |
| 87 |
</script> |
| 88 |
|
| 89 |
<style lang="scss" scoped> |
| 90 |
.ivyforms-field__select { |
| 91 |
.ivyforms-form-item { |
| 92 |
:deep(.ivyforms-form-item__label) { |
| 93 |
cursor: default !important; |
| 94 |
} |
| 95 |
// Select inputs styling to prevent focus |
| 96 |
.ivyforms-form-item-select.el-select { |
| 97 |
cursor: default; |
| 98 |
:deep(.el-select__wrapper:hover), |
| 99 |
:deep(.el-select__wrapper.is-focus), |
| 100 |
:deep(.el-select__wrapper) { |
| 101 |
border-radius: var(--Radius-radius-md, 8px); |
| 102 |
border: 1px solid var(--map-base-dusk-stroke--2); |
| 103 |
background: transparent !important; |
| 104 |
box-shadow: none; |
| 105 |
padding: 0 12px; |
| 106 |
transition: none; |
| 107 |
cursor: default; |
| 108 |
} |
| 109 |
:deep(.el-input) { |
| 110 |
cursor: default; |
| 111 |
} |
| 112 |
:deep(.el-input__wrapper) { |
| 113 |
cursor: default; |
| 114 |
box-shadow: none; |
| 115 |
background: transparent !important; |
| 116 |
} |
| 117 |
:deep(.el-input__inner) { |
| 118 |
cursor: default; |
| 119 |
&:focus { |
| 120 |
outline: none; |
| 121 |
border: 1px solid transparent; |
| 122 |
background: none; |
| 123 |
box-shadow: none; |
| 124 |
} |
| 125 |
} |
| 126 |
:deep(.el-select__caret) { |
| 127 |
cursor: default; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
</style> |
| 133 |
|