Skip to content

Commit 666e4ea

Browse files
committed
fix(core): allow multiselect when input is focused (#1842)
* fix(core): allow multiselect when input is focused Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com> * chore(changeset): add --------- Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
1 parent a0c83f8 commit 666e4ea

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

.changeset/blue-frogs-swim.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": patch
3+
---
4+
5+
Allow multi-select when input is focused.

packages/core/src/composables/useKeyPress.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MaybeRefOrGetter } from 'vue'
2-
import { ref, toRef, toValue, watch } from 'vue'
2+
import { computed, shallowRef, toValue, watch } from 'vue'
33
import type { KeyFilter, KeyPredicate } from '@vueuse/core'
44
import { onKeyStroke, useEventListener } from '@vueuse/core'
55

@@ -14,6 +14,8 @@ export interface UseKeyPressOptions {
1414

1515
const inputTags = ['INPUT', 'SELECT', 'TEXTAREA']
1616

17+
const defaultDoc = typeof document !== 'undefined' ? document : null
18+
1719
export function isInputDOMNode(event: KeyboardEvent): boolean {
1820
const target = (event.composedPath?.()?.[0] || event.target) as HTMLElement
1921

@@ -87,13 +89,9 @@ function useKeyOrCode(code: string, keysToWatch: string | string[]): KeyOrCode {
8789
* @param options - Options object
8890
*/
8991
export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | boolean | null>, options?: UseKeyPressOptions) {
90-
const actInsideInputWithModifier = toRef(() => toValue(options?.actInsideInputWithModifier) ?? false)
91-
92-
const target = toRef(() => toValue(options?.target) ?? window)
93-
94-
const preventDefault = toRef(() => toValue(options?.preventDefault) ?? true)
92+
const target = computed(() => toValue(options?.target) ?? defaultDoc)
9593

96-
const isPressed = ref(toValue(keyFilter) === true)
94+
const isPressed = shallowRef(toValue(keyFilter) === true)
9795

9896
let modifierPressed = false
9997

@@ -121,9 +119,12 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | boolean | nu
121119
onKeyStroke(
122120
(...args) => currentFilter(...args),
123121
(e) => {
122+
const actInsideInputWithModifier = toValue(options?.actInsideInputWithModifier) ?? true
123+
const preventDefault = toValue(options?.preventDefault) ?? false
124+
124125
modifierPressed = wasModifierPressed(e)
125126

126-
const preventAction = (!modifierPressed || (modifierPressed && !actInsideInputWithModifier.value)) && isInputDOMNode(e)
127+
const preventAction = (!modifierPressed || (modifierPressed && !actInsideInputWithModifier)) && isInputDOMNode(e)
127128

128129
if (preventAction) {
129130
return
@@ -132,7 +133,7 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | boolean | nu
132133
const target = (e.composedPath?.()?.[0] || e.target) as Element | null
133134
const isInteractiveElement = target?.nodeName === 'BUTTON' || target?.nodeName === 'A'
134135

135-
if (!preventDefault.value && (modifierPressed || !isInteractiveElement)) {
136+
if (!preventDefault && (modifierPressed || !isInteractiveElement)) {
136137
e.preventDefault()
137138
}
138139

@@ -144,8 +145,10 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | boolean | nu
144145
onKeyStroke(
145146
(...args) => currentFilter(...args),
146147
(e) => {
148+
const actInsideInputWithModifier = toValue(options?.actInsideInputWithModifier) ?? true
149+
147150
if (isPressed.value) {
148-
const preventAction = (!modifierPressed || (modifierPressed && !actInsideInputWithModifier.value)) && isInputDOMNode(e)
151+
const preventAction = (!modifierPressed || (modifierPressed && !actInsideInputWithModifier)) && isInputDOMNode(e)
149152

150153
if (preventAction) {
151154
return

0 commit comments

Comments
 (0)