kn-hooks 0.0.40 → 0.0.43
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 +1 -1
- package/src/usePagination/index.js +149 -119
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
1
|
+
/**
|
|
3
2
|
* @module usePagination
|
|
4
|
-
*/
|
|
5
|
-
import { useState,useMemo,useRef } from 'react';
|
|
3
|
+
*/
|
|
4
|
+
import { useState, useMemo, useRef, useEffect } from 'react';
|
|
6
5
|
import useSwitch from '../useSwitch';
|
|
7
6
|
|
|
8
7
|
/**
|
|
@@ -13,12 +12,13 @@ import useSwitch from '../useSwitch';
|
|
|
13
12
|
* @property {Array<Function>} [afterService] - api调用后监听方法列表
|
|
14
13
|
* @property {string} [mode='pagination'] - pagination普通分页形式, scrollLoad 滚动加载,取值来源usePagination.MODE
|
|
15
14
|
* @property {boolean} [debug=false] - 是否进入调试模式
|
|
15
|
+
* @property {string} [debugName='']
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
const MODE={
|
|
19
|
-
pagination:'pagination',
|
|
20
|
-
scrollLoad:'scrollLoad',
|
|
21
|
-
}
|
|
18
|
+
const MODE = {
|
|
19
|
+
pagination: 'pagination',
|
|
20
|
+
scrollLoad: 'scrollLoad',
|
|
21
|
+
};
|
|
22
22
|
/**
|
|
23
23
|
* 分页管理器
|
|
24
24
|
*
|
|
@@ -94,152 +94,191 @@ const MODE={
|
|
|
94
94
|
*
|
|
95
95
|
* @returns {UsePaginationResult}
|
|
96
96
|
*/
|
|
97
|
-
const usePagination=(props)=>{
|
|
97
|
+
const usePagination = (props) => {
|
|
98
|
+
const { service, mode = MODE.pagination, debug = false,debugName='' } = props;
|
|
99
|
+
const refDestory = useRef(0);
|
|
98
100
|
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
const DEFAULT_PAGE_CURRENT=1;
|
|
103
|
-
const [pagination,setPagination] = useState({
|
|
101
|
+
const DEFAULT_PAGE_SIZE = 20;
|
|
102
|
+
const DEFAULT_PAGE_CURRENT = 1;
|
|
103
|
+
const [pagination, setPagination] = useState({
|
|
104
104
|
current: DEFAULT_PAGE_CURRENT,
|
|
105
|
-
pageSize: props?.pagination?.pageSize??DEFAULT_PAGE_SIZE
|
|
105
|
+
pageSize: props?.pagination?.pageSize ?? DEFAULT_PAGE_SIZE,
|
|
106
106
|
});
|
|
107
|
-
const [data,setData] = useState(null);
|
|
108
|
-
const [scrollData,setScrollData] = useState(null);
|
|
107
|
+
const [data, setData] = useState(null);
|
|
108
|
+
const [scrollData, setScrollData] = useState(null);
|
|
109
109
|
|
|
110
|
-
const refListener= useRef({
|
|
111
|
-
beforeService:props?.beforeService??[],
|
|
112
|
-
afterService:props?.afterService??[]
|
|
110
|
+
const refListener = useRef({
|
|
111
|
+
beforeService: props?.beforeService ?? [],
|
|
112
|
+
afterService: props?.afterService ?? [],
|
|
113
113
|
});
|
|
114
114
|
const loading = useSwitch();
|
|
115
115
|
|
|
116
|
-
const update= async ({pagination:_pagination,clear=false}={})=>{
|
|
116
|
+
const update = async ({ pagination: _pagination, clear = false } = {}) => {
|
|
117
117
|
_pagination = _pagination ?? pagination;
|
|
118
|
-
_pagination = {...pagination
|
|
118
|
+
_pagination = { ...pagination, ..._pagination };
|
|
119
|
+
|
|
120
|
+
const { current, pageSize } = _pagination;
|
|
119
121
|
|
|
120
|
-
|
|
122
|
+
let params = { pageSize };
|
|
123
|
+
if (mode == MODE.scrollLoad) {
|
|
124
|
+
params.cursor = _pagination.cursor;
|
|
125
|
+
} else {
|
|
126
|
+
params.current = current;
|
|
127
|
+
}
|
|
121
128
|
|
|
122
|
-
let
|
|
123
|
-
let listener=refListener.current;
|
|
129
|
+
let listener = refListener.current;
|
|
124
130
|
|
|
125
131
|
loading.open();
|
|
126
|
-
const {beforeService} = listener;
|
|
127
|
-
if(beforeService){
|
|
128
|
-
if(debug)console.log('[usePagination] beforeService',params)
|
|
129
|
-
for(let i=0;i<beforeService.length;i++){
|
|
132
|
+
const { beforeService } = listener;
|
|
133
|
+
if (beforeService) {
|
|
134
|
+
if (debug) console.log('[usePagination] beforeService', params);
|
|
135
|
+
for (let i = 0; i < beforeService.length; i++) {
|
|
130
136
|
params = beforeService[i](params);
|
|
131
|
-
if(typeof params?.then == 'function'){
|
|
137
|
+
if (typeof params?.then == 'function') {
|
|
132
138
|
params = await params;
|
|
133
139
|
}
|
|
134
|
-
if(!params){
|
|
140
|
+
if (!params) {
|
|
141
|
+
loading.close();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
135
144
|
}
|
|
136
145
|
}
|
|
137
|
-
if(!params){
|
|
146
|
+
if (!params) {
|
|
138
147
|
loading.close();
|
|
139
148
|
return;
|
|
140
149
|
}
|
|
141
|
-
|
|
150
|
+
|
|
151
|
+
if(refDestory.current){return;}
|
|
152
|
+
if (debug) console.log('[usePagination] service', params,debugName);
|
|
142
153
|
let req = await service(params);
|
|
143
|
-
|
|
144
|
-
if(
|
|
145
|
-
|
|
146
|
-
|
|
154
|
+
|
|
155
|
+
if(refDestory.current){return;}
|
|
156
|
+
const { afterService } = listener;
|
|
157
|
+
if (afterService) {
|
|
158
|
+
if (debug) console.log('[usePagination] afterService', req,debugName);
|
|
159
|
+
for (let i = 0; i < afterService.length; i++) {
|
|
147
160
|
req = afterService[i](req);
|
|
148
|
-
if(
|
|
161
|
+
if (typeof req?.then == 'function') {
|
|
149
162
|
req = await req;
|
|
150
163
|
}
|
|
151
164
|
}
|
|
152
165
|
}
|
|
153
|
-
if(debug)console.log('[usePagination] response',req)
|
|
154
|
-
let response={};
|
|
155
|
-
if(req?.code==0){
|
|
156
|
-
let {
|
|
157
|
-
|
|
166
|
+
if (debug) console.log('[usePagination] response', req,debugName);
|
|
167
|
+
let response = {};
|
|
168
|
+
if (req?.code == 0) {
|
|
169
|
+
let {
|
|
170
|
+
page: { current = 1, total = 0, cursor = undefined },
|
|
171
|
+
data: reqData,
|
|
172
|
+
} = req;
|
|
173
|
+
let _pageSize = pageSize;
|
|
158
174
|
// 兼容没数据的时候如果pageSize为0会导致接下来刷新的时候也按0的pageSize来写入
|
|
159
|
-
if(req?.page?.pageSize){
|
|
160
|
-
_pageSize
|
|
175
|
+
if (req?.page?.pageSize) {
|
|
176
|
+
_pageSize = +req?.page?.pageSize;
|
|
161
177
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const startIdx= (current-1)*_pageSize;
|
|
178
|
+
total = +total;
|
|
179
|
+
let startIdx = 0;
|
|
165
180
|
let more;
|
|
166
|
-
if(mode==MODE.pagination){
|
|
167
|
-
|
|
168
|
-
|
|
181
|
+
if (mode == MODE.pagination) {
|
|
182
|
+
current = +current;
|
|
183
|
+
startIdx = (current - 1) * _pageSize;
|
|
184
|
+
more = current * _pageSize < total;
|
|
185
|
+
} else {
|
|
169
186
|
more = req.page.more;
|
|
170
187
|
}
|
|
171
|
-
response.pagination={
|
|
172
|
-
current,
|
|
188
|
+
response.pagination = {
|
|
189
|
+
current,
|
|
190
|
+
pageSize: _pageSize,
|
|
191
|
+
total,
|
|
192
|
+
startIdx,
|
|
193
|
+
more,
|
|
194
|
+
cursor,
|
|
173
195
|
};
|
|
174
196
|
setPagination(response.pagination);
|
|
175
|
-
response.data= clear?[]:(data||[]);
|
|
176
197
|
|
|
177
|
-
if(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
let pageIdx= current-1;
|
|
183
|
-
response.data[pageIdx]= response.data[pageIdx]||[];
|
|
184
|
-
response.data[pageIdx]= reqData||[];
|
|
185
|
-
response.data=[...response.data];
|
|
186
|
-
setData(response.data);
|
|
187
|
-
|
|
188
|
-
// 滚动加载模式的数据组装
|
|
189
|
-
if(mode==MODE.scrollLoad){
|
|
190
|
-
let _scrollData=[];
|
|
191
|
-
for(let data of response.data ){
|
|
192
|
-
if(data!=null && Array.isArray(data) ){
|
|
193
|
-
_scrollData = [..._scrollData,...data];
|
|
198
|
+
if (mode == MODE.pagination) {
|
|
199
|
+
response.data = clear ? [] : data || [];
|
|
200
|
+
if (response.data.length < current) {
|
|
201
|
+
for (let i = 0; i < current; i++) {
|
|
202
|
+
response.data[i] = response.data[i] || [];
|
|
194
203
|
}
|
|
195
204
|
}
|
|
196
|
-
|
|
205
|
+
let pageIdx = current - 1;
|
|
206
|
+
response.data[pageIdx] = response.data[pageIdx] || [];
|
|
207
|
+
response.data[pageIdx] = reqData || [];
|
|
208
|
+
response.data = [...response.data];
|
|
209
|
+
setData(response.data);
|
|
210
|
+
} else if (mode == MODE.scrollLoad) {
|
|
211
|
+
// 滚动加载模式的数据组装
|
|
212
|
+
let _scrollData = clear ? [] : scrollData || [];
|
|
213
|
+
_scrollData = [..._scrollData, ...reqData];
|
|
214
|
+
// for(let data of response.data ){
|
|
215
|
+
// if(data!=null && Array.isArray(data) ){
|
|
216
|
+
// _scrollData = [..._scrollData,...data];
|
|
217
|
+
// }
|
|
218
|
+
// }
|
|
219
|
+
setScrollData(_scrollData);
|
|
197
220
|
}
|
|
198
|
-
}else{
|
|
199
|
-
setScrollData(scrollData||[]);
|
|
200
|
-
setData(data||[]);
|
|
221
|
+
} else {
|
|
222
|
+
setScrollData(scrollData || []);
|
|
223
|
+
setData(data || []);
|
|
201
224
|
}
|
|
202
225
|
loading.close();
|
|
203
226
|
return response;
|
|
204
|
-
}
|
|
227
|
+
};
|
|
205
228
|
|
|
206
|
-
const nextPage= async ()=>{
|
|
207
|
-
if(!pagination.more){
|
|
229
|
+
const nextPage = async () => {
|
|
230
|
+
if (!pagination.more) {
|
|
208
231
|
return false;
|
|
209
232
|
}
|
|
210
|
-
return update({pagination:{current:pagination.current+1}})
|
|
211
|
-
}
|
|
233
|
+
return update({ pagination: { current: pagination.current + 1 } });
|
|
234
|
+
};
|
|
212
235
|
|
|
213
|
-
const reset= ()=>{
|
|
214
|
-
return update({pagination:{current:1},clear:true})
|
|
215
|
-
}
|
|
236
|
+
const reset = () => {
|
|
237
|
+
return update({ pagination: { current: 1, cursor: '' }, clear: true });
|
|
238
|
+
};
|
|
216
239
|
|
|
217
|
-
const addListener=(type,fn)=>{
|
|
218
|
-
if(!refListener.current[type]){
|
|
219
|
-
refListener.current[type]=[];
|
|
240
|
+
const addListener = (type, fn) => {
|
|
241
|
+
if (!refListener.current[type]) {
|
|
242
|
+
refListener.current[type] = [];
|
|
220
243
|
}
|
|
221
|
-
|
|
222
|
-
const repeat= refListener.current[type].some(callback=>{
|
|
223
|
-
if(callback==fn){
|
|
244
|
+
|
|
245
|
+
const repeat = refListener.current[type].some((callback) => {
|
|
246
|
+
if (callback == fn) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
224
249
|
return false;
|
|
225
|
-
})
|
|
226
|
-
if(repeat){
|
|
250
|
+
});
|
|
251
|
+
if (repeat) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
227
254
|
refListener.current[type].push(fn);
|
|
228
|
-
}
|
|
255
|
+
};
|
|
229
256
|
|
|
230
|
-
const removeListener=(type,fn)=>{
|
|
231
|
-
if(!refListener.current[type]){
|
|
232
|
-
refListener.current[type]=[];
|
|
257
|
+
const removeListener = (type, fn) => {
|
|
258
|
+
if (!refListener.current[type]) {
|
|
259
|
+
refListener.current[type] = [];
|
|
233
260
|
}
|
|
234
261
|
let list = refListener.current[type];
|
|
235
|
-
for(let i=0;i<list.length;i++){
|
|
236
|
-
if(list[i] == fn){
|
|
237
|
-
list.splice(i,1);
|
|
262
|
+
for (let i = 0; i < list.length; i++) {
|
|
263
|
+
if (list[i] == fn) {
|
|
264
|
+
list.splice(i, 1);
|
|
238
265
|
}
|
|
239
266
|
}
|
|
240
|
-
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
useEffect(() => {
|
|
270
|
+
if (props.debug) {
|
|
271
|
+
console.log(`[usePagination]新建:`, debugName || props);
|
|
272
|
+
}
|
|
273
|
+
return () => {
|
|
274
|
+
refDestory.current = 1;
|
|
275
|
+
if (props.debug) {
|
|
276
|
+
console.log(`[usePagination]销毁:`, debugName || props);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
}, []);
|
|
241
280
|
|
|
242
|
-
const action= useMemo(()=>{
|
|
281
|
+
const action = useMemo(() => {
|
|
243
282
|
return {
|
|
244
283
|
pagination,
|
|
245
284
|
update,
|
|
@@ -250,14 +289,13 @@ const usePagination=(props)=>{
|
|
|
250
289
|
removeListener,
|
|
251
290
|
loading,
|
|
252
291
|
mode,
|
|
253
|
-
scrollData
|
|
254
|
-
}
|
|
255
|
-
},[pagination,data,refListener,loading,mode,scrollData])
|
|
292
|
+
scrollData,
|
|
293
|
+
};
|
|
294
|
+
}, [pagination, data, refListener, loading, mode, scrollData]);
|
|
256
295
|
|
|
257
296
|
return action;
|
|
258
|
-
}
|
|
259
|
-
usePagination.MODE=MODE;
|
|
260
|
-
|
|
297
|
+
};
|
|
298
|
+
usePagination.MODE = MODE;
|
|
261
299
|
|
|
262
300
|
/**
|
|
263
301
|
* 分页接口请求结果
|
|
@@ -268,12 +306,11 @@ usePagination.MODE=MODE;
|
|
|
268
306
|
* @property {number} page.current - 页码
|
|
269
307
|
* @property {number} page.pageSize - 分页大小
|
|
270
308
|
* @property {number} page.total - 数据总量
|
|
271
|
-
*
|
|
309
|
+
*
|
|
272
310
|
*/
|
|
273
311
|
|
|
274
|
-
|
|
275
312
|
/**
|
|
276
|
-
* @function
|
|
313
|
+
* @function
|
|
277
314
|
* @name FunServices
|
|
278
315
|
* @description 分页请求接口格式要求
|
|
279
316
|
* @property {number} current=1 - 页码
|
|
@@ -282,7 +319,6 @@ usePagination.MODE=MODE;
|
|
|
282
319
|
* @return {FunServicesCallback}
|
|
283
320
|
*/
|
|
284
321
|
|
|
285
|
-
|
|
286
322
|
/**
|
|
287
323
|
* 分页信息
|
|
288
324
|
* @typedef {Object} Pagination
|
|
@@ -291,10 +327,9 @@ usePagination.MODE=MODE;
|
|
|
291
327
|
* @property {number} [total=1] - 总记录数
|
|
292
328
|
* @property {number} [startIdx=0] - 当前页面下第一条数据的序号
|
|
293
329
|
* @property {boolean} [more] - 是否还有下一页
|
|
294
|
-
*
|
|
330
|
+
*
|
|
295
331
|
*/
|
|
296
332
|
|
|
297
|
-
|
|
298
333
|
/**
|
|
299
334
|
* 分页数据结果
|
|
300
335
|
* @typedef {Object} PageDataResult
|
|
@@ -302,17 +337,14 @@ usePagination.MODE=MODE;
|
|
|
302
337
|
* @property {Object[][]} data - 分页数据集合
|
|
303
338
|
*/
|
|
304
339
|
|
|
305
|
-
|
|
306
|
-
|
|
307
340
|
/**
|
|
308
341
|
* 分页查询结果
|
|
309
|
-
* @typedef {callback} FunUpdate
|
|
342
|
+
* @typedef {callback} FunUpdate
|
|
310
343
|
* @property {Pagination} [pagination] - 最新分页信息
|
|
311
344
|
* @property {boolean} [clear] - 是否清空数据
|
|
312
345
|
* @returns {Promise<PageDataResult>} 最新的分页数据结果
|
|
313
346
|
*/
|
|
314
347
|
|
|
315
|
-
|
|
316
348
|
/**
|
|
317
349
|
* usePagination的返回对象
|
|
318
350
|
* @typedef {Object} UsePaginationResult
|
|
@@ -325,10 +357,9 @@ usePagination.MODE=MODE;
|
|
|
325
357
|
* @property {object} loading - loading状态,它是一个useSwitch
|
|
326
358
|
* @property {string} [mode='pagination'] - pagination普通分页形式, scrollLoad 滚动加载
|
|
327
359
|
* @property {Object[]} scrollData - 滚动加载模式下的数据集合
|
|
328
|
-
*
|
|
360
|
+
*
|
|
329
361
|
*/
|
|
330
362
|
|
|
331
|
-
|
|
332
363
|
/**
|
|
333
364
|
* 事件监听方法
|
|
334
365
|
* @typedef {callback} FunListener
|
|
@@ -336,5 +367,4 @@ usePagination.MODE=MODE;
|
|
|
336
367
|
* @returns {Object|Promise<Object>} 处理完毕的数据对象或者Promise
|
|
337
368
|
*/
|
|
338
369
|
|
|
339
|
-
|
|
340
370
|
export default usePagination;
|