mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
2 changed files with 35 additions and 14 deletions
@ -1,21 +1,31 @@
|
||||
import { isClient } from '@vueuse/core' |
||||
import type { Ref } from 'vue' |
||||
|
||||
export function useSelectedCellKeyupListener(selected: Ref<boolean>, handler: (e: KeyboardEvent) => void) { |
||||
function useSelectedCellKeyupListener( |
||||
selected: Ref<boolean>, |
||||
handler: (e: KeyboardEvent) => void, |
||||
{ immediate = false }: { immediate?: boolean } = {}, |
||||
) { |
||||
if (isClient) { |
||||
watch(selected, (nextVal, _, cleanup) => { |
||||
// bind listener when `selected` is truthy
|
||||
if (nextVal) { |
||||
document.addEventListener('keydown', handler, true) |
||||
// if `selected` is falsy then remove the event handler
|
||||
} else { |
||||
document.removeEventListener('keydown', handler, true) |
||||
} |
||||
watch( |
||||
selected, |
||||
(nextVal: boolean, _: boolean, cleanup) => { |
||||
// bind listener when `selected` is truthy
|
||||
if (nextVal) { |
||||
document.addEventListener('keydown', handler, true) |
||||
// if `selected` is falsy then remove the event handler
|
||||
} else { |
||||
document.removeEventListener('keydown', handler, true) |
||||
} |
||||
|
||||
// cleanup is called whenever the watcher is re-evaluated or stopped
|
||||
cleanup(() => { |
||||
document.removeEventListener('keydown', handler, true) |
||||
}) |
||||
}) |
||||
// cleanup is called whenever the watcher is re-evaluated or stopped
|
||||
cleanup(() => { |
||||
document.removeEventListener('keydown', handler, true) |
||||
}) |
||||
}, |
||||
{ immediate }, |
||||
) |
||||
} |
||||
} |
||||
|
||||
export { useSelectedCellKeyupListener, useSelectedCellKeyupListener as useActiveKeyupListener } |
||||
|
Loading…
Reference in new issue