react-hook-core 0.1.2 → 0.1.3
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/components.js +25 -25
- package/lib/core.js +1 -0
- package/lib/index.js +19 -3
- package/lib/useEdit.js +8 -9
- package/lib/useSearch.js +24 -27
- package/package.json +2 -2
- package/src/components.ts +39 -38
- package/src/core.ts +5 -0
- package/src/index.ts +2 -2
- package/src/useEdit.ts +10 -10
- package/src/useSearch.ts +25 -26
package/lib/components.js
CHANGED
|
@@ -305,15 +305,15 @@ var BaseSearchComponent = (function (_super) {
|
|
|
305
305
|
_this.ui = ui;
|
|
306
306
|
_this.loading = loading;
|
|
307
307
|
_this.listFormId = listFormId;
|
|
308
|
-
_this.initPageSize =
|
|
309
|
-
_this.pageSize =
|
|
308
|
+
_this.initPageSize = 24;
|
|
309
|
+
_this.pageSize = 24;
|
|
310
310
|
_this.pageIndex = 1;
|
|
311
|
-
_this.
|
|
312
|
-
_this.
|
|
311
|
+
_this.total = 0;
|
|
312
|
+
_this.pages = 0;
|
|
313
313
|
_this.sequenceNo = 'sequenceNo';
|
|
314
314
|
_this.tmpPageIndex = 1;
|
|
315
315
|
_this.pageMaxSize = 7;
|
|
316
|
-
_this.pageSizes =
|
|
316
|
+
_this.pageSizes = core_2.pageSizes;
|
|
317
317
|
_this.viewable = true;
|
|
318
318
|
_this.addable = true;
|
|
319
319
|
_this.editable = true;
|
|
@@ -338,7 +338,7 @@ var BaseSearchComponent = (function (_super) {
|
|
|
338
338
|
_this.getFields = _this.getFields.bind(_this);
|
|
339
339
|
_this.pageSizeChanged = _this.pageSizeChanged.bind(_this);
|
|
340
340
|
_this.clearQ = _this.clearQ.bind(_this);
|
|
341
|
-
_this.
|
|
341
|
+
_this.search = _this.search.bind(_this);
|
|
342
342
|
_this.resetAndSearch = _this.resetAndSearch.bind(_this);
|
|
343
343
|
_this.doSearch = _this.doSearch.bind(_this);
|
|
344
344
|
_this.call = _this.call.bind(_this);
|
|
@@ -421,7 +421,7 @@ var BaseSearchComponent = (function (_super) {
|
|
|
421
421
|
});
|
|
422
422
|
}
|
|
423
423
|
};
|
|
424
|
-
BaseSearchComponent.prototype.
|
|
424
|
+
BaseSearchComponent.prototype.search = function (event) {
|
|
425
425
|
if (event) {
|
|
426
426
|
event.preventDefault();
|
|
427
427
|
if (!this.getSearchForm()) {
|
|
@@ -475,12 +475,12 @@ var BaseSearchComponent = (function (_super) {
|
|
|
475
475
|
var results = sr.list;
|
|
476
476
|
if (results && results.length > 0) {
|
|
477
477
|
var lc = state_1.localeOf(undefined, this.getLocale);
|
|
478
|
-
search_core_1.
|
|
478
|
+
search_core_1.formatResults(results, com.pageIndex, com.pageSize, com.pageSize, com.sequenceNo, com.format, lc);
|
|
479
479
|
}
|
|
480
480
|
var am = com.appendMode;
|
|
481
481
|
com.pageIndex = (s.page && s.page >= 1 ? s.page : 1);
|
|
482
482
|
if (sr.total) {
|
|
483
|
-
com.
|
|
483
|
+
com.total = sr.total;
|
|
484
484
|
}
|
|
485
485
|
if (am) {
|
|
486
486
|
var limit = s.limit;
|
|
@@ -558,8 +558,8 @@ var BaseSearchComponent = (function (_super) {
|
|
|
558
558
|
this.doSearch();
|
|
559
559
|
};
|
|
560
560
|
BaseSearchComponent.prototype.pageChanged = function (data) {
|
|
561
|
-
var
|
|
562
|
-
search_core_1.changePage(this,
|
|
561
|
+
var page = data.page, size = data.size;
|
|
562
|
+
search_core_1.changePage(this, page, size);
|
|
563
563
|
this.doSearch();
|
|
564
564
|
};
|
|
565
565
|
return BaseSearchComponent;
|
|
@@ -578,10 +578,10 @@ var SearchComponent = (function (_super) {
|
|
|
578
578
|
if (sv) {
|
|
579
579
|
if (typeof sv === 'function') {
|
|
580
580
|
var x = sv;
|
|
581
|
-
_this.
|
|
581
|
+
_this.service = x;
|
|
582
582
|
}
|
|
583
583
|
else {
|
|
584
|
-
_this.
|
|
584
|
+
_this.service = sv.search;
|
|
585
585
|
if (sv.keys) {
|
|
586
586
|
_this.keys = sv.keys();
|
|
587
587
|
}
|
|
@@ -634,8 +634,8 @@ var SearchComponent = (function (_super) {
|
|
|
634
634
|
delete se['firstLimit'];
|
|
635
635
|
core_1.showLoading(this.loading);
|
|
636
636
|
var com = this;
|
|
637
|
-
if (this.
|
|
638
|
-
this.
|
|
637
|
+
if (this.service) {
|
|
638
|
+
this.service(s, limit, next, fields).then(function (sr) {
|
|
639
639
|
com.showResults(s, sr);
|
|
640
640
|
com.running = undefined;
|
|
641
641
|
core_1.hideLoading(com.loading);
|
|
@@ -663,7 +663,7 @@ var BaseEditComponent = (function (_super) {
|
|
|
663
663
|
_this.backOnSuccess = true;
|
|
664
664
|
_this.patchable = true;
|
|
665
665
|
_this.addable = true;
|
|
666
|
-
_this.
|
|
666
|
+
_this.create = function (event) {
|
|
667
667
|
if (event) {
|
|
668
668
|
event.preventDefault();
|
|
669
669
|
}
|
|
@@ -680,7 +680,7 @@ var BaseEditComponent = (function (_super) {
|
|
|
680
680
|
}, 100);
|
|
681
681
|
}
|
|
682
682
|
};
|
|
683
|
-
_this.
|
|
683
|
+
_this.save = function (event) {
|
|
684
684
|
event.preventDefault();
|
|
685
685
|
event.persist();
|
|
686
686
|
if (!_this.form && event && event.target) {
|
|
@@ -708,11 +708,11 @@ var BaseEditComponent = (function (_super) {
|
|
|
708
708
|
_this.showModel = _this.showModel.bind(_this);
|
|
709
709
|
_this.getModel = _this.getModel.bind(_this);
|
|
710
710
|
_this.createModel = _this.createModel.bind(_this);
|
|
711
|
-
_this.
|
|
712
|
-
_this.
|
|
711
|
+
_this.create = _this.create.bind(_this);
|
|
712
|
+
_this.save = _this.save.bind(_this);
|
|
713
713
|
_this.onSave = _this.onSave.bind(_this);
|
|
714
714
|
_this.validate = _this.validate.bind(_this);
|
|
715
|
-
_this.
|
|
715
|
+
_this.doSave = _this.doSave.bind(_this);
|
|
716
716
|
_this.succeed = _this.succeed.bind(_this);
|
|
717
717
|
_this.fail = _this.fail.bind(_this);
|
|
718
718
|
_this.postSave = _this.postSave.bind(_this);
|
|
@@ -792,7 +792,7 @@ var BaseEditComponent = (function (_super) {
|
|
|
792
792
|
com_2.validate(obj_1, function () {
|
|
793
793
|
var msg = core_2.message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
794
794
|
_this.confirm(msg.message, msg.title, function () {
|
|
795
|
-
com_2.
|
|
795
|
+
com_2.doSave(obj_1, obj_1, isBack);
|
|
796
796
|
}, msg.no, msg.yes);
|
|
797
797
|
});
|
|
798
798
|
}
|
|
@@ -806,7 +806,7 @@ var BaseEditComponent = (function (_super) {
|
|
|
806
806
|
com_2.validate(obj_1, function () {
|
|
807
807
|
var msg = core_2.message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
808
808
|
_this.confirm(msg.message, msg.title, function () {
|
|
809
|
-
com_2.
|
|
809
|
+
com_2.doSave(obj_1, diffObj_1, isBack);
|
|
810
810
|
}, msg.no, msg.yes);
|
|
811
811
|
});
|
|
812
812
|
}
|
|
@@ -824,7 +824,7 @@ var BaseEditComponent = (function (_super) {
|
|
|
824
824
|
callback(obj);
|
|
825
825
|
}
|
|
826
826
|
};
|
|
827
|
-
BaseEditComponent.prototype.
|
|
827
|
+
BaseEditComponent.prototype.doSave = function (obj, dif, isBack) {
|
|
828
828
|
};
|
|
829
829
|
BaseEditComponent.prototype.succeed = function (msg, isBack, result) {
|
|
830
830
|
if (result) {
|
|
@@ -947,7 +947,7 @@ var EditComponent = (function (_super) {
|
|
|
947
947
|
_this.keys = [];
|
|
948
948
|
}
|
|
949
949
|
_this.load = _this.load.bind(_this);
|
|
950
|
-
_this.
|
|
950
|
+
_this.doSave = _this.doSave.bind(_this);
|
|
951
951
|
_this.componentDidMount = _this.componentDidMount.bind(_this);
|
|
952
952
|
_this.ref = React.createRef();
|
|
953
953
|
return _this;
|
|
@@ -1017,7 +1017,7 @@ var EditComponent = (function (_super) {
|
|
|
1017
1017
|
}
|
|
1018
1018
|
}
|
|
1019
1019
|
};
|
|
1020
|
-
EditComponent.prototype.
|
|
1020
|
+
EditComponent.prototype.doSave = function (obj, body, isBack) {
|
|
1021
1021
|
this.running = true;
|
|
1022
1022
|
core_1.showLoading(this.loading);
|
|
1023
1023
|
var isBackO = (isBack == null || isBack === undefined ? this.backOnSuccess : isBack);
|
package/lib/core.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -2,7 +2,23 @@
|
|
|
2
2
|
function __export(m) {
|
|
3
3
|
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
4
4
|
}
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var React = require("react");
|
|
7
|
+
__export(require("./formutil"));
|
|
8
|
+
__export(require("./util"));
|
|
9
|
+
__export(require("./core"));
|
|
10
|
+
__export(require("./state"));
|
|
11
|
+
__export(require("./edit"));
|
|
12
|
+
__export(require("./route"));
|
|
13
|
+
__export(require("./components"));
|
|
14
|
+
__export(require("./diff"));
|
|
15
|
+
__export(require("./router"));
|
|
16
|
+
__export(require("./merge"));
|
|
17
|
+
__export(require("./update"));
|
|
18
|
+
__export(require("./useView"));
|
|
19
|
+
__export(require("./useEdit"));
|
|
20
|
+
__export(require("./useSearch"));
|
|
21
|
+
__export(require("./useMessage"));
|
|
6
22
|
exports.withDefaultProps = function (Component) {
|
|
7
23
|
return function (props) {
|
|
8
24
|
return React.createElement(Component, { props: props, history: props.history });
|
|
@@ -17,9 +33,9 @@ exports.Loading = function (props) {
|
|
|
17
33
|
'boxShadow': 'none'
|
|
18
34
|
};
|
|
19
35
|
if (props.error) {
|
|
20
|
-
return React.createElement(
|
|
36
|
+
return React.createElement('div', null, 'Error Load Module!');
|
|
21
37
|
}
|
|
22
38
|
else {
|
|
23
|
-
return (React.createElement(
|
|
39
|
+
return (React.createElement('div', { className: 'loader-wrapper' }, React.createElement('div', { className: 'loader-sign', style: loadingStyle }, React.createElement('div', { className: 'loader' }))));
|
|
24
40
|
}
|
|
25
41
|
};
|
package/lib/useEdit.js
CHANGED
|
@@ -145,7 +145,7 @@ exports.useCoreEdit = function (props, refForm, initialState, service, p1, p) {
|
|
|
145
145
|
}
|
|
146
146
|
};
|
|
147
147
|
var createModel = (p && p.createModel ? p.createModel : _createModel);
|
|
148
|
-
var
|
|
148
|
+
var create = function (event) {
|
|
149
149
|
event.preventDefault();
|
|
150
150
|
var obj = createModel();
|
|
151
151
|
resetState(true, obj, undefined);
|
|
@@ -184,7 +184,7 @@ exports.useCoreEdit = function (props, refForm, initialState, service, p1, p) {
|
|
|
184
184
|
validate(obj_1, function () {
|
|
185
185
|
var msg = core_1.message(p1.resource.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
186
186
|
p1.confirm(msg.message, msg.title, function () {
|
|
187
|
-
|
|
187
|
+
doSave(obj_1, undefined, version_1, isBack);
|
|
188
188
|
}, msg.no, msg.yes);
|
|
189
189
|
});
|
|
190
190
|
}
|
|
@@ -198,7 +198,7 @@ exports.useCoreEdit = function (props, refForm, initialState, service, p1, p) {
|
|
|
198
198
|
validate(obj_1, function () {
|
|
199
199
|
var msg = core_1.message(p1.resource.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
200
200
|
p1.confirm(msg.message, msg.title, function () {
|
|
201
|
-
|
|
201
|
+
doSave(obj_1, diffObj_1, version_1, isBack);
|
|
202
202
|
}, msg.no, msg.yes);
|
|
203
203
|
});
|
|
204
204
|
}
|
|
@@ -206,7 +206,7 @@ exports.useCoreEdit = function (props, refForm, initialState, service, p1, p) {
|
|
|
206
206
|
}
|
|
207
207
|
};
|
|
208
208
|
var onSave = (p && p.onSave ? p.onSave : _onSave);
|
|
209
|
-
var
|
|
209
|
+
var save = function (event) {
|
|
210
210
|
event.preventDefault();
|
|
211
211
|
event.persist();
|
|
212
212
|
onSave();
|
|
@@ -319,7 +319,7 @@ exports.useCoreEdit = function (props, refForm, initialState, service, p1, p) {
|
|
|
319
319
|
p1.showError(msg.message, msg.title);
|
|
320
320
|
};
|
|
321
321
|
var handleDuplicateKey = (p && p.handleDuplicateKey ? p.handleDuplicateKey : _handleDuplicateKey);
|
|
322
|
-
var
|
|
322
|
+
var _doSave = function (obj, body, version, isBack) {
|
|
323
323
|
setRunning(true);
|
|
324
324
|
core_1.showLoading(p1.loading);
|
|
325
325
|
var isBackO = (isBack == null || isBack === undefined ? true : isBack);
|
|
@@ -336,7 +336,7 @@ exports.useCoreEdit = function (props, refForm, initialState, service, p1, p) {
|
|
|
336
336
|
service.insert(obj).then(function (result) { return postSave(obj, result, version, isBackO); });
|
|
337
337
|
}
|
|
338
338
|
};
|
|
339
|
-
var
|
|
339
|
+
var doSave = (p && p.doSave ? p.doSave : _doSave);
|
|
340
340
|
var _load = function (_id, callback) {
|
|
341
341
|
var id = _id;
|
|
342
342
|
if (id != null && id !== '') {
|
|
@@ -400,8 +400,7 @@ exports.useCoreEdit = function (props, refForm, initialState, service, p1, p) {
|
|
|
400
400
|
getModelName: getModelName,
|
|
401
401
|
resetState: resetState,
|
|
402
402
|
handleNotFound: handleNotFound,
|
|
403
|
-
getModel: getModel, createNewModel: createModel, newOnClick:
|
|
404
|
-
saveOnClick: saveOnClick,
|
|
403
|
+
getModel: getModel, createNewModel: createModel, newOnClick: create, save: save,
|
|
405
404
|
onSave: onSave,
|
|
406
405
|
confirm: confirm,
|
|
407
406
|
validate: validate, showMessage: p1.showMessage, succeed: succeed,
|
|
@@ -409,6 +408,6 @@ exports.useCoreEdit = function (props, refForm, initialState, service, p1, p) {
|
|
|
409
408
|
postSave: postSave,
|
|
410
409
|
handleDuplicateKey: handleDuplicateKey,
|
|
411
410
|
load: load,
|
|
412
|
-
|
|
411
|
+
doSave: doSave
|
|
413
412
|
});
|
|
414
413
|
};
|
package/lib/useSearch.js
CHANGED
|
@@ -30,7 +30,7 @@ exports.callSearch = function (se, search3, showResults3, searchError3, lc, next
|
|
|
30
30
|
}
|
|
31
31
|
var offset;
|
|
32
32
|
if (!se.limit || se.limit <= 0) {
|
|
33
|
-
se.limit =
|
|
33
|
+
se.limit = 24;
|
|
34
34
|
}
|
|
35
35
|
if (se.firstLimit && se.firstLimit > 0) {
|
|
36
36
|
offset = se.limit * (page - 2) + se.firstLimit;
|
|
@@ -56,17 +56,16 @@ var appendListOfState = function (results, list, setState2) {
|
|
|
56
56
|
var setListOfState = function (list, setState2) {
|
|
57
57
|
setState2({ list: list });
|
|
58
58
|
};
|
|
59
|
-
exports.pageSizes = [10, 20, 40, 60, 100, 200, 400, 800];
|
|
60
59
|
function mergeParam(p) {
|
|
61
60
|
if (p) {
|
|
62
61
|
if (!p.sequenceNo) {
|
|
63
62
|
p.sequenceNo = 'sequenceNo';
|
|
64
63
|
}
|
|
65
64
|
if (!p.pageSize) {
|
|
66
|
-
p.pageSize =
|
|
65
|
+
p.pageSize = 24;
|
|
67
66
|
}
|
|
68
67
|
if (!p.pageSizes) {
|
|
69
|
-
p.pageSizes =
|
|
68
|
+
p.pageSizes = core_1.pageSizes;
|
|
70
69
|
}
|
|
71
70
|
if (!p.pageMaxSize || p.pageMaxSize <= 0) {
|
|
72
71
|
p.pageMaxSize = 7;
|
|
@@ -76,15 +75,14 @@ function mergeParam(p) {
|
|
|
76
75
|
else {
|
|
77
76
|
return {
|
|
78
77
|
sequenceNo: 'sequenceNo',
|
|
79
|
-
pageSize:
|
|
80
|
-
pageSizes:
|
|
78
|
+
pageSize: 24,
|
|
79
|
+
pageSizes: core_1.pageSizes,
|
|
81
80
|
pageMaxSize: 7
|
|
82
81
|
};
|
|
83
82
|
}
|
|
84
83
|
}
|
|
85
|
-
exports.useSearch = function (refForm, initialState,
|
|
86
|
-
|
|
87
|
-
var baseProps = exports.useCoreSearch(undefined, refForm, initialState, search, p2, p);
|
|
84
|
+
exports.useSearch = function (refForm, initialState, service, p2, p) {
|
|
85
|
+
var baseProps = exports.useCoreSearch(undefined, refForm, initialState, service, p2, p);
|
|
88
86
|
react_1.useEffect(function () {
|
|
89
87
|
var load = baseProps.load, setState = baseProps.setState, component = baseProps.component;
|
|
90
88
|
if (refForm) {
|
|
@@ -103,12 +101,12 @@ exports.useSearch = function (refForm, initialState, search, p2, p) {
|
|
|
103
101
|
return __assign({}, baseProps);
|
|
104
102
|
};
|
|
105
103
|
exports.useSearchOneProps = function (p) {
|
|
106
|
-
return exports.useSearch(p.refForm, p.initialState, p.
|
|
104
|
+
return exports.useSearch(p.refForm, p.initialState, p.service, p, p);
|
|
107
105
|
};
|
|
108
106
|
exports.useSearchOne = function (p) {
|
|
109
|
-
return exports.useCoreSearch(undefined, p.refForm, p.initialState, p.
|
|
107
|
+
return exports.useCoreSearch(undefined, p.refForm, p.initialState, p.service, p, p);
|
|
110
108
|
};
|
|
111
|
-
exports.useCoreSearch = function (props, refForm, initialState,
|
|
109
|
+
exports.useCoreSearch = function (props, refForm, initialState, service, p1, p2) {
|
|
112
110
|
var p = mergeParam(p2);
|
|
113
111
|
var _a = react_1.useState(), running = _a[0], setRunning = _a[1];
|
|
114
112
|
var _getModelName = function () {
|
|
@@ -156,8 +154,8 @@ exports.useCoreSearch = function (props, refForm, initialState, search, p1, p2)
|
|
|
156
154
|
se = component;
|
|
157
155
|
}
|
|
158
156
|
var keys = p && p.keys ? p.keys : undefined;
|
|
159
|
-
if (!keys && typeof
|
|
160
|
-
keys =
|
|
157
|
+
if (!keys && typeof service !== 'function' && service.keys) {
|
|
158
|
+
keys = service.keys();
|
|
161
159
|
}
|
|
162
160
|
var n = getModelName();
|
|
163
161
|
var fs = p && p.fields;
|
|
@@ -203,11 +201,11 @@ exports.useCoreSearch = function (props, refForm, initialState, search, p1, p2)
|
|
|
203
201
|
search_core_1.addParametersIntoUrl(s, isFirstLoad);
|
|
204
202
|
}
|
|
205
203
|
var lc = p1.getLocale ? p1.getLocale() : state_1.enLocale;
|
|
206
|
-
if (typeof
|
|
207
|
-
exports.callSearch(s,
|
|
204
|
+
if (typeof service === 'function') {
|
|
205
|
+
exports.callSearch(s, service, showResults, searchError, lc, se.nextPageToken);
|
|
208
206
|
}
|
|
209
207
|
else {
|
|
210
|
-
exports.callSearch(s,
|
|
208
|
+
exports.callSearch(s, service.search, showResults, searchError, lc, se.nextPageToken);
|
|
211
209
|
}
|
|
212
210
|
});
|
|
213
211
|
};
|
|
@@ -240,7 +238,7 @@ exports.useCoreSearch = function (props, refForm, initialState, search, p1, p2)
|
|
|
240
238
|
}
|
|
241
239
|
}
|
|
242
240
|
};
|
|
243
|
-
var
|
|
241
|
+
var search = function (event) {
|
|
244
242
|
if (event) {
|
|
245
243
|
event.preventDefault();
|
|
246
244
|
}
|
|
@@ -307,11 +305,12 @@ exports.useCoreSearch = function (props, refForm, initialState, search, p1, p2)
|
|
|
307
305
|
var _showResults = function (s, sr, lc) {
|
|
308
306
|
var results = sr.list;
|
|
309
307
|
if (results && results.length > 0) {
|
|
310
|
-
|
|
308
|
+
debugger;
|
|
309
|
+
search_core_1.formatResults(results, component.pageIndex, component.pageSize, component.pageSize, p ? p.sequenceNo : undefined, p ? p.format : undefined, lc);
|
|
311
310
|
}
|
|
312
311
|
var am = component.appendMode;
|
|
313
312
|
var pi = (s.page && s.page >= 1 ? s.page : 1);
|
|
314
|
-
setComponent({
|
|
313
|
+
setComponent({ total: sr.total, pageIndex: pi, nextPageToken: sr.nextPageToken });
|
|
315
314
|
if (am) {
|
|
316
315
|
var limit = s.limit;
|
|
317
316
|
if ((!s.page || s.page <= 1) && s.firstLimit && s.firstLimit > 0) {
|
|
@@ -353,10 +352,10 @@ exports.useCoreSearch = function (props, refForm, initialState, search, p1, p2)
|
|
|
353
352
|
doSearch(component);
|
|
354
353
|
};
|
|
355
354
|
var pageChanged = function (data) {
|
|
356
|
-
var
|
|
357
|
-
setComponent({ pageIndex:
|
|
358
|
-
component.pageIndex =
|
|
359
|
-
component.pageSize =
|
|
355
|
+
var page = data.page, size = data.size;
|
|
356
|
+
setComponent({ pageIndex: page, pageSize: size, append: false });
|
|
357
|
+
component.pageIndex = page;
|
|
358
|
+
component.pageSize = size;
|
|
360
359
|
component.append = false;
|
|
361
360
|
doSearch(component);
|
|
362
361
|
};
|
|
@@ -366,9 +365,7 @@ exports.useCoreSearch = function (props, refForm, initialState, search, p1, p2)
|
|
|
366
365
|
getCurrencyCode: getCurrencyCode,
|
|
367
366
|
updateDateState: updateDateState, resource: p1.resource.resource(), setComponent: setComponent,
|
|
368
367
|
component: component, showMessage: p1.showMessage, load: load,
|
|
369
|
-
add: add,
|
|
370
|
-
searchOnClick: searchOnClick,
|
|
371
|
-
sort: sort,
|
|
368
|
+
add: add, searchOnClick: search, sort: sort,
|
|
372
369
|
changeView: changeView,
|
|
373
370
|
showMore: showMore,
|
|
374
371
|
toggleFilter: toggleFilter,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-hook-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "react",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"react": "^16.8.0",
|
|
16
16
|
"react-router-dom": "^5.2.0",
|
|
17
17
|
"reflectx": "^0.0.10",
|
|
18
|
-
"search-core": "^0.1.
|
|
18
|
+
"search-core": "^0.1.1",
|
|
19
19
|
"tslint": "^5.10.0",
|
|
20
20
|
"typescript": "^3.3.3333"
|
|
21
21
|
},
|
package/src/components.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import {RouteComponentProps} from 'react-router';
|
|
3
3
|
import {clone, diff, makeDiff} from 'reflectx';
|
|
4
|
-
import {addParametersIntoUrl, append, buildMessage, changePage, changePageSize,
|
|
4
|
+
import {addParametersIntoUrl, append, buildMessage, changePage, changePageSize, formatResults, getFieldsFromForm, getModel, handleAppend, handleSortEvent, initFilter, mergeFilter as mergeFilter2, more, reset, Searchable, showPaging, validate} from 'search-core';
|
|
5
5
|
import {BaseDiffState, createDiffStatus, createEditStatus, DiffApprService, DiffParameter, DiffState, DiffStatusConfig, hideLoading, showLoading} from './core';
|
|
6
|
-
import {Attributes, buildId, EditStatusConfig, error, ErrorMessage, Filter, getCurrencyCode, getModelName as getModelName2, initForm, LoadingService, Locale, message, messageByHttpStatus, removePhoneFormat, ResourceService, SearchParameter, SearchResult, SearchService, SearchState, StringMap, UIService, ViewParameter, ViewService} from './core';
|
|
6
|
+
import {Attributes, buildId, EditStatusConfig, error, ErrorMessage, Filter, getCurrencyCode, getModelName as getModelName2, initForm, LoadingService, Locale, message, messageByHttpStatus, PageChange, pageSizes, removePhoneFormat, ResourceService, SearchParameter, SearchResult, SearchService, SearchState, StringMap, UIService, ViewParameter, ViewService} from './core';
|
|
7
7
|
import {formatDiffModel, getDataFields} from './diff';
|
|
8
8
|
import {build, createModel as createModel2, EditParameter, GenericService, handleStatus, handleVersion, initPropertyNullInModel, ResultInfo} from './edit';
|
|
9
9
|
import {focusFirstError, readOnly} from './formutil';
|
|
@@ -291,7 +291,7 @@ export class MessageComponent<S extends MessageOnlyState, P> extends BaseCompone
|
|
|
291
291
|
this.setState({ message: '' });
|
|
292
292
|
}
|
|
293
293
|
}
|
|
294
|
-
export class BaseSearchComponent<T,
|
|
294
|
+
export class BaseSearchComponent<T, F extends Filter, P, I extends SearchState<T, F>> extends BaseComponent<P, I> implements Searchable {
|
|
295
295
|
constructor(props: P,
|
|
296
296
|
protected resourceService: ResourceService,
|
|
297
297
|
protected showMessage: (msg: string) => void,
|
|
@@ -315,7 +315,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
|
|
|
315
315
|
|
|
316
316
|
this.pageSizeChanged = this.pageSizeChanged.bind(this);
|
|
317
317
|
this.clearQ = this.clearQ.bind(this);
|
|
318
|
-
this.
|
|
318
|
+
this.search = this.search.bind(this);
|
|
319
319
|
|
|
320
320
|
this.resetAndSearch = this.resetAndSearch.bind(this);
|
|
321
321
|
this.doSearch = this.doSearch.bind(this);
|
|
@@ -341,14 +341,14 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
|
|
|
341
341
|
resource: StringMap;
|
|
342
342
|
url: string;
|
|
343
343
|
|
|
344
|
-
filter?:
|
|
344
|
+
filter?: F;
|
|
345
345
|
// Pagination
|
|
346
|
-
initPageSize =
|
|
347
|
-
pageSize =
|
|
346
|
+
initPageSize = 24;
|
|
347
|
+
pageSize = 24;
|
|
348
348
|
pageIndex?: number = 1;
|
|
349
349
|
nextPageToken?: string;
|
|
350
|
-
|
|
351
|
-
|
|
350
|
+
total = 0;
|
|
351
|
+
pages = 0;
|
|
352
352
|
showPaging?: boolean;
|
|
353
353
|
append?: boolean;
|
|
354
354
|
appendMode?: boolean;
|
|
@@ -368,7 +368,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
|
|
|
368
368
|
tmpPageIndex?: number = 1;
|
|
369
369
|
|
|
370
370
|
pageMaxSize = 7;
|
|
371
|
-
pageSizes: number[] =
|
|
371
|
+
pageSizes: number[] = pageSizes;
|
|
372
372
|
|
|
373
373
|
list?: T[];
|
|
374
374
|
excluding?: string[]|number[];
|
|
@@ -391,7 +391,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
|
|
|
391
391
|
toggleFilter(event: any): void {
|
|
392
392
|
this.hideFilter = !this.hideFilter;
|
|
393
393
|
}
|
|
394
|
-
load(s:
|
|
394
|
+
load(s: F, autoSearch: boolean): void {
|
|
395
395
|
const obj2 = initFilter(s, this);
|
|
396
396
|
this.setFilter(obj2);
|
|
397
397
|
const com = this;
|
|
@@ -412,7 +412,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
|
|
|
412
412
|
}
|
|
413
413
|
return this.form;
|
|
414
414
|
}
|
|
415
|
-
setFilter(filter:
|
|
415
|
+
setFilter(filter: F): void {
|
|
416
416
|
const modelName = this.getModelName();
|
|
417
417
|
const objSet: any = {};
|
|
418
418
|
objSet[modelName] = filter;
|
|
@@ -421,7 +421,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
|
|
|
421
421
|
getCurrencyCode(): string|undefined {
|
|
422
422
|
return getCurrencyCode(this.form);
|
|
423
423
|
}
|
|
424
|
-
getFilter():
|
|
424
|
+
getFilter(): F {
|
|
425
425
|
const name = this.getModelName();
|
|
426
426
|
let lc: Locale|undefined;
|
|
427
427
|
if (this.getLocale) {
|
|
@@ -435,7 +435,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
|
|
|
435
435
|
const l = this.getList();
|
|
436
436
|
const f = this.getSearchForm();
|
|
437
437
|
const dc = (this.ui ? this.ui.decodeFromForm : undefined);
|
|
438
|
-
const obj3 = getModel<T,
|
|
438
|
+
const obj3 = getModel<T, F>(this.state, name, this, fields, this.excluding, this.keys, l, f, dc, lc, cc);
|
|
439
439
|
return obj3;
|
|
440
440
|
}
|
|
441
441
|
getFields(): string[]|undefined {
|
|
@@ -464,7 +464,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
|
|
|
464
464
|
});
|
|
465
465
|
}
|
|
466
466
|
}
|
|
467
|
-
|
|
467
|
+
search(event: any): void {
|
|
468
468
|
if (event) {
|
|
469
469
|
event.preventDefault();
|
|
470
470
|
if (!this.getSearchForm()) {
|
|
@@ -508,26 +508,27 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
|
|
|
508
508
|
});
|
|
509
509
|
}
|
|
510
510
|
|
|
511
|
-
call(s:
|
|
511
|
+
call(s: F): void {
|
|
512
512
|
|
|
513
513
|
}
|
|
514
514
|
|
|
515
|
-
validateSearch(se:
|
|
515
|
+
validateSearch(se: F, callback: () => void): void {
|
|
516
516
|
const u = this.ui;
|
|
517
517
|
const vl = (u ? u.validateForm : undefined);
|
|
518
518
|
validate(se, callback, this.getSearchForm(), localeOf(undefined, this.getLocale), vl);
|
|
519
519
|
}
|
|
520
|
-
showResults(s:
|
|
520
|
+
showResults(s: F, sr: SearchResult<T>) {
|
|
521
521
|
const com = this;
|
|
522
522
|
const results = sr.list;
|
|
523
523
|
if (results && results.length > 0) {
|
|
524
524
|
const lc = localeOf(undefined, this.getLocale);
|
|
525
|
-
formatResultsByComponent(results, com, lc);
|
|
525
|
+
// formatResultsByComponent(results, com, lc);
|
|
526
|
+
formatResults(results, com.pageIndex, com.pageSize, com.pageSize, com.sequenceNo, com.format, lc);
|
|
526
527
|
}
|
|
527
528
|
const am = com.appendMode;
|
|
528
529
|
com.pageIndex = (s.page && s.page >= 1 ? s.page : 1);
|
|
529
530
|
if (sr.total) {
|
|
530
|
-
com.
|
|
531
|
+
com.total = sr.total;
|
|
531
532
|
}
|
|
532
533
|
if (am) {
|
|
533
534
|
let limit = s.limit;
|
|
@@ -609,9 +610,9 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
|
|
|
609
610
|
this.doSearch();
|
|
610
611
|
}
|
|
611
612
|
|
|
612
|
-
pageChanged(data:
|
|
613
|
-
const {
|
|
614
|
-
changePage(this,
|
|
613
|
+
pageChanged(data: PageChange) {
|
|
614
|
+
const { page, size } = data;
|
|
615
|
+
changePage(this, page, size);
|
|
615
616
|
this.doSearch();
|
|
616
617
|
}
|
|
617
618
|
}
|
|
@@ -629,9 +630,9 @@ export class SearchComponent<T, S extends Filter, P extends RouteComponentProps,
|
|
|
629
630
|
if (sv) {
|
|
630
631
|
if (typeof sv === 'function') {
|
|
631
632
|
const x: any = sv;
|
|
632
|
-
this.
|
|
633
|
+
this.service = x;
|
|
633
634
|
} else {
|
|
634
|
-
this.
|
|
635
|
+
this.service = sv.search;
|
|
635
636
|
if (sv.keys) {
|
|
636
637
|
this.keys = sv.keys();
|
|
637
638
|
}
|
|
@@ -646,7 +647,7 @@ export class SearchComponent<T, S extends Filter, P extends RouteComponentProps,
|
|
|
646
647
|
this.ref = React.createRef();
|
|
647
648
|
}
|
|
648
649
|
protected showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
|
|
649
|
-
protected
|
|
650
|
+
protected service?: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
|
|
650
651
|
// protected service: SearchService<T, S>;
|
|
651
652
|
protected ref: any;
|
|
652
653
|
protected autoSearch: boolean;
|
|
@@ -692,8 +693,8 @@ export class SearchComponent<T, S extends Filter, P extends RouteComponentProps,
|
|
|
692
693
|
delete se['firstLimit'];
|
|
693
694
|
showLoading(this.loading);
|
|
694
695
|
const com = this;
|
|
695
|
-
if (this.
|
|
696
|
-
this.
|
|
696
|
+
if (this.service) {
|
|
697
|
+
this.service(s, limit, next, fields).then(sr => {
|
|
697
698
|
com.showResults(s, sr);
|
|
698
699
|
com.running = undefined;
|
|
699
700
|
hideLoading(com.loading);
|
|
@@ -743,11 +744,11 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
|
|
|
743
744
|
this.getModel = this.getModel.bind(this);
|
|
744
745
|
this.createModel = this.createModel.bind(this);
|
|
745
746
|
|
|
746
|
-
this.
|
|
747
|
-
this.
|
|
747
|
+
this.create = this.create.bind(this);
|
|
748
|
+
this.save = this.save.bind(this);
|
|
748
749
|
this.onSave = this.onSave.bind(this);
|
|
749
750
|
this.validate = this.validate.bind(this);
|
|
750
|
-
this.
|
|
751
|
+
this.doSave = this.doSave.bind(this);
|
|
751
752
|
this.succeed = this.succeed.bind(this);
|
|
752
753
|
this.fail = this.fail.bind(this);
|
|
753
754
|
this.postSave = this.postSave.bind(this);
|
|
@@ -822,7 +823,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
|
|
|
822
823
|
}
|
|
823
824
|
}
|
|
824
825
|
|
|
825
|
-
|
|
826
|
+
create = (event: any) => {
|
|
826
827
|
if (event) {
|
|
827
828
|
event.preventDefault();
|
|
828
829
|
}
|
|
@@ -839,7 +840,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
|
|
|
839
840
|
}, 100);
|
|
840
841
|
}
|
|
841
842
|
}
|
|
842
|
-
|
|
843
|
+
save = (event: any) => {
|
|
843
844
|
event.preventDefault();
|
|
844
845
|
(event as any).persist();
|
|
845
846
|
if (!this.form && event && event.target) {
|
|
@@ -867,7 +868,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
|
|
|
867
868
|
com.validate(obj, () => {
|
|
868
869
|
const msg = message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
869
870
|
this.confirm(msg.message, msg.title, () => {
|
|
870
|
-
com.
|
|
871
|
+
com.doSave(obj, obj, isBack);
|
|
871
872
|
}, msg.no, msg.yes);
|
|
872
873
|
});
|
|
873
874
|
} else {
|
|
@@ -879,7 +880,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
|
|
|
879
880
|
com.validate(obj, () => {
|
|
880
881
|
const msg = message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
881
882
|
this.confirm(msg.message, msg.title, () => {
|
|
882
|
-
com.
|
|
883
|
+
com.doSave(obj, diffObj, isBack);
|
|
883
884
|
}, msg.no, msg.yes);
|
|
884
885
|
});
|
|
885
886
|
}
|
|
@@ -897,7 +898,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
|
|
|
897
898
|
}
|
|
898
899
|
}
|
|
899
900
|
|
|
900
|
-
|
|
901
|
+
doSave(obj: T, dif?: T, isBack?: boolean) {
|
|
901
902
|
}
|
|
902
903
|
|
|
903
904
|
succeed(msg: string, isBack?: boolean, result?: ResultInfo<T>) {
|
|
@@ -1013,7 +1014,7 @@ export class EditComponent<T, ID, P extends RouteComponentProps, S> extends Base
|
|
|
1013
1014
|
this.keys = [];
|
|
1014
1015
|
}
|
|
1015
1016
|
this.load = this.load.bind(this);
|
|
1016
|
-
this.
|
|
1017
|
+
this.doSave = this.doSave.bind(this);
|
|
1017
1018
|
this.componentDidMount = this.componentDidMount.bind(this);
|
|
1018
1019
|
this.ref = React.createRef();
|
|
1019
1020
|
}
|
|
@@ -1078,7 +1079,7 @@ export class EditComponent<T, ID, P extends RouteComponentProps, S> extends Base
|
|
|
1078
1079
|
}
|
|
1079
1080
|
}
|
|
1080
1081
|
}
|
|
1081
|
-
|
|
1082
|
+
doSave(obj: T, body?: T, isBack?: boolean) {
|
|
1082
1083
|
this.running = true;
|
|
1083
1084
|
showLoading(this.loading);
|
|
1084
1085
|
const isBackO = (isBack == null || isBack === undefined ? this.backOnSuccess : isBack);
|
package/src/core.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import {RouteComponentProps} from 'react-router';
|
|
2
2
|
import {focusFirstElement} from './formutil';
|
|
3
3
|
|
|
4
|
+
export const pageSizes = [12, 24, 60, 100, 120, 180, 300, 600];
|
|
4
5
|
export interface ModelMap {
|
|
5
6
|
[key: string]: any;
|
|
6
7
|
}
|
|
8
|
+
export interface PageChange {
|
|
9
|
+
page: number; // currentPage
|
|
10
|
+
size: number; // itemsPerPage
|
|
11
|
+
}
|
|
7
12
|
export interface ModelProps {
|
|
8
13
|
setGlobalState?: (m: ModelMap) => void;
|
|
9
14
|
shouldBeCustomized?: boolean;
|
package/src/index.ts
CHANGED
|
@@ -32,9 +32,9 @@ export const Loading = (props: LoadingProps) => {
|
|
|
32
32
|
'boxShadow': 'none'
|
|
33
33
|
};
|
|
34
34
|
if (props.error) {
|
|
35
|
-
return React.createElement(
|
|
35
|
+
return React.createElement('div', null, 'Error Load Module!'); // return (<div>Error Load Module!</div>);
|
|
36
36
|
} else {
|
|
37
|
-
return (React.createElement(
|
|
37
|
+
return (React.createElement('div', { className: 'loader-wrapper' }, React.createElement('div', { className: 'loader-sign', style: loadingStyle }, React.createElement('div', { className: 'loader' }))));
|
|
38
38
|
/*
|
|
39
39
|
return (
|
|
40
40
|
<div className='loader-wrapper'>
|
package/src/useEdit.ts
CHANGED
|
@@ -41,7 +41,7 @@ export interface BaseEditComponentParam<T, ID> {
|
|
|
41
41
|
postSave?: (obj: T, res: number|ResultInfo<T>, version?: string, backOnSave?: boolean) => void;
|
|
42
42
|
handleDuplicateKey?: (result?: ResultInfo<T>) => void;
|
|
43
43
|
load?: (i: ID, callback?: (m: T, showM: (m2: T) => void) => void) => void;
|
|
44
|
-
|
|
44
|
+
doSave?: (obj: T, diff?: T, version?: string, isBack?: boolean) => void;
|
|
45
45
|
prepareCustomData?: (data: any) => void; // need to review
|
|
46
46
|
}
|
|
47
47
|
export interface HookBaseEditParameter<T, ID, S> extends BaseEditComponentParam<T, ID> {
|
|
@@ -222,7 +222,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
222
222
|
};
|
|
223
223
|
const createModel = (p && p.createModel ? p.createModel : _createModel);
|
|
224
224
|
|
|
225
|
-
const
|
|
225
|
+
const create = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
226
226
|
event.preventDefault();
|
|
227
227
|
const obj = createModel();
|
|
228
228
|
resetState(true, obj, undefined);
|
|
@@ -260,7 +260,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
260
260
|
validate(obj, () => {
|
|
261
261
|
const msg = message(p1.resource.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
262
262
|
p1.confirm(msg.message, msg.title, () => {
|
|
263
|
-
|
|
263
|
+
doSave(obj, undefined, version, isBack);
|
|
264
264
|
}, msg.no, msg.yes);
|
|
265
265
|
});
|
|
266
266
|
} else {
|
|
@@ -272,7 +272,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
272
272
|
validate(obj, () => {
|
|
273
273
|
const msg = message(p1.resource.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
274
274
|
p1.confirm(msg.message, msg.title, () => {
|
|
275
|
-
|
|
275
|
+
doSave(obj, diffObj, version, isBack);
|
|
276
276
|
}, msg.no, msg.yes);
|
|
277
277
|
});
|
|
278
278
|
}
|
|
@@ -281,7 +281,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
281
281
|
};
|
|
282
282
|
const onSave = (p && p.onSave ? p.onSave : _onSave);
|
|
283
283
|
|
|
284
|
-
const
|
|
284
|
+
const save = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
285
285
|
event.preventDefault();
|
|
286
286
|
event.persist();
|
|
287
287
|
onSave();
|
|
@@ -387,7 +387,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
387
387
|
};
|
|
388
388
|
const handleDuplicateKey = (p && p.handleDuplicateKey ? p.handleDuplicateKey : _handleDuplicateKey);
|
|
389
389
|
|
|
390
|
-
const
|
|
390
|
+
const _doSave = (obj: T, body?: T, version?: string, isBack?: boolean) => {
|
|
391
391
|
setRunning(true);
|
|
392
392
|
showLoading(p1.loading);
|
|
393
393
|
const isBackO = (isBack == null || isBack === undefined ? true : isBack);
|
|
@@ -402,7 +402,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
402
402
|
service.insert(obj).then(result => postSave(obj, result, version, isBackO));
|
|
403
403
|
}
|
|
404
404
|
};
|
|
405
|
-
const
|
|
405
|
+
const doSave = (p && p.doSave ? p.doSave : _doSave);
|
|
406
406
|
|
|
407
407
|
const _load = (_id: ID, callback?: (m: T, showM: (m2: T) => void) => void) => {
|
|
408
408
|
const id: any = _id;
|
|
@@ -469,8 +469,8 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
469
469
|
handleNotFound,
|
|
470
470
|
getModel,
|
|
471
471
|
createNewModel: createModel,
|
|
472
|
-
newOnClick,
|
|
473
|
-
|
|
472
|
+
newOnClick: create,
|
|
473
|
+
save,
|
|
474
474
|
onSave,
|
|
475
475
|
confirm,
|
|
476
476
|
validate,
|
|
@@ -480,6 +480,6 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
480
480
|
postSave,
|
|
481
481
|
handleDuplicateKey,
|
|
482
482
|
load,
|
|
483
|
-
|
|
483
|
+
doSave
|
|
484
484
|
};
|
|
485
485
|
};
|
package/src/useSearch.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {RouteComponentProps} from 'react-router';
|
|
|
3
3
|
import {useHistory, useRouteMatch} from 'react-router-dom';
|
|
4
4
|
import {clone} from 'reflectx';
|
|
5
5
|
import {addParametersIntoUrl, append, buildMessage, Filter, formatResults, getFieldsFromForm, getModel, handleAppend, handleSort, initFilter, mergeFilter as mergeFilter2, Pagination, removeSortStatus, showPaging, Sortable, validate} from 'search-core';
|
|
6
|
-
import {error, getDecodeFromForm, getName, getRemoveError, getValidateForm, hideLoading, initForm, Locale, removeFormError, ResourceService, SearchParameter, SearchResult, SearchService, showLoading} from './core';
|
|
6
|
+
import {error, getDecodeFromForm, getName, getRemoveError, getValidateForm, hideLoading, initForm, Locale, PageChange, pageSizes, removeFormError, ResourceService, SearchParameter, SearchResult, SearchService, showLoading} from './core';
|
|
7
7
|
import {DispatchWithCallback, useMergeState} from './merge';
|
|
8
8
|
import {buildFromUrl} from './route';
|
|
9
9
|
import {enLocale} from './state';
|
|
@@ -24,7 +24,7 @@ export const callSearch = <T, S extends Filter>(se: S, search3: (s: S, limit?: n
|
|
|
24
24
|
}
|
|
25
25
|
let offset: number;
|
|
26
26
|
if (!se.limit || se.limit <= 0) {
|
|
27
|
-
se.limit =
|
|
27
|
+
se.limit = 24;
|
|
28
28
|
}
|
|
29
29
|
if (se.firstLimit && se.firstLimit > 0) {
|
|
30
30
|
offset = se.limit * (page - 2) + se.firstLimit;
|
|
@@ -95,7 +95,7 @@ export interface SearchComponentParam<T, M extends Filter> {
|
|
|
95
95
|
export interface HookBaseSearchParameter<T, S extends Filter, ST extends SearchComponentState<T, S>> extends SearchComponentParam<T, S> {
|
|
96
96
|
refForm: any;
|
|
97
97
|
initialState: ST;
|
|
98
|
-
|
|
98
|
+
service: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>;
|
|
99
99
|
resource: ResourceService;
|
|
100
100
|
showMessage: (msg: string) => void;
|
|
101
101
|
showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
|
|
@@ -131,7 +131,6 @@ export interface SearchComponentState<T, S> extends Pagination, Sortable {
|
|
|
131
131
|
approvable?: boolean;
|
|
132
132
|
deletable?: boolean;
|
|
133
133
|
}
|
|
134
|
-
export const pageSizes = [10, 20, 40, 60, 100, 200, 400, 800];
|
|
135
134
|
|
|
136
135
|
function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>): SearchComponentParam<T, S> {
|
|
137
136
|
if (p) {
|
|
@@ -139,7 +138,7 @@ function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>): Search
|
|
|
139
138
|
p.sequenceNo = 'sequenceNo';
|
|
140
139
|
}
|
|
141
140
|
if (!p.pageSize) {
|
|
142
|
-
p.pageSize =
|
|
141
|
+
p.pageSize = 24;
|
|
143
142
|
}
|
|
144
143
|
if (!p.pageSizes) {
|
|
145
144
|
p.pageSizes = pageSizes;
|
|
@@ -151,7 +150,7 @@ function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>): Search
|
|
|
151
150
|
} else {
|
|
152
151
|
return {
|
|
153
152
|
sequenceNo: 'sequenceNo',
|
|
154
|
-
pageSize:
|
|
153
|
+
pageSize: 24,
|
|
155
154
|
pageSizes,
|
|
156
155
|
pageMaxSize: 7
|
|
157
156
|
};
|
|
@@ -160,12 +159,11 @@ function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>): Search
|
|
|
160
159
|
export const useSearch = <T, S extends Filter, ST extends SearchComponentState<T, S>>(
|
|
161
160
|
refForm: any,
|
|
162
161
|
initialState: ST,
|
|
163
|
-
|
|
162
|
+
service: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
164
163
|
p2: SearchParameter,
|
|
165
164
|
p?: InitSearchComponentParam<T, S, ST>,
|
|
166
165
|
) => {
|
|
167
|
-
|
|
168
|
-
const baseProps = useCoreSearch(undefined, refForm, initialState, search, p2, p);
|
|
166
|
+
const baseProps = useCoreSearch(undefined, refForm, initialState, service, p2, p);
|
|
169
167
|
|
|
170
168
|
useEffect(() => {
|
|
171
169
|
const { load, setState, component } = baseProps;
|
|
@@ -184,16 +182,16 @@ export const useSearch = <T, S extends Filter, ST extends SearchComponentState<T
|
|
|
184
182
|
return { ...baseProps };
|
|
185
183
|
};
|
|
186
184
|
export const useSearchOneProps = <T, S extends Filter, ST extends SearchComponentState<T, S>, P extends RouteComponentProps>(p: HookPropsSearchParameter<T, S, ST, P>) => {
|
|
187
|
-
return useSearch(p.refForm, p.initialState, p.
|
|
185
|
+
return useSearch(p.refForm, p.initialState, p.service, p, p);
|
|
188
186
|
};
|
|
189
187
|
export const useSearchOne = <T, S extends Filter, ST extends SearchComponentState<T, S>>(p: HookBaseSearchParameter<T, S, ST>) => {
|
|
190
|
-
return useCoreSearch(undefined, p.refForm, p.initialState, p.
|
|
188
|
+
return useCoreSearch(undefined, p.refForm, p.initialState, p.service, p, p);
|
|
191
189
|
};
|
|
192
190
|
export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
193
191
|
props: P|undefined,
|
|
194
192
|
refForm: any,
|
|
195
193
|
initialState: ST,
|
|
196
|
-
|
|
194
|
+
service: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
197
195
|
p1: SearchParameter,
|
|
198
196
|
p2?: SearchComponentParam<T, S>
|
|
199
197
|
) => {
|
|
@@ -253,8 +251,8 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
253
251
|
se = component;
|
|
254
252
|
}
|
|
255
253
|
let keys = p && p.keys ? p.keys : undefined;
|
|
256
|
-
if (!keys && typeof
|
|
257
|
-
keys =
|
|
254
|
+
if (!keys && typeof service !== 'function' && service.keys) {
|
|
255
|
+
keys = service.keys();
|
|
258
256
|
}
|
|
259
257
|
const n = getModelName();
|
|
260
258
|
let fs = p && p.fields;
|
|
@@ -303,10 +301,10 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
303
301
|
addParametersIntoUrl(s, isFirstLoad);
|
|
304
302
|
}
|
|
305
303
|
const lc = p1.getLocale ? p1.getLocale() : enLocale;
|
|
306
|
-
if (typeof
|
|
307
|
-
callSearch<T, S>(s,
|
|
304
|
+
if (typeof service === 'function') {
|
|
305
|
+
callSearch<T, S>(s, service, showResults, searchError, lc, se.nextPageToken);
|
|
308
306
|
} else {
|
|
309
|
-
callSearch<T, S>(s,
|
|
307
|
+
callSearch<T, S>(s, service.search, showResults, searchError, lc, se.nextPageToken);
|
|
310
308
|
}
|
|
311
309
|
});
|
|
312
310
|
};
|
|
@@ -343,7 +341,7 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
343
341
|
}
|
|
344
342
|
};
|
|
345
343
|
|
|
346
|
-
const
|
|
344
|
+
const search = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void => {
|
|
347
345
|
if (event) {
|
|
348
346
|
event.preventDefault();
|
|
349
347
|
}
|
|
@@ -411,11 +409,12 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
411
409
|
const _showResults = (s: S, sr: SearchResult<T>, lc: Locale) => {
|
|
412
410
|
const results = sr.list;
|
|
413
411
|
if (results && results.length > 0) {
|
|
414
|
-
|
|
412
|
+
debugger;
|
|
413
|
+
formatResults(results, component.pageIndex, component.pageSize, component.pageSize, p ? p.sequenceNo : undefined, p ? p.format : undefined, lc);
|
|
415
414
|
}
|
|
416
415
|
const am = component.appendMode;
|
|
417
416
|
const pi = (s.page && s.page >= 1 ? s.page : 1);
|
|
418
|
-
setComponent({
|
|
417
|
+
setComponent({ total: sr.total, pageIndex: pi, nextPageToken: sr.nextPageToken });
|
|
419
418
|
if (am) {
|
|
420
419
|
let limit = s.limit;
|
|
421
420
|
if ((!s.page || s.page <= 1) && s.firstLimit && s.firstLimit > 0) {
|
|
@@ -456,11 +455,11 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
456
455
|
doSearch(component);
|
|
457
456
|
};
|
|
458
457
|
|
|
459
|
-
const pageChanged = (data:
|
|
460
|
-
const {
|
|
461
|
-
setComponent({ pageIndex:
|
|
462
|
-
component.pageIndex =
|
|
463
|
-
component.pageSize =
|
|
458
|
+
const pageChanged = (data: PageChange) => {
|
|
459
|
+
const { page, size } = data;
|
|
460
|
+
setComponent({ pageIndex: page, pageSize: size, append: false });
|
|
461
|
+
component.pageIndex = page;
|
|
462
|
+
component.pageSize = size;
|
|
464
463
|
component.append = false;
|
|
465
464
|
doSearch(component);
|
|
466
465
|
};
|
|
@@ -477,7 +476,7 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
477
476
|
showMessage: p1.showMessage,
|
|
478
477
|
load,
|
|
479
478
|
add,
|
|
480
|
-
searchOnClick,
|
|
479
|
+
searchOnClick: search,
|
|
481
480
|
sort,
|
|
482
481
|
changeView,
|
|
483
482
|
showMore,
|