mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.9 KiB
131 lines
3.9 KiB
2 years ago
|
<script setup lang="ts">
|
||
|
import Draggable from 'vuedraggable'
|
||
|
import { UITypes } from 'nocodb-sdk'
|
||
|
import { enumColor } from '@/utils'
|
||
|
import MdiDragIcon from '~icons/mdi/drag-vertical'
|
||
|
import MdiArrowDownDropCircle from '~icons/mdi/arrow-down-drop-circle'
|
||
|
import MdiClose from '~icons/mdi/close'
|
||
|
import MdiPlusIcon from '~icons/mdi/plus'
|
||
|
|
||
2 years ago
|
interface Props {
|
||
|
value: Record<string, any>
|
||
|
}
|
||
|
|
||
|
const props = defineProps<Props>()
|
||
|
const emit = defineEmits(['update:value'])
|
||
|
const vModel = useVModel(props, 'value', emit)
|
||
|
|
||
2 years ago
|
const { setAdditionalValidations, validateInfos } = useColumnCreateStoreOrThrow()
|
||
2 years ago
|
|
||
|
let options = $ref<any[]>([])
|
||
|
const colorMenus = $ref<any>({})
|
||
|
const colors = $ref(enumColor.light)
|
||
2 years ago
|
const inputs = ref()
|
||
2 years ago
|
|
||
|
const validators = {
|
||
|
'colOptions.options': [
|
||
|
{
|
||
2 years ago
|
validator: (_: any, _opt: any) => {
|
||
2 years ago
|
return new Promise<void>((resolve, reject) => {
|
||
|
for (const opt of options) {
|
||
|
if (!opt.title.length) {
|
||
|
return reject(new Error("Select options can't be null"))
|
||
|
}
|
||
2 years ago
|
if (vModel.value.uidt === UITypes.MultiSelect && opt.title.includes(',')) {
|
||
2 years ago
|
return reject(new Error("MultiSelect columns can't have commas(',')"))
|
||
|
}
|
||
|
if (options.filter((el) => el.title === opt.title).length !== 1) {
|
||
|
return reject(new Error("Select options can't have duplicates"))
|
||
|
}
|
||
|
}
|
||
|
resolve()
|
||
|
})
|
||
|
},
|
||
|
},
|
||
|
],
|
||
|
}
|
||
|
|
||
|
setAdditionalValidations({
|
||
|
...validators,
|
||
|
})
|
||
|
|
||
|
const getNextColor = () => {
|
||
|
let tempColor = colors[0]
|
||
|
if (options.length && options[options.length - 1].color) {
|
||
|
const lastColor = colors.indexOf(options[options.length - 1].color)
|
||
|
tempColor = colors[(lastColor + 1) % colors.length]
|
||
|
}
|
||
|
return tempColor
|
||
|
}
|
||
|
|
||
|
const addNewOption = () => {
|
||
|
const tempOption = {
|
||
|
title: '',
|
||
|
color: getNextColor(),
|
||
|
}
|
||
|
options.push(tempOption)
|
||
|
}
|
||
|
|
||
|
const removeOption = (index: number) => {
|
||
|
options.splice(index, 1)
|
||
|
}
|
||
|
|
||
|
onMounted(() => {
|
||
2 years ago
|
if (!vModel.value.colOptions?.options) {
|
||
|
vModel.value.colOptions = {
|
||
2 years ago
|
options: [],
|
||
|
}
|
||
|
}
|
||
2 years ago
|
options = vModel.value.colOptions.options
|
||
2 years ago
|
// Support for older options
|
||
|
for (const op of options.filter((el) => el.order === null)) {
|
||
|
op.title = op.title.replace(/^'/, '').replace(/'$/, '')
|
||
|
}
|
||
|
})
|
||
2 years ago
|
|
||
|
// focus last created input
|
||
|
watch(inputs, () => {
|
||
|
if (inputs.value?.$el) {
|
||
|
inputs.value.$el.focus()
|
||
|
}
|
||
|
})
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<div class="w-full">
|
||
2 years ago
|
<Draggable :list="options" item-key="id" handle=".nc-child-draggable-icon">
|
||
2 years ago
|
<template #item="{ element, index }">
|
||
2 years ago
|
<div class="flex py-1 items-center">
|
||
2 years ago
|
<MdiDragIcon small class="nc-child-draggable-icon handle" />
|
||
2 years ago
|
<a-dropdown
|
||
|
v-model:visible="colorMenus[index]"
|
||
|
:trigger="['click']"
|
||
|
overlay-class-name="nc-dropdown-select-color-options"
|
||
|
>
|
||
2 years ago
|
<template #overlay>
|
||
2 years ago
|
<GeneralColorPicker v-model="element.color" :pick-button="true" @update:model-value="colorMenus[index] = false" />
|
||
2 years ago
|
</template>
|
||
|
<MdiArrowDownDropCircle :style="{ 'font-size': '1.5em', 'color': element.color }" class="mr-2" />
|
||
|
</a-dropdown>
|
||
2 years ago
|
|
||
2 years ago
|
<a-input ref="inputs" v-model:value="element.title" class="caption" />
|
||
2 years ago
|
<MdiClose class="ml-2" :style="{ color: 'red' }" @click="removeOption(index)" />
|
||
|
</div>
|
||
|
</template>
|
||
|
<template #footer>
|
||
2 years ago
|
<div v-if="validateInfos?.['colOptions.options']?.help?.[0]?.[0]" class="text-error text-[10px] my-2">
|
||
2 years ago
|
{{ validateInfos['colOptions.options'].help[0][0] }}
|
||
|
</div>
|
||
2 years ago
|
<a-button type="dashed" class="w-full caption mt-2" @click="addNewOption()">
|
||
2 years ago
|
<div class="flex items-center">
|
||
|
<MdiPlusIcon />
|
||
|
<span class="flex-auto">Add option</span>
|
||
|
</div>
|
||
2 years ago
|
</a-button>
|
||
|
</template>
|
||
2 years ago
|
</Draggable>
|
||
2 years ago
|
</div>
|
||
|
</template>
|
||
|
|
||
|
<style scoped lang="scss"></style>
|