react-hook-core 0.4.0 → 0.4.2
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/lib/search.js +54 -52
- package/lib/useSearch.js +21 -21
- package/package.json +1 -1
- package/src/search.ts +75 -74
- package/src/useSearch.ts +22 -23
package/lib/search.js
CHANGED
|
@@ -83,31 +83,31 @@ function initFilter(m, com) {
|
|
|
83
83
|
var page = parseInt(m.page, 10);
|
|
84
84
|
m.page = page;
|
|
85
85
|
if (page >= 1) {
|
|
86
|
-
com.
|
|
86
|
+
com.page = page;
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
if (!isNaN(m.limit)) {
|
|
90
90
|
var pageSize = parseInt(m.limit, 10);
|
|
91
91
|
m.limit = pageSize;
|
|
92
92
|
if (pageSize > 0) {
|
|
93
|
-
com.
|
|
93
|
+
com.limit = pageSize;
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
-
if (!m.limit && com.
|
|
97
|
-
m.limit = com.
|
|
96
|
+
if (!m.limit && com.limit) {
|
|
97
|
+
m.limit = com.limit;
|
|
98
98
|
}
|
|
99
99
|
if (!isNaN(m.firstLimit)) {
|
|
100
100
|
var initPageSize = parseInt(m.firstLimit, 10);
|
|
101
101
|
if (initPageSize > 0) {
|
|
102
102
|
m.firstLimit = initPageSize;
|
|
103
|
-
com.
|
|
103
|
+
com.initLimit = initPageSize;
|
|
104
104
|
}
|
|
105
105
|
else {
|
|
106
|
-
com.
|
|
106
|
+
com.initLimit = com.limit;
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
else {
|
|
110
|
-
com.
|
|
110
|
+
com.initLimit = com.limit;
|
|
111
111
|
}
|
|
112
112
|
var st = m.sort;
|
|
113
113
|
if (st && st.length > 0) {
|
|
@@ -126,11 +126,11 @@ function initFilter(m, com) {
|
|
|
126
126
|
exports.initFilter = initFilter;
|
|
127
127
|
function more(com) {
|
|
128
128
|
com.append = true;
|
|
129
|
-
if (!com.
|
|
130
|
-
com.
|
|
129
|
+
if (!com.page) {
|
|
130
|
+
com.page = 1;
|
|
131
131
|
}
|
|
132
132
|
else {
|
|
133
|
-
com.
|
|
133
|
+
com.page = com.page + 1;
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
exports.more = more;
|
|
@@ -139,32 +139,32 @@ function reset(com) {
|
|
|
139
139
|
com.sortTarget = undefined;
|
|
140
140
|
com.sortField = undefined;
|
|
141
141
|
com.append = false;
|
|
142
|
-
com.
|
|
142
|
+
com.page = 1;
|
|
143
143
|
}
|
|
144
144
|
exports.reset = reset;
|
|
145
145
|
function changePageSize(com, size) {
|
|
146
|
-
com.
|
|
147
|
-
com.
|
|
148
|
-
com.
|
|
146
|
+
com.initLimit = size;
|
|
147
|
+
com.limit = size;
|
|
148
|
+
com.page = 1;
|
|
149
149
|
}
|
|
150
150
|
exports.changePageSize = changePageSize;
|
|
151
151
|
function changePage(com, pageIndex, pageSize) {
|
|
152
|
-
com.
|
|
153
|
-
com.
|
|
152
|
+
com.page = pageIndex;
|
|
153
|
+
com.limit = pageSize;
|
|
154
154
|
com.append = false;
|
|
155
155
|
}
|
|
156
156
|
exports.changePage = changePage;
|
|
157
157
|
function optimizeFilter(obj, searchable, fields) {
|
|
158
158
|
obj.fields = fields;
|
|
159
|
-
if (searchable.
|
|
160
|
-
obj.page = searchable.
|
|
159
|
+
if (searchable.page && searchable.page > 1) {
|
|
160
|
+
obj.page = searchable.page;
|
|
161
161
|
}
|
|
162
162
|
else {
|
|
163
163
|
delete obj.page;
|
|
164
164
|
}
|
|
165
|
-
obj.limit = searchable.
|
|
166
|
-
if (searchable.appendMode && searchable.
|
|
167
|
-
obj.firstLimit = searchable.
|
|
165
|
+
obj.limit = searchable.limit;
|
|
166
|
+
if (searchable.appendMode && searchable.initLimit !== searchable.limit) {
|
|
167
|
+
obj.firstLimit = searchable.initLimit;
|
|
168
168
|
}
|
|
169
169
|
else {
|
|
170
170
|
delete obj.firstLimit;
|
|
@@ -279,10 +279,10 @@ function getFields(form, arr) {
|
|
|
279
279
|
}
|
|
280
280
|
exports.getFields = getFields;
|
|
281
281
|
function formatResultsByComponent(results, c, lc) {
|
|
282
|
-
formatResults(results, c.
|
|
282
|
+
formatResults(results, c.page, c.limit, c.limit, c.sequenceNo, c.format, lc);
|
|
283
283
|
}
|
|
284
284
|
exports.formatResultsByComponent = formatResultsByComponent;
|
|
285
|
-
function formatResults(results,
|
|
285
|
+
function formatResults(results, page, limit, initPageSize, sequenceNo, ft, lc) {
|
|
286
286
|
if (results && results.length > 0) {
|
|
287
287
|
var hasSequencePro = false;
|
|
288
288
|
if (ft) {
|
|
@@ -311,21 +311,21 @@ function formatResults(results, pageIndex, pageSize, initPageSize, sequenceNo, f
|
|
|
311
311
|
}
|
|
312
312
|
}
|
|
313
313
|
if (sequenceNo && sequenceNo.length > 0 && !hasSequencePro) {
|
|
314
|
-
if (!
|
|
315
|
-
|
|
314
|
+
if (!page) {
|
|
315
|
+
page = 1;
|
|
316
316
|
}
|
|
317
|
-
if (
|
|
317
|
+
if (limit) {
|
|
318
318
|
if (!initPageSize) {
|
|
319
|
-
initPageSize =
|
|
319
|
+
initPageSize = limit;
|
|
320
320
|
}
|
|
321
|
-
if (
|
|
321
|
+
if (page <= 1) {
|
|
322
322
|
for (var i = 0; i < results.length; i++) {
|
|
323
|
-
results[i][sequenceNo] = i -
|
|
323
|
+
results[i][sequenceNo] = i - limit + limit * page + 1;
|
|
324
324
|
}
|
|
325
325
|
}
|
|
326
326
|
else {
|
|
327
327
|
for (var i = 0; i < results.length; i++) {
|
|
328
|
-
results[i][sequenceNo] = i -
|
|
328
|
+
results[i][sequenceNo] = i - limit + limit * page + 1 - (limit - initPageSize);
|
|
329
329
|
}
|
|
330
330
|
}
|
|
331
331
|
}
|
|
@@ -378,19 +378,19 @@ function formatText() {
|
|
|
378
378
|
return formatted;
|
|
379
379
|
}
|
|
380
380
|
exports.formatText = formatText;
|
|
381
|
-
function buildMessage(resource, results,
|
|
381
|
+
function buildMessage(resource, results, limit, page, total) {
|
|
382
382
|
if (!results || results.length === 0) {
|
|
383
383
|
return resource.msg_no_data_found;
|
|
384
384
|
}
|
|
385
385
|
else {
|
|
386
|
-
if (!
|
|
387
|
-
|
|
386
|
+
if (!page) {
|
|
387
|
+
page = 1;
|
|
388
388
|
}
|
|
389
|
-
var fromIndex = (
|
|
389
|
+
var fromIndex = (page - 1) * limit + 1;
|
|
390
390
|
var toIndex = fromIndex + results.length - 1;
|
|
391
|
-
var pageTotal = getPageTotal(
|
|
391
|
+
var pageTotal = getPageTotal(limit, total);
|
|
392
392
|
if (pageTotal > 1) {
|
|
393
|
-
var msg2 = formatText(resource.msg_search_result_page_sequence, fromIndex, toIndex, total,
|
|
393
|
+
var msg2 = formatText(resource.msg_search_result_page_sequence, fromIndex, toIndex, total, page, pageTotal);
|
|
394
394
|
return msg2;
|
|
395
395
|
}
|
|
396
396
|
else {
|
|
@@ -410,10 +410,10 @@ function getPrefix(url) {
|
|
|
410
410
|
function addParametersIntoUrl(ft, isFirstLoad, page, fields, limit) {
|
|
411
411
|
if (!isFirstLoad) {
|
|
412
412
|
if (!fields || fields.length === 0) {
|
|
413
|
-
fields =
|
|
413
|
+
fields = "fields";
|
|
414
414
|
}
|
|
415
415
|
if (!limit || limit.length === 0) {
|
|
416
|
-
limit =
|
|
416
|
+
limit = "limit";
|
|
417
417
|
}
|
|
418
418
|
if (page && page > 1) {
|
|
419
419
|
if (!ft.page || ft.page <= 1) {
|
|
@@ -432,7 +432,7 @@ function addParametersIntoUrl(ft, isFirstLoad, page, fields, limit) {
|
|
|
432
432
|
var objValue = ft[key];
|
|
433
433
|
if (objValue) {
|
|
434
434
|
if (key !== fields) {
|
|
435
|
-
if (typeof objValue ===
|
|
435
|
+
if (typeof objValue === "string" || typeof objValue === "number") {
|
|
436
436
|
if (key === limit) {
|
|
437
437
|
if (objValue !== core_1.resources.limit) {
|
|
438
438
|
url += getPrefix(url) + (key + "=" + objValue);
|
|
@@ -442,7 +442,7 @@ function addParametersIntoUrl(ft, isFirstLoad, page, fields, limit) {
|
|
|
442
442
|
url += getPrefix(url) + (key + "=" + objValue);
|
|
443
443
|
}
|
|
444
444
|
}
|
|
445
|
-
else if (typeof objValue ===
|
|
445
|
+
else if (typeof objValue === "object") {
|
|
446
446
|
if (objValue instanceof Date) {
|
|
447
447
|
url += getPrefix(url) + (key + "=" + objValue.toISOString());
|
|
448
448
|
}
|
|
@@ -452,14 +452,14 @@ function addParametersIntoUrl(ft, isFirstLoad, page, fields, limit) {
|
|
|
452
452
|
var strs = [];
|
|
453
453
|
for (var _a = 0, objValue_1 = objValue; _a < objValue_1.length; _a++) {
|
|
454
454
|
var subValue = objValue_1[_a];
|
|
455
|
-
if (typeof subValue ===
|
|
455
|
+
if (typeof subValue === "string") {
|
|
456
456
|
strs.push(subValue);
|
|
457
457
|
}
|
|
458
|
-
else if (typeof subValue ===
|
|
458
|
+
else if (typeof subValue === "number") {
|
|
459
459
|
strs.push(subValue.toString());
|
|
460
460
|
}
|
|
461
461
|
}
|
|
462
|
-
url += getPrefix(url) + (key + "=" + strs.join(
|
|
462
|
+
url += getPrefix(url) + (key + "=" + strs.join(","));
|
|
463
463
|
}
|
|
464
464
|
}
|
|
465
465
|
else {
|
|
@@ -467,11 +467,13 @@ function addParametersIntoUrl(ft, isFirstLoad, page, fields, limit) {
|
|
|
467
467
|
for (var _b = 0, keysLvl2_1 = keysLvl2; _b < keysLvl2_1.length; _b++) {
|
|
468
468
|
var key2 = keysLvl2_1[_b];
|
|
469
469
|
var objValueLvl2 = objValue[key2];
|
|
470
|
-
if (objValueLvl2
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
470
|
+
if (objValueLvl2) {
|
|
471
|
+
if (objValueLvl2 instanceof Date) {
|
|
472
|
+
url += getPrefix(url) + (key + "." + key2 + "=" + objValueLvl2.toISOString());
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
url += getPrefix(url) + (key + "." + key2 + "=" + objValueLvl2);
|
|
476
|
+
}
|
|
475
477
|
}
|
|
476
478
|
}
|
|
477
479
|
}
|
|
@@ -480,15 +482,15 @@ function addParametersIntoUrl(ft, isFirstLoad, page, fields, limit) {
|
|
|
480
482
|
}
|
|
481
483
|
}
|
|
482
484
|
}
|
|
483
|
-
var p =
|
|
485
|
+
var p = "http://";
|
|
484
486
|
var loc = window.location.href;
|
|
485
487
|
if (loc.length >= 8) {
|
|
486
488
|
var ss = loc.substring(0, 8);
|
|
487
|
-
if (ss ===
|
|
488
|
-
p =
|
|
489
|
+
if (ss === "https://") {
|
|
490
|
+
p = "https://";
|
|
489
491
|
}
|
|
490
492
|
}
|
|
491
|
-
window.history.replaceState({ path: currentUrl },
|
|
493
|
+
window.history.replaceState({ path: currentUrl }, "", p + url);
|
|
492
494
|
}
|
|
493
495
|
}
|
|
494
496
|
exports.addParametersIntoUrl = addParametersIntoUrl;
|
package/lib/useSearch.js
CHANGED
|
@@ -50,8 +50,8 @@ function mergeParam(p) {
|
|
|
50
50
|
if (!p.sequenceNo) {
|
|
51
51
|
p.sequenceNo = 'sequenceNo';
|
|
52
52
|
}
|
|
53
|
-
if (!p.
|
|
54
|
-
p.
|
|
53
|
+
if (!p.limit) {
|
|
54
|
+
p.limit = 24;
|
|
55
55
|
}
|
|
56
56
|
if (!p.pageSizes) {
|
|
57
57
|
p.pageSizes = core_1.pageSizes;
|
|
@@ -67,7 +67,7 @@ function mergeParam(p) {
|
|
|
67
67
|
else {
|
|
68
68
|
return {
|
|
69
69
|
sequenceNo: 'sequenceNo',
|
|
70
|
-
|
|
70
|
+
limit: 24,
|
|
71
71
|
pageSizes: core_1.pageSizes,
|
|
72
72
|
pageMaxSize: 7,
|
|
73
73
|
hideFilter: true,
|
|
@@ -202,12 +202,12 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
202
202
|
var validateSearch = p && p.validateSearch ? p.validateSearch : _validateSearch;
|
|
203
203
|
var pageSizeChanged = function (event) {
|
|
204
204
|
var size = parseInt(event.currentTarget.value, 10);
|
|
205
|
-
component.
|
|
206
|
-
component.
|
|
205
|
+
component.limit = size;
|
|
206
|
+
component.page = 1;
|
|
207
207
|
component.tmpPageIndex = 1;
|
|
208
208
|
setComponent({
|
|
209
|
-
|
|
210
|
-
|
|
209
|
+
limit: size,
|
|
210
|
+
page: 1,
|
|
211
211
|
tmpPageIndex: 1
|
|
212
212
|
});
|
|
213
213
|
doSearch(component);
|
|
@@ -269,25 +269,25 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
269
269
|
};
|
|
270
270
|
var resetAndSearch = function () {
|
|
271
271
|
if (running === true) {
|
|
272
|
-
setComponent({
|
|
272
|
+
setComponent({ page: 1, triggerSearch: true });
|
|
273
273
|
return;
|
|
274
274
|
}
|
|
275
|
-
setComponent({
|
|
275
|
+
setComponent({ page: 1, tmpPageIndex: 1 });
|
|
276
276
|
search_1.removeSortStatus(component.sortTarget);
|
|
277
277
|
setComponent({
|
|
278
278
|
sortTarget: undefined,
|
|
279
279
|
sortField: undefined,
|
|
280
280
|
append: false,
|
|
281
|
-
|
|
281
|
+
page: 1
|
|
282
282
|
});
|
|
283
283
|
component.sortTarget = undefined;
|
|
284
284
|
component.sortField = undefined;
|
|
285
285
|
component.append = false;
|
|
286
|
-
component.
|
|
286
|
+
component.page = 1;
|
|
287
287
|
doSearch(component);
|
|
288
288
|
};
|
|
289
289
|
var searchError = function (err) {
|
|
290
|
-
setComponent({
|
|
290
|
+
setComponent({ page: component.tmpPageIndex });
|
|
291
291
|
core_1.error(err, p1.resource.value, p1.showError);
|
|
292
292
|
core_1.hideLoading(p1.loading);
|
|
293
293
|
};
|
|
@@ -299,11 +299,11 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
299
299
|
}
|
|
300
300
|
var results = (sr === null || sr === void 0 ? void 0 : sr.list) || [];
|
|
301
301
|
if (results && results.length > 0) {
|
|
302
|
-
search_1.formatResults(results, component.
|
|
302
|
+
search_1.formatResults(results, component.page, component.limit, component.limit, p ? p.sequenceNo : undefined, p ? p.format : undefined, lc);
|
|
303
303
|
}
|
|
304
304
|
var am = component.appendMode;
|
|
305
305
|
var pi = (s.page && s.page >= 1 ? s.page : 1);
|
|
306
|
-
setComponent({ total: sr.total,
|
|
306
|
+
setComponent({ total: sr.total, page: pi, nextPageToken: sr.next });
|
|
307
307
|
if (am) {
|
|
308
308
|
var limit = s.limit;
|
|
309
309
|
if ((!s.page || s.page <= 1) && s.firstLimit && s.firstLimit > 0) {
|
|
@@ -336,19 +336,19 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
336
336
|
var showResults = (p && p.showResults ? p.showResults : _showResults);
|
|
337
337
|
var showMore = function (event) {
|
|
338
338
|
event.preventDefault();
|
|
339
|
-
var n = component.
|
|
340
|
-
var m = component.
|
|
341
|
-
setComponent({ tmpPageIndex: m,
|
|
339
|
+
var n = component.page ? component.page + 1 : 2;
|
|
340
|
+
var m = component.page;
|
|
341
|
+
setComponent({ tmpPageIndex: m, page: n, append: true });
|
|
342
342
|
component.tmpPageIndex = m;
|
|
343
|
-
component.
|
|
343
|
+
component.page = n;
|
|
344
344
|
component.append = true;
|
|
345
345
|
doSearch(component);
|
|
346
346
|
};
|
|
347
347
|
var pageChanged = function (data) {
|
|
348
348
|
var page = data.page, size = data.size;
|
|
349
|
-
setComponent({
|
|
350
|
-
component.
|
|
351
|
-
component.
|
|
349
|
+
setComponent({ page: page, limit: size, append: false });
|
|
350
|
+
component.page = page;
|
|
351
|
+
component.limit = size;
|
|
352
352
|
component.append = false;
|
|
353
353
|
doSearch(component);
|
|
354
354
|
};
|
package/package.json
CHANGED
package/src/search.ts
CHANGED
|
@@ -8,10 +8,10 @@ export interface Sortable {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export interface Pagination {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
initLimit?: number;
|
|
12
|
+
limit: number;
|
|
13
13
|
// limit: number;
|
|
14
|
-
|
|
14
|
+
page?: number;
|
|
15
15
|
total?: number;
|
|
16
16
|
pages?: number;
|
|
17
17
|
showPaging?: boolean;
|
|
@@ -92,29 +92,29 @@ export function initFilter<S extends Filter>(m: S, com: Searchable): S {
|
|
|
92
92
|
const page = parseInt(m.page as any, 10);
|
|
93
93
|
m.page = page;
|
|
94
94
|
if (page >= 1) {
|
|
95
|
-
com.
|
|
95
|
+
com.page = page;
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
if (!isNaN(m.limit as any)) {
|
|
99
99
|
const pageSize = parseInt(m.limit as any, 10);
|
|
100
100
|
m.limit = pageSize;
|
|
101
101
|
if (pageSize > 0) {
|
|
102
|
-
com.
|
|
102
|
+
com.limit = pageSize;
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
if (!m.limit && com.
|
|
106
|
-
m.limit = com.
|
|
105
|
+
if (!m.limit && com.limit) {
|
|
106
|
+
m.limit = com.limit;
|
|
107
107
|
}
|
|
108
108
|
if (!isNaN(m.firstLimit as any)) {
|
|
109
109
|
const initPageSize = parseInt(m.firstLimit as any, 10);
|
|
110
110
|
if (initPageSize > 0) {
|
|
111
111
|
m.firstLimit = initPageSize;
|
|
112
|
-
com.
|
|
112
|
+
com.initLimit = initPageSize;
|
|
113
113
|
} else {
|
|
114
|
-
com.
|
|
114
|
+
com.initLimit = com.limit;
|
|
115
115
|
}
|
|
116
116
|
} else {
|
|
117
|
-
com.
|
|
117
|
+
com.initLimit = com.limit;
|
|
118
118
|
}
|
|
119
119
|
const st = m.sort;
|
|
120
120
|
if (st && st.length > 0) {
|
|
@@ -136,10 +136,10 @@ export function initFilter<S extends Filter>(m: S, com: Searchable): S {
|
|
|
136
136
|
}
|
|
137
137
|
export function more(com: Pagination): void {
|
|
138
138
|
com.append = true;
|
|
139
|
-
if (!com.
|
|
140
|
-
com.
|
|
139
|
+
if (!com.page) {
|
|
140
|
+
com.page = 1;
|
|
141
141
|
} else {
|
|
142
|
-
com.
|
|
142
|
+
com.page = com.page + 1;
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
|
@@ -148,30 +148,30 @@ export function reset(com: Searchable): void {
|
|
|
148
148
|
com.sortTarget = undefined;
|
|
149
149
|
com.sortField = undefined;
|
|
150
150
|
com.append = false;
|
|
151
|
-
com.
|
|
151
|
+
com.page = 1;
|
|
152
152
|
}
|
|
153
153
|
export function changePageSize(com: Pagination, size: number): void {
|
|
154
|
-
com.
|
|
155
|
-
com.
|
|
156
|
-
com.
|
|
154
|
+
com.initLimit = size;
|
|
155
|
+
com.limit = size;
|
|
156
|
+
com.page = 1;
|
|
157
157
|
}
|
|
158
158
|
export function changePage(com: Pagination, pageIndex: number, pageSize: number): void {
|
|
159
|
-
com.
|
|
160
|
-
com.
|
|
159
|
+
com.page = pageIndex;
|
|
160
|
+
com.limit = pageSize;
|
|
161
161
|
com.append = false;
|
|
162
162
|
}
|
|
163
163
|
export function optimizeFilter<S extends Filter>(obj: S, searchable: Searchable, fields?: string[]): S {
|
|
164
164
|
// const sLimit = searchable.limit;
|
|
165
165
|
obj.fields = fields;
|
|
166
|
-
if (searchable.
|
|
167
|
-
obj.page = searchable.
|
|
166
|
+
if (searchable.page && searchable.page > 1) {
|
|
167
|
+
obj.page = searchable.page;
|
|
168
168
|
} else {
|
|
169
169
|
delete obj.page;
|
|
170
170
|
}
|
|
171
|
-
obj.limit = searchable.
|
|
171
|
+
obj.limit = searchable.limit;
|
|
172
172
|
|
|
173
|
-
if (searchable.appendMode && searchable.
|
|
174
|
-
obj.firstLimit = searchable.
|
|
173
|
+
if (searchable.appendMode && searchable.initLimit !== searchable.limit) {
|
|
174
|
+
obj.firstLimit = searchable.initLimit;
|
|
175
175
|
} else {
|
|
176
176
|
delete obj.firstLimit;
|
|
177
177
|
}
|
|
@@ -292,16 +292,15 @@ export function getFields(form?: HTMLFormElement, arr?: string[]): string[] | un
|
|
|
292
292
|
return fields.length > 0 ? fields : undefined
|
|
293
293
|
}
|
|
294
294
|
interface Component<T> {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
initPageSize?: number;
|
|
295
|
+
page?: number;
|
|
296
|
+
limit?: number;
|
|
298
297
|
sequenceNo?: string;
|
|
299
298
|
format?: (oj: T, lc?: Locale) => T;
|
|
300
299
|
}
|
|
301
300
|
export function formatResultsByComponent<T>(results: T[], c: Component<T>, lc: Locale) {
|
|
302
|
-
formatResults(results, c.
|
|
301
|
+
formatResults(results, c.page, c.limit, c.limit, c.sequenceNo, c.format, lc);
|
|
303
302
|
}
|
|
304
|
-
export function formatResults<T>(results: T[],
|
|
303
|
+
export function formatResults<T>(results: T[], page?: number, limit?: number, initPageSize?: number, sequenceNo?: string, ft?: (oj: T, lc?: Locale) => T, lc?: Locale): void {
|
|
305
304
|
if (results && results.length > 0) {
|
|
306
305
|
let hasSequencePro = false;
|
|
307
306
|
if (ft) {
|
|
@@ -325,20 +324,20 @@ export function formatResults<T>(results: T[], pageIndex?: number, pageSize?: nu
|
|
|
325
324
|
}
|
|
326
325
|
}
|
|
327
326
|
if (sequenceNo && sequenceNo.length > 0 && !hasSequencePro) {
|
|
328
|
-
if (!
|
|
329
|
-
|
|
327
|
+
if (!page) {
|
|
328
|
+
page = 1;
|
|
330
329
|
}
|
|
331
|
-
if (
|
|
330
|
+
if (limit) {
|
|
332
331
|
if (!initPageSize) {
|
|
333
|
-
initPageSize =
|
|
332
|
+
initPageSize = limit;
|
|
334
333
|
}
|
|
335
|
-
if (
|
|
334
|
+
if (page <= 1) {
|
|
336
335
|
for (let i = 0; i < results.length; i++) {
|
|
337
|
-
(results[i] as any)[sequenceNo] = i -
|
|
336
|
+
(results[i] as any)[sequenceNo] = i - limit + limit * page + 1;
|
|
338
337
|
}
|
|
339
338
|
} else {
|
|
340
339
|
for (let i = 0; i < results.length; i++) {
|
|
341
|
-
(results[i] as any)[sequenceNo] = i -
|
|
340
|
+
(results[i] as any)[sequenceNo] = i - limit + limit * page + 1 - (limit - initPageSize);
|
|
342
341
|
}
|
|
343
342
|
}
|
|
344
343
|
} else {
|
|
@@ -383,18 +382,18 @@ export function formatText(...args: any[]): string {
|
|
|
383
382
|
}
|
|
384
383
|
return formatted
|
|
385
384
|
}
|
|
386
|
-
export function buildMessage<T>(resource: StringMap, results: T[],
|
|
385
|
+
export function buildMessage<T>(resource: StringMap, results: T[], limit: number, page: number | undefined, total?: number): string {
|
|
387
386
|
if (!results || results.length === 0) {
|
|
388
387
|
return resource.msg_no_data_found
|
|
389
388
|
} else {
|
|
390
|
-
if (!
|
|
391
|
-
|
|
389
|
+
if (!page) {
|
|
390
|
+
page = 1
|
|
392
391
|
}
|
|
393
|
-
const fromIndex = (
|
|
392
|
+
const fromIndex = (page - 1) * limit + 1
|
|
394
393
|
const toIndex = fromIndex + results.length - 1
|
|
395
|
-
const pageTotal = getPageTotal(
|
|
394
|
+
const pageTotal = getPageTotal(limit, total)
|
|
396
395
|
if (pageTotal > 1) {
|
|
397
|
-
const msg2 = formatText(resource.msg_search_result_page_sequence, fromIndex, toIndex, total,
|
|
396
|
+
const msg2 = formatText(resource.msg_search_result_page_sequence, fromIndex, toIndex, total, page, pageTotal)
|
|
398
397
|
return msg2
|
|
399
398
|
} else {
|
|
400
399
|
const msg3 = formatText(resource.msg_search_result_sequence, fromIndex, toIndex)
|
|
@@ -415,59 +414,61 @@ function getPrefix(url: string): string {
|
|
|
415
414
|
export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: boolean, page?: number, fields?: string, limit?: string): void {
|
|
416
415
|
if (!isFirstLoad) {
|
|
417
416
|
if (!fields || fields.length === 0) {
|
|
418
|
-
fields =
|
|
417
|
+
fields = "fields"
|
|
419
418
|
}
|
|
420
419
|
if (!limit || limit.length === 0) {
|
|
421
|
-
limit =
|
|
420
|
+
limit = "limit"
|
|
422
421
|
}
|
|
423
422
|
if (page && page > 1) {
|
|
424
423
|
if (!ft.page || ft.page <= 1) {
|
|
425
|
-
ft.page = page
|
|
424
|
+
ft.page = page
|
|
426
425
|
}
|
|
427
426
|
}
|
|
428
|
-
const pageIndex = ft.page
|
|
427
|
+
const pageIndex = ft.page
|
|
429
428
|
if (pageIndex && !isNaN(pageIndex) && pageIndex <= 1) {
|
|
430
|
-
delete ft.page
|
|
429
|
+
delete ft.page
|
|
431
430
|
}
|
|
432
|
-
const keys = Object.keys(ft)
|
|
433
|
-
const currentUrl = window.location.host + window.location.pathname
|
|
434
|
-
let url = removeFormatUrl(currentUrl)
|
|
431
|
+
const keys = Object.keys(ft)
|
|
432
|
+
const currentUrl = window.location.host + window.location.pathname
|
|
433
|
+
let url = removeFormatUrl(currentUrl)
|
|
435
434
|
for (const key of keys) {
|
|
436
|
-
const objValue = (ft as any)[key]
|
|
435
|
+
const objValue = (ft as any)[key]
|
|
437
436
|
if (objValue) {
|
|
438
437
|
if (key !== fields) {
|
|
439
|
-
if (typeof objValue ===
|
|
438
|
+
if (typeof objValue === "string" || typeof objValue === "number") {
|
|
440
439
|
if (key === limit) {
|
|
441
440
|
if (objValue !== resources.limit) {
|
|
442
|
-
url += getPrefix(url) + `${key}=${objValue}
|
|
441
|
+
url += getPrefix(url) + `${key}=${objValue}`
|
|
443
442
|
}
|
|
444
443
|
} else {
|
|
445
|
-
url += getPrefix(url) + `${key}=${objValue}
|
|
444
|
+
url += getPrefix(url) + `${key}=${objValue}`
|
|
446
445
|
}
|
|
447
|
-
} else if (typeof objValue ===
|
|
446
|
+
} else if (typeof objValue === "object") {
|
|
448
447
|
if (objValue instanceof Date) {
|
|
449
|
-
url += getPrefix(url) + `${key}=${objValue.toISOString()}
|
|
448
|
+
url += getPrefix(url) + `${key}=${objValue.toISOString()}`
|
|
450
449
|
} else {
|
|
451
450
|
if (Array.isArray(objValue)) {
|
|
452
451
|
if (objValue.length > 0) {
|
|
453
|
-
const strs: string[] = []
|
|
452
|
+
const strs: string[] = []
|
|
454
453
|
for (const subValue of objValue) {
|
|
455
|
-
if (typeof subValue ===
|
|
456
|
-
strs.push(subValue)
|
|
457
|
-
} else if (typeof subValue ===
|
|
458
|
-
strs.push(subValue.toString())
|
|
454
|
+
if (typeof subValue === "string") {
|
|
455
|
+
strs.push(subValue)
|
|
456
|
+
} else if (typeof subValue === "number") {
|
|
457
|
+
strs.push(subValue.toString())
|
|
459
458
|
}
|
|
460
459
|
}
|
|
461
|
-
url += getPrefix(url) + `${key}=${strs.join(
|
|
460
|
+
url += getPrefix(url) + `${key}=${strs.join(",")}`
|
|
462
461
|
}
|
|
463
462
|
} else {
|
|
464
|
-
const keysLvl2 = Object.keys(objValue)
|
|
463
|
+
const keysLvl2 = Object.keys(objValue)
|
|
465
464
|
for (const key2 of keysLvl2) {
|
|
466
|
-
const objValueLvl2 = objValue[key2]
|
|
467
|
-
if (objValueLvl2
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
465
|
+
const objValueLvl2 = objValue[key2]
|
|
466
|
+
if (objValueLvl2) {
|
|
467
|
+
if (objValueLvl2 instanceof Date) {
|
|
468
|
+
url += getPrefix(url) + `${key}.${key2}=${objValueLvl2.toISOString()}`
|
|
469
|
+
} else {
|
|
470
|
+
url += getPrefix(url) + `${key}.${key2}=${objValueLvl2}`
|
|
471
|
+
}
|
|
471
472
|
}
|
|
472
473
|
}
|
|
473
474
|
}
|
|
@@ -476,15 +477,15 @@ export function addParametersIntoUrl<S extends Filter>(ft: S, isFirstLoad?: bool
|
|
|
476
477
|
}
|
|
477
478
|
}
|
|
478
479
|
}
|
|
479
|
-
let p =
|
|
480
|
-
const loc = window.location.href
|
|
480
|
+
let p = "http://"
|
|
481
|
+
const loc = window.location.href
|
|
481
482
|
if (loc.length >= 8) {
|
|
482
|
-
const ss = loc.substring(0, 8)
|
|
483
|
-
if (ss ===
|
|
484
|
-
p =
|
|
483
|
+
const ss = loc.substring(0, 8)
|
|
484
|
+
if (ss === "https://") {
|
|
485
|
+
p = "https://"
|
|
485
486
|
}
|
|
486
487
|
}
|
|
487
|
-
window.history.replaceState({path: currentUrl},
|
|
488
|
+
window.history.replaceState({ path: currentUrl }, "", p + url)
|
|
488
489
|
}
|
|
489
490
|
}
|
|
490
491
|
|
package/src/useSearch.ts
CHANGED
|
@@ -61,8 +61,7 @@ export interface SearchComponentParam<T, M extends Filter> {
|
|
|
61
61
|
appendMode?: boolean;
|
|
62
62
|
pageSizes?: number[];
|
|
63
63
|
pageIndex?: number;
|
|
64
|
-
|
|
65
|
-
initPageSize?: number;
|
|
64
|
+
limit: number;
|
|
66
65
|
pageMaxSize?: number;
|
|
67
66
|
ignoreUrlParam?: boolean;
|
|
68
67
|
|
|
@@ -133,8 +132,8 @@ export function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>):
|
|
|
133
132
|
if (!p.sequenceNo) {
|
|
134
133
|
p.sequenceNo = 'sequenceNo';
|
|
135
134
|
}
|
|
136
|
-
if (!p.
|
|
137
|
-
p.
|
|
135
|
+
if (!p.limit) {
|
|
136
|
+
p.limit = 24;
|
|
138
137
|
}
|
|
139
138
|
if (!p.pageSizes) {
|
|
140
139
|
p.pageSizes = pageSizes;
|
|
@@ -163,7 +162,7 @@ export function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>):
|
|
|
163
162
|
} else {
|
|
164
163
|
return {
|
|
165
164
|
sequenceNo: 'sequenceNo',
|
|
166
|
-
|
|
165
|
+
limit: 24,
|
|
167
166
|
pageSizes,
|
|
168
167
|
pageMaxSize: 7,
|
|
169
168
|
hideFilter: true,
|
|
@@ -329,12 +328,12 @@ export const useCoreSearch = <T, S extends Filter, ST>(
|
|
|
329
328
|
|
|
330
329
|
const pageSizeChanged = (event: any) => {
|
|
331
330
|
const size = parseInt(event.currentTarget.value, 10);
|
|
332
|
-
component.
|
|
333
|
-
component.
|
|
331
|
+
component.limit = size;
|
|
332
|
+
component.page = 1;
|
|
334
333
|
component.tmpPageIndex = 1;
|
|
335
334
|
setComponent({
|
|
336
|
-
|
|
337
|
-
|
|
335
|
+
limit: size,
|
|
336
|
+
page: 1,
|
|
338
337
|
tmpPageIndex: 1
|
|
339
338
|
});
|
|
340
339
|
doSearch(component);
|
|
@@ -398,26 +397,26 @@ export const useCoreSearch = <T, S extends Filter, ST>(
|
|
|
398
397
|
|
|
399
398
|
const resetAndSearch = () => {
|
|
400
399
|
if (running === true) {
|
|
401
|
-
setComponent({
|
|
400
|
+
setComponent({ page: 1, triggerSearch: true });
|
|
402
401
|
return;
|
|
403
402
|
}
|
|
404
|
-
setComponent({
|
|
403
|
+
setComponent({ page: 1, tmpPageIndex: 1 });
|
|
405
404
|
removeSortStatus(component.sortTarget);
|
|
406
405
|
setComponent({
|
|
407
406
|
sortTarget: undefined,
|
|
408
407
|
sortField: undefined,
|
|
409
408
|
append: false,
|
|
410
|
-
|
|
409
|
+
page: 1
|
|
411
410
|
});
|
|
412
411
|
component.sortTarget = undefined;
|
|
413
412
|
component.sortField = undefined;
|
|
414
413
|
component.append = false;
|
|
415
|
-
component.
|
|
414
|
+
component.page = 1;
|
|
416
415
|
doSearch(component);
|
|
417
416
|
};
|
|
418
417
|
|
|
419
418
|
const searchError = (err: any): void => {
|
|
420
|
-
setComponent({
|
|
419
|
+
setComponent({ page: component.tmpPageIndex });
|
|
421
420
|
error(err, p1.resource.value, p1.showError);
|
|
422
421
|
hideLoading(p1.loading)
|
|
423
422
|
};
|
|
@@ -429,11 +428,11 @@ export const useCoreSearch = <T, S extends Filter, ST>(
|
|
|
429
428
|
}
|
|
430
429
|
const results = sr?.list || [];
|
|
431
430
|
if (results && results.length > 0) {
|
|
432
|
-
formatResults(results, component.
|
|
431
|
+
formatResults(results, component.page, component.limit, component.limit, p ? p.sequenceNo : undefined, p ? p.format : undefined, lc);
|
|
433
432
|
}
|
|
434
433
|
const am = component.appendMode;
|
|
435
434
|
const pi = (s.page && s.page >= 1 ? s.page : 1);
|
|
436
|
-
setComponent({ total: sr.total,
|
|
435
|
+
setComponent({ total: sr.total, page: pi, nextPageToken: sr.next });
|
|
437
436
|
if (am) {
|
|
438
437
|
let limit = s.limit;
|
|
439
438
|
if ((!s.page || s.page <= 1) && s.firstLimit && s.firstLimit > 0) {
|
|
@@ -465,20 +464,20 @@ export const useCoreSearch = <T, S extends Filter, ST>(
|
|
|
465
464
|
|
|
466
465
|
const showMore = (event: any) => {
|
|
467
466
|
event.preventDefault();
|
|
468
|
-
const n = component.
|
|
469
|
-
const m = component.
|
|
470
|
-
setComponent({ tmpPageIndex: m,
|
|
467
|
+
const n = component.page ? component.page + 1 : 2;
|
|
468
|
+
const m = component.page;
|
|
469
|
+
setComponent({ tmpPageIndex: m, page: n, append: true });
|
|
471
470
|
component.tmpPageIndex = m;
|
|
472
|
-
component.
|
|
471
|
+
component.page = n;
|
|
473
472
|
component.append = true;
|
|
474
473
|
doSearch(component);
|
|
475
474
|
};
|
|
476
475
|
|
|
477
476
|
const pageChanged = (data: PageChange) => {
|
|
478
477
|
const { page, size } = data;
|
|
479
|
-
setComponent({
|
|
480
|
-
component.
|
|
481
|
-
component.
|
|
478
|
+
setComponent({ page: page, limit: size, append: false });
|
|
479
|
+
component.page = page;
|
|
480
|
+
component.limit = size;
|
|
482
481
|
component.append = false;
|
|
483
482
|
doSearch(component);
|
|
484
483
|
};
|