mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
9 changed files with 49 additions and 89 deletions
@ -1,20 +1,21 @@ |
|||||||
|
import { isClient } from '@vueuse/core' |
||||||
import type { Ref } from 'vue' |
import type { Ref } from 'vue' |
||||||
import { onUnmounted, useEventListener } from '#imports' |
|
||||||
|
|
||||||
export function useSelectedCellKeyupListener(selected: Ref<boolean>, handler: (e: KeyboardEvent) => void) { |
export function useSelectedCellKeyupListener(selected: Ref<boolean>, handler: (e: KeyboardEvent) => void) { |
||||||
const cleanup: Ref<ReturnType<typeof useEventListener> | null> = ref(null) |
if (isClient) { |
||||||
|
watch(selected, (nextVal, _, cleanup) => { |
||||||
watch(selected, (value) => { |
// bind listener when `selected` is truthy
|
||||||
if (value) { |
if (nextVal) { |
||||||
cleanup.value = useEventListener('keydown', handler, true) |
document.addEventListener('keyup', handler) |
||||||
|
// if `selected` is falsy then remove the event handler
|
||||||
} else { |
} else { |
||||||
cleanup.value?.() |
document.removeEventListener('keyup', handler) |
||||||
} |
} |
||||||
}) |
|
||||||
|
|
||||||
onUnmounted(() => cleanup.value?.()) |
|
||||||
|
|
||||||
return { |
// cleanup is called whenever the watcher is re-evaluated or stopped
|
||||||
cleanup, |
cleanup(() => { |
||||||
|
document.removeEventListener('keyup', handler) |
||||||
|
}) |
||||||
|
}) |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue