Browse Source

refactor(gui): use a-select for single select

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/4406/head
Pranav C 2 years ago
parent
commit
be2bbc7d84
  1. 10
      packages/nc-gui/components/cell/MultiSelect.vue
  2. 33
      packages/nc-gui/components/cell/SingleSelect.vue

10
packages/nc-gui/components/cell/MultiSelect.vue

@ -48,7 +48,7 @@ const isOpen = ref(false)
const isKanban = inject(IsKanbanInj, ref(false)) const isKanban = inject(IsKanbanInj, ref(false))
const options = computed<SelectOptionType[]>(() => { const options = computed<(SelectOptionType & { value?: string })[]>(() => {
if (column?.value.colOptions) { if (column?.value.colOptions) {
const opts = column.value.colOptions const opts = column.value.colOptions
? (column.value.colOptions as SelectOptionsType).options.filter((el: SelectOptionType) => el.title !== '') || [] ? (column.value.colOptions as SelectOptionsType).options.filter((el: SelectOptionType) => el.title !== '') || []
@ -167,7 +167,12 @@ const { $api } = useNuxtApp()
const { getMeta } = useMetas() const { getMeta } = useMetas()
async function addIfMissingAndSave() { async function addIfMissingAndSave() {
const newOptValue = aselect.value?.$el?.querySelector('.ant-select-selection-search-input')?.value const searchInput = aselect.value?.$el?.querySelector('.ant-select-selection-search-input')
if (!searchInput) return false
const newOptValue = searchInput?.value
if (newOptValue && !options.value.some((o) => o.title === newOptValue)) { if (newOptValue && !options.value.some((o) => o.title === newOptValue)) {
const newOptions = [...options.value] const newOptions = [...options.value]
newOptions.push({ title: newOptValue, value: newOptValue }) newOptions.push({ title: newOptValue, value: newOptValue })
@ -179,6 +184,7 @@ async function addIfMissingAndSave() {
await getMeta(column.value.fk_model_id!, true) await getMeta(column.value.fk_model_id!, true)
vModel.value = [...vModel.value, newOptValue] vModel.value = [...vModel.value, newOptValue]
searchInput.value = ''
} }
} }
</script> </script>

33
packages/nc-gui/components/cell/SingleSelect.vue

@ -60,12 +60,17 @@ const options = computed<(SelectOptionType & { value: string })[]>(() => {
return [] return []
}) })
const handleKeys = (e: KeyboardEvent) => { const handleKeys = async (e: KeyboardEvent) => {
switch (e.key) { switch (e.key) {
case 'Escape': case 'Escape':
e.preventDefault() e.preventDefault()
isOpen.value = false isOpen.value = false
break break
case 'Enter':
e.preventDefault()
if (await addIfMissingAndSave())
e.stopPropagation()
break
} }
} }
@ -102,8 +107,12 @@ useSelectedCellKeyupListener(active, (e) => {
const val = ref() const val = ref()
const { $api } = useNuxtApp() const { $api } = useNuxtApp()
const addIfMissingAndSave = async () => { async function addIfMissingAndSave() {
const newOptValue = aselect.value?.$el?.querySelector('.ant-select-selection-search-input')?.value const searchInput = aselect.value?.$el?.querySelector('.ant-select-selection-search-input')
if(!searchInput) return false
const newOptValue = searchInput?.value
if (newOptValue && !options.value.some((o) => o.title === newOptValue)) { if (newOptValue && !options.value.some((o) => o.title === newOptValue)) {
options.value.push({ title: newOptValue, value: newOptValue }) options.value.push({ title: newOptValue, value: newOptValue })
@ -117,6 +126,24 @@ const addIfMissingAndSave = async () => {
</script> </script>
<template> <template>
<!-- <a-auto-complete
ref="aselect"
v-model:value="val"
class="w-full h-fill"
dropdown-class-name="nc-dropdown-single-select-cell"
:options="options"
@select="vModel = val"
:defaultActiveFirstOption="false"
@keydown.enter.prevent.stop="addIfMissingAndSave"
>
<template #option="item">
<div style="display: flex; justify-content: space-between">
{{ item.value }}
</div>
</template>
-->
<a-select <a-select
ref="aselect" ref="aselect"
v-model:value="vModel" v-model:value="vModel"

Loading…
Cancel
Save