crud-page-react 0.0.2 → 0.0.4
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.
- package/dist/index.d.ts +10 -0
- package/dist/index.esm.js +63 -7
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +63 -7
- package/dist/index.js.map +1 -1
- package/dist/types/schema.d.ts +10 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -129,17 +129,27 @@ interface ActionPermission {
|
|
|
129
129
|
role?: string[];
|
|
130
130
|
condition?: string;
|
|
131
131
|
}
|
|
132
|
+
interface ActionApiConfig {
|
|
133
|
+
url: string;
|
|
134
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
135
|
+
data?: Record<string, unknown>;
|
|
136
|
+
headers?: Record<string, string>;
|
|
137
|
+
responseType?: 'json' | 'blob' | 'text';
|
|
138
|
+
}
|
|
132
139
|
interface ActionSchema {
|
|
133
140
|
key: string;
|
|
134
141
|
label: string;
|
|
135
142
|
type: ActionType;
|
|
136
143
|
icon?: string;
|
|
137
144
|
danger?: boolean;
|
|
145
|
+
color?: string;
|
|
138
146
|
permission?: ActionPermission;
|
|
147
|
+
condition?: Record<string, unknown>;
|
|
139
148
|
confirm?: {
|
|
140
149
|
title: string;
|
|
141
150
|
content?: string;
|
|
142
151
|
};
|
|
152
|
+
api?: ActionApiConfig;
|
|
143
153
|
}
|
|
144
154
|
interface PaginationConfig {
|
|
145
155
|
pageSize?: number;
|
package/dist/index.esm.js
CHANGED
|
@@ -810,9 +810,12 @@ function DynamicForm({ schema, mode, visible, initialValues, onSubmit, onCancel,
|
|
|
810
810
|
}
|
|
811
811
|
|
|
812
812
|
const { Title } = Typography;
|
|
813
|
-
/**
|
|
814
|
-
function buildUrl(template,
|
|
815
|
-
return template.replace(/:
|
|
813
|
+
/** 动态替换 URL 模板中的占位符 */
|
|
814
|
+
function buildUrl(template, record) {
|
|
815
|
+
return template.replace(/:(\w+)/g, (match, fieldName) => {
|
|
816
|
+
const value = record[fieldName];
|
|
817
|
+
return value !== undefined ? String(value) : match;
|
|
818
|
+
});
|
|
816
819
|
}
|
|
817
820
|
/** 通用请求封装 */
|
|
818
821
|
async function apiRequest(url, options) {
|
|
@@ -920,7 +923,7 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
|
|
|
920
923
|
const id = record[rowKey];
|
|
921
924
|
if (schema.api.delete) {
|
|
922
925
|
try {
|
|
923
|
-
await request(buildUrl(schema.api.delete,
|
|
926
|
+
await request(buildUrl(schema.api.delete, record), { method: 'DELETE' });
|
|
924
927
|
messageApi.success('删除成功');
|
|
925
928
|
fetchList();
|
|
926
929
|
return;
|
|
@@ -934,7 +937,7 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
|
|
|
934
937
|
messageApi.success('删除成功(演示模式)');
|
|
935
938
|
}, [request, schema.api.delete, rowKey, fetchList, localFilter, filterParams, page, pageSize, messageApi]);
|
|
936
939
|
// ---------- 操作列点击 ----------
|
|
937
|
-
const handleAction = useCallback((action, record) => {
|
|
940
|
+
const handleAction = useCallback(async (action, record) => {
|
|
938
941
|
if (action.type === 'view') {
|
|
939
942
|
setModalState({ open: true, mode: 'view', record });
|
|
940
943
|
}
|
|
@@ -944,7 +947,60 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
|
|
|
944
947
|
else if (action.type === 'delete') {
|
|
945
948
|
handleDelete(record);
|
|
946
949
|
}
|
|
947
|
-
|
|
950
|
+
else if (action.type === 'custom' && action.api) {
|
|
951
|
+
// 处理自定义 action 的 API 调用
|
|
952
|
+
try {
|
|
953
|
+
// 构建 URL,动态替换占位符
|
|
954
|
+
let url = action.api.url;
|
|
955
|
+
// 替换所有 :fieldName 格式的占位符
|
|
956
|
+
url = url.replace(/:(\w+)/g, (match, fieldName) => {
|
|
957
|
+
const value = record[fieldName];
|
|
958
|
+
return value !== undefined ? String(value) : match;
|
|
959
|
+
});
|
|
960
|
+
// 构建请求选项
|
|
961
|
+
const options = {
|
|
962
|
+
method: action.api.method,
|
|
963
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, action.api.headers)
|
|
964
|
+
};
|
|
965
|
+
// 添加请求体数据
|
|
966
|
+
if (action.api.data && ['POST', 'PUT', 'PATCH'].includes(action.api.method)) {
|
|
967
|
+
options.body = JSON.stringify(Object.assign(Object.assign({}, action.api.data), {
|
|
968
|
+
// 可以添加动态数据
|
|
969
|
+
recordId: record[rowKey], timestamp: new Date().toISOString() }));
|
|
970
|
+
}
|
|
971
|
+
// 调用 API
|
|
972
|
+
const response = await request(url, options);
|
|
973
|
+
// 处理特殊响应类型
|
|
974
|
+
if (action.api.responseType === 'blob') {
|
|
975
|
+
// 处理文件下载
|
|
976
|
+
const blob = new Blob([response]);
|
|
977
|
+
const downloadUrl = URL.createObjectURL(blob);
|
|
978
|
+
const a = document.createElement('a');
|
|
979
|
+
a.href = downloadUrl;
|
|
980
|
+
a.download = `${action.key}.pdf`;
|
|
981
|
+
a.click();
|
|
982
|
+
URL.revokeObjectURL(downloadUrl);
|
|
983
|
+
messageApi.success(`${action.label}成功`);
|
|
984
|
+
}
|
|
985
|
+
else {
|
|
986
|
+
// 处理 JSON 响应
|
|
987
|
+
const result = response;
|
|
988
|
+
if (result.success !== false) {
|
|
989
|
+
messageApi.success(`${action.label}成功`);
|
|
990
|
+
// 刷新数据
|
|
991
|
+
await fetchList();
|
|
992
|
+
}
|
|
993
|
+
else {
|
|
994
|
+
messageApi.error(result.message || `${action.label}失败`);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
catch (error) {
|
|
999
|
+
console.error(`Action ${action.key} failed:`, error);
|
|
1000
|
+
messageApi.error(`${action.label}失败`);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
}, [handleDelete, rowKey, request, messageApi, fetchList]);
|
|
948
1004
|
// ---------- 新增 / 编辑提交 ----------
|
|
949
1005
|
const handleFormOk = useCallback(async (values) => {
|
|
950
1006
|
const isCreate = modalState.mode === 'create';
|
|
@@ -974,7 +1030,7 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
|
|
|
974
1030
|
const id = values[rowKey];
|
|
975
1031
|
if (schema.api.update) {
|
|
976
1032
|
try {
|
|
977
|
-
await request(buildUrl(schema.api.update,
|
|
1033
|
+
await request(buildUrl(schema.api.update, values), {
|
|
978
1034
|
method: 'PUT',
|
|
979
1035
|
body: JSON.stringify(values),
|
|
980
1036
|
});
|