@hzab/list-render 0.1.2 → 0.1.4

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": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "",
5
5
  "main": "lib",
6
6
  "scripts": {
@@ -19,6 +19,7 @@
19
19
  "author": "CaiYansong",
20
20
  "license": "ISC",
21
21
  "devDependencies": {
22
+ "@hzab/data-model": "^0.0.2",
22
23
  "@hzab/webpack-config": "0.0.12",
23
24
  "@types/react": "^18.0.26",
24
25
  "@types/react-dom": "^18.0.9",
package/src/data-model.js CHANGED
@@ -1,228 +1,5 @@
1
- import _ from "lodash";
2
- import axios from "axios";
1
+ export * from "@hzab/data-model";
3
2
 
4
- const _$Temp = {};
5
-
6
- class DataModel {
7
- constructor(params) {
8
- const {
9
- ctx,
10
- query,
11
- createApi,
12
- createMap,
13
- getApi,
14
- getMap,
15
- getListApi,
16
- getListMap,
17
- getListFunc,
18
- updateApi,
19
- updateMap,
20
- deleteApi,
21
- multipleDeleteApi,
22
- axios: as,
23
- axiosConf,
24
- } = params;
25
-
26
- this.ctx = ctx || {};
27
- this.query = query || {};
28
- this.axios = as || _$Temp.axios || axios;
29
- this.axiosConf = axiosConf || {};
30
-
31
- this.createApi = createApi;
32
- this.createMap = createMap;
33
- this.getApi = getApi;
34
- this.getMap = getMap;
35
- this.getListApi = getListApi;
36
- this.getListMap = getListMap;
37
- this.getListFunc = getListFunc;
38
- this.updateApi = updateApi;
39
- this.updateMap = updateMap;
40
- this.deleteApi = deleteApi;
41
- this.multipleDeleteApi = multipleDeleteApi;
42
- }
43
-
44
- getApiUrl(api, record, ctx = this.ctx) {
45
- if (!api) {
46
- throw new Error("Error api 不能为空", api, record, ctx);
47
- }
48
- let apiUrl = api;
49
- const params = _.merge({}, record, ctx);
50
- _.each(params, (value, key) => {
51
- if (!_.isString(value) || !_.isNumber(value) || _.isBoolean(value)) {
52
- apiUrl = apiUrl.replace(`:${key}`, value);
53
- }
54
- });
55
- return apiUrl;
56
- }
57
-
58
- get(q = {}, ctx = {}) {
59
- let query = _.merge({}, this.query, q);
60
- query = _.pickBy(query, (val) => !_.isNil(val) && val !== "");
61
-
62
- return new Promise((resolve, reject) => {
63
- const apiUrl = this.getApiUrl(this.getApi, query, ctx);
64
- this.axios
65
- .get(apiUrl, {
66
- ...this.axiosConf,
67
- params: query,
68
- })
69
- .then((response) => {
70
- this.handleRes(
71
- response,
72
- (res) => {
73
- if (this.getMap) {
74
- res = this.getMap(res);
75
- }
76
- resolve(res);
77
- },
78
- reject,
79
- );
80
- })
81
- .catch((err) => this.errorHandler(err, reject));
82
- });
83
- }
84
-
85
- async getList(q, ctx) {
86
- let query = _.merge({}, this.query, q);
87
- query = _.pickBy(query, (val) => !_.isNil(val) && val !== "");
88
-
89
- let resultList = null;
90
- if (this.getListFunc) {
91
- resultList = await this.getListFunc(query);
92
- } else {
93
- const getPro = new Promise((resolve, reject) => {
94
- const apiUrl = this.getApiUrl(this.getListApi, q, ctx);
95
- this.axios
96
- .get(apiUrl, {
97
- ...this.axiosConf,
98
- params: query,
99
- })
100
- .then((response) => {
101
- this.handleRes(
102
- response,
103
- (res) => {
104
- // data: { list: [], pagination: { current: 0, total: 1 } }
105
- if (this.getListMap) {
106
- res.list = res.list.map((record) => this.getListMap(record));
107
- }
108
- resolve(res);
109
- },
110
- reject,
111
- );
112
- })
113
- .catch((err) => this.errorHandler(err, reject));
114
- });
115
- resultList = await getPro;
116
- }
117
- return resultList;
118
- }
119
-
120
- create(params, ctx) {
121
- return new Promise((resolve, reject) => {
122
- const apiUrl = this.getApiUrl(this.createApi, params, ctx);
123
- const opt = { ...this.axiosConf };
124
- if (params instanceof FormData) {
125
- opt.headers = { "Content-Type": "multipart/form-data" };
126
- }
127
- this.axios
128
- .post(apiUrl, params, opt)
129
- .then((response) => {
130
- this.handleRes(response, resolve, reject);
131
- })
132
- .catch((err) => this.errorHandler(err, reject));
133
- });
134
- }
135
-
136
- update(params, ctx) {
137
- return new Promise((resolve, reject) => {
138
- const apiUrl = this.getApiUrl(this.updateApi, params, ctx);
139
- const opt = { ...this.axiosConf };
140
- if (params instanceof FormData) {
141
- opt.headers = { "Content-Type": "multipart/form-data" };
142
- }
143
- this.axios
144
- .put(apiUrl, params, opt)
145
- .then((response) => {
146
- this.handleRes(response, resolve, reject);
147
- })
148
- .catch((err) => this.errorHandler(err, reject));
149
- });
150
- }
151
-
152
- delete(params, ctx) {
153
- return new Promise((resolve, reject) => {
154
- const apiUrl = this.getApiUrl(this.deleteApi, params, ctx);
155
- this.axios
156
- .delete(apiUrl, { ...this.axiosConf, ...params })
157
- .then((response) => {
158
- this.handleRes(response, resolve, reject);
159
- })
160
- .catch((err) => this.errorHandler(err, reject));
161
- });
162
- }
163
-
164
- multipleDelete(params, ctx) {
165
- return new Promise((resolve, reject) => {
166
- const apiUrl = this.getApiUrl(this.multipleDeleteApi, params, ctx);
167
- this.axios
168
- .delete(apiUrl, { ...this.axiosConf, ...params })
169
- .then((response) => {
170
- this.handleRes(response, resolve, reject);
171
- })
172
- .catch((err) => this.errorHandler(err, reject));
173
- });
174
- }
175
-
176
- handleRes(response, resolve, reject) {
177
- if (typeof response !== "object") {
178
- reject(new Error("response not object"));
179
- return;
180
- }
181
- let _res = response;
182
- // 兼容 传入的是 response.data 的情况
183
- if (_res.data && _res.headers && _res.request && typeof _res.data?.code === "number") {
184
- _res = _res.data;
185
- }
186
- const { code, message, data, msg } = _res;
187
- if (code === 200) {
188
- let _data = data ?? {};
189
- if (_.isObject(_data) && _data.message === undefined) {
190
- // 前缀 _ 避免与 data 里已有的 message 冲突
191
- _data._message = message || msg;
192
- }
193
- resolve(_data);
194
- } else {
195
- const error = new Error(message || msg);
196
- error.code = code;
197
- error.response = response;
198
- error._message = message || msg;
199
- reject(error);
200
- }
201
- }
202
-
203
- errorHandler(err, reject) {
204
- const response = err.response || err;
205
- if (response) {
206
- const message = (response.data && (response.data.message || response.data.msg)) || response.msg;
207
- const error = new Error(message || response.statusText || "未知错误");
208
- error.code = response.status;
209
- error.response = response;
210
- if (message) {
211
- // 前缀 _ 避免与 data 里已有的 message 冲突
212
- error._message = message;
213
- }
214
- return reject(error);
215
- }
216
- return reject(err);
217
- }
218
- }
219
-
220
- function setDefaultAxios(ax) {
221
- if (ax) {
222
- _$Temp.axios = ax;
223
- }
224
- }
225
-
226
- export { axios, DataModel, setDefaultAxios };
3
+ import DataModel from "@hzab/data-model";
227
4
 
228
5
  export default DataModel;
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import ListRender from "./list-render.jsx";
2
2
 
3
- export * from "./data-model.js";
3
+ export * from "@hzab/data-model";
4
4
 
5
5
  export default ListRender;
@@ -225,6 +225,7 @@ const ListRender = forwardRef(function (props, parentRef) {
225
225
  list={list}
226
226
  config={props.tableConf}
227
227
  hasAction={props.hasAction}
228
+ query={model?.query}
228
229
  Slots={props.Slots}
229
230
  onEdit={onEdit}
230
231
  onDel={onDel}
@@ -238,7 +239,6 @@ const ListRender = forwardRef(function (props, parentRef) {
238
239
  ref={formDialogRef}
239
240
  schema={schema}
240
241
  dialogConf={props.dialogConf}
241
- formSlots={props.formSlots}
242
242
  formInitialValues={props.formInitialValues}
243
243
  onClose={props.onFormDialogClose}
244
244
  onSubmit={formState === "edit" ? onEditSubmit : onCreateSubmit}
@@ -7,7 +7,7 @@ const pageSizeOptions = [10, 20, 50, 100];
7
7
 
8
8
  function PaginationRender(props) {
9
9
  const [pageSize, setPageSize] = useState(
10
- props.query?.pageSize || (props.pageSizeOptions ? props.pageSizeOptions[0] : pageSizeOptions[0]),
10
+ props.query?.pageSize || (props.config?.pageSizeOptions ? props.config?.pageSizeOptions[0] : pageSizeOptions[0]),
11
11
  );
12
12
  const [pageNum, setPageNumber] = useState(props.query?.pageNum || 1);
13
13
 
@@ -43,7 +43,7 @@ function PaginationRender(props) {
43
43
  current={pageNum}
44
44
  pageSize={pageSize}
45
45
  onChange={onChange}
46
- pageSizeOptions={props.pageSizeOptions || pageSizeOptions}
46
+ pageSizeOptions={props.config?.pageSizeOptions || pageSizeOptions}
47
47
  total={props.total}
48
48
  showTotal={(total) => `总条数 ${total} 条`}
49
49
  showSizeChanger
@@ -6,7 +6,8 @@ import { getVal, getFieldList } from "../common/utils";
6
6
  import "./index.less";
7
7
 
8
8
  function TableRender(props) {
9
- const { config = {} } = props;
9
+ const { config = {}, query = {} } = props;
10
+ const { orderColType, orderColWidth } = config || {};
10
11
  const [columns, setColumns] = useState([]);
11
12
 
12
13
  useEffect(() => {
@@ -15,6 +16,27 @@ function TableRender(props) {
15
16
  }
16
17
  const fieldList = getFieldList(props.schema);
17
18
  const columns = [];
19
+
20
+ // 序号列
21
+ if (orderColType === "page") {
22
+ columns.push({
23
+ title: "序号",
24
+ width: orderColWidth,
25
+ render: (text, record, index) => `${index + 1}`,
26
+ });
27
+ } else if (orderColType == "all") {
28
+ columns.push({
29
+ title: "序号",
30
+ width: orderColWidth,
31
+ render: (text, record, index) => {
32
+ const { pageSize = 10, pageNum = 1 } = query || {};
33
+ console.log(pageNum, index);
34
+ // 当前页数减1乘以每一页页数再加当前页序号+1
35
+ return `${(pageNum - 1) * pageSize + (index + 1)}`;
36
+ },
37
+ });
38
+ }
39
+
18
40
  /*
19
41
  title: "姓名",
20
42
  dataIndex: "name",
@@ -89,7 +111,7 @@ function TableRender(props) {
89
111
  });
90
112
 
91
113
  if (props.hasAction !== false) {
92
- const { hasEdit, hasDel } = props.config || {};
114
+ const { hasEdit, hasDel, hasDelTips } = props.config || {};
93
115
 
94
116
  const _colConf = props.config?.colConf?._$actions || {};
95
117
 
@@ -104,14 +126,14 @@ function TableRender(props) {
104
126
  return <Slots.tableActionsSlot {...slotProps} />;
105
127
  }
106
128
 
107
- let _hasEdit = hasEdit !== false;
108
- if (_hasEdit && typeof _colConf.hasEdit === "function") {
109
- _hasEdit = _colConf.hasEdit(record, index);
110
- }
111
- let _hasDel = hasDel !== false;
112
- if (_hasDel && typeof _colConf.hasDel === "function") {
113
- _hasDel = _colConf.hasDel(record, index);
114
- }
129
+ //编辑按钮权限
130
+ const _hasEdit = hasEdit && typeof hasEdit === "function" ? hasEdit(record, index) : hasEdit !== false;
131
+
132
+ //删除按钮权限
133
+ const _hasDel = hasDel && typeof hasDel === "function" ? hasEdit(record, index) : hasDel !== false;
134
+
135
+ //删除按钮提示
136
+ const delTips = hasDelTips && typeof hasDelTips === "function" ? hasDelTips(record, index) : hasDelTips || "确认删除该项?";
115
137
 
116
138
  return (
117
139
  <div style={{ width: _colConf?.width, maxWidth: "100%" }}>
@@ -130,7 +152,7 @@ function TableRender(props) {
130
152
  {_hasDel ? (
131
153
  <Popconfirm
132
154
  placement="topRight"
133
- title={"确认删除该项?"}
155
+ title={delTips}
134
156
  onConfirm={() => {
135
157
  props.onDel && props.onDel(record, index);
136
158
  }}