kn-hooks 0.0.40 → 0.0.41
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 +34 -25
package/package.json
CHANGED
|
@@ -102,7 +102,7 @@ const usePagination=(props)=>{
|
|
|
102
102
|
const DEFAULT_PAGE_CURRENT=1;
|
|
103
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
107
|
const [data,setData] = useState(null);
|
|
108
108
|
const [scrollData,setScrollData] = useState(null);
|
|
@@ -119,7 +119,13 @@ const usePagination=(props)=>{
|
|
|
119
119
|
|
|
120
120
|
const {current,pageSize} = _pagination;
|
|
121
121
|
|
|
122
|
-
let params = {
|
|
122
|
+
let params = {pageSize};
|
|
123
|
+
if(mode==MODE.scrollLoad){
|
|
124
|
+
params.cursor = _pagination.cursor;
|
|
125
|
+
}else{
|
|
126
|
+
params.current= current;
|
|
127
|
+
}
|
|
128
|
+
|
|
123
129
|
let listener=refListener.current;
|
|
124
130
|
|
|
125
131
|
loading.open();
|
|
@@ -153,46 +159,49 @@ const usePagination=(props)=>{
|
|
|
153
159
|
if(debug)console.log('[usePagination] response',req)
|
|
154
160
|
let response={};
|
|
155
161
|
if(req?.code==0){
|
|
156
|
-
let {page:{current,total=0},data:reqData} = req;
|
|
162
|
+
let {page:{current=1,total=0,cursor=undefined},data:reqData} = req;
|
|
157
163
|
let _pageSize=pageSize;
|
|
158
164
|
// 兼容没数据的时候如果pageSize为0会导致接下来刷新的时候也按0的pageSize来写入
|
|
159
165
|
if(req?.page?.pageSize){
|
|
160
166
|
_pageSize=+req?.page?.pageSize;
|
|
161
167
|
}
|
|
162
|
-
current=+current;
|
|
163
168
|
total=+total;
|
|
164
|
-
|
|
169
|
+
let startIdx=0;
|
|
165
170
|
let more;
|
|
166
171
|
if(mode==MODE.pagination){
|
|
172
|
+
current=+current;
|
|
173
|
+
startIdx= (current-1)*_pageSize;
|
|
167
174
|
more = current*_pageSize<total;
|
|
168
175
|
}else{
|
|
169
176
|
more = req.page.more;
|
|
170
177
|
}
|
|
171
178
|
response.pagination={
|
|
172
|
-
current,pageSize:_pageSize,total,startIdx,more
|
|
179
|
+
current,pageSize:_pageSize,total,startIdx,more,cursor
|
|
173
180
|
};
|
|
174
181
|
setPagination(response.pagination);
|
|
175
|
-
response.data= clear?[]:(data||[]);
|
|
176
182
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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];
|
|
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]||[];
|
|
194
189
|
}
|
|
195
190
|
}
|
|
191
|
+
let pageIdx= current-1;
|
|
192
|
+
response.data[pageIdx]= response.data[pageIdx]||[];
|
|
193
|
+
response.data[pageIdx]= reqData||[];
|
|
194
|
+
response.data=[...response.data];
|
|
195
|
+
setData(response.data);
|
|
196
|
+
}
|
|
197
|
+
else if(mode==MODE.scrollLoad){// 滚动加载模式的数据组装
|
|
198
|
+
let _scrollData=clear?[]:(scrollData||[]);
|
|
199
|
+
_scrollData = [..._scrollData,...reqData];
|
|
200
|
+
// for(let data of response.data ){
|
|
201
|
+
// if(data!=null && Array.isArray(data) ){
|
|
202
|
+
// _scrollData = [..._scrollData,...data];
|
|
203
|
+
// }
|
|
204
|
+
// }
|
|
196
205
|
setScrollData(_scrollData)
|
|
197
206
|
}
|
|
198
207
|
}else{
|
|
@@ -211,7 +220,7 @@ const usePagination=(props)=>{
|
|
|
211
220
|
}
|
|
212
221
|
|
|
213
222
|
const reset= ()=>{
|
|
214
|
-
return update({pagination:{current:1},clear:true})
|
|
223
|
+
return update({pagination:{current:1,cursor:''},clear:true})
|
|
215
224
|
}
|
|
216
225
|
|
|
217
226
|
const addListener=(type,fn)=>{
|