@mi-avalon/libs 0.0.22 → 0.0.24
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/MForm/MFormItemConst.d.ts +25 -0
- package/dist/components/MForm/MFormItemConst.js +69 -0
- package/dist/components/MForm/index.d.ts +9 -0
- package/dist/components/MForm/index.js +48 -0
- package/dist/components/MForm/type.d.ts +178 -0
- package/dist/components/MForm/type.js +18 -0
- package/dist/components/MSearch/index.d.ts +26 -0
- package/dist/components/MSearch/index.js +68 -0
- package/dist/components/index.d.ts +3 -0
- package/dist/components/index.js +3 -3
- 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 +2033 -671
- package/dist/index.js +1 -1
- package/dist/index.umd.js +73 -11
- package/dist/style.css +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { debounce } from '../utils';
|
|
3
|
+
export const usePagination = (server, deps, // 依赖条件 数据更新默认执行server
|
|
4
|
+
option) => {
|
|
5
|
+
const { isReady = true, dataSource: propDataSource = [], current: propCurrent = 1, pageSize: propPageSize = 10, } = option || {};
|
|
6
|
+
const [current, setCurrent] = useState(propCurrent);
|
|
7
|
+
const [pageSize, setPageSize] = useState(propPageSize);
|
|
8
|
+
const [dataSource, setDataSource] = useState(propDataSource);
|
|
9
|
+
const [total, setTotal] = useState(0);
|
|
10
|
+
const [loading, setLoading] = useState(false);
|
|
11
|
+
const [isFirstComplete, setIsFirstComplete] = useState(false);
|
|
12
|
+
const currentRef = useRef(current);
|
|
13
|
+
const pageSizeRef = useRef(pageSize);
|
|
14
|
+
// 在状态更新时同步更新ref
|
|
15
|
+
const setCurrentAndRef = (val) => {
|
|
16
|
+
setCurrent(val);
|
|
17
|
+
currentRef.current = val;
|
|
18
|
+
};
|
|
19
|
+
const setPageSizeAndRef = (val) => {
|
|
20
|
+
setPageSize(val);
|
|
21
|
+
pageSizeRef.current = val;
|
|
22
|
+
};
|
|
23
|
+
// 计数器
|
|
24
|
+
const seq = useRef(0);
|
|
25
|
+
const doSearch = async () => {
|
|
26
|
+
if (!isReady) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
let _current = currentRef.current;
|
|
30
|
+
let _pageSize = pageSizeRef.current;
|
|
31
|
+
setLoading(true);
|
|
32
|
+
seq.current++;
|
|
33
|
+
const _seq = seq.current;
|
|
34
|
+
try {
|
|
35
|
+
// 发送请求
|
|
36
|
+
let offset = Math.round((_current - 1) * _pageSize);
|
|
37
|
+
if (offset < 0) {
|
|
38
|
+
offset = 0;
|
|
39
|
+
}
|
|
40
|
+
if (_pageSize < 1) {
|
|
41
|
+
_pageSize = 1;
|
|
42
|
+
}
|
|
43
|
+
let { dataSource: data_source, total: _total } = await server({
|
|
44
|
+
limit: _pageSize,
|
|
45
|
+
offset,
|
|
46
|
+
current: _current,
|
|
47
|
+
});
|
|
48
|
+
if (_seq !== seq.current)
|
|
49
|
+
return;
|
|
50
|
+
if (pageSize * (_current - 1) >= _total && _current !== 1) {
|
|
51
|
+
_current = 1;
|
|
52
|
+
const totalPage = Math.ceil(_total / pageSize);
|
|
53
|
+
({ dataSource: data_source, total: _total } = await server({
|
|
54
|
+
limit: _pageSize,
|
|
55
|
+
offset,
|
|
56
|
+
current: _current,
|
|
57
|
+
}));
|
|
58
|
+
if (_seq !== seq.current)
|
|
59
|
+
return;
|
|
60
|
+
_current = totalPage;
|
|
61
|
+
}
|
|
62
|
+
setDataSource(data_source);
|
|
63
|
+
setCurrentAndRef(_current);
|
|
64
|
+
setPageSizeAndRef(_pageSize);
|
|
65
|
+
setTotal(_total);
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
// eslint-disable-next-line no-console
|
|
69
|
+
console.error('fetch err', error);
|
|
70
|
+
if (_seq !== seq.current)
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
setIsFirstComplete(true);
|
|
75
|
+
setLoading(false);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const refresh = async (resetPage) => {
|
|
79
|
+
if (resetPage) {
|
|
80
|
+
setDataSource([]);
|
|
81
|
+
setCurrentAndRef(propCurrent);
|
|
82
|
+
setPageSizeAndRef(propPageSize);
|
|
83
|
+
}
|
|
84
|
+
await doSearch();
|
|
85
|
+
};
|
|
86
|
+
const debounceRefresh = useCallback(debounce(refresh, 500), [refresh]);
|
|
87
|
+
/* 重置逻辑 */
|
|
88
|
+
const _deps = [...(deps || []), isReady];
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
if (!isReady)
|
|
91
|
+
return;
|
|
92
|
+
debounceRefresh(true);
|
|
93
|
+
}, _deps);
|
|
94
|
+
const onChange = async (page, pageSize) => {
|
|
95
|
+
setCurrentAndRef(page);
|
|
96
|
+
setPageSizeAndRef(pageSize);
|
|
97
|
+
await refresh();
|
|
98
|
+
};
|
|
99
|
+
const onShowSizeChange = async (size, current) => {
|
|
100
|
+
setPageSizeAndRef(size);
|
|
101
|
+
setCurrentAndRef(current);
|
|
102
|
+
await refresh();
|
|
103
|
+
};
|
|
104
|
+
const paginationProps = {
|
|
105
|
+
current,
|
|
106
|
+
pageSize,
|
|
107
|
+
total,
|
|
108
|
+
onChange,
|
|
109
|
+
onShowSizeChange,
|
|
110
|
+
};
|
|
111
|
+
return {
|
|
112
|
+
tableProps: {
|
|
113
|
+
loading,
|
|
114
|
+
dataSource,
|
|
115
|
+
},
|
|
116
|
+
loading,
|
|
117
|
+
dataSource,
|
|
118
|
+
paginationProps,
|
|
119
|
+
isFirstComplete,
|
|
120
|
+
refresh,
|
|
121
|
+
debounceRefresh,
|
|
122
|
+
setDataSource,
|
|
123
|
+
};
|
|
124
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from 'react';
|
|
2
|
+
const useReactive = (initialState) => {
|
|
3
|
+
const [state, setState] = useState(initialState);
|
|
4
|
+
const stateRef = useRef(state);
|
|
5
|
+
const updateState = useCallback((newState) => {
|
|
6
|
+
setState(prev => {
|
|
7
|
+
const updatedPart = typeof newState === 'function' ? newState(prev) : newState;
|
|
8
|
+
const newFullState = { ...prev, ...updatedPart };
|
|
9
|
+
stateRef.current = newFullState; // 同步更新ref
|
|
10
|
+
return newFullState;
|
|
11
|
+
});
|
|
12
|
+
}, []);
|
|
13
|
+
const getState = useCallback(() => stateRef.current, []);
|
|
14
|
+
const resetState = useCallback(() => {
|
|
15
|
+
setState(initialState);
|
|
16
|
+
stateRef.current = initialState;
|
|
17
|
+
}, [initialState]);
|
|
18
|
+
return [state, updateState, getState, resetState];
|
|
19
|
+
};
|
|
20
|
+
export { useReactive };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
2
|
+
function useTimeout(callback, delay, immediate = false) {
|
|
3
|
+
const timerRef = useRef(undefined);
|
|
4
|
+
const savedCallback = useRef(callback);
|
|
5
|
+
// Update callback ref if callback changes
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
savedCallback.current = callback;
|
|
8
|
+
}, [callback]);
|
|
9
|
+
const clear = useCallback(() => {
|
|
10
|
+
if (timerRef.current) {
|
|
11
|
+
clearTimeout(timerRef.current);
|
|
12
|
+
timerRef.current = undefined;
|
|
13
|
+
}
|
|
14
|
+
}, []);
|
|
15
|
+
const start = useCallback(() => {
|
|
16
|
+
clear();
|
|
17
|
+
if (delay !== null && delay !== undefined) {
|
|
18
|
+
timerRef.current = setTimeout(() => savedCallback.current(), delay);
|
|
19
|
+
}
|
|
20
|
+
}, [delay, clear]);
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (immediate) {
|
|
23
|
+
savedCallback.current();
|
|
24
|
+
}
|
|
25
|
+
start();
|
|
26
|
+
return clear;
|
|
27
|
+
}, [delay, start, clear, immediate]);
|
|
28
|
+
return { start, clear, isRunning: !!timerRef.current };
|
|
29
|
+
}
|
|
30
|
+
export { useTimeout };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { DependencyList } from 'react';
|
|
2
|
+
export interface IPaginationResult<T> {
|
|
3
|
+
/**
|
|
4
|
+
* 数据
|
|
5
|
+
*
|
|
6
|
+
* @memberof IPaginationResult
|
|
7
|
+
*/
|
|
8
|
+
dataSource: T[];
|
|
9
|
+
/**
|
|
10
|
+
* 加载状态
|
|
11
|
+
*
|
|
12
|
+
* @memberof IPaginationResult
|
|
13
|
+
*/
|
|
14
|
+
loading?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 是否完成了一次请求
|
|
17
|
+
*
|
|
18
|
+
* @memberof IPaginationResult
|
|
19
|
+
*/
|
|
20
|
+
isFirstComplete: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* 分页信息
|
|
23
|
+
*
|
|
24
|
+
* @memberof IPaginationResult
|
|
25
|
+
*/
|
|
26
|
+
paginationProps: {
|
|
27
|
+
current: number;
|
|
28
|
+
pageSize: number;
|
|
29
|
+
total: number;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* reset 是否重置查询
|
|
33
|
+
*
|
|
34
|
+
* @memberof IPaginationResult
|
|
35
|
+
*/
|
|
36
|
+
refresh: (reset?: boolean) => Promise<void>;
|
|
37
|
+
debounceRefresh: (reset?: boolean) => void;
|
|
38
|
+
setDataSource: (data: T[]) => void;
|
|
39
|
+
setTotal: (total: number) => void;
|
|
40
|
+
}
|
|
41
|
+
interface IServerParams {
|
|
42
|
+
offset: number;
|
|
43
|
+
limit: number;
|
|
44
|
+
current: number;
|
|
45
|
+
}
|
|
46
|
+
type IInfoBack<T> = {
|
|
47
|
+
dataSource: T[];
|
|
48
|
+
total: number;
|
|
49
|
+
};
|
|
50
|
+
type IInfoServer<T> = (params: IServerParams) => IInfoBack<T> | Promise<IInfoBack<T>>;
|
|
51
|
+
interface IInfoOption<T> {
|
|
52
|
+
/**
|
|
53
|
+
* 第一次是否执行server
|
|
54
|
+
*
|
|
55
|
+
*/
|
|
56
|
+
isReady?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* 默认的数据
|
|
59
|
+
*
|
|
60
|
+
*/
|
|
61
|
+
dataSource?: T[];
|
|
62
|
+
/**
|
|
63
|
+
* 默认当前第几页
|
|
64
|
+
*
|
|
65
|
+
*/
|
|
66
|
+
current?: number;
|
|
67
|
+
/**
|
|
68
|
+
* 默认一页几条数据
|
|
69
|
+
*
|
|
70
|
+
*/
|
|
71
|
+
pageSize?: number;
|
|
72
|
+
}
|
|
73
|
+
export declare const useVirtualList: <T>(server: IInfoServer<T>, deps?: DependencyList, // 依赖条件 数据更新默认执行server
|
|
74
|
+
option?: IInfoOption<T>) => IPaginationResult<T>;
|
|
75
|
+
export {};
|
|
@@ -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