2022-09-20 19:59:52 +00:00
|
|
|
<template>
|
2023-11-17 11:18:40 +00:00
|
|
|
<input-wrapper
|
|
|
|
v-bind="$props"
|
|
|
|
>
|
|
|
|
<template #label>
|
|
|
|
<slot name="label" />
|
|
|
|
</template>
|
|
|
|
|
2022-09-20 19:59:52 +00: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="hasValidation && form.errors.has(name)"
|
2023-11-17 11:18:40 +00:00
|
|
|
:allow-creation="allowCreation"
|
2023-03-22 14:49:52 +00:00
|
|
|
:disabled="disabled"
|
2023-04-19 08:13:50 +00:00
|
|
|
:help="help"
|
|
|
|
:help-position="helpPosition"
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
@update-options="updateOptions"
|
|
|
|
>
|
|
|
|
<template #selected="{option}">
|
2023-11-17 11:48:43 +00:00
|
|
|
<slot name="selected" :option="option" :optionName="getOptionName(option)">
|
|
|
|
<template v-if="multiple">
|
|
|
|
<div class="flex items-center truncate mr-6">
|
|
|
|
<span v-for="(item,index) in option" :key="item" class="truncate">
|
|
|
|
<span v-if="index!==0">, </span>
|
|
|
|
{{ getOptionName(item) }}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<template v-else>
|
2022-09-20 19:59:52 +00:00
|
|
|
<div class="flex items-center truncate mr-6">
|
|
|
|
<div>{{ getOptionName(option) }}</div>
|
|
|
|
</div>
|
2023-11-17 11:48:43 +00:00
|
|
|
</template>
|
|
|
|
</slot>
|
2022-09-20 19:59:52 +00:00
|
|
|
</template>
|
|
|
|
<template #option="{option, selected}">
|
|
|
|
<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"
|
|
|
|
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"
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</slot>
|
|
|
|
</template>
|
|
|
|
</v-select>
|
2023-11-17 11:18:40 +00:00
|
|
|
|
|
|
|
<template #help>
|
|
|
|
<slot name="help" />
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<template #error>
|
|
|
|
<slot name="error" />
|
|
|
|
</template>
|
|
|
|
</input-wrapper>
|
2022-09-20 19:59:52 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-11-17 11:18:40 +00:00
|
|
|
import { inputProps, useFormInput } from './useFormInput.js'
|
|
|
|
import InputWrapper from './components/InputWrapper.vue'
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Options: {name,value} objects
|
|
|
|
*/
|
|
|
|
export default {
|
|
|
|
name: 'SelectInput',
|
2023-11-17 11:18:40 +00:00
|
|
|
components: { InputWrapper },
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
props: {
|
2023-11-17 11:18:40 +00:00
|
|
|
...inputProps,
|
2022-09-20 19:59:52 +00:00
|
|
|
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 }
|
|
|
|
},
|
2023-11-17 11:18:40 +00:00
|
|
|
|
|
|
|
setup (props, context) {
|
|
|
|
const {
|
|
|
|
compVal,
|
|
|
|
inputStyle,
|
|
|
|
hasValidation,
|
|
|
|
hasError
|
|
|
|
} = useFormInput(props, context)
|
|
|
|
|
|
|
|
return {
|
|
|
|
compVal,
|
|
|
|
inputStyle,
|
|
|
|
hasValidation,
|
|
|
|
hasError
|
|
|
|
}
|
|
|
|
},
|
2022-09-20 19:59:52 +00:00
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
additionalOptions: []
|
|
|
|
}
|
|
|
|
},
|
2023-11-17 11:18:40 +00:00
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
computed: {
|
2023-11-17 11:18:40 +00:00
|
|
|
finalOptions () {
|
2022-09-20 19:59:52 +00:00
|
|
|
return this.options.concat(this.additionalOptions)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getOptionName (val) {
|
|
|
|
const option = this.finalOptions.find((optionCandidate) => {
|
|
|
|
return optionCandidate[this.optionKey] === val
|
|
|
|
})
|
|
|
|
if (option) return option[this.displayKey]
|
|
|
|
return null
|
|
|
|
},
|
2023-11-17 11:18:40 +00:00
|
|
|
updateOptions (newItem) {
|
|
|
|
if (newItem) {
|
2022-09-20 19:59:52 +00:00
|
|
|
this.additionalOptions.push(newItem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-17 11:18:40 +00:00
|
|
|
</script>
|