@hzab/list-render 1.9.3-beta1 → 1.9.4-beta

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/list-render",
3
- "version": "1.9.3-beta1",
3
+ "version": "1.9.4-beta",
4
4
  "description": "",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -1,4 +1,4 @@
1
- import ListRender from "./list-render";
1
+ import ListRender from "./list-render.jsx";
2
2
 
3
3
  export * from "@hzab/data-model";
4
4
 
@@ -6,7 +6,6 @@ import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "re
6
6
  import { Button, message } from "antd";
7
7
  import _ from "lodash";
8
8
  import axios, { getCancelTokenSource } from "@hzab/data-model/src/axios";
9
- import { CancelTokenSource } from "axios";
10
9
 
11
10
  import QueryRender from "./query-render";
12
11
  import Pagination from "./pagination-render";
@@ -16,11 +15,9 @@ import DetailModal from "./DetailModal";
16
15
 
17
16
  import { objToFormData } from "./common/utils";
18
17
 
19
- import { IListRenderProps, IFormModal, IDetailModal, IQuery } from "./type";
20
-
21
18
  import "./index.less";
22
19
 
23
- const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
20
+ const ListRender = forwardRef(function (props, parentRef) {
24
21
  const {
25
22
  idKey = "id",
26
23
  i18n,
@@ -38,26 +35,22 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
38
35
  * 表单提交是否使用 FormData 格式
39
36
  */
40
37
  useFormData: _useFormData,
41
- /**
42
- * 是否有分页
43
- */
44
- hasPagination = true,
45
38
  } = props;
46
-
47
39
  const { createText = props.createText } = i18n || {};
48
40
  const [total, setTotal] = useState(0);
49
41
  const [list, setList] = useState([]);
50
42
  const [formState, setFormState] = useState("");
51
43
  const [rowId, setRowId] = useState(0);
52
44
  const [listLoading, setListLoading] = useState(false);
53
- const formModalRef = useRef<IFormModal>();
54
- const detailModalRef = useRef<IDetailModal>();
55
- const queryRef = useRef<IQuery>();
56
- const formQueryRef = useRef<IQuery>({});
57
- const modelQueryRef = useRef<IQuery>({});
58
- const paginationQueryRef = useRef<IQuery>(hasPagination ? { pageNum: 1, pageSize: 10 } : {});
45
+ const formModalRef = useRef();
46
+ const detailModalRef = useRef();
47
+ const queryRef = useRef();
48
+ const modelQueryRef = useRef({});
49
+ const formQueryRef = useRef({});
50
+ const paginationQueryRef = useRef({ pageNum: 1, pageSize: 10 });
59
51
  const useFormData = _useFormData ?? dialogConf.useFormData ?? modalConf?.useFormData;
60
- const getListSourceRef = useRef<CancelTokenSource>();
52
+ const getListSourceRef = useRef();
53
+ const [showSearch, setShowSearch] = useState(true);
61
54
 
62
55
  useImperativeHandle(parentRef, () => ({
63
56
  getList,
@@ -71,7 +64,7 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
71
64
  onDel,
72
65
  }));
73
66
 
74
- const { schema, config = {}, model, msgConf = {} }: IListRenderProps = props;
67
+ const { schema = {}, config = {}, model = {}, msgConf = {} } = props;
75
68
 
76
69
  useEffect(() => {
77
70
  if (model) {
@@ -80,9 +73,7 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
80
73
  } else {
81
74
  modelQueryRef.current = model?.query;
82
75
  }
83
- if (hasPagination) {
84
- model.query.pageNum = 1;
85
- }
76
+ model.query.pageNum = 1;
86
77
  }
87
78
  !props.closeAutoRequest && getList();
88
79
  }, []);
@@ -110,7 +101,7 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
110
101
  if (!model?.getList && Array.isArray(props.list)) {
111
102
  setListLoading(true);
112
103
  const { list } = props;
113
- const { pageNum = 1, pageSize = hasPagination ? 10 : Infinity } = modelQueryRef.current || {};
104
+ const { pageNum = 1, pageSize = 10 } = modelQueryRef.current || {};
114
105
  setList(list.slice(pageSize * (pageNum - 1), pageNum * pageSize));
115
106
  setTotal(list.length);
116
107
  props.onGetListEnd && props.onGetListEnd({ list, pagination: { pageNum, pageSize } });
@@ -142,9 +133,7 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
142
133
  model.query = mergedQueries;
143
134
 
144
135
  // 取消上一次请求
145
- getListSourceRef.current?.cancel(
146
- JSON.stringify({ code: 601, message: "取消上一次请求", getListApi: model.getListApi, query: model.query }),
147
- );
136
+ getListSourceRef.current?.cancel({ code: 601, message: "取消上一次请求" });
148
137
 
149
138
  // 重新获取 source
150
139
  getListSourceRef.current = getCancelTokenSource();
@@ -197,9 +186,7 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
197
186
  model.query.pageSize = 10;
198
187
  }
199
188
  formQueryRef.current = query;
200
- if (model.query) {
201
- model.query = Object.assign(model.query, query);
202
- }
189
+ model.query = Object.assign(model.query, query);
203
190
  getList(query);
204
191
  }
205
192
 
@@ -233,7 +220,7 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
233
220
 
234
221
  function onDetail(row, idx) {
235
222
  if (props.fetchOnEdit !== false) {
236
- const getQuery = { id: undefined };
223
+ const getQuery = {};
237
224
  if (props.fetchById !== false) {
238
225
  getQuery.id = row[idKey];
239
226
  } else {
@@ -261,9 +248,7 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
261
248
 
262
249
  function onEdit(row, idx) {
263
250
  if (props.fetchOnEdit !== false) {
264
- const getQuery = {
265
- id: undefined,
266
- };
251
+ const getQuery = {};
267
252
  if (props.fetchById !== false) {
268
253
  getQuery.id = row[idKey];
269
254
  } else {
@@ -293,7 +278,7 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
293
278
  async function onEditSubmit(data) {
294
279
  let _data = data;
295
280
  if (isPatchUpdate) {
296
- if (typeof model?.patchMap === "function") {
281
+ if (model?.patchMap === "function") {
297
282
  _data = model.patchMap(data);
298
283
  }
299
284
  } else if (typeof model?.updateMap === "function") {
@@ -340,10 +325,14 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
340
325
  const modalProps = { ...props.dialogProps, ...props.modalProps };
341
326
  const modalFormMount = props.dialogFormMount ?? props.modalFormMount;
342
327
 
328
+ const handleSetShowSearch = () => {
329
+ setShowSearch(!showSearch);
330
+ };
331
+
343
332
  return (
344
333
  <div className={`list-render ${props.className}`}>
345
334
  <div className={`list-header ${props.verticalHeader ? "vertical-header" : ""}`}>
346
- {props.hasQuery !== false ? (
335
+ {props.hasQuery !== false && showSearch ? (
347
336
  <QueryRender
348
337
  ref={queryRef}
349
338
  schema={schema}
@@ -394,6 +383,7 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
394
383
  onDetail={onDetail}
395
384
  onSearch={onSearch}
396
385
  getList={getList}
386
+ setShowSearch={handleSetShowSearch}
397
387
  loading={listLoading}
398
388
  tableProps={props.tableProps}
399
389
  getFieldListOpt={props.getFieldListOpt}
@@ -446,4 +436,10 @@ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
446
436
  );
447
437
  });
448
438
 
439
+ ListRender.defaultProps = {
440
+ model: {
441
+ query: {},
442
+ },
443
+ };
444
+
449
445
  export default ListRender;
@@ -1,7 +1,7 @@
1
1
  import { useEffect, useState, useMemo, useCallback, useRef } from "react";
2
- import { Table, Button, Popconfirm, Tooltip } from "antd";
2
+ import { Table, Button, Popconfirm, Tooltip, Checkbox, Popover } from "antd";
3
3
  import { Schema } from "@formily/json-schema";
4
- import { QuestionCircleOutlined } from "@ant-design/icons";
4
+ import { QuestionCircleOutlined, FilterOutlined, SettingOutlined } from "@ant-design/icons";
5
5
  import _ from "lodash";
6
6
 
7
7
  import { FormilyField } from "../components/Formily/FormilyField";
@@ -19,8 +19,15 @@ const scenario = "table-render";
19
19
  function TableRender(props) {
20
20
  const { config = {}, query = {}, i18n } = props;
21
21
  const { tableDel = "删除", tableEdit = "编辑", tableDelTip = "确认删除该项?", tableDetail = "详情" } = i18n || {};
22
- const { orderColType, orderColWidth, tableEmptyValue, isTableSortXIdex = false } = config || {};
22
+ const {
23
+ orderColType,
24
+ orderColWidth,
25
+ tableEmptyValue,
26
+ isShowTableFilter = false,
27
+ isTableSortXIdex = false,
28
+ } = config || {};
23
29
  const [columns, setColumns] = useState([]);
30
+ const [columnList, setColumnList] = useState([]);
24
31
 
25
32
  const formilyRef = useRef({
26
33
  fields: {},
@@ -238,6 +245,8 @@ function TableRender(props) {
238
245
  }
239
246
 
240
247
  setColumns(columns);
248
+ setColumnList(columns);
249
+ console.log("columns", columns);
241
250
  }, [props.schema, props.config]);
242
251
 
243
252
  // 解决 FormilyField 中无相关参数报错的问题。自定义参数都从这里传入
@@ -245,8 +254,58 @@ function TableRender(props) {
245
254
  scenario: scenario,
246
255
  };
247
256
 
257
+ const onChange = (e, col) => {
258
+ console.log(`checked = ${e.target.checked}`);
259
+
260
+ if (e.target.checked) {
261
+ const list = [...columns, col];
262
+ // 创建映射表提高查找效率
263
+ const columnIndexMap = new Map(columnList.map((item, index) => [item.key, index]));
264
+ setColumns(list.sort((a, b) => columnIndexMap.get(a.key) - columnIndexMap.get(b.key)));
265
+ } else {
266
+ setColumns(columns.filter((item) => item.key !== col.key));
267
+ }
268
+ };
269
+
248
270
  return (
249
271
  <div className="table-render-wrap">
272
+ {isShowTableFilter ? (
273
+ <div className="table-render-wrap-header">
274
+ <FilterOutlined
275
+ className="table-filter-icon"
276
+ onClick={() => {
277
+ props.setShowSearch && props.setShowSearch();
278
+ }}
279
+ />
280
+ <Popover
281
+ placement="bottomRight"
282
+ content={
283
+ <div className="table-popover-content">
284
+ {columnList.map((col) => {
285
+ if (col.key === "_$actions") {
286
+ return null;
287
+ }
288
+ return (
289
+ <Checkbox
290
+ key={col.key}
291
+ checked={columns.findIndex((item) => item.key === col.key) !== -1}
292
+ onChange={(e) => onChange(e, col)}
293
+ >
294
+ {col.title}
295
+ </Checkbox>
296
+ );
297
+ })}
298
+ </div>
299
+ }
300
+ trigger="click"
301
+ >
302
+ <div className="table-filter">
303
+ <SettingOutlined className="table-filter-icon" />
304
+ 显示列
305
+ </div>
306
+ </Popover>
307
+ </div>
308
+ ) : null}
250
309
  <Table
251
310
  className="table-render"
252
311
  rowKey={props.idKey || "id"}
@@ -1,4 +1,19 @@
1
1
  .table-render-wrap {
2
+ .table-render-wrap-header {
3
+ padding: 0 12px;
4
+ display: flex;
5
+ justify-content: flex-end;
6
+ align-items: center;
7
+ margin-bottom: 12px;
8
+ .table-filter {
9
+ font-size: 16px;
10
+ cursor: pointer;
11
+ .table-filter-icon {
12
+ margin-left: 10px;
13
+ cursor: pointer;
14
+ }
15
+ }
16
+ }
2
17
  .inline-block-max-w {
3
18
  display: inline-block;
4
19
  max-width: 100%;
@@ -15,3 +30,12 @@
15
30
  text-overflow: ellipsis;
16
31
  }
17
32
  }
33
+ .table-popover-content {
34
+ max-width: 400px;
35
+ display: flex;
36
+ flex-direction: column;
37
+ .ant-checkbox-wrapper {
38
+ margin-bottom: 8px;
39
+ margin-left: 0;
40
+ }
41
+ }
package/src/type.ts DELETED
@@ -1,335 +0,0 @@
1
- import { Schema } from "@formily/react";
2
-
3
- /**
4
- * 文案
5
- */
6
- export interface II18n {
7
- /**
8
- * 新增按钮文案 默认 新增
9
- */
10
- createText?: string;
11
- }
12
-
13
- /**
14
- * 弹窗配置
15
- */
16
- export interface IModalConf {
17
- /**
18
- * 使用 FormData 格式传输数据
19
- */
20
- useFormData?: boolean;
21
- }
22
-
23
- /**
24
- * query 参数
25
- */
26
- export interface IQuery {
27
- /**
28
- * 分页当前页码
29
- */
30
- pageNum?: number;
31
- /**
32
- * 分页条数
33
- */
34
- pageSize?: number;
35
- }
36
-
37
- /**
38
- * 表单弹窗
39
- */
40
- export interface IFormModal {
41
- show: Function;
42
- }
43
-
44
- /**
45
- * 详情弹窗
46
- */
47
- export interface IDetailModal {
48
- show: Function;
49
- }
50
-
51
- /**
52
- * data model
53
- */
54
- export interface IDataModel {
55
- /**
56
- * 列表请求 api
57
- */
58
- getListApi: string;
59
- /**
60
- * 列表请求函数
61
- */
62
- getList: Function;
63
- /**
64
- * 详情请求函数
65
- */
66
- get: Function;
67
- /**
68
- * 新增请求函数
69
- */
70
- create: Function;
71
- /**
72
- * put 编辑请求函数
73
- */
74
- update: Function;
75
- /**
76
- * patch 编辑请求函数
77
- */
78
- patch: Function;
79
- /**
80
- * 删除请求函数
81
- */
82
- delete: Function;
83
- /**
84
- * get 请求入参
85
- */
86
- query?: {
87
- /**
88
- * 分页当前页码
89
- */
90
- pageNum?: number;
91
- /**
92
- * 分页条数
93
- */
94
- pageSize?: number;
95
- };
96
- /**
97
- * 新增入参数据处理
98
- * @param d
99
- * @returns
100
- */
101
- createMap?: (d) => typeof d;
102
- /**
103
- * 编辑数据处理
104
- * @param d
105
- * @returns
106
- */
107
- updateMap?: (d) => typeof d;
108
- /**
109
- * patch 编辑数据处理
110
- * @param d
111
- * @returns
112
- */
113
- patchMap?: (d) => typeof d;
114
- }
115
-
116
- export interface ISlots {
117
- /**
118
- * header 操作按钮前插槽
119
- */
120
- headerActionPrefix?: Function;
121
- /**
122
- * header 操作按钮后插槽
123
- */
124
- headerActionSuffix?: Function;
125
- /**
126
- * header 外面的插槽
127
- */
128
- HeaderOthersSuffix?: Function;
129
- }
130
-
131
- /**
132
- * list render props
133
- */
134
- export interface IListRenderProps {
135
- /**
136
- * 容器类名
137
- */
138
- className?: string;
139
- /**
140
- * 列表 schema 配置
141
- */
142
- schema: { schema: Schema };
143
- /**
144
- * data-model 实例
145
- */
146
- model?: IDataModel;
147
- /**
148
- * 列表数据,用于直接渲染而不进行请求或处理。需要本地增删改查使用 data-mode/src/array-data-model.js
149
- */
150
- list?: [Object];
151
- /**
152
- * 列表配置
153
- */
154
- config?: Object;
155
- /**
156
- * message 消息配置
157
- */
158
- msgConf?: Object;
159
- /**
160
- * 新增按钮文案 默认 新增
161
- */
162
- createText?: string;
163
- /**
164
- * 列表唯一值 默认 id
165
- */
166
- idKey: string;
167
- /**
168
- * i18n 文案
169
- */
170
- i18n?: II18n;
171
- /**
172
- * query 是否继承 model query, 默认 false
173
- */
174
- queryFormIsExtendModelQuery?: boolean;
175
- /**
176
- * dialog 弹窗配置
177
- */
178
- dialogConf: IModalConf;
179
- /**
180
- * dialog drawer 弹窗配置
181
- */
182
- modalConf: IModalConf;
183
- /**
184
- * 编辑接口使用 patch 发起请求 默认 false
185
- */
186
- isPatchUpdate: boolean;
187
- /**
188
- * 表单、详情 展示模式: dialog drawer
189
- */
190
- modalMode?: "dialog" | "drawer";
191
- /**
192
- * 表单提交是否使用 FormData 格式
193
- */
194
- useFormData?: boolean;
195
- /**
196
- * 是否关闭初始自动请求
197
- */
198
- closeAutoRequest?: boolean;
199
- /**
200
- * 获取列表完成回调
201
- */
202
- onGetListEnd?: ({ list, pagination: { pageNum, pageSize } }) => void;
203
- /**
204
- * 新增完成回调
205
- */
206
- onCreateSuc?: (res) => void;
207
- /**
208
- * 编辑完成回调
209
- */
210
- onEditSuc?: (res) => void;
211
- /**
212
- * 删除完成回调
213
- */
214
- onDelSuc?: (res) => void;
215
- /**
216
- * 请求获取详情数据,而非直接使用列表数据 默认 true
217
- */
218
- fetchOnEdit?: boolean;
219
- /**
220
- * 通过 id 请求获取详情数据
221
- */
222
- fetchById?: boolean;
223
- /**
224
- * 列表插槽
225
- */
226
- Slots?: ISlots;
227
- /**
228
- * 【废弃】弹窗表单 props
229
- */
230
- dialogFormProps?: Object;
231
- /**
232
- * 弹窗表单 props
233
- */
234
- modalFormProps?: Object;
235
- /**
236
- * 【废弃】表单弹窗关闭回调
237
- */
238
- onFormDialogClose?: Function;
239
- /**
240
- * 表单弹窗关闭回调
241
- */
242
- onFormModalClose?: Function;
243
- /**
244
- * 【废弃】表单弹窗 props
245
- */
246
- dialogProps?: Object;
247
- /**
248
- * 表单弹窗 props
249
- */
250
- modalProps?: Object;
251
- /**
252
- * 【废弃】弹窗表单 渲染完毕回调
253
- */
254
- dialogFormMount?: Function;
255
- /**
256
- * 弹窗表单 渲染完毕回调
257
- */
258
- modalFormMount?: Function;
259
- /**
260
- * 【废弃】弹窗详情组件 props
261
- */
262
- dialogDetailProps?: Function;
263
- /**
264
- * 弹窗详情组件 props
265
- */
266
- modalDetailProps?: Function;
267
- /**
268
- * header 布局
269
- */
270
- verticalHeader?: boolean;
271
- /**
272
- * 是否有过滤项
273
- */
274
- hasQuery?: boolean;
275
- /**
276
- * 搜索过滤项文案,入参字段为 search
277
- */
278
- search?: string;
279
- /**
280
- * 过滤项字段数组
281
- */
282
- filters?: [string];
283
- /**
284
- * query 配置
285
- */
286
- queryConf?: Object;
287
- /**
288
- * query 表单默认值
289
- */
290
- queryFormInitialValues?: Object;
291
- /**
292
- * formily scope 自定义参数
293
- */
294
- schemaScope?: Object;
295
- /**
296
- * formily components 自定义组件
297
- */
298
- components?: Object;
299
- /**
300
- * 是否有新增按钮
301
- */
302
- hasCreate?: boolean;
303
- /**
304
- * 是否有操作列
305
- */
306
- hasAction?: boolean;
307
- /**
308
- * 是否有 分页
309
- */
310
- hasPagination?: boolean;
311
- /**
312
- * table 配置
313
- */
314
- tableConf?: Object;
315
- /**
316
- * table props 参数
317
- */
318
- tableProps?: Object;
319
- /**
320
- * getFieldList opt 获取 field list 的配置
321
- */
322
- getFieldListOpt?: Object;
323
- /**
324
- * 分页器配置 props
325
- */
326
- paginationConf?: Object;
327
- /**
328
- * 新增、编辑、详情表单初始值
329
- */
330
- formInitialValues?: Object;
331
- /**
332
- * 详情自定义组件
333
- */
334
- detailComponents?: Object;
335
- }
File without changes