Browse Source

fix: proper multi/single select behaviour with keyboard

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/4222/head
Pranav C 2 years ago
parent
commit
07728342d8
  1. 39
      packages/nc-gui/components/cell/MultiSelect.vue
  2. 34
      packages/nc-gui/components/cell/SingleSelect.vue
  3. 4
      packages/nc-gui/components/smartsheet/Cell.vue
  4. 8
      packages/nc-gui/components/smartsheet/Grid.vue

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

@ -74,36 +74,19 @@ const selectedTitles = computed(() =>
? typeof modelValue === 'string' ? typeof modelValue === 'string'
? isMysql ? isMysql
? modelValue.split(',').sort((a, b) => { ? modelValue.split(',').sort((a, b) => {
const opa = options.value.find((el) => el.title === a) const opa = options.value.find((el) => el.title === a)
const opb = options.value.find((el) => el.title === b) const opb = options.value.find((el) => el.title === b)
if (opa && opb) { if (opa && opb) {
return opa.order! - opb.order! return opa.order! - opb.order!
} }
return 0 return 0
}) })
: modelValue.split(',') : modelValue.split(',')
: modelValue : modelValue
: [], : [],
) )
const v = Math.floor(Math.random() * 1000)
const v = Math.floor(Math.random() *1000)
const handleKeys = (e: KeyboardEvent) => {
console.log(`handleKeys ${v}`, e.key)
if (!active.value) return true
switch (e.key) {
case 'ArrowDown':
case 'ArrowUp':
if (isOpen.value) {
e.stopPropagation()
}
break
case 'Enter':
e.stopPropagation()
break
}
}
const handleClose = (e: MouseEvent) => { const handleClose = (e: MouseEvent) => {
if (aselect.value && !aselect.value.$el.contains(e.target)) { if (aselect.value && !aselect.value.$el.contains(e.target)) {
@ -146,17 +129,14 @@ watch(isOpen, (n, _o) => {
} }
}) })
useSelectedCellKeyupListener(active, (e) => { useSelectedCellKeyupListener(active, (e) => {
switch (e.key) { switch (e.key) {
case 'Escape': case 'Escape':
isOpen.value = false isOpen.value = false
select?.value?.blur()
break break
case 'Enter': case 'Enter':
if (active.value && !isOpen.value) { if (active.value && !isOpen.value) {
isOpen.value = true isOpen.value = true
e.stopPropagation()
} }
break break
} }
@ -167,15 +147,16 @@ useSelectedCellKeyupListener(active, (e) => {
<a-select <a-select
ref="aselect" ref="aselect"
v-model:value="vModel" v-model:value="vModel"
v-model:open="isOpen"
mode="multiple" mode="multiple"
class="w-full" class="w-full"
:bordered="false" :bordered="false"
:show-arrow="!readOnly" :show-arrow="!readOnly"
:show-search="false" :show-search="false"
v-model:open="isOpen"
:disabled="readOnly" :disabled="readOnly"
:class="{ '!ml-[-8px]': readOnly }" :class="{ '!ml-[-8px]': readOnly }"
:dropdown-class-name="`nc-dropdown-multi-select-cell ${isOpen ? 'active' : ''}`" :dropdown-class-name="`nc-dropdown-multi-select-cell ${isOpen ? 'active' : ''}`"
@keydown.enter.stop
@click="isOpen = active && !isOpen" @click="isOpen = active && !isOpen"
> >
<a-select-option <a-select-option

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

@ -45,24 +45,6 @@ const options = computed<SelectOptionType[]>(() => {
return [] return []
}) })
const handleKeys = (e: KeyboardEvent) => {
switch (e.key) {
case 'Escape':
e.preventDefault()
isOpen.value = false
break
case 'ArrowDown':
case 'ArrowUp':
if (isOpen.value) e.stopPropagation()
break
case 'Enter':
isOpen.value = true
e.stopPropagation()
break
}
}
const handleClose = (e: MouseEvent) => { const handleClose = (e: MouseEvent) => {
if (aselect.value && !aselect.value.$el.contains(e.target)) { if (aselect.value && !aselect.value.$el.contains(e.target)) {
isOpen.value = false isOpen.value = false
@ -72,7 +54,6 @@ const handleClose = (e: MouseEvent) => {
useEventListener(document, 'click', handleClose) useEventListener(document, 'click', handleClose)
watch(isOpen, (n, _o) => { watch(isOpen, (n, _o) => {
if (!n) { if (!n) {
aselect.value?.$el?.querySelector('input')?.blur() aselect.value?.$el?.querySelector('input')?.blur()
@ -80,6 +61,19 @@ watch(isOpen, (n, _o) => {
aselect.value?.$el?.querySelector('input')?.focus() aselect.value?.$el?.querySelector('input')?.focus()
} }
}) })
useSelectedCellKeyupListener(active, (e) => {
switch (e.key) {
case 'Escape':
isOpen.value = false
break
case 'Enter':
if (active.value && !isOpen.value) {
isOpen.value = true
}
break
}
})
</script> </script>
<template> <template>
@ -94,7 +88,7 @@ watch(isOpen, (n, _o) => {
:show-arrow="!readOnly && (active || vModel === null)" :show-arrow="!readOnly && (active || vModel === null)"
:dropdown-class-name="`nc-dropdown-single-select-cell ${isOpen ? 'active' : ''}`" :dropdown-class-name="`nc-dropdown-single-select-cell ${isOpen ? 'active' : ''}`"
@select="isOpen = false" @select="isOpen = false"
@keydown="isOpen ? handleKeys : null" @keydown.enter.stop
@click="isOpen = active && !isOpen" @click="isOpen = active && !isOpen"
> >
<a-select-option <a-select-option

4
packages/nc-gui/components/smartsheet/Cell.vue

@ -128,9 +128,9 @@ const syncAndNavigate = (dir: NavigateDir, e: KeyboardEvent) => {
<div <div
class="nc-cell w-full" class="nc-cell w-full"
:class="[`nc-cell-${(column?.uidt || 'default').toLowerCase()}`, { 'text-blue-600': isPrimary && !virtual && !isForm }]" :class="[`nc-cell-${(column?.uidt || 'default').toLowerCase()}`, { 'text-blue-600': isPrimary && !virtual && !isForm }]"
><!-- @keydown.enter.exact="syncAndNavigate(NavigateDir.NEXT, $event)" @keydown.enter.exact="syncAndNavigate(NavigateDir.NEXT, $event)"
@keydown.shift.enter.exact="syncAndNavigate(NavigateDir.PREV, $event)" @keydown.shift.enter.exact="syncAndNavigate(NavigateDir.PREV, $event)"
>--> >
<LazyCellTextArea v-if="isTextArea" v-model="vModel" /> <LazyCellTextArea v-if="isTextArea" v-model="vModel" />
<LazyCellCheckbox v-else-if="isBoolean" v-model="vModel" /> <LazyCellCheckbox v-else-if="isBoolean" v-model="vModel" />
<LazyCellAttachment v-else-if="isAttachment" v-model="vModel" :row-index="props.rowIndex" /> <LazyCellAttachment v-else-if="isAttachment" v-model="vModel" :row-index="props.rowIndex" />

8
packages/nc-gui/components/smartsheet/Grid.vue

@ -145,8 +145,8 @@ const getContainerScrollForElement = (
relativePos.right + (offset?.right || 0) > 0 relativePos.right + (offset?.right || 0) > 0
? container.scrollLeft + relativePos.right + (offset?.right || 0) ? container.scrollLeft + relativePos.right + (offset?.right || 0)
: relativePos.left - (offset?.left || 0) < 0 : relativePos.left - (offset?.left || 0) < 0
? container.scrollLeft + relativePos.left - (offset?.left || 0) ? container.scrollLeft + relativePos.left - (offset?.left || 0)
: container.scrollLeft : container.scrollLeft
/* /*
* If the element is below the container, scroll down (positive) * If the element is below the container, scroll down (positive)
@ -156,8 +156,8 @@ const getContainerScrollForElement = (
relativePos.bottom + (offset?.bottom || 0) > 0 relativePos.bottom + (offset?.bottom || 0) > 0
? container.scrollTop + relativePos.bottom + (offset?.bottom || 0) ? container.scrollTop + relativePos.bottom + (offset?.bottom || 0)
: relativePos.top - (offset?.top || 0) < 0 : relativePos.top - (offset?.top || 0) < 0
? container.scrollTop + relativePos.top - (offset?.top || 0) ? container.scrollTop + relativePos.top - (offset?.top || 0)
: container.scrollTop : container.scrollTop
return scroll return scroll
} }

Loading…
Cancel
Save