crud-page-react 0.2.2 → 0.3.1

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.js CHANGED
@@ -89,8 +89,10 @@ function shouldShowAction(condition, record) {
89
89
  for (const [fieldPath, allowedValues] of Object.entries(condition)) {
90
90
  // 支持点分路径获取嵌套值
91
91
  const actualValue = getNestedValue(record, fieldPath);
92
+ // 确保 allowedValues 是数组
93
+ const valuesArray = Array.isArray(allowedValues) ? allowedValues : [allowedValues];
92
94
  // 检查值是否在允许的值列表中
93
- if (!allowedValues.includes(actualValue)) {
95
+ if (!valuesArray.includes(actualValue)) {
94
96
  return false;
95
97
  }
96
98
  }
@@ -758,8 +760,7 @@ function DynamicForm({ schema, mode, visible, initialValues, onSubmit, onCancel,
758
760
  view: `查看${entityName}`,
759
761
  };
760
762
  const formFields = schema.fields.filter((f) => {
761
- if (mode === 'view')
762
- return f.table !== false && f.table !== undefined;
763
+ // 查看模式和编辑模式都应该遵循form字段配置
763
764
  return f.form !== false && f.form !== undefined;
764
765
  });
765
766
  react.useEffect(() => {
@@ -870,7 +871,22 @@ function DynamicForm({ schema, mode, visible, initialValues, onSubmit, onCancel,
870
871
  }
871
872
 
872
873
  const { Title } = antd.Typography;
873
- /** 处理模板数据,支持 {{fieldName}} 格式的变量替换 */
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}} 格式的变量替换,支持嵌套对象和数组 */
874
890
  function processTemplateData(data, record) {
875
891
  const result = {};
876
892
  for (const [key, value] of Object.entries(data)) {
@@ -881,6 +897,27 @@ function processTemplateData(data, record) {
881
897
  return fieldValue !== undefined ? String(fieldValue) : match;
882
898
  });
883
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
+ }
884
921
  else {
885
922
  result[key] = value;
886
923
  }
@@ -954,17 +991,32 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
954
991
  method: listApiConfig.method || 'GET',
955
992
  headers: Object.assign({ 'Content-Type': 'application/json' }, listApiConfig.headers)
956
993
  };
957
- // 如果是 POST 请求,将查询参数放到请求体中
958
- let url = listApiConfig.url;
994
+ // 构建 URL,支持动态占位符替换
995
+ let url = buildUrl(listApiConfig.url, params);
959
996
  if (listApiConfig.method === 'POST') {
960
997
  const queryParams = {};
961
998
  query.forEach((value, key) => {
962
999
  queryParams[key] = value;
963
1000
  });
964
- const requestData = Object.assign(Object.assign({}, queryParams), listApiConfig.data);
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);
965
1008
  options.body = JSON.stringify(requestData);
966
1009
  }
967
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
+ }
968
1020
  url = `${url}?${query}`;
969
1021
  }
970
1022
  const json = await request(url, options);
@@ -1012,11 +1064,7 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
1012
1064
  const deleteApiConfig = schema.api.delete;
1013
1065
  try {
1014
1066
  // 构建 URL,动态替换占位符
1015
- let url = deleteApiConfig.url;
1016
- url = url.replace(/:(\w+)/g, (match, fieldName) => {
1017
- const value = record[fieldName];
1018
- return value !== undefined ? String(value) : match;
1019
- });
1067
+ let url = buildUrl(deleteApiConfig.url, record);
1020
1068
  // 构建请求选项
1021
1069
  const options = {
1022
1070
  method: deleteApiConfig.method || 'DELETE',
@@ -1038,11 +1086,56 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
1038
1086
  }, [request, schema.api.delete, fetchList, messageApi, rowKey]);
1039
1087
  // ---------- 操作列点击 ----------
1040
1088
  const handleAction = react.useCallback(async (action, record) => {
1041
- if (action.type === 'view') {
1042
- setModalState({ open: true, mode: 'view', record });
1043
- }
1044
- else if (action.type === 'edit') {
1045
- setModalState({ open: true, mode: 'edit', record });
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
+ }
1046
1139
  }
1047
1140
  else if (action.type === 'delete') {
1048
1141
  handleDelete(record);
@@ -1076,11 +1169,7 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
1076
1169
  }
1077
1170
  try {
1078
1171
  // 构建 URL,动态替换占位符
1079
- let url = apiConfig.url;
1080
- url = url.replace(/:(\w+)/g, (match, fieldName) => {
1081
- const value = record[fieldName];
1082
- return value !== undefined ? String(value) : match;
1083
- });
1172
+ let url = buildUrl(apiConfig.url, record);
1084
1173
  // 构建请求选项
1085
1174
  const options = {
1086
1175
  method: apiConfig.method || 'GET',
@@ -1147,7 +1236,9 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
1147
1236
  requestData = Object.assign(Object.assign(Object.assign({}, requestData), processedData), { timestamp: new Date().toISOString() });
1148
1237
  }
1149
1238
  options.body = JSON.stringify(requestData);
1150
- await request(createApiConfig.url, options);
1239
+ // 构建 URL,支持动态占位符替换
1240
+ const url = buildUrl(createApiConfig.url, values);
1241
+ await request(url, options);
1151
1242
  messageApi.success('新增成功');
1152
1243
  setModalState({ open: false, mode: 'create' });
1153
1244
  fetchList();
@@ -1165,11 +1256,7 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, loca
1165
1256
  const updateApiConfig = schema.api.update;
1166
1257
  try {
1167
1258
  // 构建 URL,动态替换占位符
1168
- let url = updateApiConfig.url;
1169
- url = url.replace(/:(\w+)/g, (match, fieldName) => {
1170
- const value = values[fieldName];
1171
- return value !== undefined ? String(value) : match;
1172
- });
1259
+ let url = buildUrl(updateApiConfig.url, values);
1173
1260
  // 构建请求选项
1174
1261
  const options = {
1175
1262
  method: updateApiConfig.method || 'PUT',