kit.tsx
17.5 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
"use client";
/**
* 系统管理页共用 UI 套件(蓝底主题,与门户/登录页的蓝色品牌统一)。
* 提供:页面骨架、工具条、主按钮、状态徽章、可排序表头、分页器、右侧预览抽屉、字段行。
*/
import { useEffect, useRef, useState, type ReactNode } from "react";
import {
ArrowDown,
ArrowUp,
ChevronLeft,
ChevronRight,
ChevronsLeft,
ChevronsRight,
Loader2,
Search,
X,
} from "lucide-react";
import { cn } from "@/lib/utils";
export type SortState = { by: string; order: "asc" | "desc" };
/** 监听 Esc 键关闭弹层。 */
export function useEscClose(active: boolean, onClose: () => void) {
useEffect(() => {
if (!active) return;
const h = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
};
window.addEventListener("keydown", h);
return () => window.removeEventListener("keydown", h);
}, [active, onClose]);
}
/** 页面骨架:撑满高度,标题 + 右侧动作区 + 内容自适应滚动。 */
export function SysPage({
title,
subtitle,
actions,
children,
}: {
title: string;
subtitle?: string;
actions?: ReactNode;
children: ReactNode;
}) {
return (
<div className="flex h-full min-h-0 flex-col gap-3 overflow-hidden p-5">
<div className="flex shrink-0 items-end justify-between gap-4">
<div>
<h1 className="text-xl font-bold text-slate-800">{title}</h1>
{subtitle && <p className="mt-1 text-xs text-slate-500">{subtitle}</p>}
</div>
<div className="flex items-center gap-2">{actions}</div>
</div>
{children}
</div>
);
}
/** 白色搜索输入框(统一样式,避免各页深色差异)。 */
export function SearchInput({
value,
onChange,
onEnter,
placeholder,
className,
}: {
value: string;
onChange: (v: string) => void;
onEnter?: () => void;
placeholder?: string;
className?: string;
}) {
return (
<div className={cn("relative", className)}>
<Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-400" />
<input
value={value}
onChange={(e) => onChange(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && onEnter?.()}
placeholder={placeholder}
className="w-full rounded-lg border border-slate-200 bg-white py-2 pl-9 pr-3 text-sm text-slate-800 placeholder:text-slate-400 outline-none transition focus:border-blue-400 focus:ring-2 focus:ring-blue-400/20"
/>
</div>
);
}
/** 卡片容器:白底、圆角、细边、蓝色阴影,撑满剩余高度并内部滚动。 */
export function Card({ children, className }: { children: ReactNode; className?: string }) {
return (
<div
className={cn(
"flex min-h-0 flex-1 flex-col overflow-hidden rounded-2xl border border-blue-100 bg-white shadow-sm shadow-blue-900/5",
className
)}
>
{children}
</div>
);
}
export function PrimaryButton({
children,
onClick,
disabled,
className,
type = "button",
}: {
children: ReactNode;
onClick?: () => void;
disabled?: boolean;
className?: string;
type?: "button" | "submit";
}) {
return (
<button
type={type}
onClick={onClick}
disabled={disabled}
className={cn(
"inline-flex items-center gap-1.5 rounded-lg bg-blue-600 px-3.5 py-2 text-sm font-medium text-white shadow-sm shadow-blue-500/25 transition hover:bg-blue-700 disabled:cursor-not-allowed disabled:bg-blue-300",
className
)}
>
{children}
</button>
);
}
export function GhostButton({
children,
onClick,
disabled,
danger,
className,
}: {
children: ReactNode;
onClick?: () => void;
disabled?: boolean;
danger?: boolean;
className?: string;
}) {
return (
<button
type="button"
onClick={onClick}
disabled={disabled}
className={cn(
"inline-flex items-center gap-1.5 rounded-lg border px-3.5 py-2 text-sm font-medium transition disabled:cursor-not-allowed disabled:opacity-50",
danger
? "border-rose-200 bg-rose-50 text-rose-600 hover:bg-rose-100"
: "border-slate-200 bg-white text-slate-600 hover:bg-slate-50",
className
)}
>
{children}
</button>
);
}
export function StatusBadge({ status }: { status: string }) {
const active = status === "active";
return (
<span
className={cn(
"inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-xs font-medium",
active ? "bg-emerald-50 text-emerald-600" : "bg-slate-100 text-slate-400"
)}
>
<span className={cn("h-1.5 w-1.5 rounded-full", active ? "bg-emerald-500" : "bg-slate-400")} />
{active ? "启用" : "停用"}
</span>
);
}
/** 工具条:白底、可换行,承载搜索框与筛选下拉。 */
export function Toolbar({ children }: { children: ReactNode }) {
return (
<div className="flex shrink-0 flex-wrap items-center gap-2 rounded-xl border border-slate-200 bg-white p-2.5">
{children}
</div>
);
}
export function Select({
value,
onChange,
children,
className,
}: {
value: string;
onChange: (v: string) => void;
children: ReactNode;
className?: string;
}) {
return (
<select
value={value}
onChange={(e) => onChange(e.target.value)}
className={cn(
"rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700 outline-none transition focus:border-blue-500 focus:ring-2 focus:ring-blue-500/20",
className
)}
>
{children}
</select>
);
}
/** 可排序表头单元格。点击切换该列排序(同列翻转升降序,换列默认降序)。 */
export function SortHeader({
label,
field,
sort,
onSort,
className,
}: {
label: string;
field: string;
sort: SortState;
onSort: (s: SortState) => void;
className?: string;
}) {
const activeCol = sort.by === field;
const toggle = () =>
onSort(
activeCol
? { by: field, order: sort.order === "asc" ? "desc" : "asc" }
: { by: field, order: "desc" }
);
return (
<th className={cn("select-none px-4 py-2.5", className)}>
<button
onClick={toggle}
className={cn(
"inline-flex items-center gap-1 font-semibold transition",
activeCol ? "text-blue-700" : "text-slate-500 hover:text-slate-700"
)}
>
{label}
{activeCol ? (
sort.order === "asc" ? (
<ArrowUp className="h-3.5 w-3.5" />
) : (
<ArrowDown className="h-3.5 w-3.5" />
)
) : (
<ArrowDown className="h-3.5 w-3.5 opacity-25" />
)}
</button>
</th>
);
}
const PAGE_SIZE_OPTIONS = [10, 20, 50, 100];
function pageRange(current: number, totalPages: number): (number | "…")[] {
if (totalPages <= 7) return Array.from({ length: totalPages }, (_, i) => i + 1);
const set = new Set<number>([1, totalPages, current, current - 1, current + 1]);
const sorted = Array.from(set)
.filter((p) => p >= 1 && p <= totalPages)
.sort((a, b) => a - b);
const out: (number | "…")[] = [];
sorted.forEach((p, i) => {
if (i > 0 && p - sorted[i - 1] > 1) out.push("…");
out.push(p);
});
return out;
}
/** 分页器(1 基页码)。参考 Wiki 原始文件页的分页组件。 */
export function Pagination({
page,
pageSize,
total,
onPage,
onPageSize,
selectedCount,
}: {
page: number;
pageSize: number;
total: number;
onPage: (p: number) => void;
onPageSize: (s: number) => void;
selectedCount?: number;
}) {
const totalPages = Math.max(1, Math.ceil(total / pageSize));
const cur = Math.min(page, totalPages);
return (
<div className="flex shrink-0 flex-wrap items-center justify-between gap-3 border-t border-blue-100 bg-white px-4 py-2.5 text-sm text-slate-500">
<div className="flex items-center gap-3">
<span>
共 <span className="font-semibold text-slate-700">{total}</span> 条
{selectedCount ? (
<span className="ml-2 text-blue-600">已选 {selectedCount}</span>
) : null}
</span>
<Select value={String(pageSize)} onChange={(v) => onPageSize(Number(v))} className="py-1">
{PAGE_SIZE_OPTIONS.map((s) => (
<option key={s} value={s}>
{s} 条/页
</option>
))}
</Select>
</div>
<div className="flex items-center gap-1">
<PagerBtn onClick={() => onPage(1)} disabled={cur <= 1}>
<ChevronsLeft className="h-4 w-4" />
</PagerBtn>
<PagerBtn onClick={() => onPage(cur - 1)} disabled={cur <= 1}>
<ChevronLeft className="h-4 w-4" />
</PagerBtn>
{pageRange(cur, totalPages).map((p, i) =>
p === "…" ? (
<span key={`e${i}`} className="px-1 text-slate-300">
…
</span>
) : (
<button
key={p}
onClick={() => onPage(p)}
className={cn(
"min-w-[2rem] rounded-lg px-2 py-1 text-sm font-medium transition",
p === cur
? "bg-blue-600 text-white shadow-sm shadow-blue-500/25"
: "text-slate-600 hover:bg-blue-50"
)}
>
{p}
</button>
)
)}
<PagerBtn onClick={() => onPage(cur + 1)} disabled={cur >= totalPages}>
<ChevronRight className="h-4 w-4" />
</PagerBtn>
<PagerBtn onClick={() => onPage(totalPages)} disabled={cur >= totalPages}>
<ChevronsRight className="h-4 w-4" />
</PagerBtn>
</div>
</div>
);
}
function PagerBtn({
children,
onClick,
disabled,
}: {
children: ReactNode;
onClick: () => void;
disabled: boolean;
}) {
return (
<button
onClick={onClick}
disabled={disabled}
className="rounded-lg p-1.5 text-slate-500 transition hover:bg-blue-50 disabled:cursor-not-allowed disabled:opacity-30"
>
{children}
</button>
);
}
/** 右侧预览抽屉。点击列表行后滑出,展示详情 + 底部操作区。 */
export function Drawer({
open,
title,
subtitle,
onClose,
children,
footer,
}: {
open: boolean;
title: ReactNode;
subtitle?: ReactNode;
onClose: () => void;
children: ReactNode;
footer?: ReactNode;
}) {
const downOnSelf = useRef(false);
useEscClose(open, onClose);
// 挂载后下一帧再置 entered,使条件渲染(一挂载 open 即 true)时进入动画也能播放
const [entered, setEntered] = useState(false);
useEffect(() => {
const id = requestAnimationFrame(() => setEntered(true));
return () => cancelAnimationFrame(id);
}, []);
const shown = open && entered;
return (
<div
className={cn(
"fixed inset-0 z-40 transition",
open ? "pointer-events-auto" : "pointer-events-none"
)}
>
<div
onMouseDown={(e) => (downOnSelf.current = e.target === e.currentTarget)}
onMouseUp={(e) => {
if (downOnSelf.current && e.target === e.currentTarget) onClose();
downOnSelf.current = false;
}}
className={cn(
"absolute inset-0 bg-slate-900/20 transition-opacity duration-300",
shown ? "opacity-100" : "opacity-0"
)}
/>
<aside
className={cn(
"absolute right-0 top-0 flex h-full w-full max-w-lg flex-col bg-white shadow-2xl transition-transform duration-300",
shown ? "translate-x-0" : "translate-x-full"
)}
>
<div className="flex items-start justify-between gap-3 border-b border-blue-100 bg-gradient-to-r from-blue-50 to-white px-6 py-4">
<div className="min-w-0">
<div className="truncate text-base font-semibold text-slate-800">{title}</div>
{subtitle && <div className="mt-0.5 truncate text-xs text-slate-500">{subtitle}</div>}
</div>
<button
onClick={onClose}
className="shrink-0 rounded-lg p-1 text-slate-400 transition hover:bg-slate-100 hover:text-slate-600"
>
<X className="h-5 w-5" />
</button>
</div>
<div className="min-h-0 flex-1 overflow-y-auto px-6 py-5">{children}</div>
{footer && (
<div className="flex flex-wrap justify-end gap-2 border-t border-slate-100 px-6 py-3.5">
{footer}
</div>
)}
</aside>
</div>
);
}
/** 详情字段行(预览抽屉内使用)。 */
export function Field({ label, children }: { label: string; children: ReactNode }) {
return (
<div className="grid grid-cols-[5.5rem_1fr] gap-3 border-b border-slate-50 py-2.5 text-sm last:border-0">
<div className="text-slate-400">{label}</div>
<div className="break-words text-slate-700">{children ?? "—"}</div>
</div>
);
}
/** 居中加载态。 */
export function Spinner() {
return (
<div className="flex flex-1 items-center justify-center py-16">
<Loader2 className="h-6 w-6 animate-spin text-blue-400" />
</div>
);
}
/** 表格复选框(蓝色)。 */
export function Check({
checked,
indeterminate,
onChange,
}: {
checked: boolean;
indeterminate?: boolean;
onChange: (v: boolean) => void;
}) {
return (
<input
type="checkbox"
checked={checked}
ref={(el) => {
if (el) el.indeterminate = Boolean(indeterminate) && !checked;
}}
onChange={(e) => onChange(e.target.checked)}
onClick={(e) => e.stopPropagation()}
className="h-4 w-4 cursor-pointer rounded border-slate-300 text-blue-600 accent-blue-600"
/>
);
}
const MODAL_W: Record<string, string> = {
md: "max-w-md",
lg: "max-w-lg",
xl: "max-w-2xl",
"2xl": "max-w-3xl",
"3xl": "max-w-5xl",
};
/** 轻量弹窗(表单用,蓝色标题条)。支持 Esc 关闭、多种宽度、更大内边距。 */
export function Modal({
title,
onClose,
children,
wide,
size,
}: {
title: string;
onClose: () => void;
children: ReactNode;
wide?: boolean;
size?: "md" | "lg" | "xl" | "2xl" | "3xl";
}) {
const downOnSelf = useRef(false);
useEscClose(true, onClose);
const w = MODAL_W[size ?? (wide ? "lg" : "md")];
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-slate-900/40 p-4"
onMouseDown={(e) => (downOnSelf.current = e.target === e.currentTarget)}
onMouseUp={(e) => {
if (downOnSelf.current && e.target === e.currentTarget) onClose();
downOnSelf.current = false;
}}
>
<div className={cn("flex max-h-[90vh] w-full flex-col overflow-hidden rounded-2xl bg-white shadow-2xl", w)}>
<div className="shrink-0 border-b border-blue-100 bg-gradient-to-r from-blue-50 to-white px-7 py-5 text-lg font-semibold text-slate-800">
{title}
</div>
<div className="min-h-0 flex-1 overflow-y-auto px-7 py-6">{children}</div>
</div>
</div>
);
}
/** 表单输入框。 */
export function Input({
value,
onChange,
placeholder,
type = "text",
}: {
value: string;
onChange: (v: string) => void;
placeholder?: string;
type?: string;
}) {
return (
<input
type={type}
value={value}
onChange={(e) => onChange(e.target.value)}
placeholder={placeholder}
className="w-full rounded-lg border border-slate-200 px-3 py-2 text-sm outline-none transition focus:border-blue-500 focus:ring-2 focus:ring-blue-500/20"
/>
);
}
/** 表单标签 + 控件包装。 */
export function FormRow({ label, children }: { label: string; children: ReactNode }) {
return (
<label className="block space-y-1">
<span className="text-xs font-medium text-slate-500">{label}</span>
{children}
</label>
);
}
/** 带搜索的多选清单(岗位/角色等多选,选项多时可过滤)。 */
export function MultiSelectList({
options,
selected,
onToggle,
searchable,
height = "max-h-56",
emptyText = "无可选项",
}: {
options: { id: string; label: string; hint?: string }[];
selected: Set<string>;
onToggle: (id: string) => void;
searchable?: boolean;
/** 列表最大高度类(如 max-h-48 ≈5 行);内容少自动收缩、超出滚动。 */
height?: string;
emptyText?: string;
}) {
const [q, setQ] = useState("");
const filtered = q.trim()
? options.filter((o) => o.label.includes(q.trim()) || (o.hint ?? "").includes(q.trim()))
: options;
return (
<div className="flex flex-col rounded-lg border border-slate-200">
{searchable && (
<div className="shrink-0 border-b border-slate-100 p-1.5">
<SearchInput value={q} onChange={setQ} placeholder="输入以筛选…" />
</div>
)}
<div className={cn("space-y-0.5 overflow-y-auto p-1.5", height)}>
{filtered.length === 0 ? (
<div className="py-6 text-center text-xs text-slate-400">{emptyText}</div>
) : (
filtered.map((o) => (
<label
key={o.id}
title={o.label}
className={cn(
"flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5 transition hover:bg-blue-50",
selected.has(o.id) && "bg-blue-50/70"
)}
>
<input
type="checkbox"
checked={selected.has(o.id)}
onChange={() => onToggle(o.id)}
className="h-4 w-4 shrink-0 accent-blue-600"
/>
<span className="flex-1 truncate whitespace-nowrap text-sm text-slate-700">{o.label}</span>
{o.hint && <span className="shrink-0 truncate whitespace-nowrap text-[11px] text-slate-400">{o.hint}</span>}
</label>
))
)}
</div>
</div>
);
}