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