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.
124 lines
3.2 KiB
124 lines
3.2 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'
|
||
2 years ago
|
import { ActiveCellInj, ColumnInj, ReadonlyInj, computed, inject, ref, useEventListener, watch } from '#imports'
|
||
2 years ago
|
|
||
|
interface Props {
|
||
2 years ago
|
modelValue?: string | undefined
|
||
2 years ago
|
}
|
||
|
|
||
|
const { modelValue } = defineProps<Props>()
|
||
|
|
||
2 years ago
|
const emit = defineEmits(['update:modelValue'])
|
||
2 years ago
|
|
||
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
|
|
||
2 years ago
|
const aselect = ref<typeof AntSelect>()
|
||
2 years ago
|
|
||
2 years ago
|
const isOpen = ref(false)
|
||
|
|
||
2 years ago
|
const vModel = computed({
|
||
2 years ago
|
get: () => modelValue,
|
||
2 years ago
|
set: (val) => emit('update:modelValue', val || null),
|
||
2 years ago
|
})
|
||
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
|
? // todo: fix colOptions type, options does not exist as a property
|
||
2 years ago
|
(column.value.colOptions as any).options.filter((el: SelectOptionsType) => 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 handleKeys = (e: KeyboardEvent) => {
|
||
2 years ago
|
switch (e.key) {
|
||
|
case 'Escape':
|
||
|
e.preventDefault()
|
||
|
isOpen.value = false
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
const handleClose = (e: MouseEvent) => {
|
||
|
if (aselect.value && !aselect.value.$el.contains(e.target)) {
|
||
|
isOpen.value = false
|
||
|
aselect.value.blur()
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
useEventListener(document, 'click', handleClose)
|
||
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>
|
||
|
|
||
2 years ago
|
<template>
|
||
2 years ago
|
<a-select
|
||
2 years ago
|
ref="aselect"
|
||
2 years ago
|
v-model:value="vModel"
|
||
|
class="w-full"
|
||
|
:allow-clear="!column.rqd && active"
|
||
|
:bordered="false"
|
||
2 years ago
|
:open="isOpen"
|
||
2 years ago
|
:disabled="readOnly"
|
||
|
:show-arrow="!readOnly && (active || vModel === null)"
|
||
2 years ago
|
@select="isOpen = false"
|
||
|
@keydown="handleKeys"
|
||
|
@click="isOpen = !isOpen"
|
||
2 years ago
|
>
|
||
2 years ago
|
<a-select-option v-for="op of options" :key="op.title" :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>
|
||
|
</a-select>
|
||
2 years ago
|
</template>
|
||
|
|
||
2 years ago
|
<style scoped lang="scss">
|
||
|
.rounded-tag {
|
||
|
padding: 0px 12px;
|
||
|
border-radius: 12px;
|
||
|
}
|
||
|
:deep(.ant-tag) {
|
||
|
@apply "rounded-tag";
|
||
|
}
|
||
2 years ago
|
:deep(.ant-select-clear) {
|
||
|
opacity: 1;
|
||
|
}
|
||
2 years ago
|
</style>
|
||
2 years ago
|
<!--
|
||
|
/**
|
||
|
* @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/>.
|
||
|
*
|
||
|
*/
|
||
|
-->
|