crud-page-react 0.2.0 → 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.d.ts CHANGED
@@ -150,7 +150,12 @@ interface ActionSchema {
150
150
  danger?: boolean;
151
151
  color?: string;
152
152
  permission?: ActionPermission;
153
- condition?: Record<string, unknown>;
153
+ /**
154
+ * 操作按钮显示条件
155
+ * 支持字段值匹配:{ status: ['active', 'pending'] }
156
+ * 支持嵌套字段:{ 'user.role': ['admin'] }
157
+ */
158
+ condition?: ActionCondition;
154
159
  confirm?: {
155
160
  title: string;
156
161
  content?: string;
@@ -209,6 +214,15 @@ declare function getNestedValue(obj: Record<string, unknown>, path: string): unk
209
214
  * 'name' → null
210
215
  */
211
216
  declare function getFieldGroupPrefix(key: string): string | null;
217
+ /**
218
+ * 操作按钮显示条件配置
219
+ * 支持两种格式:
220
+ * 1. 字段值匹配:{ fieldName: [value1, value2] } - 当字段值在数组中时显示
221
+ * 2. 自定义表达式:{ expression: "record.status === 'active'" } - 使用表达式判断
222
+ */
223
+ interface ActionCondition {
224
+ [fieldName: string]: (string | number | boolean)[];
225
+ }
212
226
 
213
227
  interface CrudPageProps {
214
228
  schema: CrudPageSchema;
package/dist/index.esm.js CHANGED
@@ -66,6 +66,35 @@ function getFieldGroupPrefix(key) {
66
66
  const idx = key.indexOf('.');
67
67
  return idx >= 0 ? key.slice(0, idx) : null;
68
68
  }
69
+ /**
70
+ * 检查操作按钮是否应该显示
71
+ *
72
+ * @param condition - 条件配置
73
+ * @param record - 当前行数据
74
+ * @returns 是否应该显示该操作
75
+ *
76
+ * @example
77
+ * // 字段值匹配
78
+ * shouldShowAction({ status: ['active', 'pending'] }, { status: 'active' }) → true
79
+ * shouldShowAction({ status: ['active', 'pending'] }, { status: 'deleted' }) → false
80
+ *
81
+ * // 支持嵌套字段(点分路径)
82
+ * shouldShowAction({ 'user.role': ['admin'] }, { user: { role: 'admin' } }) → true
83
+ */
84
+ function shouldShowAction(condition, record) {
85
+ if (!condition || Object.keys(condition).length === 0) {
86
+ return true; // 没有条件时默认显示
87
+ }
88
+ for (const [fieldPath, allowedValues] of Object.entries(condition)) {
89
+ // 支持点分路径获取嵌套值
90
+ const actualValue = getNestedValue(record, fieldPath);
91
+ // 检查值是否在允许的值列表中
92
+ if (!allowedValues.includes(actualValue)) {
93
+ return false;
94
+ }
95
+ }
96
+ return true;
97
+ }
69
98
 
70
99
  const { RangePicker } = DatePicker;
71
100
  /** 解析 FilterConfig */
@@ -253,9 +282,21 @@ function DynamicTable({ schema, data, loading, pagination, onView, onEdit, onDel
253
282
  render: (_, record) => {
254
283
  var _a;
255
284
  const actions = (_a = schema.actions) !== null && _a !== void 0 ? _a : [];
256
- // 分离基础操作和自定义操作
257
- const basicActions = actions.filter(action => action.type === 'view' || action.type === 'edit' || action.type === 'delete');
258
- const customActions = actions.filter(action => action.type === 'custom');
285
+ // 分离基础操作和自定义操作,并根据 condition 过滤
286
+ const basicActions = actions.filter(action => {
287
+ // 检查是否是基础操作类型
288
+ const isBasicAction = action.type === 'view' || action.type === 'edit' || action.type === 'delete';
289
+ if (!isBasicAction)
290
+ return false;
291
+ // 检查 condition 条件
292
+ return shouldShowAction(action.condition, record);
293
+ });
294
+ const customActions = actions.filter(action => {
295
+ if (action.type !== 'custom')
296
+ return false;
297
+ // 检查 condition 条件
298
+ return shouldShowAction(action.condition, record);
299
+ });
259
300
  // 构建下拉菜单项(仅包含自定义操作)
260
301
  const dropdownItems = customActions.map(action => ({
261
302
  key: action.key,