kn-hooks 0.0.41 → 0.0.44

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