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.
223 lines
5.3 KiB
223 lines
5.3 KiB
2 years ago
|
<script lang="ts" setup>
|
||
2 years ago
|
import type { Select as AntSelect } from 'ant-design-vue'
|
||
2 years ago
|
import type { SelectOptionsType } from 'nocodb-sdk'
|
||
|
import {
|
||
|
ActiveCellInj,
|
||
|
ColumnInj,
|
||
2 years ago
|
ReadonlyInj,
|
||
2 years ago
|
computed,
|
||
|
h,
|
||
|
inject,
|
||
|
onMounted,
|
||
|
ref,
|
||
|
useEventListener,
|
||
|
useProject,
|
||
|
watch,
|
||
|
} from '#imports'
|
||
2 years ago
|
import MdiCloseCircle from '~icons/mdi/close-circle'
|
||
2 years ago
|
|
||
|
interface Props {
|
||
2 years ago
|
modelValue?: string | string[]
|
||
2 years ago
|
}
|
||
|
|
||
|
const { modelValue } = defineProps<Props>()
|
||
2 years ago
|
|
||
|
const emit = defineEmits(['update:modelValue'])
|
||
|
|
||
2 years ago
|
const { isMysql } = useProject()
|
||
|
|
||
2 years ago
|
const column = inject(ColumnInj)!
|
||
2 years ago
|
|
||
2 years ago
|
const readOnly = inject(ReadonlyInj)!
|
||
2 years ago
|
|
||
2 years ago
|
const active = inject(ActiveCellInj, ref(false))
|
||
|
|
||
2 years ago
|
const selectedIds = ref<string[]>([])
|
||
2 years ago
|
|
||
2 years ago
|
const aselect = ref<typeof AntSelect>()
|
||
2 years ago
|
|
||
2 years ago
|
const isOpen = ref(false)
|
||
2 years ago
|
|
||
2 years ago
|
const options = computed<SelectOptionsType[]>(() => {
|
||
2 years ago
|
if (column?.value.colOptions) {
|
||
|
const opts = column.value.colOptions
|
||
2 years ago
|
? (column.value.colOptions as any).options.filter((el: any) => el.title !== '') || []
|
||
2 years ago
|
: []
|
||
2 years ago
|
for (const op of opts.filter((el: any) => el.order === null)) {
|
||
2 years ago
|
op.title = op.title.replace(/^'/, '').replace(/'$/, '')
|
||
|
}
|
||
|
return opts
|
||
|
}
|
||
|
return []
|
||
|
})
|
||
2 years ago
|
|
||
2 years ago
|
const vModel = computed({
|
||
2 years ago
|
get: () => selectedIds.value.map((el) => options.value.find((op) => op.id === el)?.title),
|
||
2 years ago
|
set: (val) => emit('update:modelValue', val.length === 0 ? null : val.join(',')),
|
||
2 years ago
|
})
|
||
2 years ago
|
|
||
|
const selectedTitles = computed(() =>
|
||
|
modelValue
|
||
|
? typeof modelValue === 'string'
|
||
|
? isMysql
|
||
|
? modelValue.split(',').sort((a, b) => {
|
||
2 years ago
|
const opa = options.value.find((el) => el.title === a)
|
||
|
const opb = options.value.find((el) => el.title === b)
|
||
2 years ago
|
if (opa && opb) {
|
||
2 years ago
|
return opa.order! - opb.order!
|
||
2 years ago
|
}
|
||
|
return 0
|
||
|
})
|
||
|
: modelValue.split(',')
|
||
|
: modelValue
|
||
|
: [],
|
||
|
)
|
||
|
|
||
2 years ago
|
const handleKeys = (e: KeyboardEvent) => {
|
||
2 years ago
|
switch (e.key) {
|
||
|
case 'Escape':
|
||
|
e.preventDefault()
|
||
|
isOpen.value = false
|
||
|
break
|
||
|
case 'Enter':
|
||
|
e.stopPropagation()
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
const handleClose = (e: MouseEvent) => {
|
||
|
if (aselect.value && !aselect.value.$el.contains(e.target)) {
|
||
|
isOpen.value = false
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
onMounted(() => {
|
||
2 years ago
|
selectedIds.value = selectedTitles.value.flatMap((el) => {
|
||
|
const item = options.value.find((op) => op.title === el)?.id
|
||
|
if (item) {
|
||
|
return [item]
|
||
|
}
|
||
|
|
||
|
return []
|
||
|
})
|
||
2 years ago
|
})
|
||
|
|
||
2 years ago
|
useEventListener(document, 'click', handleClose)
|
||
2 years ago
|
|
||
|
watch(
|
||
|
() => modelValue,
|
||
2 years ago
|
() => {
|
||
2 years ago
|
selectedIds.value = selectedIds.value = selectedTitles.value.flatMap((el) => {
|
||
|
const item = options.value.find((op) => op.title === el)?.id
|
||
|
if (item) {
|
||
|
return [item]
|
||
|
}
|
||
|
|
||
|
return []
|
||
|
})
|
||
2 years ago
|
},
|
||
|
)
|
||
2 years ago
|
|
||
2 years ago
|
watch(isOpen, (n, _o) => {
|
||
2 years ago
|
if (!n) aselect.value?.$el.blur()
|
||
2 years ago
|
})
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
2 years ago
|
<a-select
|
||
2 years ago
|
ref="aselect"
|
||
2 years ago
|
v-model:value="vModel"
|
||
|
mode="multiple"
|
||
|
class="w-full"
|
||
|
:bordered="false"
|
||
2 years ago
|
:show-arrow="!readOnly"
|
||
2 years ago
|
:show-search="false"
|
||
2 years ago
|
:open="isOpen"
|
||
2 years ago
|
:disabled="readOnly"
|
||
2 years ago
|
@keydown="handleKeys"
|
||
|
@click="isOpen = !isOpen"
|
||
2 years ago
|
>
|
||
2 years ago
|
<a-select-option v-for="op of options" :key="op.id" :value="op.title" @click.stop>
|
||
2 years ago
|
<a-tag class="rounded-tag" :color="op.color">
|
||
2 years ago
|
<span class="text-slate-500">{{ op.title }}</span>
|
||
|
</a-tag>
|
||
|
</a-select-option>
|
||
2 years ago
|
<template #tagRender="{ value: val, onClose }">
|
||
|
<a-tag
|
||
2 years ago
|
v-if="options.find((el) => el.title === val)"
|
||
2 years ago
|
class="rounded-tag"
|
||
2 years ago
|
:style="{ display: 'flex', alignItems: 'center' }"
|
||
2 years ago
|
:color="options.find((el) => el.title === val).color"
|
||
2 years ago
|
:closable="active && (vModel.length > 1 || !column?.rqd)"
|
||
2 years ago
|
:close-icon="h(MdiCloseCircle, { class: ['ms-close-icon'] })"
|
||
2 years ago
|
@close="onClose"
|
||
|
>
|
||
2 years ago
|
<span class="w-full text-slate-500">{{ val }}</span>
|
||
2 years ago
|
</a-tag>
|
||
|
</template>
|
||
|
</a-select>
|
||
2 years ago
|
</template>
|
||
|
|
||
2 years ago
|
<style scoped>
|
||
2 years ago
|
.ms-close-icon {
|
||
|
color: rgba(0, 0, 0, 0.25);
|
||
|
cursor: pointer;
|
||
|
display: flex;
|
||
|
font-size: 12px;
|
||
|
font-style: normal;
|
||
|
height: 12px;
|
||
|
line-height: 1;
|
||
|
text-align: center;
|
||
|
text-transform: none;
|
||
|
transition: color 0.3s ease, opacity 0.15s ease;
|
||
|
width: 12px;
|
||
|
z-index: 1;
|
||
|
margin-right: -6px;
|
||
|
margin-left: 3px;
|
||
|
}
|
||
|
.ms-close-icon:before {
|
||
|
display: block;
|
||
|
}
|
||
|
.ms-close-icon:hover {
|
||
|
color: rgba(0, 0, 0, 0.45);
|
||
|
}
|
||
2 years ago
|
.rounded-tag {
|
||
|
padding: 0px 12px;
|
||
|
border-radius: 12px;
|
||
|
}
|
||
|
:deep(.ant-tag) {
|
||
|
@apply "rounded-tag";
|
||
|
}
|
||
2 years ago
|
:deep(.ant-tag-close-icon) {
|
||
|
@apply "text-slate-500";
|
||
|
}
|
||
2 years ago
|
|
||
|
:deep(.ant-select-selection-overflow-item) {
|
||
|
@apply "flex overflow-hidden";
|
||
|
}
|
||
2 years ago
|
</style>
|
||
|
<!--
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|
||
|
-->
|