react-hook-core 0.2.0 → 0.2.1
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 +46 -62
- package/lib/formutil.js +36 -22
- package/lib/useEdit.js +423 -425
- package/lib/useView.js +107 -107
- package/package.json +1 -1
- package/src/components.ts +29 -39
- package/src/formutil.ts +31 -20
- package/src/useEdit.ts +1 -1
- package/src/useView.ts +3 -3
package/lib/useView.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
14
|
var react_1 = require("react");
|
|
@@ -17,110 +17,110 @@ var core_1 = require("./core");
|
|
|
17
17
|
var formutil_1 = require("./formutil");
|
|
18
18
|
var merge_1 = require("./merge");
|
|
19
19
|
exports.useViewOne = function (p) {
|
|
20
|
-
|
|
20
|
+
return exports.useCoreView(p.refForm, p.initialState, p.service, p, p);
|
|
21
21
|
};
|
|
22
22
|
exports.useView = function (refForm, initialState, service, p1, p) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
23
|
+
var baseProps = exports.useCoreView(refForm, initialState, service, p1, p);
|
|
24
|
+
var _a = merge_1.useMergeState(initialState), setState = _a[1];
|
|
25
|
+
var params = react_router_1.useParams();
|
|
26
|
+
react_1.useEffect(function () {
|
|
27
|
+
if (baseProps.refForm) {
|
|
28
|
+
core_1.initForm(baseProps.refForm.current);
|
|
29
|
+
}
|
|
30
|
+
var id = core_1.buildId(params, p ? p.keys : undefined);
|
|
31
|
+
if (id) {
|
|
32
|
+
if (p && p.initialize) {
|
|
33
|
+
p.initialize(id, baseProps.load, setState, p.callback);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
baseProps.load(id, p ? p.callback : undefined);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}, []);
|
|
40
|
+
return __assign({}, baseProps);
|
|
41
41
|
};
|
|
42
42
|
exports.useCoreView = function (refForm, initialState, service, p1, p) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
navigate(-1);
|
|
51
|
-
};
|
|
52
|
-
var getModelName = function (f) {
|
|
53
|
-
if (p && p.name) {
|
|
54
|
-
return p.name;
|
|
55
|
-
}
|
|
56
|
-
return core_1.getModelName(f, 'model');
|
|
57
|
-
};
|
|
58
|
-
var showModel = function (model) {
|
|
59
|
-
var n = getModelName(refForm.current);
|
|
60
|
-
var objSet = {};
|
|
61
|
-
objSet[n] = model;
|
|
62
|
-
setState(objSet);
|
|
63
|
-
};
|
|
64
|
-
var _handleNotFound = function (form) {
|
|
65
|
-
var msg = core_1.message(p1.resource.value, 'error_not_found', 'error');
|
|
66
|
-
if (form) {
|
|
67
|
-
formutil_1.readOnly(form);
|
|
68
|
-
}
|
|
69
|
-
p1.showError(msg.message, msg.title);
|
|
70
|
-
};
|
|
71
|
-
var handleNotFound = (p && p.handleNotFound ? p.handleNotFound : _handleNotFound);
|
|
72
|
-
var _load = function (_id, callback) {
|
|
73
|
-
var id = _id;
|
|
74
|
-
if (id != null && id !== '') {
|
|
75
|
-
setRunning(true);
|
|
76
|
-
core_1.showLoading(p1.loading);
|
|
77
|
-
var fn = (typeof service === 'function' ? service : service.load);
|
|
78
|
-
fn(id).then(function (obj) {
|
|
79
|
-
if (!obj) {
|
|
80
|
-
handleNotFound(refForm.current);
|
|
43
|
+
var _a = merge_1.useMergeState(initialState), state = _a[0], setState = _a[1];
|
|
44
|
+
var _b = react_1.useState(), running = _b[0], setRunning = _b[1];
|
|
45
|
+
var navigate = react_router_1.useNavigate();
|
|
46
|
+
var back = function (event) {
|
|
47
|
+
if (event) {
|
|
48
|
+
event.preventDefault();
|
|
81
49
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
showModel(obj);
|
|
88
|
-
}
|
|
50
|
+
navigate(-1);
|
|
51
|
+
};
|
|
52
|
+
var getModelName = function (f) {
|
|
53
|
+
if (p && p.name) {
|
|
54
|
+
return p.name;
|
|
89
55
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
var
|
|
94
|
-
var
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
56
|
+
return core_1.getModelName(f, 'model');
|
|
57
|
+
};
|
|
58
|
+
var showModel = function (model) {
|
|
59
|
+
var n = getModelName(refForm.current);
|
|
60
|
+
var objSet = {};
|
|
61
|
+
objSet[n] = model;
|
|
62
|
+
setState(objSet);
|
|
63
|
+
};
|
|
64
|
+
var _handleNotFound = function (form) {
|
|
65
|
+
var msg = core_1.message(p1.resource.value, 'error_not_found', 'error');
|
|
66
|
+
if (form) {
|
|
67
|
+
formutil_1.setReadOnly(form);
|
|
99
68
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
69
|
+
p1.showError(msg.message, msg.title);
|
|
70
|
+
};
|
|
71
|
+
var handleNotFound = (p && p.handleNotFound ? p.handleNotFound : _handleNotFound);
|
|
72
|
+
var _load = function (_id, callback) {
|
|
73
|
+
var id = _id;
|
|
74
|
+
if (id != null && id !== '') {
|
|
75
|
+
setRunning(true);
|
|
76
|
+
core_1.showLoading(p1.loading);
|
|
77
|
+
var fn = (typeof service === 'function' ? service : service.load);
|
|
78
|
+
fn(id).then(function (obj) {
|
|
79
|
+
if (!obj) {
|
|
80
|
+
handleNotFound(refForm.current);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
if (callback) {
|
|
84
|
+
callback(obj, showModel);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
showModel(obj);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
setRunning(false);
|
|
91
|
+
core_1.hideLoading(p1.loading);
|
|
92
|
+
}).catch(function (err) {
|
|
93
|
+
var data = (err && err.response) ? err.response : err;
|
|
94
|
+
var r = p1.resource;
|
|
95
|
+
var title = r.value('error');
|
|
96
|
+
var msg = r.value('error_internal');
|
|
97
|
+
if (data && data.status === 404) {
|
|
98
|
+
handleNotFound(refForm.current);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
if (data && data.status) {
|
|
102
|
+
msg = core_1.messageByHttpStatus(data.status, r.value);
|
|
103
|
+
}
|
|
104
|
+
formutil_1.setReadOnly(refForm.current);
|
|
105
|
+
p1.showError(msg, title);
|
|
106
|
+
}
|
|
107
|
+
setRunning(false);
|
|
108
|
+
core_1.hideLoading(p1.loading);
|
|
109
|
+
});
|
|
106
110
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
handleNotFound: handleNotFound,
|
|
123
|
-
load: load,
|
|
124
|
-
back: back
|
|
125
|
-
};
|
|
111
|
+
};
|
|
112
|
+
var load = (p && p.load ? p.load : _load);
|
|
113
|
+
return {
|
|
114
|
+
state: state,
|
|
115
|
+
setState: setState,
|
|
116
|
+
refForm: refForm,
|
|
117
|
+
resource: p1.resource.resource(),
|
|
118
|
+
running: running,
|
|
119
|
+
setRunning: setRunning,
|
|
120
|
+
showModel: showModel,
|
|
121
|
+
getModelName: getModelName,
|
|
122
|
+
handleNotFound: handleNotFound,
|
|
123
|
+
load: load,
|
|
124
|
+
back: back
|
|
125
|
+
};
|
|
126
126
|
};
|
package/package.json
CHANGED
package/src/components.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {BaseDiffState, DiffApprService, DiffParameter, DiffState, handleToggle,
|
|
|
3
3
|
import {Attributes, 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';
|
|
4
4
|
import {formatDiffModel, getDataFields} from './diff';
|
|
5
5
|
import {build, createModel as createModel2, EditParameter, GenericService, handleVersion, initPropertyNullInModel} from './edit';
|
|
6
|
-
import {focusFirstError,
|
|
6
|
+
import {focusFirstError, setReadOnly} from './formutil';
|
|
7
7
|
import {getAutoSearch, getConfirmFunc, getErrorFunc, getLoadingFunc, getLocaleFunc, getMsgFunc, getResource, getUIService} from './input';
|
|
8
8
|
import {clone, diff, makeDiff} from './reflect';
|
|
9
9
|
import {buildFromUrl} from './route';
|
|
@@ -105,7 +105,7 @@ export class ViewComponent<T, ID, P, S> extends React.Component<P, S> {
|
|
|
105
105
|
handleNotFound(form?: HTMLFormElement): void {
|
|
106
106
|
const msg = message(this.resourceService.value, 'error_not_found', 'error');
|
|
107
107
|
if (form) {
|
|
108
|
-
|
|
108
|
+
setReadOnly(form);
|
|
109
109
|
}
|
|
110
110
|
this.showError(msg.message, msg.title);
|
|
111
111
|
}
|
|
@@ -366,11 +366,11 @@ export class BaseSearchComponent<T, F extends Filter, P, I extends SearchState<T
|
|
|
366
366
|
// locationSearch: string;
|
|
367
367
|
// _currentSortField: string;
|
|
368
368
|
|
|
369
|
-
viewable?: boolean = true;
|
|
370
|
-
addable?: boolean = true;
|
|
371
|
-
editable?: boolean = true;
|
|
372
|
-
approvable?: boolean;
|
|
373
|
-
deletable?: boolean;
|
|
369
|
+
// viewable?: boolean = true;
|
|
370
|
+
// addable?: boolean = true;
|
|
371
|
+
// editable?: boolean = true;
|
|
372
|
+
// approvable?: boolean;
|
|
373
|
+
// deletable?: boolean;
|
|
374
374
|
|
|
375
375
|
getModelName(): string {
|
|
376
376
|
return 'filter';
|
|
@@ -751,9 +751,9 @@ export abstract class BaseEditComponent<T, P, S> extends BaseComponent<P, S> {
|
|
|
751
751
|
protected patchable = true;
|
|
752
752
|
protected orginalModel?: T;
|
|
753
753
|
|
|
754
|
-
addable?: boolean = true;
|
|
754
|
+
// addable?: boolean = true;
|
|
755
755
|
readOnly?: boolean;
|
|
756
|
-
deletable?: boolean;
|
|
756
|
+
// deletable?: boolean;
|
|
757
757
|
|
|
758
758
|
insertSuccessMsg: string;
|
|
759
759
|
updateSuccessMsg: string;
|
|
@@ -770,7 +770,7 @@ export abstract class BaseEditComponent<T, P, S> extends BaseComponent<P, S> {
|
|
|
770
770
|
handleNotFound(form?: HTMLFormElement|null): void {
|
|
771
771
|
const msg = message(this.resourceService.value, 'error_not_found', 'error');
|
|
772
772
|
if (form) {
|
|
773
|
-
|
|
773
|
+
setReadOnly(form);
|
|
774
774
|
}
|
|
775
775
|
this.showError(msg.message, msg.title);
|
|
776
776
|
}
|
|
@@ -791,7 +791,7 @@ export abstract class BaseEditComponent<T, P, S> extends BaseComponent<P, S> {
|
|
|
791
791
|
objSet[modelName] = model;
|
|
792
792
|
this.setState(objSet, () => {
|
|
793
793
|
if (this.readOnly) {
|
|
794
|
-
|
|
794
|
+
setReadOnly(f);
|
|
795
795
|
}
|
|
796
796
|
});
|
|
797
797
|
}
|
|
@@ -834,40 +834,30 @@ export abstract class BaseEditComponent<T, P, S> extends BaseComponent<P, S> {
|
|
|
834
834
|
}
|
|
835
835
|
onSave(isBack?: boolean) {
|
|
836
836
|
const r = this.resourceService;
|
|
837
|
-
if (this.
|
|
838
|
-
const m = message(r.value, 'error_permission_add', 'error_permission');
|
|
839
|
-
this.showError(m.message, m.title);
|
|
840
|
-
return;
|
|
841
|
-
} else if (!this.newMode && this.readOnly) {
|
|
842
|
-
const msg = message(r.value, 'error_permission_edit', 'error_permission');
|
|
843
|
-
this.showError(msg.message, msg.title);
|
|
837
|
+
if (this.running) {
|
|
844
838
|
return;
|
|
839
|
+
}
|
|
840
|
+
const com = this;
|
|
841
|
+
const obj = com.getModel();
|
|
842
|
+
if (this.newMode) {
|
|
843
|
+
com.validate(obj, () => {
|
|
844
|
+
const msg = message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
845
|
+
this.confirm(msg.message, msg.title, () => {
|
|
846
|
+
com.doSave(obj, obj, isBack);
|
|
847
|
+
}, msg.no, msg.yes);
|
|
848
|
+
});
|
|
845
849
|
} else {
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
if (this.newMode) {
|
|
850
|
+
const diffObj = makeDiff(initPropertyNullInModel(this.orginalModel, this.metadata), obj, this.keys, this.version);
|
|
851
|
+
const keys = Object.keys(diffObj as any);
|
|
852
|
+
if (keys.length === 0) {
|
|
853
|
+
this.showMessage(r.value('msg_no_change'));
|
|
854
|
+
} else {
|
|
852
855
|
com.validate(obj, () => {
|
|
853
856
|
const msg = message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
854
857
|
this.confirm(msg.message, msg.title, () => {
|
|
855
|
-
com.doSave(obj,
|
|
858
|
+
com.doSave(obj, diffObj, isBack);
|
|
856
859
|
}, msg.no, msg.yes);
|
|
857
860
|
});
|
|
858
|
-
} else {
|
|
859
|
-
const diffObj = makeDiff(initPropertyNullInModel(this.orginalModel, this.metadata), obj, this.keys, this.version);
|
|
860
|
-
const keys = Object.keys(diffObj as any);
|
|
861
|
-
if (keys.length === 0) {
|
|
862
|
-
this.showMessage(r.value('msg_no_change'));
|
|
863
|
-
} else {
|
|
864
|
-
com.validate(obj, () => {
|
|
865
|
-
const msg = message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
866
|
-
this.confirm(msg.message, msg.title, () => {
|
|
867
|
-
com.doSave(obj, diffObj, isBack);
|
|
868
|
-
}, msg.no, msg.yes);
|
|
869
|
-
});
|
|
870
|
-
}
|
|
871
861
|
}
|
|
872
862
|
}
|
|
873
863
|
}
|
|
@@ -1037,7 +1027,7 @@ export class EditComponent<T, ID, P, S> extends BaseEditComponent<T, P, S> {
|
|
|
1037
1027
|
msg = messageByHttpStatus(data.status, gv);
|
|
1038
1028
|
}
|
|
1039
1029
|
if (data && (data.status === 401 || data.status === 403)) {
|
|
1040
|
-
|
|
1030
|
+
setReadOnly(com.form);
|
|
1041
1031
|
}
|
|
1042
1032
|
com.showError(msg, title);
|
|
1043
1033
|
}
|
package/src/formutil.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export function
|
|
1
|
+
export function setReadOnly(form?: HTMLFormElement|null, ...args: string[]): void {
|
|
2
2
|
if (!form) {
|
|
3
3
|
return;
|
|
4
4
|
}
|
|
@@ -6,28 +6,39 @@ export function readOnly(form?: HTMLFormElement|null): void {
|
|
|
6
6
|
for (let i = 0; i < len; i++) {
|
|
7
7
|
const ctrl = form[i] as HTMLInputElement;
|
|
8
8
|
const name = ctrl.getAttribute('name');
|
|
9
|
+
let skip = false;
|
|
9
10
|
if (name != null && name.length > 0 && name !== 'btnBack') {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
if (arguments.length > 1) {
|
|
12
|
+
for (let j = 1; j < arguments.length; j++) {
|
|
13
|
+
if (arguments[j] === name) {
|
|
14
|
+
skip = true;
|
|
15
|
+
// continue; has bugs => why?
|
|
16
|
+
}
|
|
17
|
+
}
|
|
14
18
|
}
|
|
15
|
-
if (
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
if (skip === false) {
|
|
20
|
+
let nodeName = ctrl.nodeName;
|
|
21
|
+
const type = ctrl.getAttribute('type');
|
|
22
|
+
if (nodeName === 'INPUT' && type !== null) {
|
|
23
|
+
nodeName = type.toUpperCase();
|
|
24
|
+
}
|
|
25
|
+
if (nodeName !== 'BUTTON'
|
|
26
|
+
&& nodeName !== 'RESET'
|
|
27
|
+
&& nodeName !== 'SUBMIT'
|
|
28
|
+
&& nodeName !== 'SELECT') {
|
|
29
|
+
switch (type) {
|
|
30
|
+
case 'checkbox':
|
|
31
|
+
ctrl.disabled = true;
|
|
32
|
+
break;
|
|
33
|
+
case 'radio':
|
|
34
|
+
ctrl.disabled = true;
|
|
35
|
+
break;
|
|
36
|
+
default:
|
|
37
|
+
ctrl.readOnly = true;
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
ctrl.disabled = true;
|
|
28
41
|
}
|
|
29
|
-
} else {
|
|
30
|
-
ctrl.disabled = true;
|
|
31
42
|
}
|
|
32
43
|
}
|
|
33
44
|
}
|
package/src/useEdit.ts
CHANGED
|
@@ -2,7 +2,7 @@ import {useEffect, useState} from 'react';
|
|
|
2
2
|
import {useNavigate, useParams} from 'react-router';
|
|
3
3
|
import {Attributes, buildId, ErrorMessage, getModelName as getModelName2, hideLoading, initForm, LoadingService, Locale, message, messageByHttpStatus, ResourceService, showLoading, UIService} from './core';
|
|
4
4
|
import {build, createModel as createModel2, EditParameter, GenericService, handleVersion, initPropertyNullInModel} from './edit';
|
|
5
|
-
import {focusFirstError,
|
|
5
|
+
import {focusFirstError, setReadOnly} from './formutil';
|
|
6
6
|
import {DispatchWithCallback, useMergeState} from './merge';
|
|
7
7
|
import {clone, makeDiff} from './reflect';
|
|
8
8
|
import {localeOf} from './state';
|
package/src/useView.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {useEffect, useState} from 'react';
|
|
2
2
|
import {useNavigate, useParams} from 'react-router';
|
|
3
3
|
import {buildId, getModelName as getModelName2, hideLoading, initForm, LoadingService, Locale, message, messageByHttpStatus, ResourceService, showLoading, ViewParameter, ViewService} from './core';
|
|
4
|
-
import {
|
|
4
|
+
import {setReadOnly} from './formutil';
|
|
5
5
|
import {DispatchWithCallback, useMergeState} from './merge';
|
|
6
6
|
|
|
7
7
|
export interface BaseViewComponentParam<T, ID> {
|
|
@@ -97,7 +97,7 @@ export const useCoreView = <T, ID, S>(
|
|
|
97
97
|
const _handleNotFound = (form?: any): void => {
|
|
98
98
|
const msg = message(p1.resource.value, 'error_not_found', 'error');
|
|
99
99
|
if (form) {
|
|
100
|
-
|
|
100
|
+
setReadOnly(form);
|
|
101
101
|
}
|
|
102
102
|
p1.showError(msg.message, msg.title);
|
|
103
103
|
};
|
|
@@ -132,7 +132,7 @@ export const useCoreView = <T, ID, S>(
|
|
|
132
132
|
if (data && data.status) {
|
|
133
133
|
msg = messageByHttpStatus(data.status, r.value);
|
|
134
134
|
}
|
|
135
|
-
|
|
135
|
+
setReadOnly(refForm.current);
|
|
136
136
|
p1.showError(msg, title);
|
|
137
137
|
}
|
|
138
138
|
setRunning(false);
|