mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-15 07:35:27 +03:00
Prevent keyboard shortcuts from running when input is focused
This commit is contained in:
parent
ae547d27e4
commit
dc321b4c79
@ -1,4 +1,4 @@
|
|||||||
import { useState, useRef, useEffect, useCallback } from "react";
|
import React, { useState, useRef, useEffect, useCallback } from "react";
|
||||||
import {
|
import {
|
||||||
LuX,
|
LuX,
|
||||||
LuFilter,
|
LuFilter,
|
||||||
@ -45,6 +45,8 @@ import useSWR from "swr";
|
|||||||
import { FrigateConfig } from "@/types/frigateConfig";
|
import { FrigateConfig } from "@/types/frigateConfig";
|
||||||
|
|
||||||
type InputWithTagsProps = {
|
type InputWithTagsProps = {
|
||||||
|
inputFocused: boolean;
|
||||||
|
setInputFocused: React.Dispatch<React.SetStateAction<boolean>>;
|
||||||
filters: SearchFilter;
|
filters: SearchFilter;
|
||||||
setFilters: (filter: SearchFilter) => void;
|
setFilters: (filter: SearchFilter) => void;
|
||||||
search: string;
|
search: string;
|
||||||
@ -55,6 +57,8 @@ type InputWithTagsProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default function InputWithTags({
|
export default function InputWithTags({
|
||||||
|
inputFocused,
|
||||||
|
setInputFocused,
|
||||||
filters,
|
filters,
|
||||||
setFilters,
|
setFilters,
|
||||||
search,
|
search,
|
||||||
@ -69,7 +73,6 @@ export default function InputWithTags({
|
|||||||
const [currentFilterType, setCurrentFilterType] = useState<FilterType | null>(
|
const [currentFilterType, setCurrentFilterType] = useState<FilterType | null>(
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
const [inputFocused, setInputFocused] = useState(false);
|
|
||||||
const [isSimilaritySearch, setIsSimilaritySearch] = useState(false);
|
const [isSimilaritySearch, setIsSimilaritySearch] = useState(false);
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
const commandRef = useRef<HTMLDivElement>(null);
|
const commandRef = useRef<HTMLDivElement>(null);
|
||||||
@ -381,7 +384,7 @@ export default function InputWithTags({
|
|||||||
|
|
||||||
const handleInputFocus = useCallback(() => {
|
const handleInputFocus = useCallback(() => {
|
||||||
setInputFocused(true);
|
setInputFocused(true);
|
||||||
}, []);
|
}, [setInputFocused]);
|
||||||
|
|
||||||
const handleClearInput = useCallback(() => {
|
const handleClearInput = useCallback(() => {
|
||||||
setInputFocused(false);
|
setInputFocused(false);
|
||||||
@ -392,16 +395,19 @@ export default function InputWithTags({
|
|||||||
setFilters({});
|
setFilters({});
|
||||||
setCurrentFilterType(null);
|
setCurrentFilterType(null);
|
||||||
setIsSimilaritySearch(false);
|
setIsSimilaritySearch(false);
|
||||||
}, [setFilters, resetSuggestions, setSearch]);
|
}, [setFilters, resetSuggestions, setSearch, setInputFocused]);
|
||||||
|
|
||||||
const handleInputBlur = useCallback((e: React.FocusEvent) => {
|
const handleInputBlur = useCallback(
|
||||||
|
(e: React.FocusEvent) => {
|
||||||
if (
|
if (
|
||||||
commandRef.current &&
|
commandRef.current &&
|
||||||
!commandRef.current.contains(e.relatedTarget as Node)
|
!commandRef.current.contains(e.relatedTarget as Node)
|
||||||
) {
|
) {
|
||||||
setInputFocused(false);
|
setInputFocused(false);
|
||||||
}
|
}
|
||||||
}, []);
|
},
|
||||||
|
[setInputFocused],
|
||||||
|
);
|
||||||
|
|
||||||
const handleSuggestionClick = useCallback(
|
const handleSuggestionClick = useCallback(
|
||||||
(suggestion: string) => {
|
(suggestion: string) => {
|
||||||
@ -449,7 +455,7 @@ export default function InputWithTags({
|
|||||||
setInputFocused(false);
|
setInputFocused(false);
|
||||||
inputRef?.current?.blur();
|
inputRef?.current?.blur();
|
||||||
},
|
},
|
||||||
[setSearch],
|
[setSearch, setInputFocused],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleInputKeyDown = useCallback(
|
const handleInputKeyDown = useCallback(
|
||||||
|
|||||||
@ -10,6 +10,7 @@ export type KeyModifiers = {
|
|||||||
export default function useKeyboardListener(
|
export default function useKeyboardListener(
|
||||||
keys: string[],
|
keys: string[],
|
||||||
listener: (key: string | null, modifiers: KeyModifiers) => void,
|
listener: (key: string | null, modifiers: KeyModifiers) => void,
|
||||||
|
preventDefault: boolean = true,
|
||||||
) {
|
) {
|
||||||
const keyDownListener = useCallback(
|
const keyDownListener = useCallback(
|
||||||
(e: KeyboardEvent) => {
|
(e: KeyboardEvent) => {
|
||||||
@ -25,13 +26,13 @@ export default function useKeyboardListener(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (keys.includes(e.key)) {
|
if (keys.includes(e.key)) {
|
||||||
e.preventDefault();
|
if (preventDefault) e.preventDefault();
|
||||||
listener(e.key, modifiers);
|
listener(e.key, modifiers);
|
||||||
} else if (e.key === "Shift" || e.key === "Control" || e.key === "Meta") {
|
} else if (e.key === "Shift" || e.key === "Control" || e.key === "Meta") {
|
||||||
listener(null, modifiers);
|
listener(null, modifiers);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[keys, listener],
|
[keys, listener, preventDefault],
|
||||||
);
|
);
|
||||||
|
|
||||||
const keyUpListener = useCallback(
|
const keyUpListener = useCallback(
|
||||||
|
|||||||
@ -209,9 +209,11 @@ export default function SearchView({
|
|||||||
|
|
||||||
// keyboard listener
|
// keyboard listener
|
||||||
|
|
||||||
|
const [inputFocused, setInputFocused] = useState(false);
|
||||||
|
|
||||||
const onKeyboardShortcut = useCallback(
|
const onKeyboardShortcut = useCallback(
|
||||||
(key: string | null, modifiers: KeyModifiers) => {
|
(key: string | null, modifiers: KeyModifiers) => {
|
||||||
if (!modifiers.down || !uniqueResults) {
|
if (!modifiers.down || !uniqueResults || inputFocused) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,10 +238,14 @@ export default function SearchView({
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[uniqueResults],
|
[uniqueResults, inputFocused],
|
||||||
);
|
);
|
||||||
|
|
||||||
useKeyboardListener(["ArrowLeft", "ArrowRight"], onKeyboardShortcut);
|
useKeyboardListener(
|
||||||
|
["ArrowLeft", "ArrowRight"],
|
||||||
|
onKeyboardShortcut,
|
||||||
|
!inputFocused,
|
||||||
|
);
|
||||||
|
|
||||||
// scroll into view
|
// scroll into view
|
||||||
|
|
||||||
@ -310,6 +316,8 @@ export default function SearchView({
|
|||||||
{config?.semantic_search?.enabled && (
|
{config?.semantic_search?.enabled && (
|
||||||
<div className={cn("z-[41] w-full lg:absolute lg:top-0 lg:w-1/3")}>
|
<div className={cn("z-[41] w-full lg:absolute lg:top-0 lg:w-1/3")}>
|
||||||
<InputWithTags
|
<InputWithTags
|
||||||
|
inputFocused={inputFocused}
|
||||||
|
setInputFocused={setInputFocused}
|
||||||
filters={searchFilter ?? {}}
|
filters={searchFilter ?? {}}
|
||||||
setFilters={setSearchFilter}
|
setFilters={setSearchFilter}
|
||||||
search={search}
|
search={search}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user