Skip to content
This repository was archived by the owner on Dec 5, 2023. It is now read-only.

Skip event for given node types #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface config {
minLength?: number,
/** Ignore scan input if this node is focused.*/
ignoreIfFocusOn?: Node,
/** nodes to skip i.e. text useful when you want to stick to default behaviour for inputs, but trigger something special otherwise */
skipNodeTypes?: string[];
/** Stop propagation on keydown event. Defaults to false.*/
stopPropagation?: Boolean,
/** Prevent default on keydown event. Defaults to false.*/
Expand All @@ -51,6 +53,7 @@ const useScanDetection = ({
onError,
minLength = 1,
ignoreIfFocusOn,
skipNodeTypes = [],
stopPropagation = false,
preventDefault = false,
container = document
Expand Down Expand Up @@ -87,22 +90,26 @@ const useScanDetection = ({
}

const onKeyDown: Function = useCallback((event: KeyboardEvent) => {
if (event.currentTarget !== ignoreIfFocusOn) {
if (endCharacter.includes(event.keyCode)) {
evaluateBuffer()
}
if (buffer.current.length > 0 || startCharacter.includes(event.keyCode) || startCharacter.length === 0) {
clearTimeout(timeout.current)
timeout.current = setTimeout(evaluateBuffer, timeToEvaluate)
buffer.current.push({ time: performance.now(), char: event.key })
}
}
if (stopPropagation) {
event.stopPropagation()
}
if (preventDefault) {
event.preventDefault()
}
if (event.currentTarget === ignoreIfFocusOn) {
return
}
if(skipNodeTypes.includes((event.target as any).type)) {
return
}
if (endCharacter.includes(event.keyCode)) {
evaluateBuffer()
}
if (buffer.current.length > 0 || startCharacter.includes(event.keyCode) || startCharacter.length === 0) {
clearTimeout(timeout.current)
timeout.current = setTimeout(evaluateBuffer, timeToEvaluate)
buffer.current.push({time: performance.now(), char: event.key})
}
}, [
startCharacter,
endCharacter,
Expand Down