@ug666/admin-react 0.0.0-stage → 0.1.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/LICENSE +21 -0
  2. package/dist/AdminColumnSettings-09epsf86.js +80 -0
  3. package/dist/AdminColumnSettings-4cv6a9m2.js +125 -0
  4. package/dist/AdminColumnSettings-93ajawas.js +64 -0
  5. package/dist/AdminColumnSettings-aqqcc4wq.js +34 -0
  6. package/dist/AdminColumnSettings-be5c6tmr.js +43 -0
  7. package/dist/AdminColumnSettings-nnrtybx5.js +88 -0
  8. package/dist/AdminColumnSettings-sz4q6vza.js +177 -0
  9. package/dist/AdminColumnSettings-t2b7bfqc.js +43 -0
  10. package/dist/AdminColumnSettings-tf48jqrt.js +132 -0
  11. package/dist/index.js +42 -0
  12. package/dist/index.ts.js +1 -0
  13. package/dist/patterns/AdminColumnSettings.js +8 -0
  14. package/dist/patterns/AdminConfirmAction.js +6 -0
  15. package/dist/patterns/AdminDetailPage.js +7 -0
  16. package/dist/patterns/AdminFormDialog.js +6 -0
  17. package/dist/patterns/AdminFormPage.js +7 -0
  18. package/dist/patterns/AdminListPage.js +7 -0
  19. package/dist/patterns/AdminPage.js +6 -0
  20. package/dist/patterns/AdminSearchForm.js +8 -0
  21. package/dist/patterns/AdminTabContextMenu.js +6 -0
  22. package/package.json +46 -4
  23. package/src/index.ts +11 -0
  24. package/src/patterns/AdminColumnSettings.tsx +168 -0
  25. package/src/patterns/AdminConfirmAction.tsx +44 -0
  26. package/src/patterns/AdminDetailPage.tsx +44 -0
  27. package/src/patterns/AdminFormDialog.tsx +96 -0
  28. package/src/patterns/AdminFormPage.tsx +113 -0
  29. package/src/patterns/AdminListPage.tsx +62 -0
  30. package/src/patterns/AdminPage.tsx +55 -0
  31. package/src/patterns/AdminSearchForm.tsx +124 -0
  32. package/src/patterns/AdminTabContextMenu.tsx +67 -0
  33. package/README.md +0 -3
@@ -0,0 +1,132 @@
1
+ // packages/admin-react/src/patterns/AdminSearchForm.tsx
2
+ import {
3
+ UGButton,
4
+ UGForm,
5
+ UGFormControl,
6
+ UGFormField,
7
+ UGFormItem,
8
+ UGFormLabel
9
+ } from "@ug666/ui-react";
10
+ import { ChevronDown, ChevronUp, RotateCcw, Search } from "lucide-react";
11
+ import {
12
+ useId,
13
+ useState
14
+ } from "react";
15
+ import { jsxDEV } from "react/jsx-dev-runtime";
16
+ function AdminSearchField2({
17
+ name,
18
+ label,
19
+ labelFor,
20
+ className,
21
+ itemClassName,
22
+ itemStyle,
23
+ children
24
+ }) {
25
+ return /* @__PURE__ */ jsxDEV(UGFormField, {
26
+ name,
27
+ className: className ? `admin-search-field ${className}` : "admin-search-field",
28
+ children: /* @__PURE__ */ jsxDEV(UGFormItem, {
29
+ className: itemClassName,
30
+ style: itemStyle,
31
+ children: [
32
+ /* @__PURE__ */ jsxDEV(UGFormLabel, {
33
+ htmlFor: labelFor,
34
+ children: label
35
+ }, undefined, false, undefined, this),
36
+ /* @__PURE__ */ jsxDEV(UGFormControl, {
37
+ children
38
+ }, undefined, false, undefined, this)
39
+ ]
40
+ }, undefined, true, undefined, this)
41
+ }, undefined, false, undefined, this);
42
+ }
43
+ function AdminSearchForm2({
44
+ children,
45
+ expandedContent,
46
+ labelWidth = 0,
47
+ expandLabel = "展开",
48
+ collapseLabel = "收起",
49
+ loading = false,
50
+ onReset,
51
+ onSubmit
52
+ }) {
53
+ const [expanded, setExpanded] = useState(false);
54
+ const expandedId = useId();
55
+ function handleSubmit(event) {
56
+ event.preventDefault();
57
+ onSubmit();
58
+ }
59
+ return /* @__PURE__ */ jsxDEV(UGForm, {
60
+ className: "admin-search-form",
61
+ layout: "horizontal",
62
+ labelPosition: "right",
63
+ labelWidth,
64
+ size: "default",
65
+ onSubmit: handleSubmit,
66
+ children: [
67
+ /* @__PURE__ */ jsxDEV("div", {
68
+ className: "admin-search-form__fields",
69
+ children: [
70
+ children,
71
+ expandedContent ? /* @__PURE__ */ jsxDEV("div", {
72
+ id: expandedId,
73
+ className: "admin-search-form__expanded",
74
+ hidden: !expanded,
75
+ children: expandedContent
76
+ }, undefined, false, undefined, this) : null
77
+ ]
78
+ }, undefined, true, undefined, this),
79
+ /* @__PURE__ */ jsxDEV("div", {
80
+ className: "admin-search-form__actions",
81
+ children: [
82
+ expandedContent ? /* @__PURE__ */ jsxDEV(UGButton, {
83
+ "aria-expanded": expanded,
84
+ "aria-controls": expandedId,
85
+ onClick: () => setExpanded((value) => !value),
86
+ tone: "neutral",
87
+ type: "button",
88
+ variant: "text",
89
+ children: [
90
+ expanded ? /* @__PURE__ */ jsxDEV(ChevronUp, {
91
+ "aria-hidden": "true",
92
+ size: 16
93
+ }, undefined, false, undefined, this) : /* @__PURE__ */ jsxDEV(ChevronDown, {
94
+ "aria-hidden": "true",
95
+ size: 16
96
+ }, undefined, false, undefined, this),
97
+ expanded ? collapseLabel : expandLabel
98
+ ]
99
+ }, undefined, true, undefined, this) : null,
100
+ /* @__PURE__ */ jsxDEV(UGButton, {
101
+ loading,
102
+ tone: "primary",
103
+ type: "submit",
104
+ variant: "outline",
105
+ children: [
106
+ /* @__PURE__ */ jsxDEV(Search, {
107
+ "aria-hidden": "true",
108
+ size: 16
109
+ }, undefined, false, undefined, this),
110
+ "查询"
111
+ ]
112
+ }, undefined, true, undefined, this),
113
+ /* @__PURE__ */ jsxDEV(UGButton, {
114
+ onClick: onReset,
115
+ tone: "neutral",
116
+ type: "button",
117
+ variant: "outline",
118
+ children: [
119
+ /* @__PURE__ */ jsxDEV(RotateCcw, {
120
+ "aria-hidden": "true",
121
+ size: 16
122
+ }, undefined, false, undefined, this),
123
+ "重置"
124
+ ]
125
+ }, undefined, true, undefined, this)
126
+ ]
127
+ }, undefined, true, undefined, this)
128
+ ]
129
+ }, undefined, true, undefined, this);
130
+ }
131
+
132
+ export { AdminSearchField2, AdminSearchForm2 };
package/dist/index.js ADDED
@@ -0,0 +1,42 @@
1
+ import {
2
+ useAdminColumnLayout2,
3
+ AdminColumnSettings2
4
+ } from "./AdminColumnSettings-sz4q6vza.js";
5
+ import {
6
+ AdminConfirmAction2
7
+ } from "./AdminColumnSettings-aqqcc4wq.js";
8
+ import {
9
+ AdminPage2
10
+ } from "./AdminColumnSettings-be5c6tmr.js";
11
+ import {
12
+ AdminDetailPage2
13
+ } from "./AdminColumnSettings-t2b7bfqc.js";
14
+ import {
15
+ AdminFormDialog2
16
+ } from "./AdminColumnSettings-nnrtybx5.js";
17
+ import {
18
+ AdminFormPage2
19
+ } from "./AdminColumnSettings-4cv6a9m2.js";
20
+ import {
21
+ AdminListPage2
22
+ } from "./AdminColumnSettings-09epsf86.js";
23
+ import {
24
+ AdminSearchField2,
25
+ AdminSearchForm2
26
+ } from "./AdminColumnSettings-tf48jqrt.js";
27
+ import {
28
+ AdminTabContextMenu2
29
+ } from "./AdminColumnSettings-93ajawas.js";
30
+ export {
31
+ AdminColumnSettings2 as AdminColumnSettings,
32
+ AdminConfirmAction2 as AdminConfirmAction,
33
+ AdminDetailPage2 as AdminDetailPage,
34
+ AdminFormDialog2 as AdminFormDialog,
35
+ AdminFormPage2 as AdminFormPage,
36
+ AdminListPage2 as AdminListPage,
37
+ AdminPage2 as AdminPage,
38
+ AdminSearchField2 as AdminSearchField,
39
+ AdminSearchForm2 as AdminSearchForm,
40
+ AdminTabContextMenu2 as AdminTabContextMenu,
41
+ useAdminColumnLayout2 as useAdminColumnLayout
42
+ };
@@ -0,0 +1 @@
1
+ export * from "./index.ts/index.js";
@@ -0,0 +1,8 @@
1
+ import {
2
+ useAdminColumnLayout2,
3
+ AdminColumnSettings2
4
+ } from "../AdminColumnSettings-sz4q6vza.js";
5
+ export {
6
+ AdminColumnSettings2 as AdminColumnSettings,
7
+ useAdminColumnLayout2 as useAdminColumnLayout
8
+ };
@@ -0,0 +1,6 @@
1
+ import {
2
+ AdminConfirmAction2
3
+ } from "../AdminColumnSettings-aqqcc4wq.js";
4
+ export {
5
+ AdminConfirmAction2 as AdminConfirmAction
6
+ };
@@ -0,0 +1,7 @@
1
+ import {
2
+ AdminDetailPage2
3
+ } from "../AdminColumnSettings-t2b7bfqc.js";
4
+ import"../AdminColumnSettings-be5c6tmr.js";
5
+ export {
6
+ AdminDetailPage2 as AdminDetailPage
7
+ };
@@ -0,0 +1,6 @@
1
+ import {
2
+ AdminFormDialog2
3
+ } from "../AdminColumnSettings-nnrtybx5.js";
4
+ export {
5
+ AdminFormDialog2 as AdminFormDialog
6
+ };
@@ -0,0 +1,7 @@
1
+ import {
2
+ AdminFormPage2
3
+ } from "../AdminColumnSettings-4cv6a9m2.js";
4
+ import"../AdminColumnSettings-be5c6tmr.js";
5
+ export {
6
+ AdminFormPage2 as AdminFormPage
7
+ };
@@ -0,0 +1,7 @@
1
+ import {
2
+ AdminListPage2
3
+ } from "../AdminColumnSettings-09epsf86.js";
4
+ import"../AdminColumnSettings-be5c6tmr.js";
5
+ export {
6
+ AdminListPage2 as AdminListPage
7
+ };
@@ -0,0 +1,6 @@
1
+ import {
2
+ AdminPage2
3
+ } from "../AdminColumnSettings-be5c6tmr.js";
4
+ export {
5
+ AdminPage2 as AdminPage
6
+ };
@@ -0,0 +1,8 @@
1
+ import {
2
+ AdminSearchField2,
3
+ AdminSearchForm2
4
+ } from "../AdminColumnSettings-tf48jqrt.js";
5
+ export {
6
+ AdminSearchField2 as AdminSearchField,
7
+ AdminSearchForm2 as AdminSearchForm
8
+ };
@@ -0,0 +1,6 @@
1
+ import {
2
+ AdminTabContextMenu2
3
+ } from "../AdminColumnSettings-93ajawas.js";
4
+ export {
5
+ AdminTabContextMenu2 as AdminTabContextMenu
6
+ };
package/package.json CHANGED
@@ -1,6 +1,48 @@
1
1
  {
2
2
  "name": "@ug666/admin-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-beta.2",
4
+ "description": "UG Admin React 渲染层:AdminPage/AdminListPage/AdminFormPage/AdminDetailPage 等页面壳与组合模式",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./src/index.ts",
9
+ "import": "./dist/index.js"
10
+ },
11
+ "./patterns/*": {
12
+ "types": "./src/patterns/*.tsx",
13
+ "default": "./dist/patterns/*.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "src"
19
+ ],
20
+ "dependencies": {},
21
+ "devDependencies": {
22
+ "@types/react": "^19.0.0",
23
+ "@types/react-dom": "^19.0.0",
24
+ "@ug666/admin-core": "file:../admin-core",
25
+ "@ug666/ui-react": "0.3.0-beta.3",
26
+ "lucide-react": "^1.61.0",
27
+ "react": "^19.0.0",
28
+ "react-router-dom": "^7.18.2"
29
+ },
30
+ "license": "MIT",
31
+ "main": "./dist/index.js",
32
+ "types": "./src/index.ts",
33
+ "scripts": {
34
+ "build": "bun run scripts/packages/build-packages.ts"
35
+ },
36
+ "peerDependencies": {
37
+ "@ug666/admin-core": "^0.1.0-0",
38
+ "@ug666/ui-react": "^0.3.0-0",
39
+ "lucide-react": "*",
40
+ "react": "^19.0.0",
41
+ "react-router-dom": "^7.0.0"
42
+ },
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/uguang2003/ug-admin.git",
46
+ "directory": "packages/admin-react"
47
+ }
48
+ }
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ /** @description: admin-react 公开入口;按子路径导出,构建产物与类型统一从这里收口。 */
2
+
3
+ export * from "./patterns/AdminColumnSettings";
4
+ export * from "./patterns/AdminConfirmAction";
5
+ export * from "./patterns/AdminDetailPage";
6
+ export * from "./patterns/AdminFormDialog";
7
+ export * from "./patterns/AdminFormPage";
8
+ export * from "./patterns/AdminListPage";
9
+ export * from "./patterns/AdminPage";
10
+ export * from "./patterns/AdminSearchForm";
11
+ export * from "./patterns/AdminTabContextMenu";
@@ -0,0 +1,168 @@
1
+ import {
2
+ type AdminColumnLayout,
3
+ adminColumnPinLabel,
4
+ cycleAdminColumnSide,
5
+ moveAdminColumn,
6
+ orderAdminColumns,
7
+ readAdminColumnLayout,
8
+ writeAdminColumnLayout,
9
+ } from "@ug666/admin-core/page/column-layout";
10
+ import {
11
+ UGButton,
12
+ UGCheckbox,
13
+ UGPopover,
14
+ UGPopoverContent,
15
+ UGPopoverTrigger,
16
+ } from "@ug666/ui-react";
17
+ import { GripVertical, Pin, Settings2 } from "lucide-react";
18
+ import { type ReactNode, useRef, useState } from "react";
19
+
20
+ export interface AdminColumnSettingsProps {
21
+ /** 可配置的列,按当前顺序展示 */
22
+ columns: readonly { id: string; header?: ReactNode; required?: boolean }[];
23
+ ariaLabel?: string;
24
+ triggerLabel?: string;
25
+ /** 被隐藏的列 id */
26
+ hidden: readonly string[];
27
+ /** 隐藏列变化;至少保留一列 */
28
+ onChange: (hidden: string[]) => void;
29
+ layout?: AdminColumnLayout;
30
+ onLayoutChange?: (layout: AdminColumnLayout) => void;
31
+ }
32
+
33
+ export function useAdminColumnLayout(scope: string) {
34
+ const [layout, setLayout] = useState(() => readAdminColumnLayout(scope, window.localStorage));
35
+ function changeLayout(next: AdminColumnLayout) {
36
+ setLayout(next);
37
+ writeAdminColumnLayout(scope, next, window.localStorage);
38
+ }
39
+ return [layout, changeLayout] as const;
40
+ }
41
+
42
+ /** 列表页的列设置弹层:勾选可见列,并按页面配置调整顺序与固定方向。 */
43
+ export function AdminColumnSettings({
44
+ columns,
45
+ hidden,
46
+ onChange,
47
+ ariaLabel = "列设置",
48
+ triggerLabel,
49
+ layout,
50
+ onLayoutChange,
51
+ }: AdminColumnSettingsProps) {
52
+ const dragged = useRef<string | null>(null);
53
+ const orderedColumns = orderAdminColumns(columns, layout?.order ?? []);
54
+ const order = orderedColumns.map((column) => column.id);
55
+ const visibleCount = columns.filter(
56
+ (column) => column.required || !hidden.includes(column.id),
57
+ ).length;
58
+
59
+ function toggle(id: string, visible: boolean) {
60
+ if (!visible && visibleCount <= 1) return;
61
+ onChange(visible ? hidden.filter((item) => item !== id) : [...new Set([...hidden, id])]);
62
+ }
63
+
64
+ function move(id: string, target: string) {
65
+ if (layout && id !== target)
66
+ onLayoutChange?.({ ...layout, order: moveAdminColumn(order, id, target) });
67
+ }
68
+
69
+ return (
70
+ <UGPopover>
71
+ <UGPopoverTrigger className="inline-flex">
72
+ <UGButton
73
+ aria-label={ariaLabel}
74
+ size={triggerLabel ? "default" : "icon"}
75
+ title={triggerLabel ? undefined : ariaLabel}
76
+ type="button"
77
+ variant="outline"
78
+ >
79
+ <Settings2 aria-hidden="true" size={16} />
80
+ {triggerLabel}
81
+ </UGButton>
82
+ </UGPopoverTrigger>
83
+ <UGPopoverContent align="end" className="admin-column-settings" side="bottom">
84
+ <div className="admin-column-settings__head">
85
+ <strong>显示列</strong>
86
+ <UGButton onClick={() => onChange([])} size="sm" type="button" variant="text">
87
+ 全部显示
88
+ </UGButton>
89
+ </div>
90
+ <div className="admin-column-settings__list">
91
+ {orderedColumns.map((column, index) => {
92
+ const visible = Boolean(column.required) || !hidden.includes(column.id);
93
+ const title = typeof column.header === "string" ? column.header : column.id;
94
+ const side = layout?.fixed[column.id];
95
+ const pinLabel = adminColumnPinLabel(side, title);
96
+ return (
97
+ // biome-ignore lint/a11y/noStaticElementInteractions: 整行接收拖放,键盘通过同一行的排序按钮操作。
98
+ <div
99
+ className="admin-column-settings__row"
100
+ key={column.id}
101
+ onDragOver={(event) => {
102
+ if (dragged.current) event.preventDefault();
103
+ }}
104
+ onDrop={(event) => {
105
+ if (!dragged.current) return;
106
+ event.preventDefault();
107
+ move(dragged.current, column.id);
108
+ dragged.current = null;
109
+ }}
110
+ >
111
+ {layout && onLayoutChange ? (
112
+ <UGButton
113
+ aria-label={`调整${title}的列顺序`}
114
+ aria-keyshortcuts="ArrowUp ArrowDown"
115
+ className="admin-column-settings__grip"
116
+ draggable
117
+ onDragStart={(event) => {
118
+ dragged.current = column.id;
119
+ event.dataTransfer.effectAllowed = "move";
120
+ event.dataTransfer.setData("text/plain", column.id);
121
+ }}
122
+ onDragEnd={() => {
123
+ dragged.current = null;
124
+ }}
125
+ onKeyDown={(event) => {
126
+ if (event.key !== "ArrowUp" && event.key !== "ArrowDown") return;
127
+ event.preventDefault();
128
+ const target = order[index + (event.key === "ArrowUp" ? -1 : 1)];
129
+ if (target) move(column.id, target);
130
+ }}
131
+ size="icon-sm"
132
+ title="拖动或按方向键调整列顺序"
133
+ type="button"
134
+ variant="ghost"
135
+ >
136
+ <GripVertical aria-hidden="true" size={14} />
137
+ </UGButton>
138
+ ) : null}
139
+ <UGCheckbox
140
+ checked={visible}
141
+ disabled={column.required || (visible && visibleCount <= 1)}
142
+ label={title}
143
+ onCheckedChange={(checked) => toggle(column.id, checked)}
144
+ />
145
+ {layout && onLayoutChange ? (
146
+ <UGButton
147
+ aria-label={pinLabel}
148
+ className="admin-column-settings__pin"
149
+ disabled={!visible}
150
+ onClick={() => onLayoutChange(cycleAdminColumnSide(layout, column.id))}
151
+ size="sm"
152
+ title={pinLabel}
153
+ tone={side ? "primary" : "neutral"}
154
+ type="button"
155
+ variant="ghost"
156
+ >
157
+ <Pin aria-hidden="true" size={14} />
158
+ {side ? <span aria-hidden="true">{side === "left" ? "左" : "右"}</span> : null}
159
+ </UGButton>
160
+ ) : null}
161
+ </div>
162
+ );
163
+ })}
164
+ </div>
165
+ </UGPopoverContent>
166
+ </UGPopover>
167
+ );
168
+ }
@@ -0,0 +1,44 @@
1
+ import { UGAlert, UGAlertDescription } from "@ug666/ui-react";
2
+ import { UGConfirmDialog } from "@ug666/ui-react/patterns";
3
+
4
+ export interface AdminConfirmActionProps {
5
+ open: boolean;
6
+ title: string;
7
+ description: string;
8
+ confirmText: string;
9
+ loading?: boolean;
10
+ error?: string;
11
+ onOpenChange: (open: boolean) => void;
12
+ onConfirm: () => void | Promise<void>;
13
+ }
14
+
15
+ export function AdminConfirmAction({
16
+ open,
17
+ title,
18
+ description,
19
+ confirmText,
20
+ loading = false,
21
+ error,
22
+ onOpenChange,
23
+ onConfirm,
24
+ }: AdminConfirmActionProps) {
25
+ return (
26
+ <UGConfirmDialog
27
+ closeOnConfirm={false}
28
+ confirmText={confirmText}
29
+ description={description}
30
+ loading={loading}
31
+ onConfirm={onConfirm}
32
+ onOpenChange={onOpenChange}
33
+ open={open}
34
+ title={title}
35
+ variant="destructive"
36
+ >
37
+ {error ? (
38
+ <UGAlert variant="destructive">
39
+ <UGAlertDescription>{error}</UGAlertDescription>
40
+ </UGAlert>
41
+ ) : null}
42
+ </UGConfirmDialog>
43
+ );
44
+ }
@@ -0,0 +1,44 @@
1
+ import { UGCard, UGCardContent, UGCardHeader, UGCardTitle } from "@ug666/ui-react";
2
+ import type { ReactNode } from "react";
3
+ import { AdminPage, type AdminPageProps } from "./AdminPage";
4
+
5
+ export interface AdminDetailItem {
6
+ label: string;
7
+ value: ReactNode;
8
+ }
9
+
10
+ export interface AdminDetailSection {
11
+ id: string;
12
+ title: string;
13
+ items: readonly AdminDetailItem[];
14
+ }
15
+
16
+ export interface AdminDetailPageProps extends Omit<AdminPageProps, "children"> {
17
+ sections: readonly AdminDetailSection[];
18
+ }
19
+
20
+ export function AdminDetailPage({ sections, ...pageProps }: AdminDetailPageProps) {
21
+ return (
22
+ <AdminPage {...pageProps}>
23
+ <div className="admin-detail-sections">
24
+ {sections.map((section) => (
25
+ <UGCard key={section.id}>
26
+ <UGCardHeader>
27
+ <UGCardTitle>{section.title}</UGCardTitle>
28
+ </UGCardHeader>
29
+ <UGCardContent className="admin-card-content">
30
+ <dl className="admin-description-list">
31
+ {section.items.map((item) => (
32
+ <div className="admin-description-list__item" key={item.label}>
33
+ <dt>{item.label}</dt>
34
+ <dd>{item.value ?? "—"}</dd>
35
+ </div>
36
+ ))}
37
+ </dl>
38
+ </UGCardContent>
39
+ </UGCard>
40
+ ))}
41
+ </div>
42
+ </AdminPage>
43
+ );
44
+ }
@@ -0,0 +1,96 @@
1
+ import {
2
+ UGButton,
3
+ UGModal,
4
+ UGModalBody,
5
+ UGModalCloseButton,
6
+ UGModalContent,
7
+ UGModalFooter,
8
+ UGModalHeader,
9
+ UGModalTitle,
10
+ } from "@ug666/ui-react";
11
+ import { type FormEvent, type ReactNode, useId } from "react";
12
+
13
+ export interface AdminFormDialogProps {
14
+ open: boolean;
15
+ title: string;
16
+ /** 提交按钮文案 */
17
+ submitLabel: string;
18
+ /** 取消按钮文案 */
19
+ cancelLabel?: string;
20
+ loading?: boolean;
21
+ disabled?: boolean;
22
+ closeOnOverlay?: boolean;
23
+ initialFocusSelector?: string;
24
+ size?: "sm" | "md" | "lg";
25
+ /** 关闭请求;提交中应自行忽略 */
26
+ onClose: () => void;
27
+ onSubmit: () => void;
28
+ children: ReactNode;
29
+ }
30
+
31
+ /**
32
+ * 列表页的“新增/编辑”弹窗:表单区单列限宽,提交与取消在页脚同一行,
33
+ * 不再把表单摊在列表卡片顶部。
34
+ */
35
+ export function AdminFormDialog({
36
+ open,
37
+ title,
38
+ submitLabel,
39
+ cancelLabel = "取消",
40
+ loading = false,
41
+ disabled = false,
42
+ closeOnOverlay = true,
43
+ initialFocusSelector,
44
+ size,
45
+ onClose,
46
+ onSubmit,
47
+ children,
48
+ }: AdminFormDialogProps) {
49
+ const formId = useId();
50
+
51
+ function handleSubmit(event: FormEvent<HTMLFormElement>) {
52
+ event.preventDefault();
53
+ if (!loading && !disabled) onSubmit();
54
+ }
55
+
56
+ return (
57
+ <UGModal
58
+ ariaLabel={title}
59
+ closeOnEsc={!loading}
60
+ closeOnOverlay={closeOnOverlay && !loading}
61
+ initialFocusSelector={initialFocusSelector}
62
+ onClose={() => {
63
+ if (!loading) onClose();
64
+ }}
65
+ open={open}
66
+ size={size}
67
+ >
68
+ <UGModalContent>
69
+ <UGModalHeader>
70
+ <UGModalTitle>{title}</UGModalTitle>
71
+ {!loading ? <UGModalCloseButton /> : null}
72
+ </UGModalHeader>
73
+ <UGModalBody>
74
+ <form className="admin-section-form" id={formId} onSubmit={handleSubmit}>
75
+ {children}
76
+ </form>
77
+ </UGModalBody>
78
+ <UGModalFooter>
79
+ <UGButton disabled={loading} onClick={onClose} type="button" variant="text">
80
+ {cancelLabel}
81
+ </UGButton>
82
+ <UGButton
83
+ disabled={disabled}
84
+ form={formId}
85
+ loading={loading}
86
+ tone="primary"
87
+ type="submit"
88
+ variant="solid"
89
+ >
90
+ {submitLabel}
91
+ </UGButton>
92
+ </UGModalFooter>
93
+ </UGModalContent>
94
+ </UGModal>
95
+ );
96
+ }