-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathindex.tsx
More file actions
94 lines (83 loc) · 2.19 KB
/
Copy pathindex.tsx
File metadata and controls
94 lines (83 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { useBoolean, useKeyPress } from "ahooks";
import type { InputRef } from "antd";
import { Input } from "antd";
import {
type FC,
type HTMLAttributes,
useContext,
useEffect,
useRef,
useState,
} from "react";
import { useTranslation } from "react-i18next";
import UnoIcon from "@/components/UnoIcon";
import { PRESET_SHORTCUT } from "@/constants";
import { useTauriFocus } from "@/hooks/useTauriFocus";
import { clipboardStore } from "@/stores/clipboard";
import { MainContext } from "../..";
const SearchInput: FC<HTMLAttributes<HTMLDivElement>> = (props) => {
const { rootState } = useContext(MainContext);
const inputRef = useRef<InputRef>(null);
const [value, setValue] = useState<string>();
const [isComposition, { setTrue, setFalse }] = useBoolean();
const { t } = useTranslation();
useEffect(() => {
if (isComposition) return;
// 搜索防抖:避免每个按键都触发一次查库
const timer = window.setTimeout(() => {
rootState.search = value;
}, 200);
return () => {
window.clearTimeout(timer);
};
}, [value, isComposition]);
useTauriFocus({
onBlur() {
const { search } = clipboardStore;
// 搜索框自动清空
if (search.autoClear) {
setValue(void 0);
}
},
onFocus() {
const { search } = clipboardStore;
// 搜索框默认聚焦
if (search.defaultFocus) {
inputRef.current?.focus();
} else {
inputRef.current?.blur();
}
},
});
useKeyPress(PRESET_SHORTCUT.SEARCH, () => {
inputRef.current?.focus();
});
useKeyPress(
["enter", "uparrow", "downarrow"],
() => {
inputRef.current?.blur();
},
{
target: inputRef.current?.input,
},
);
return (
<div {...props}>
<Input
allowClear
autoCorrect="off"
onChange={(event) => {
setValue(event.target.value);
}}
onCompositionEnd={setFalse}
onCompositionStart={setTrue}
placeholder={t("clipboard.hints.search_placeholder")}
prefix={<UnoIcon name="i-lucide:search" />}
ref={inputRef}
size="small"
value={value}
/>
</div>
);
};
export default SearchInput;