2023-12-09 15:47:03 +01:00
|
|
|
<template>
|
2024-03-19 13:35:51 +01:00
|
|
|
<input-wrapper v-bind="inputWrapperProps">
|
2023-12-09 15:47:03 +01:00
|
|
|
<template #label>
|
|
|
|
|
<slot name="label" />
|
|
|
|
|
</template>
|
2024-03-19 13:35:51 +01:00
|
|
|
<v-select v-model="compVal" :data="finalOptions" :label="label" :option-key="optionKey" :emit-key="emitKey"
|
|
|
|
|
:required="required" :multiple="multiple" :searchable="searchable" :loading="loading" :color="color"
|
|
|
|
|
:placeholder="placeholder" :uppercase-labels="uppercaseLabels" :theme="theme" :has-error="hasError"
|
|
|
|
|
:allow-creation="allowCreation" :disabled="disabled ? true : null" :help="help" :help-position="helpPosition"
|
|
|
|
|
@update-options="updateOptions" @update:model-value="updateModelValue">
|
|
|
|
|
<template #selected="{ option }">
|
2023-12-09 15:47:03 +01:00
|
|
|
<slot name="selected" :option="option" :optionName="getOptionName(option)">
|
|
|
|
|
<template v-if="multiple">
|
|
|
|
|
<div class="flex items-center truncate mr-6">
|
2024-02-03 17:14:45 +01:00
|
|
|
<span class="truncate">
|
2024-03-19 13:35:51 +01:00
|
|
|
{{ getOptionNames(selectedValues).join(', ') }}
|
2023-12-09 15:47:03 +01:00
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
<template v-else>
|
|
|
|
|
<div class="flex items-center truncate mr-6">
|
|
|
|
|
<div>{{ getOptionName(option) }}</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
</slot>
|
|
|
|
|
</template>
|
2024-03-19 13:35:51 +01:00
|
|
|
<template #option="{ option, selected }">
|
2023-12-09 15:47:03 +01:00
|
|
|
<slot name="option" :option="option" :selected="selected">
|
|
|
|
|
<span class="flex group-hover:text-white">
|
|
|
|
|
<p class="flex-grow group-hover:text-white">
|
|
|
|
|
{{ option.name }}
|
|
|
|
|
</p>
|
|
|
|
|
<span v-if="selected" class="absolute inset-y-0 right-0 flex items-center pr-4 dark:text-white">
|
|
|
|
|
<svg class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
|
|
|
|
|
<path fill-rule="evenodd"
|
2024-03-19 13:35:51 +01:00
|
|
|
d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
|
|
|
|
|
clip-rule="evenodd" />
|
2023-12-09 15:47:03 +01:00
|
|
|
</svg>
|
|
|
|
|
</span>
|
|
|
|
|
</span>
|
|
|
|
|
</slot>
|
|
|
|
|
</template>
|
|
|
|
|
</v-select>
|
|
|
|
|
|
|
|
|
|
<template #help>
|
|
|
|
|
<slot name="help" />
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<template #error>
|
|
|
|
|
<slot name="error" />
|
|
|
|
|
</template>
|
|
|
|
|
</input-wrapper>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
import { inputProps, useFormInput } from './useFormInput.js'
|
|
|
|
|
import InputWrapper from './components/InputWrapper.vue'
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Options: {name,value} objects
|
|
|
|
|
*/
|
|
|
|
|
export default {
|
|
|
|
|
name: 'SelectInput',
|
|
|
|
|
components: { InputWrapper },
|
|
|
|
|
|
|
|
|
|
props: {
|
|
|
|
|
...inputProps,
|
|
|
|
|
options: { type: Array, required: true },
|
|
|
|
|
optionKey: { type: String, default: 'value' },
|
|
|
|
|
emitKey: { type: String, default: 'value' },
|
|
|
|
|
displayKey: { type: String, default: 'name' },
|
|
|
|
|
loading: { type: Boolean, default: false },
|
|
|
|
|
multiple: { type: Boolean, default: false },
|
|
|
|
|
searchable: { type: Boolean, default: false },
|
|
|
|
|
allowCreation: { type: Boolean, default: false }
|
|
|
|
|
},
|
|
|
|
|
|
2024-03-19 13:35:51 +01:00
|
|
|
setup(props, context) {
|
2023-12-09 15:47:03 +01:00
|
|
|
return {
|
|
|
|
|
...useFormInput(props, context)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2024-03-19 13:35:51 +01:00
|
|
|
data() {
|
2023-12-09 15:47:03 +01:00
|
|
|
return {
|
2024-02-03 17:14:45 +01:00
|
|
|
additionalOptions: [],
|
2024-03-19 13:35:51 +01:00
|
|
|
selectedValues: [],
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
computed: {
|
2024-03-19 13:35:51 +01:00
|
|
|
finalOptions() {
|
2023-12-09 15:47:03 +01:00
|
|
|
return this.options.concat(this.additionalOptions)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
2024-03-19 13:35:51 +01:00
|
|
|
getOptionName(val) {
|
2023-12-09 15:47:03 +01:00
|
|
|
const option = this.finalOptions.find((optionCandidate) => {
|
|
|
|
|
return optionCandidate[this.optionKey] === val
|
|
|
|
|
})
|
|
|
|
|
if (option) return option[this.displayKey]
|
|
|
|
|
return null
|
|
|
|
|
},
|
2024-03-19 13:35:51 +01:00
|
|
|
getOptionNames(values) {
|
|
|
|
|
return values.map(val => {
|
|
|
|
|
return this.getOptionName(val)
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
updateModelValue(newValues) {
|
2024-02-10 12:46:17 +01:00
|
|
|
if (newValues === null) newValues = []
|
2024-02-03 17:14:45 +01:00
|
|
|
this.selectedValues = newValues
|
|
|
|
|
},
|
2024-03-19 13:35:51 +01:00
|
|
|
updateOptions(newItem) {
|
2023-12-09 15:47:03 +01:00
|
|
|
if (newItem) {
|
|
|
|
|
this.additionalOptions.push(newItem)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|