From 469b96c8b21958b0afb4654283e85747147dc7dc Mon Sep 17 00:00:00 2001 From: mertmit Date: Sun, 7 Aug 2022 23:09:18 +0300 Subject: [PATCH] feat(gui-v2): custom mouse and key events for select cells Signed-off-by: mertmit --- .../nc-gui-v2/components/cell/MultiSelect.vue | 43 ++++++++++++++++- .../components/cell/SingleSelect.vue | 46 ++++++++++++++++++- 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/packages/nc-gui-v2/components/cell/MultiSelect.vue b/packages/nc-gui-v2/components/cell/MultiSelect.vue index db48493878..23a3c22214 100644 --- a/packages/nc-gui-v2/components/cell/MultiSelect.vue +++ b/packages/nc-gui-v2/components/cell/MultiSelect.vue @@ -20,6 +20,8 @@ const editEnabled = inject(EditModeInj, ref(false)) const active = inject(ActiveCellInj, ref(false)) const selectedIds = ref([]) +const aselect = ref(null) +const isOpen = ref(false) const options = computed(() => { if (column?.colOptions) { @@ -54,10 +56,36 @@ const selectedTitles = computed(() => : [], ) +const handleKeys = (e: any) => { + switch (e.key) { + case 'Escape': + e.preventDefault() + isOpen.value = false + break + case 'Enter': + e.stopPropagation() + break + } +} + +const handleClose = (e: any) => { + if (aselect.value) { + const selectClick = aselect.value.$el.contains(e.target) + if (!selectClick) { + isOpen.value = false + } + } +} + onMounted(() => { selectedIds.value = selectedTitles.value.map((el) => { return options.value.find((op: SelectOptionType) => op.title === el).id }) + document.addEventListener('click', handleClose) +}) + +onUnmounted(() => { + document.removeEventListener('click', handleClose) }) watch( @@ -68,10 +96,20 @@ watch( }) }, ) + +watch( + () => isOpen.value, + (n, _o) => { + if (n === false) { + aselect.value.blur() + } + }, +)