@mi-avalon/libs 0.0.23 → 0.0.25
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/components/MSearch/index.d.ts +26 -0
- package/dist/components/MSearch/index.js +68 -0
- package/dist/components/MiModal/index.d.ts +15 -0
- package/dist/components/MiModal/index.js +70 -0
- package/dist/components/index.d.ts +2 -0
- package/dist/components/index.js +2 -2
- package/dist/hooks/index.d.ts +7 -0
- package/dist/hooks/index.js +7 -0
- package/dist/hooks/useFuncRequest.d.ts +16 -0
- package/dist/hooks/useFuncRequest.js +67 -0
- package/dist/hooks/useInterval.d.ts +8 -0
- package/dist/hooks/useInterval.js +30 -0
- package/dist/hooks/usePagination.d.ts +42 -0
- package/dist/hooks/usePagination.js +124 -0
- package/dist/hooks/useQuery.d.ts +3 -0
- package/dist/hooks/useQuery.js +6 -0
- package/dist/hooks/useReactive.d.ts +2 -0
- package/dist/hooks/useReactive.js +20 -0
- package/dist/hooks/useTimeout.d.ts +8 -0
- package/dist/hooks/useTimeout.js +30 -0
- package/dist/hooks/useVirtualList.d.ts +75 -0
- package/dist/hooks/useVirtualList.js +121 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +1411 -932
- package/dist/index.js +1 -1
- package/dist/index.umd.js +16 -16
- package/dist/style.css +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -1
- package/dist/utils/openModal.d.ts +15 -0
- package/dist/utils/openModal.js +98 -0
- package/package.json +1 -1
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// 虚拟列表加载
|
|
2
|
+
import { useCallback, useEffect, useRef, useState, } from 'react';
|
|
3
|
+
import { PAGE_SIZE } from '../constants';
|
|
4
|
+
import { debounce } from '../utils';
|
|
5
|
+
export const useVirtualList = (server, deps, // 依赖条件 数据更新默认执行server
|
|
6
|
+
option) => {
|
|
7
|
+
const { isReady = true, dataSource: propDataSource = [], current: propCurrent = 1, pageSize: propPageSize = PAGE_SIZE, } = option || {};
|
|
8
|
+
// 是否完成了一次请求
|
|
9
|
+
const [isFirstComplete, setIsFirstComplete] = useState(false);
|
|
10
|
+
// 分页
|
|
11
|
+
const [current, setCurrent] = useState(propCurrent);
|
|
12
|
+
const [pageSize, setPageSize] = useState(propPageSize);
|
|
13
|
+
const [total, setTotal] = useState(0);
|
|
14
|
+
// 表格
|
|
15
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
16
|
+
const [dataSource, setDataSource] = useState(propDataSource);
|
|
17
|
+
const currentRef = useRef(current);
|
|
18
|
+
const pageSizeRef = useRef(pageSize);
|
|
19
|
+
// 在状态更新时同步更新ref
|
|
20
|
+
const setCurrentAndRef = (val) => {
|
|
21
|
+
setCurrent(val);
|
|
22
|
+
currentRef.current = val;
|
|
23
|
+
};
|
|
24
|
+
const setPageSizeAndRef = (val) => {
|
|
25
|
+
setPageSize(val);
|
|
26
|
+
pageSizeRef.current = val;
|
|
27
|
+
};
|
|
28
|
+
// 计数器
|
|
29
|
+
const seq = useRef(0);
|
|
30
|
+
const doSearch = async (reset) => {
|
|
31
|
+
if (!isReady) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
let _current = currentRef.current;
|
|
35
|
+
let _pageSize = pageSizeRef.current;
|
|
36
|
+
setIsLoading(true);
|
|
37
|
+
seq.current += 1;
|
|
38
|
+
const _seq = seq.current;
|
|
39
|
+
try {
|
|
40
|
+
// 发送请求
|
|
41
|
+
let offset = Math.round((_current - 1) * _pageSize);
|
|
42
|
+
if (offset < 0) {
|
|
43
|
+
offset = 0;
|
|
44
|
+
}
|
|
45
|
+
if (_pageSize < 1) {
|
|
46
|
+
_pageSize = 1;
|
|
47
|
+
}
|
|
48
|
+
let { dataSource: data0, total: total0 } = await server({
|
|
49
|
+
limit: _pageSize,
|
|
50
|
+
offset,
|
|
51
|
+
current: _current,
|
|
52
|
+
});
|
|
53
|
+
if (_seq !== seq.current)
|
|
54
|
+
return;
|
|
55
|
+
if (pageSize * (_current - 1) >= total0 && _current !== 1) {
|
|
56
|
+
const totalPage = Math.ceil(total0 / pageSize);
|
|
57
|
+
({ dataSource: data0, total: total0 } = await server({
|
|
58
|
+
limit: _pageSize,
|
|
59
|
+
offset: Math.round((totalPage - 1) * _pageSize),
|
|
60
|
+
current: _current,
|
|
61
|
+
}));
|
|
62
|
+
if (_seq !== seq.current)
|
|
63
|
+
return;
|
|
64
|
+
_current = totalPage;
|
|
65
|
+
// message.error('数据源发生变化,该页没有数据,自动加载最后一页');
|
|
66
|
+
}
|
|
67
|
+
const d = reset ? data0 : dataSource.concat(data0);
|
|
68
|
+
setDataSource(d);
|
|
69
|
+
setCurrentAndRef(_current);
|
|
70
|
+
setPageSizeAndRef(_pageSize);
|
|
71
|
+
setTotal(total0);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
console.error('fetch err', error);
|
|
75
|
+
if (_seq !== seq.current)
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
setIsFirstComplete(true);
|
|
79
|
+
setIsLoading(false);
|
|
80
|
+
};
|
|
81
|
+
// 加载下一页数据
|
|
82
|
+
const refresh = async (reset) => {
|
|
83
|
+
if (reset) {
|
|
84
|
+
setDataSource([]);
|
|
85
|
+
setCurrentAndRef(propCurrent);
|
|
86
|
+
setPageSizeAndRef(propPageSize);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const curTotal = pageSize * current;
|
|
90
|
+
if (total && total <= curTotal)
|
|
91
|
+
return;
|
|
92
|
+
setCurrentAndRef(current + 1);
|
|
93
|
+
}
|
|
94
|
+
await doSearch(reset);
|
|
95
|
+
};
|
|
96
|
+
// 防抖 加载下一页数据
|
|
97
|
+
const debounceRefresh = useCallback(debounce(refresh, 500), [refresh]);
|
|
98
|
+
/* 重置逻辑 */
|
|
99
|
+
const _deps = [...(deps || []), isReady];
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
if (!isReady)
|
|
102
|
+
return;
|
|
103
|
+
debounceRefresh(true);
|
|
104
|
+
}, _deps);
|
|
105
|
+
return {
|
|
106
|
+
loading: isLoading,
|
|
107
|
+
dataSource,
|
|
108
|
+
paginationProps: {
|
|
109
|
+
current,
|
|
110
|
+
pageSize,
|
|
111
|
+
total,
|
|
112
|
+
},
|
|
113
|
+
isFirstComplete,
|
|
114
|
+
refresh: async (reset) => {
|
|
115
|
+
await refresh(reset);
|
|
116
|
+
},
|
|
117
|
+
debounceRefresh,
|
|
118
|
+
setDataSource,
|
|
119
|
+
setTotal,
|
|
120
|
+
};
|
|
121
|
+
};
|
package/dist/index.d.ts
CHANGED