crud-page-react 0.2.2 → 0.3.0
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.esm.js +114 -26
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +114 -26
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -88,8 +88,10 @@ function shouldShowAction(condition, record) {
|
|
|
88
88
|
for (const [fieldPath, allowedValues] of Object.entries(condition)) {
|
|
89
89
|
// 支持点分路径获取嵌套值
|
|
90
90
|
const actualValue = getNestedValue(record, fieldPath);
|
|
91
|
+
// 确保 allowedValues 是数组
|
|
92
|
+
const valuesArray = Array.isArray(allowedValues) ? allowedValues : [allowedValues];
|
|
91
93
|
// 检查值是否在允许的值列表中
|
|
92
|
-
if (!
|
|
94
|
+
if (!valuesArray.includes(actualValue)) {
|
|
93
95
|
return false;
|
|
94
96
|
}
|
|
95
97
|
}
|
|
@@ -869,7 +871,22 @@ function DynamicForm({ schema, mode, visible, initialValues, onSubmit, onCancel,
|
|
|
869
871
|
}
|
|
870
872
|
|
|
871
873
|
const { Title } = Typography;
|
|
872
|
-
/**
|
|
874
|
+
/** 动态替换 URL 模板中的占位符,支持 :fieldName 和 {{fieldName}} 两种格式 */
|
|
875
|
+
function buildUrl(template, record) {
|
|
876
|
+
let result = template;
|
|
877
|
+
// 替换 :fieldName 格式
|
|
878
|
+
result = result.replace(/:(\w+)/g, (match, fieldName) => {
|
|
879
|
+
const value = record[fieldName];
|
|
880
|
+
return value !== undefined ? String(value) : match;
|
|
881
|
+
});
|
|
882
|
+
// 替换 {{fieldName}} 格式
|
|
883
|
+
result = result.replace(/\{\{(\w+)\}\}/g, (match, fieldName) => {
|
|
884
|
+
const value = record[fieldName];
|
|
885
|
+
return value !== undefined ? String(value) : match;
|
|
886
|
+
});
|
|
887
|
+
return result;
|
|
888
|
+
}
|
|
889
|
+
/** 处理模板数据,支持 {{fieldName}} 格式的变量替换,支持嵌套对象和数组 */
|
|
873
890
|
function processTemplateData(data, record) {
|
|
874
891
|
const result = {};
|
|
875
892
|
for (const [key, value] of Object.entries(data)) {
|
|
@@ -880,6 +897,27 @@ function processTemplateData(data, record) {
|
|
|
880
897
|
return fieldValue !== undefined ? String(fieldValue) : match;
|
|
881
898
|
});
|
|
882
899
|
}
|
|
900
|
+
else if (typeof value === 'object' && value !== null) {
|
|
901
|
+
if (Array.isArray(value)) {
|
|
902
|
+
// 处理数组
|
|
903
|
+
result[key] = value.map(item => {
|
|
904
|
+
if (typeof item === 'object' && item !== null) {
|
|
905
|
+
return processTemplateData(item, record);
|
|
906
|
+
}
|
|
907
|
+
else if (typeof item === 'string' && item.includes('{{') && item.includes('}}')) {
|
|
908
|
+
return item.replace(/\{\{(\w+)\}\}/g, (match, fieldName) => {
|
|
909
|
+
const fieldValue = record[fieldName];
|
|
910
|
+
return fieldValue !== undefined ? String(fieldValue) : match;
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
return item;
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
else {
|
|
917
|
+
// 处理嵌套对象
|
|
918
|
+
result[key] = processTemplateData(value, record);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
883
921
|
else {
|
|
884
922
|
result[key] = value;
|
|
885
923
|
}
|
|
@@ -953,17 +991,32 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
|
|
|
953
991
|
method: listApiConfig.method || 'GET',
|
|
954
992
|
headers: Object.assign({ 'Content-Type': 'application/json' }, listApiConfig.headers)
|
|
955
993
|
};
|
|
956
|
-
//
|
|
957
|
-
let url = listApiConfig.url;
|
|
994
|
+
// 构建 URL,支持动态占位符替换
|
|
995
|
+
let url = buildUrl(listApiConfig.url, params);
|
|
958
996
|
if (listApiConfig.method === 'POST') {
|
|
959
997
|
const queryParams = {};
|
|
960
998
|
query.forEach((value, key) => {
|
|
961
999
|
queryParams[key] = value;
|
|
962
1000
|
});
|
|
963
|
-
|
|
1001
|
+
// 处理模板数据(如果有的话)
|
|
1002
|
+
let processedApiData = {};
|
|
1003
|
+
if (listApiConfig.data) {
|
|
1004
|
+
// 对于list API,通常没有特定的record,使用查询参数作为上下文
|
|
1005
|
+
processedApiData = processTemplateData(listApiConfig.data, queryParams);
|
|
1006
|
+
}
|
|
1007
|
+
const requestData = Object.assign(Object.assign({}, queryParams), processedApiData);
|
|
964
1008
|
options.body = JSON.stringify(requestData);
|
|
965
1009
|
}
|
|
966
1010
|
else {
|
|
1011
|
+
// 对于GET请求,如果有data配置,将其作为查询参数处理
|
|
1012
|
+
if (listApiConfig.data) {
|
|
1013
|
+
const processedApiData = processTemplateData(listApiConfig.data, params);
|
|
1014
|
+
Object.entries(processedApiData).forEach(([key, value]) => {
|
|
1015
|
+
if (value !== undefined && value !== null && value !== '') {
|
|
1016
|
+
query.set(key, String(value));
|
|
1017
|
+
}
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
967
1020
|
url = `${url}?${query}`;
|
|
968
1021
|
}
|
|
969
1022
|
const json = await request(url, options);
|
|
@@ -1011,11 +1064,7 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
|
|
|
1011
1064
|
const deleteApiConfig = schema.api.delete;
|
|
1012
1065
|
try {
|
|
1013
1066
|
// 构建 URL,动态替换占位符
|
|
1014
|
-
let url = deleteApiConfig.url;
|
|
1015
|
-
url = url.replace(/:(\w+)/g, (match, fieldName) => {
|
|
1016
|
-
const value = record[fieldName];
|
|
1017
|
-
return value !== undefined ? String(value) : match;
|
|
1018
|
-
});
|
|
1067
|
+
let url = buildUrl(deleteApiConfig.url, record);
|
|
1019
1068
|
// 构建请求选项
|
|
1020
1069
|
const options = {
|
|
1021
1070
|
method: deleteApiConfig.method || 'DELETE',
|
|
@@ -1037,11 +1086,56 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
|
|
|
1037
1086
|
}, [request, schema.api.delete, fetchList, messageApi, rowKey]);
|
|
1038
1087
|
// ---------- 操作列点击 ----------
|
|
1039
1088
|
const handleAction = useCallback(async (action, record) => {
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1089
|
+
var _a, _b;
|
|
1090
|
+
if (action.type === 'view' || action.type === 'edit') {
|
|
1091
|
+
// 如果配置了detail API,先调用获取详细数据
|
|
1092
|
+
if (schema.api.detail) {
|
|
1093
|
+
try {
|
|
1094
|
+
const detailApiConfig = schema.api.detail;
|
|
1095
|
+
// 构建 URL,动态替换占位符
|
|
1096
|
+
let url = buildUrl(detailApiConfig.url, record);
|
|
1097
|
+
// 构建请求选项
|
|
1098
|
+
const options = {
|
|
1099
|
+
method: detailApiConfig.method || 'GET',
|
|
1100
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, detailApiConfig.headers)
|
|
1101
|
+
};
|
|
1102
|
+
// 处理请求体数据(对于GET请求,通常不需要body,但有些API可能需要)
|
|
1103
|
+
if (detailApiConfig.data && ['POST', 'PUT', 'PATCH'].includes(detailApiConfig.method || 'GET')) {
|
|
1104
|
+
const processedData = processTemplateData(detailApiConfig.data, record);
|
|
1105
|
+
options.body = JSON.stringify(processedData);
|
|
1106
|
+
}
|
|
1107
|
+
else if (detailApiConfig.method === 'GET' && detailApiConfig.data) {
|
|
1108
|
+
// 对于GET请求,将data作为查询参数
|
|
1109
|
+
const processedData = processTemplateData(detailApiConfig.data, record);
|
|
1110
|
+
const query = new URLSearchParams();
|
|
1111
|
+
Object.entries(processedData).forEach(([key, value]) => {
|
|
1112
|
+
if (value !== undefined && value !== null) {
|
|
1113
|
+
query.set(key, String(value));
|
|
1114
|
+
}
|
|
1115
|
+
});
|
|
1116
|
+
url = `${url}?${query}`;
|
|
1117
|
+
}
|
|
1118
|
+
const response = await request(url, options);
|
|
1119
|
+
// 提取详细数据
|
|
1120
|
+
let detailData = record; // 默认使用列表数据
|
|
1121
|
+
if (response && typeof response === 'object') {
|
|
1122
|
+
const responseObj = response;
|
|
1123
|
+
// 尝试从不同的响应结构中提取数据
|
|
1124
|
+
detailData = ((_b = (_a = responseObj.data) !== null && _a !== void 0 ? _a : responseObj.result) !== null && _b !== void 0 ? _b : responseObj);
|
|
1125
|
+
}
|
|
1126
|
+
setModalState({ open: true, mode: action.type, record: detailData });
|
|
1127
|
+
}
|
|
1128
|
+
catch (error) {
|
|
1129
|
+
console.error('Failed to fetch detail:', error);
|
|
1130
|
+
messageApi.error('获取详细数据失败,使用列表数据');
|
|
1131
|
+
// 如果获取详细数据失败,仍然使用列表数据
|
|
1132
|
+
setModalState({ open: true, mode: action.type, record });
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
else {
|
|
1136
|
+
// 没有配置detail API,直接使用列表数据
|
|
1137
|
+
setModalState({ open: true, mode: action.type, record });
|
|
1138
|
+
}
|
|
1045
1139
|
}
|
|
1046
1140
|
else if (action.type === 'delete') {
|
|
1047
1141
|
handleDelete(record);
|
|
@@ -1075,11 +1169,7 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
|
|
|
1075
1169
|
}
|
|
1076
1170
|
try {
|
|
1077
1171
|
// 构建 URL,动态替换占位符
|
|
1078
|
-
let url = apiConfig.url;
|
|
1079
|
-
url = url.replace(/:(\w+)/g, (match, fieldName) => {
|
|
1080
|
-
const value = record[fieldName];
|
|
1081
|
-
return value !== undefined ? String(value) : match;
|
|
1082
|
-
});
|
|
1172
|
+
let url = buildUrl(apiConfig.url, record);
|
|
1083
1173
|
// 构建请求选项
|
|
1084
1174
|
const options = {
|
|
1085
1175
|
method: apiConfig.method || 'GET',
|
|
@@ -1146,7 +1236,9 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
|
|
|
1146
1236
|
requestData = Object.assign(Object.assign(Object.assign({}, requestData), processedData), { timestamp: new Date().toISOString() });
|
|
1147
1237
|
}
|
|
1148
1238
|
options.body = JSON.stringify(requestData);
|
|
1149
|
-
|
|
1239
|
+
// 构建 URL,支持动态占位符替换
|
|
1240
|
+
const url = buildUrl(createApiConfig.url, values);
|
|
1241
|
+
await request(url, options);
|
|
1150
1242
|
messageApi.success('新增成功');
|
|
1151
1243
|
setModalState({ open: false, mode: 'create' });
|
|
1152
1244
|
fetchList();
|
|
@@ -1164,11 +1256,7 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
|
|
|
1164
1256
|
const updateApiConfig = schema.api.update;
|
|
1165
1257
|
try {
|
|
1166
1258
|
// 构建 URL,动态替换占位符
|
|
1167
|
-
let url = updateApiConfig.url;
|
|
1168
|
-
url = url.replace(/:(\w+)/g, (match, fieldName) => {
|
|
1169
|
-
const value = values[fieldName];
|
|
1170
|
-
return value !== undefined ? String(value) : match;
|
|
1171
|
-
});
|
|
1259
|
+
let url = buildUrl(updateApiConfig.url, values);
|
|
1172
1260
|
// 构建请求选项
|
|
1173
1261
|
const options = {
|
|
1174
1262
|
method: updateApiConfig.method || 'PUT',
|