crud-page-react 0.1.5 → 0.2.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
@@ -4,8 +4,12 @@ var jsxRuntime = require('react/jsx-runtime');
4
4
  var react = require('react');
5
5
  var antd = require('antd');
6
6
  var icons = require('@ant-design/icons');
7
+ var zhCN = require('antd/locale/zh_CN');
7
8
  var dayjs = require('dayjs');
8
9
  var customParseFormat = require('dayjs/plugin/customParseFormat');
10
+ var en_US = require('antd/locale/en_US');
11
+ var ja_JP = require('antd/locale/ja_JP');
12
+ var ko_KR = require('antd/locale/ko_KR');
9
13
 
10
14
  /* ─── 工具函数 ──────────────────────────────────────── */
11
15
  /**
@@ -63,6 +67,35 @@ function getFieldGroupPrefix(key) {
63
67
  const idx = key.indexOf('.');
64
68
  return idx >= 0 ? key.slice(0, idx) : null;
65
69
  }
70
+ /**
71
+ * 检查操作按钮是否应该显示
72
+ *
73
+ * @param condition - 条件配置
74
+ * @param record - 当前行数据
75
+ * @returns 是否应该显示该操作
76
+ *
77
+ * @example
78
+ * // 字段值匹配
79
+ * shouldShowAction({ status: ['active', 'pending'] }, { status: 'active' }) → true
80
+ * shouldShowAction({ status: ['active', 'pending'] }, { status: 'deleted' }) → false
81
+ *
82
+ * // 支持嵌套字段(点分路径)
83
+ * shouldShowAction({ 'user.role': ['admin'] }, { user: { role: 'admin' } }) → true
84
+ */
85
+ function shouldShowAction(condition, record) {
86
+ if (!condition || Object.keys(condition).length === 0) {
87
+ return true; // 没有条件时默认显示
88
+ }
89
+ for (const [fieldPath, allowedValues] of Object.entries(condition)) {
90
+ // 支持点分路径获取嵌套值
91
+ const actualValue = getNestedValue(record, fieldPath);
92
+ // 检查值是否在允许的值列表中
93
+ if (!allowedValues.includes(actualValue)) {
94
+ return false;
95
+ }
96
+ }
97
+ return true;
98
+ }
66
99
 
67
100
  const { RangePicker } = antd.DatePicker;
68
101
  /** 解析 FilterConfig */
@@ -250,9 +283,21 @@ function DynamicTable({ schema, data, loading, pagination, onView, onEdit, onDel
250
283
  render: (_, record) => {
251
284
  var _a;
252
285
  const actions = (_a = schema.actions) !== null && _a !== void 0 ? _a : [];
253
- // 分离基础操作和自定义操作
254
- const basicActions = actions.filter(action => action.type === 'view' || action.type === 'edit' || action.type === 'delete');
255
- const customActions = actions.filter(action => action.type === 'custom');
286
+ // 分离基础操作和自定义操作,并根据 condition 过滤
287
+ const basicActions = actions.filter(action => {
288
+ // 检查是否是基础操作类型
289
+ const isBasicAction = action.type === 'view' || action.type === 'edit' || action.type === 'delete';
290
+ if (!isBasicAction)
291
+ return false;
292
+ // 检查 condition 条件
293
+ return shouldShowAction(action.condition, record);
294
+ });
295
+ const customActions = actions.filter(action => {
296
+ if (action.type !== 'custom')
297
+ return false;
298
+ // 检查 condition 条件
299
+ return shouldShowAction(action.condition, record);
300
+ });
256
301
  // 构建下拉菜单项(仅包含自定义操作)
257
302
  const dropdownItems = customActions.map(action => ({
258
303
  key: action.key,
@@ -864,7 +909,7 @@ function extractListResponse(json) {
864
909
  }
865
910
  return { list: [], total: 0 };
866
911
  }
867
- const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) => {
912
+ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest, locale = zhCN }) => {
868
913
  var _a;
869
914
  const rowKey = schema.rowKey || 'id';
870
915
  // 使用传入的apiRequest或默认的
@@ -1148,19 +1193,23 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
1148
1193
  }, [
1149
1194
  request, modalState.mode, schema.api, fetchList, messageApi,
1150
1195
  ]);
1151
- return (jsxRuntime.jsxs("div", { style: { padding: 24, background: '#f5f6fa', minHeight: '100vh' }, children: [contextHolder, jsxRuntime.jsxs("div", { style: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 16 }, children: [jsxRuntime.jsx(Title, { level: 4, style: { margin: 0 }, children: schema.title }), schema.api.create && (jsxRuntime.jsx(antd.Button, { type: "primary", icon: jsxRuntime.jsx(icons.PlusOutlined, {}), onClick: () => setModalState({ open: true, mode: 'create', record: undefined }), children: schema.createButtonLabel || '新增' }))] }), jsxRuntime.jsx(DynamicFilter, { schema: schema, onSearch: handleSearch, onReset: () => handleSearch({}) }), jsxRuntime.jsx(DynamicTable, { schema: schema, data: data, loading: loading, pagination: {
1152
- current: page,
1153
- pageSize,
1154
- total,
1155
- onChange: handlePageChange,
1156
- }, onView: (record) => handleAction({ key: 'view', label: '查看', type: 'view' }, record), onEdit: (record) => handleAction({ key: 'edit', label: '编辑', type: 'edit' }, record), onDelete: (record) => handleDelete(record), onCustomAction: (actionKey, record) => {
1157
- var _a;
1158
- const action = (_a = schema.actions) === null || _a === void 0 ? void 0 : _a.find(a => a.key === actionKey);
1159
- if (action)
1160
- handleAction(action, record);
1161
- } }), jsxRuntime.jsx(DynamicForm, { schema: schema, visible: modalState.open, mode: modalState.mode, initialValues: modalState.record, onSubmit: handleFormOk, onCancel: () => setModalState({ open: false, mode: 'create' }) })] }));
1196
+ return (jsxRuntime.jsx(antd.ConfigProvider, { locale: locale, children: jsxRuntime.jsxs("div", { style: { padding: 24, background: '#f5f6fa', minHeight: '100vh' }, children: [contextHolder, jsxRuntime.jsxs("div", { style: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 16 }, children: [jsxRuntime.jsx(Title, { level: 4, style: { margin: 0 }, children: schema.title }), schema.api.create && (jsxRuntime.jsx(antd.Button, { type: "primary", icon: jsxRuntime.jsx(icons.PlusOutlined, {}), onClick: () => setModalState({ open: true, mode: 'create', record: undefined }), children: schema.createButtonLabel || '新增' }))] }), jsxRuntime.jsx(DynamicFilter, { schema: schema, onSearch: handleSearch, onReset: () => handleSearch({}) }), jsxRuntime.jsx(DynamicTable, { schema: schema, data: data, loading: loading, pagination: {
1197
+ current: page,
1198
+ pageSize,
1199
+ total,
1200
+ onChange: handlePageChange,
1201
+ }, onView: (record) => handleAction({ key: 'view', label: '查看', type: 'view' }, record), onEdit: (record) => handleAction({ key: 'edit', label: '编辑', type: 'edit' }, record), onDelete: (record) => handleDelete(record), onCustomAction: (actionKey, record) => {
1202
+ var _a;
1203
+ const action = (_a = schema.actions) === null || _a === void 0 ? void 0 : _a.find(a => a.key === actionKey);
1204
+ if (action)
1205
+ handleAction(action, record);
1206
+ } }), jsxRuntime.jsx(DynamicForm, { schema: schema, visible: modalState.open, mode: modalState.mode, initialValues: modalState.record, onSubmit: handleFormOk, onCancel: () => setModalState({ open: false, mode: 'create' }) })] }) }));
1162
1207
  };
1163
1208
 
1209
+ exports.zhCN = zhCN;
1210
+ exports.enUS = en_US;
1211
+ exports.jaJP = ja_JP;
1212
+ exports.koKR = ko_KR;
1164
1213
  exports.BUILTIN_RULES = BUILTIN_RULES;
1165
1214
  exports.CrudPage = CrudPage;
1166
1215
  exports.DynamicFilter = DynamicFilter;