RoleFormDrawer.tsx
5.23 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
import { useEffect } from 'react';
import { useMutation } from '@tanstack/react-query';
import { UButton, UDrawer, UForm, useUMessage } from '@uj/ui';
import { role, type RolePageItem } from '@/api';
import { CombiForm, type FieldSchema } from '@/components';
import { dataScopeOptions } from '@/constants/dataScope';
interface RoleFormValues extends Record<string, unknown> {
roleName: string;
roleCode: string;
sort: string;
status: number;
description?: string;
defaultDataScope: number;
}
export interface RoleFormDrawerProps {
open: boolean;
roleItem?: RolePageItem;
onClose: () => void;
onSuccess: () => void;
}
const statusOptions = [
{ label: '启用', value: 1 },
{ label: '停用', value: 0 },
];
export function RoleFormDrawer({ open, roleItem, onClose, onSuccess }: RoleFormDrawerProps) {
const message = useUMessage();
const [form] = UForm.useForm<RoleFormValues>();
const isEdit = Boolean(roleItem);
const saveMutation = useMutation({
mutationFn: (values: RoleFormValues) => {
const payload = {
roleName: values.roleName.trim(),
defaultDataScope: Number(1),
sort: Number(values.sort),
status: Number(values.status),
description: values.description?.trim() || undefined,
};
return roleItem
? role.updateSystem({ ...payload, roleId: roleItem.roleId })
: role.createSystem({ ...payload, roleCode: values.roleCode.trim() });
},
onSuccess: () => {
void message.success(isEdit ? '角色已保存' : '角色已创建');
onSuccess();
},
onError: (error: Error) => void message.error(error.message || '保存失败,请稍后重试'),
});
useEffect(() => {
if (!open) return;
form.setFieldsValue({
roleName: roleItem?.roleName ?? '',
roleCode: roleItem?.roleCode ?? '',
sort: String(roleItem?.sort ?? 10),
status: roleItem?.status ?? 1,
description: roleItem?.description ?? '',
defaultDataScope: Number(roleItem?.defaultDataScope ?? 1),
});
}, [form, open, roleItem]);
const fields: FieldSchema<RoleFormValues>[] = [
{
name: 'roleName',
label: '角色名称',
type: 'input',
section: '基本信息',
inputProps: { maxLength: 20 },
rules: [
{ required: true, message: '请输入角色名称' },
{ max: 20, message: '角色名称不能超过 20 个字符' },
],
},
{
name: 'roleCode',
label: '角色编码',
type: 'input',
section: '基本信息',
disabled: isEdit,
inputProps: { maxLength: 50 },
placeholder: '例如:REGION_OPS',
rules: [
{ required: true, message: '请输入角色编码' },
{ pattern: /^[A-Z0-9_]+$/, message: '仅支持大写字母、数字和下划线' },
],
},
{
name: 'sort',
label: '显示排序',
type: 'input',
section: '基本信息',
inputProps: { inputMode: 'numeric' },
rules: [{ pattern: /^\d+$/, message: '请输入整数' }],
},
{
name: 'status',
label: '状态',
type: 'select',
section: '基本信息',
options: statusOptions,
selectProps: { allowClear: false },
},
{
name: 'description',
label: '角色描述',
type: 'textarea',
section: '基本信息',
itemStyle: { gridColumn: '1 / -1' },
textareaProps: { maxLength: 200, rows: 2 },
placeholder: '选填,用于说明角色的职责边界',
rules: [{ max: 200, message: '角色描述不能超过 200 个字符' }],
},
{
name: 'defaultDataScope',
label: '默认数据域',
type: 'select',
disabled: true,
section: '数据域',
options: dataScopeOptions,
rules: [{ required: true, message: '请选择默认数据域' }],
},
];
return (
<UDrawer
open={open}
title={isEdit ? '编辑角色' : '新增角色'}
size={560}
onClose={onClose}
footer={
<div style={footerStyle}>
<UButton type="default" onClick={onClose}>取消</UButton>
<UButton type="primary" loading={saveMutation.isPending} onClick={() => form.submit()}>
保存
</UButton>
</div>
}
>
<CombiForm<RoleFormValues>
mode="form"
form={form}
layout="vertical"
initialValues={{ sort: '10', status: 1 }}
fields={fields}
onSubmit={(values) => saveMutation.mutate(values)}
sectionContentStyle={twoColumnStyle}
renderSectionTitle={(title) => <SectionTitle>{title}</SectionTitle>}
/>
</UDrawer>
);
}
const footerStyle = { display: 'flex', justifyContent: 'flex-end', gap: 8 };
const twoColumnStyle = { display: 'grid', gridTemplateColumns: '1fr 1fr', columnGap: 12 };
function SectionTitle({ children }: { children: string }) {
return (
<h3 style={sectionTitleStyle}>
<span style={sectionMarkerStyle} />
{children}
</h3>
);
}
const sectionTitleStyle = {
alignItems: 'center',
borderBottom: '1px solid var(--uj-divider)',
display: 'flex',
fontSize: 14,
gap: 6,
lineHeight: '16px',
margin: '0 0 12px',
padding: '0 0 8px',
};
const sectionMarkerStyle = {
backgroundColor: 'var(--uj-primary)',
borderRadius: 2,
height: 10,
width: 3,
};