@hzab/list-render 1.9.2-beta6 → 1.9.3-beta1

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.2-beta6",
3
+ "version": "1.9.3-beta1",
4
4
  "description": "",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -1,4 +1,4 @@
1
- import ListRender from "./list-render.jsx";
1
+ import ListRender from "./list-render";
2
2
 
3
3
  export * from "@hzab/data-model";
4
4
 
@@ -6,6 +6,7 @@ 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";
9
10
 
10
11
  import QueryRender from "./query-render";
11
12
  import Pagination from "./pagination-render";
@@ -15,9 +16,11 @@ import DetailModal from "./DetailModal";
15
16
 
16
17
  import { objToFormData } from "./common/utils";
17
18
 
19
+ import { IListRenderProps, IFormModal, IDetailModal, IQuery } from "./type";
20
+
18
21
  import "./index.less";
19
22
 
20
- const ListRender = forwardRef(function (props, parentRef) {
23
+ const ListRender = forwardRef(function (props: IListRenderProps, parentRef) {
21
24
  const {
22
25
  idKey = "id",
23
26
  i18n,
@@ -35,21 +38,26 @@ const ListRender = forwardRef(function (props, parentRef) {
35
38
  * 表单提交是否使用 FormData 格式
36
39
  */
37
40
  useFormData: _useFormData,
41
+ /**
42
+ * 是否有分页
43
+ */
44
+ hasPagination = true,
38
45
  } = props;
46
+
39
47
  const { createText = props.createText } = i18n || {};
40
48
  const [total, setTotal] = useState(0);
41
49
  const [list, setList] = useState([]);
42
50
  const [formState, setFormState] = useState("");
43
51
  const [rowId, setRowId] = useState(0);
44
52
  const [listLoading, setListLoading] = useState(false);
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 });
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 } : {});
51
59
  const useFormData = _useFormData ?? dialogConf.useFormData ?? modalConf?.useFormData;
52
- const getListSourceRef = useRef();
60
+ const getListSourceRef = useRef<CancelTokenSource>();
53
61
 
54
62
  useImperativeHandle(parentRef, () => ({
55
63
  getList,
@@ -63,7 +71,7 @@ const ListRender = forwardRef(function (props, parentRef) {
63
71
  onDel,
64
72
  }));
65
73
 
66
- const { schema = {}, config = {}, model = {}, msgConf = {} } = props;
74
+ const { schema, config = {}, model, msgConf = {} }: IListRenderProps = props;
67
75
 
68
76
  useEffect(() => {
69
77
  if (model) {
@@ -72,7 +80,9 @@ const ListRender = forwardRef(function (props, parentRef) {
72
80
  } else {
73
81
  modelQueryRef.current = model?.query;
74
82
  }
75
- model.query.pageNum = 1;
83
+ if (hasPagination) {
84
+ model.query.pageNum = 1;
85
+ }
76
86
  }
77
87
  !props.closeAutoRequest && getList();
78
88
  }, []);
@@ -100,7 +110,7 @@ const ListRender = forwardRef(function (props, parentRef) {
100
110
  if (!model?.getList && Array.isArray(props.list)) {
101
111
  setListLoading(true);
102
112
  const { list } = props;
103
- const { pageNum = 1, pageSize = 10 } = modelQueryRef.current || {};
113
+ const { pageNum = 1, pageSize = hasPagination ? 10 : Infinity } = modelQueryRef.current || {};
104
114
  setList(list.slice(pageSize * (pageNum - 1), pageNum * pageSize));
105
115
  setTotal(list.length);
106
116
  props.onGetListEnd && props.onGetListEnd({ list, pagination: { pageNum, pageSize } });
@@ -132,7 +142,9 @@ const ListRender = forwardRef(function (props, parentRef) {
132
142
  model.query = mergedQueries;
133
143
 
134
144
  // 取消上一次请求
135
- getListSourceRef.current?.cancel({ code: 601, message: "取消上一次请求" });
145
+ getListSourceRef.current?.cancel(
146
+ JSON.stringify({ code: 601, message: "取消上一次请求", getListApi: model.getListApi, query: model.query }),
147
+ );
136
148
 
137
149
  // 重新获取 source
138
150
  getListSourceRef.current = getCancelTokenSource();
@@ -185,7 +197,9 @@ const ListRender = forwardRef(function (props, parentRef) {
185
197
  model.query.pageSize = 10;
186
198
  }
187
199
  formQueryRef.current = query;
188
- model.query = Object.assign(model.query, query);
200
+ if (model.query) {
201
+ model.query = Object.assign(model.query, query);
202
+ }
189
203
  getList(query);
190
204
  }
191
205
 
@@ -219,7 +233,7 @@ const ListRender = forwardRef(function (props, parentRef) {
219
233
 
220
234
  function onDetail(row, idx) {
221
235
  if (props.fetchOnEdit !== false) {
222
- const getQuery = {};
236
+ const getQuery = { id: undefined };
223
237
  if (props.fetchById !== false) {
224
238
  getQuery.id = row[idKey];
225
239
  } else {
@@ -247,7 +261,9 @@ const ListRender = forwardRef(function (props, parentRef) {
247
261
 
248
262
  function onEdit(row, idx) {
249
263
  if (props.fetchOnEdit !== false) {
250
- const getQuery = {};
264
+ const getQuery = {
265
+ id: undefined,
266
+ };
251
267
  if (props.fetchById !== false) {
252
268
  getQuery.id = row[idKey];
253
269
  } else {
@@ -277,7 +293,7 @@ const ListRender = forwardRef(function (props, parentRef) {
277
293
  async function onEditSubmit(data) {
278
294
  let _data = data;
279
295
  if (isPatchUpdate) {
280
- if (model?.patchMap === "function") {
296
+ if (typeof model?.patchMap === "function") {
281
297
  _data = model.patchMap(data);
282
298
  }
283
299
  } else if (typeof model?.updateMap === "function") {
@@ -430,10 +446,4 @@ const ListRender = forwardRef(function (props, parentRef) {
430
446
  );
431
447
  });
432
448
 
433
- ListRender.defaultProps = {
434
- model: {
435
- query: {},
436
- },
437
- };
438
-
439
449
  export default ListRender;
package/src/type.ts ADDED
@@ -0,0 +1,335 @@
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