kn-hooks 0.0.31 → 0.0.33
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/README.md +1 -0
- package/md/usePagination.md +1 -0
- package/package.json +1 -1
- package/src/useDictionary/index.js +6 -5
- package/src/usePagination/index.js +14 -4
- package/test/usePagination/index.jsx +15 -2
package/README.md
CHANGED
|
@@ -494,6 +494,7 @@ usePagination的返回对象
|
|
|
494
494
|
| next | <code>FunUpdate</code> | 获取下一页数据 |
|
|
495
495
|
| addListener | <code>function</code> | 监听事件 (type='beforeService'|'afterService',fn:FunListener)=>object |
|
|
496
496
|
| removeListener | <code>function</code> | 移除监听事件 (type,fn:FunListener)=>void |
|
|
497
|
+
| loading | <code>object</code> | loading状态,它是一个useSwitch |
|
|
497
498
|
|
|
498
499
|
<a id="module_usepagination__funlistener"></a>
|
|
499
500
|
### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">T</span> FunListener ⇒ <code>Object</code> \| <code>Promise.<Object></code>
|
package/md/usePagination.md
CHANGED
|
@@ -170,6 +170,7 @@ usePagination的返回对象
|
|
|
170
170
|
| next | <code>FunUpdate</code> | 获取下一页数据 |
|
|
171
171
|
| addListener | <code>function</code> | 监听事件 (type='beforeService'|'afterService',fn:FunListener)=>object |
|
|
172
172
|
| removeListener | <code>function</code> | 移除监听事件 (type,fn:FunListener)=>void |
|
|
173
|
+
| loading | <code>object</code> | loading状态,它是一个useSwitch |
|
|
173
174
|
|
|
174
175
|
<a id="module_usepagination__funlistener"></a>
|
|
175
176
|
### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">T</span> FunListener ⇒ <code>Object</code> \| <code>Promise.<Object></code>
|
package/package.json
CHANGED
|
@@ -14,7 +14,7 @@ const DEFAULT_CONFIG={
|
|
|
14
14
|
* [Demo - CodeSandBox]{@link https://codesandbox.io/s/usedictionary-j7cw8?file=/index.js}
|
|
15
15
|
*
|
|
16
16
|
* @param {Object} props
|
|
17
|
-
* @param {Api} props.api - 用于获取字典列表的接口
|
|
17
|
+
* @param {Api} [props.api] - 用于获取字典列表的接口
|
|
18
18
|
* @param {string} [props.idKey='id'] - 字段id的key键值
|
|
19
19
|
* @param {string} [props.nameKey='name'] - 字段name的key键值
|
|
20
20
|
* @param {string} [props.labelKey='label'] - 字段label的key键值
|
|
@@ -200,7 +200,7 @@ export const SetConfig = ({SelectOption,RadioOption})=>{
|
|
|
200
200
|
* @function
|
|
201
201
|
* @description 创建字典hooks工具
|
|
202
202
|
* @param {Object} options
|
|
203
|
-
* @param {Api} options.api - 用于获取字典列表的接口
|
|
203
|
+
* @param {Api} [options.api] - 用于获取字典列表的接口
|
|
204
204
|
* @param {string} [options.idKey='id'] - 字段id的key键值
|
|
205
205
|
* @param {string} [options.nameKey='name'] - 字段name的key键值
|
|
206
206
|
* @param {string} [options.labelKey='label'] - 字段label的key键值
|
|
@@ -356,9 +356,10 @@ export const createDictionary=options=>{
|
|
|
356
356
|
|
|
357
357
|
|
|
358
358
|
/**
|
|
359
|
-
* @
|
|
360
|
-
* @
|
|
361
|
-
* @
|
|
359
|
+
* @template [T=object]
|
|
360
|
+
* @callback Api
|
|
361
|
+
* @param {Object} params - 调用接口用到的参数
|
|
362
|
+
* @returns {Promise<T>}
|
|
362
363
|
*/
|
|
363
364
|
|
|
364
365
|
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* @module usePagination
|
|
4
4
|
*/
|
|
5
5
|
import { useState,useMemo,useRef } from 'react';
|
|
6
|
+
import useSwitch from '../useSwitch';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* 分页管理器
|
|
@@ -99,6 +100,7 @@ const usePagination=(props)=>{
|
|
|
99
100
|
beforeService:props?.beforeService??[],
|
|
100
101
|
afterService:props?.afterService??[]
|
|
101
102
|
});
|
|
103
|
+
const loading = useSwitch();
|
|
102
104
|
|
|
103
105
|
const update= async ({pagination:_pagination,clear=false}={})=>{
|
|
104
106
|
_pagination = _pagination ?? pagination;
|
|
@@ -108,6 +110,8 @@ const usePagination=(props)=>{
|
|
|
108
110
|
|
|
109
111
|
let params = {current,pageSize};
|
|
110
112
|
let listener=refListener.current;
|
|
113
|
+
|
|
114
|
+
loading.open();
|
|
111
115
|
const {beforeService} = listener;
|
|
112
116
|
if(beforeService){
|
|
113
117
|
console.log('[usePagination] beforeService',params)
|
|
@@ -116,10 +120,13 @@ const usePagination=(props)=>{
|
|
|
116
120
|
if(typeof params?.then == 'function'){
|
|
117
121
|
params = await params;
|
|
118
122
|
}
|
|
119
|
-
if(!params){return;}
|
|
123
|
+
if(!params){loading.close();return;}
|
|
120
124
|
}
|
|
121
125
|
}
|
|
122
|
-
if(!params)
|
|
126
|
+
if(!params){
|
|
127
|
+
loading.close();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
123
130
|
console.log('[usePagination] service',params)
|
|
124
131
|
let req = await service(params);
|
|
125
132
|
const {afterService} = listener;
|
|
@@ -160,6 +167,7 @@ const usePagination=(props)=>{
|
|
|
160
167
|
}else{
|
|
161
168
|
setData(data||[]);
|
|
162
169
|
}
|
|
170
|
+
loading.close();
|
|
163
171
|
return response;
|
|
164
172
|
}
|
|
165
173
|
|
|
@@ -207,9 +215,10 @@ const usePagination=(props)=>{
|
|
|
207
215
|
nextPage,
|
|
208
216
|
data,
|
|
209
217
|
addListener,
|
|
210
|
-
removeListener
|
|
218
|
+
removeListener,
|
|
219
|
+
loading
|
|
211
220
|
}
|
|
212
|
-
},[pagination,data,refListener])
|
|
221
|
+
},[pagination,data,refListener,loading])
|
|
213
222
|
|
|
214
223
|
return action;
|
|
215
224
|
}
|
|
@@ -279,6 +288,7 @@ const usePagination=(props)=>{
|
|
|
279
288
|
* @property {FunUpdate} next - 获取下一页数据
|
|
280
289
|
* @property {function} addListener - 监听事件 (type='beforeService'|'afterService',fn:FunListener)=>object
|
|
281
290
|
* @property {function} removeListener - 移除监听事件 (type,fn:FunListener)=>void
|
|
291
|
+
* @property {object} loading - loading状态,它是一个useSwitch
|
|
282
292
|
*/
|
|
283
293
|
|
|
284
294
|
|
|
@@ -17,6 +17,19 @@ const GET_LIST=(params)=>{
|
|
|
17
17
|
data=data.map((item,idx)=>{
|
|
18
18
|
return {id:`第${current}页-${item}`, value:`第${current}页-${item}`,params:others}
|
|
19
19
|
})
|
|
20
|
+
return new Promise(resolve=>{
|
|
21
|
+
setTimeout(()=>{
|
|
22
|
+
resolve({
|
|
23
|
+
code:0,
|
|
24
|
+
data,
|
|
25
|
+
page:{
|
|
26
|
+
current:current,
|
|
27
|
+
pageSize:pageSize,
|
|
28
|
+
total,
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
},2000)
|
|
32
|
+
})
|
|
20
33
|
return Promise.resolve({
|
|
21
34
|
code:0,
|
|
22
35
|
data,
|
|
@@ -94,11 +107,11 @@ const Index=()=>{
|
|
|
94
107
|
<section>
|
|
95
108
|
<Button onClick={onNext} type='primary'>加载下一页</Button>
|
|
96
109
|
<Button onClick={onReset} type='primary'>重置</Button>
|
|
97
|
-
|
|
110
|
+
<span>loading.state={page.loading.state?'1':'0'}, loading.count={page.loading.count}</span>
|
|
98
111
|
|
|
99
112
|
<Table
|
|
100
113
|
rowKey={'id'}
|
|
101
|
-
loading={!page?.data?.length>0}
|
|
114
|
+
loading={!page?.data?.length>0||page.loading.state}
|
|
102
115
|
columns={columns}
|
|
103
116
|
dataSource={getDataSource()}
|
|
104
117
|
pagination={false}
|