StaffPicker.tsx
4.94 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { useEffect, useRef, useState } from "react";
import { SearchOutlined } from "@ant-design/icons";
import { searchStaff, type StaffSearchVO } from "@/api/staff";
interface Props {
value: string;
onChange: (name: string, staffId: number | null) => void;
disabled?: boolean;
required?: boolean;
placeholder?: string;
}
const fieldControl: React.CSSProperties = {
flex: 1,
minWidth: 0,
height: "var(--input-h)",
border: "1px solid var(--border-input)",
background: "var(--bg-input)",
padding: "0 26px 0 6px",
fontSize: 12,
color: "var(--text)",
borderRadius: 0,
outline: "none",
fontFamily: "inherit",
};
export default function StaffPicker({
value,
onChange,
disabled,
required,
placeholder,
}: Props) {
const [open, setOpen] = useState(false);
const [items, setItems] = useState<StaffSearchVO[]>([]);
const [loading, setLoading] = useState(false);
const wrapRef = useRef<HTMLDivElement>(null);
const debounce = useRef<number | null>(null);
useEffect(() => {
function onDocClick(e: MouseEvent) {
if (wrapRef.current && !wrapRef.current.contains(e.target as Node)) {
setOpen(false);
}
}
document.addEventListener("mousedown", onDocClick);
return () => document.removeEventListener("mousedown", onDocClick);
}, []);
const fetchSuggestions = async (kw: string) => {
setLoading(true);
try {
const list = await searchStaff(kw, 20);
setItems(list);
} catch {
setItems([]);
} finally {
setLoading(false);
}
};
const onFocus = () => {
if (disabled) return;
setOpen(true);
void fetchSuggestions(value);
};
const onInputChange = (next: string) => {
// Typing clears any previously bound staff id; user must re-select
onChange(next, null);
setOpen(true);
if (debounce.current) window.clearTimeout(debounce.current);
debounce.current = window.setTimeout(() => fetchSuggestions(next), 200);
};
const select = (s: StaffSearchVO) => {
onChange(s.sStaffName, s.iIncrement);
setOpen(false);
};
return (
<div
ref={wrapRef}
style={{ position: "relative", flex: 1, minWidth: 0, display: "flex", alignItems: "center" }}
>
<input
type="text"
value={value}
onChange={(e) => onInputChange(e.target.value)}
onFocus={onFocus}
disabled={disabled}
placeholder={placeholder}
style={{
...fieldControl,
...(disabled ? { background: "var(--bg-disabled)", color: "var(--text-muted)" } : {}),
...(required && !disabled ? { background: "#d4e8f7" } : {}),
}}
/>
<span
style={{
position: "absolute",
right: 6,
top: "50%",
transform: "translateY(-50%)",
color: "var(--text-faint)",
pointerEvents: "none",
fontSize: 12,
}}
>
<SearchOutlined />
</span>
{open && !disabled && (
<div
style={{
position: "absolute",
top: "calc(100% + 1px)",
left: 0,
right: 0,
zIndex: 50,
background: "#fff",
border: "1px solid var(--border-input)",
boxShadow: "0 2px 8px rgba(0,0,0,0.08)",
maxHeight: 240,
overflow: "auto",
}}
>
{loading && (
<div style={{ padding: "8px 12px", fontSize: 12, color: "var(--text-faint)" }}>
加载中…
</div>
)}
{!loading && items.length === 0 && (
<div style={{ padding: "8px 12px", fontSize: 12, color: "var(--text-faint)" }}>
没有匹配的职员
</div>
)}
{!loading &&
items.map((s) => (
<div
key={s.iIncrement}
onMouseDown={(e) => {
e.preventDefault();
select(s);
}}
style={{
padding: "6px 12px",
fontSize: 12,
cursor: "pointer",
display: "flex",
alignItems: "center",
gap: 8,
borderBottom: "1px solid #f0f2f5",
}}
onMouseEnter={(e) => {
e.currentTarget.style.background = "var(--bg-row-hover)";
}}
onMouseLeave={(e) => {
e.currentTarget.style.background = "transparent";
}}
>
<span style={{ color: "var(--text)" }}>{s.sStaffName}</span>
<span style={{ color: "var(--text-faint)", fontSize: 11 }} className="mono">
{s.sStaffNo}
</span>
<span style={{ flex: 1 }} />
<span style={{ color: "var(--text-muted)", fontSize: 11 }}>
{s.sDepartment ?? ""}
</span>
</div>
))}
</div>
)}
</div>
);
}