ztxkui 1.8.2 → 1.8.6

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.
@@ -24,7 +24,7 @@ var __rest = (this && this.__rest) || function (s, e) {
24
24
  * @author cyx
25
25
  * @description 增强下拉框功能
26
26
  */
27
- import React, { useCallback, useEffect, memo } from 'react';
27
+ import React, { useCallback, useEffect, memo, useRef } from 'react';
28
28
  import { Select } from 'antd';
29
29
  import { useFetchState } from 'ztxkutils/dist/hooks';
30
30
  import debounce from 'lodash/debounce';
@@ -74,17 +74,28 @@ function EnhanceSelect(_a) {
74
74
  var list = _a.list, defaultList = _a.defaultList, params = _a.params, url = _a.url, _b = _a.method, method = _b === void 0 ? 'GET' : _b, _c = _a.dataKey, dataKey = _c === void 0 ? 'id' : _c, componentKey = _a.componentKey, _d = _a.titleKey, titleKey = _d === void 0 ? 'name' : _d, showAll = _a.showAll, isCatch = _a.isCatch, onCompleted = _a.onCompleted, onChange = _a.onChange, request = _a.request, remoteSearch = _a.remoteSearch, remoteSearchKey = _a.remoteSearchKey, transformData = _a.transformData, joinKey = _a.joinKey, _e = _a.joinStr, joinStr = _e === void 0 ? '-' : _e, restProps = __rest(_a, ["list", "defaultList", "params", "url", "method", "dataKey", "componentKey", "titleKey", "showAll", "isCatch", "onCompleted", "onChange", "request", "remoteSearch", "remoteSearchKey", "transformData", "joinKey", "joinStr"]);
75
75
  // 下拉数据源
76
76
  var _f = useFetchState([]), selectList = _f[0], setSelectList = _f[1];
77
+ var _g = useFetchState(false), loading = _g[0], setLoading = _g[1];
78
+ var fetchId = useRef(0); // 请求Id,每次请求都会加1
77
79
  // 如果selectList 发生改变,那么触发onCompleted事件
78
80
  useEffect(function () {
79
81
  if (Array.isArray(selectList) && onCompleted) {
80
82
  onCompleted(selectList);
81
83
  }
82
- }, [selectList, onCompleted]);
84
+ // eslint-disable-next-line react-hooks/exhaustive-deps
85
+ }, [selectList]);
83
86
  // 获取数据,并设置数据
84
87
  var getDataHandle = useCallback(function (request, options, appointObj) {
88
+ setLoading(true);
89
+ // 利用闭包,存到当前请求的id
90
+ fetchId.current++;
91
+ var fetchIdClosure = fetchId.current;
85
92
  getData(request, options)
86
93
  .then(function (resData) {
87
94
  var _a;
95
+ if (fetchIdClosure < fetchId.current) {
96
+ return;
97
+ }
98
+ setLoading(false);
88
99
  var res = transformData ? transformData(resData) : resData;
89
100
  var result;
90
101
  if (Array.isArray(res.data)) {
@@ -100,9 +111,10 @@ function EnhanceSelect(_a) {
100
111
  }
101
112
  })
102
113
  .catch(function (err) {
114
+ setLoading(false);
103
115
  console.log(err);
104
116
  });
105
- }, [setSelectList, transformData]);
117
+ }, [setSelectList, setLoading, transformData]);
106
118
  // 远程搜索
107
119
  var getRemoteDataHandle = useCallback(function (value) {
108
120
  var _a;
@@ -112,6 +124,10 @@ function EnhanceSelect(_a) {
112
124
  outParams = JSON.parse(params);
113
125
  }
114
126
  catch (err) { }
127
+ setLoading(true);
128
+ // 利用闭包,存到当前请求的id
129
+ fetchId.current++;
130
+ var fetchIdClosure = fetchId.current;
115
131
  getData(request, {
116
132
  url: url,
117
133
  method: method,
@@ -119,6 +135,10 @@ function EnhanceSelect(_a) {
119
135
  })
120
136
  .then(function (resData) {
121
137
  var _a, _b, _c;
138
+ if (fetchIdClosure < fetchId.current) {
139
+ return;
140
+ }
141
+ setLoading(false);
122
142
  var res = transformData ? transformData(resData) : resData;
123
143
  var result;
124
144
  if (Array.isArray(res.data)) {
@@ -135,6 +155,7 @@ function EnhanceSelect(_a) {
135
155
  setSelectList(result);
136
156
  })
137
157
  .catch(function (err) {
158
+ setLoading(false);
138
159
  console.log(err);
139
160
  });
140
161
  }, [
@@ -146,6 +167,7 @@ function EnhanceSelect(_a) {
146
167
  titleKey,
147
168
  setSelectList,
148
169
  transformData,
170
+ setLoading,
149
171
  ]);
150
172
  var searchHandle = function (value) {
151
173
  // 当value有值时才进行搜索
@@ -210,13 +232,28 @@ function EnhanceSelect(_a) {
210
232
  // console.log('TEST: 远程搜索');
211
233
  getRemoteDataHandle('');
212
234
  }, [url, remoteSearch, getRemoteDataHandle]);
235
+ // 数据合并
236
+ function concatData(originalData, newData) {
237
+ if (!newData) {
238
+ return originalData;
239
+ }
240
+ var resultData = originalData.slice() || [];
241
+ if (Array.isArray(newData)) {
242
+ newData.forEach(function (item) {
243
+ if (!resultData.find(function (resultItem) { return resultItem[dataKey] === item[dataKey]; })) {
244
+ resultData.unshift(item);
245
+ }
246
+ });
247
+ }
248
+ return resultData;
249
+ }
213
250
  // 渲染数据
214
251
  function renderSelectOption() {
215
252
  var _defaultList = Array.isArray(defaultList) ? defaultList : [];
216
253
  return (React.createElement(React.Fragment, null,
217
254
  showAll && (React.createElement(Select.Option, { title: "\u5168\u90E8", value: "" }, "\u5168\u90E8")),
218
255
  Array.isArray(selectList) &&
219
- selectList.concat(_defaultList).map(function (item) {
256
+ concatData(selectList, _defaultList).map(function (item) {
220
257
  var key = componentKey ? item[componentKey] : item[dataKey];
221
258
  var value = item[dataKey];
222
259
  var title = item[titleKey];
@@ -262,6 +299,6 @@ function EnhanceSelect(_a) {
262
299
  onChange && onChange(value, option);
263
300
  }
264
301
  };
265
- return (React.createElement(Select, __assign({ defaultValue: showAll ? '' : undefined, allowClear: true, showSearch: true, filterOption: remoteSearch ? false : filterOptionHandle, onSearch: remoteSearch ? onSearchHandle : undefined, onChange: onChangeHandle, optionLabelProp: "title" }, restProps), renderSelectOption()));
302
+ return (React.createElement(Select, __assign({ defaultValue: showAll ? '' : undefined, allowClear: true, showSearch: true, filterOption: remoteSearch ? false : filterOptionHandle, onSearch: remoteSearch ? onSearchHandle : undefined, onChange: onChangeHandle, optionLabelProp: "title", loading: loading }, restProps), renderSelectOption()));
266
303
  }
267
304
  export default memo(EnhanceSelect);
@@ -20,7 +20,7 @@ var __rest = (this && this.__rest) || function (s, e) {
20
20
  }
21
21
  return t;
22
22
  };
23
- import React, { useEffect, useCallback } from 'react';
23
+ import React, { useEffect, useCallback, useRef } from 'react';
24
24
  import { TreeSelect } from 'antd';
25
25
  import { useFetchState } from 'ztxkutils/dist/hooks';
26
26
  // 获取接口数据
@@ -53,6 +53,7 @@ function EnhanceTreeSelect(_a) {
53
53
  * 保证数据源唯一
54
54
  */
55
55
  var _d = useFetchState([]), treeData = _d[0], setTreeData = _d[1];
56
+ var fetchId = useRef(0); // 请求Id,每次请求都会加1
56
57
  // 如果selectList 发生改变,那么触发onCompleted事件
57
58
  useEffect(function () {
58
59
  if (Array.isArray(treeData) && onCompleted) {
@@ -61,9 +62,15 @@ function EnhanceTreeSelect(_a) {
61
62
  }, [treeData, onCompleted]);
62
63
  // 获取数据,并设置数据
63
64
  var getDataHandle = useCallback(function (request, options, isConcat) {
65
+ // 利用闭包,存到当前请求的id
66
+ fetchId.current++;
67
+ var fetchIdClosure = fetchId.current;
64
68
  return getData(request, options)
65
69
  .then(function (resData) {
66
70
  var _a;
71
+ if (fetchIdClosure < fetchId.current) {
72
+ return;
73
+ }
67
74
  var res = transformData ? transformData(resData) : resData;
68
75
  var result;
69
76
  if (Array.isArray(res.data)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztxkui",
3
- "version": "1.8.2",
3
+ "version": "1.8.6",
4
4
  "private": false,
5
5
  "description": "React components library",
6
6
  "author": "zt-front-end",