react-hook-core 0.1.2 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/components.js +51 -92
- package/lib/core.js +21 -12
- package/lib/diff.js +2 -2
- package/lib/formutil.js +2 -2
- package/lib/index.js +44 -8
- package/lib/reflect.js +268 -0
- package/lib/search.js +579 -0
- package/lib/state.js +4 -4
- package/lib/update.js +7 -20
- package/lib/useEdit.js +63 -49
- package/lib/useSearch.js +65 -69
- package/lib/useView.js +24 -26
- package/package.json +5 -7
- package/src/components.ts +56 -97
- package/src/core.ts +70 -59
- package/src/diff.ts +1 -1
- package/src/formutil.ts +2 -2
- package/src/index.ts +35 -11
- package/src/reflect.ts +244 -0
- package/src/search.ts +600 -0
- package/src/state.ts +1 -1
- package/src/update.ts +7 -19
- package/src/useEdit.ts +75 -59
- package/src/useSearch.ts +74 -64
- package/src/useView.ts +31 -34
- package/tsconfig.json +3 -1
package/src/useEdit.ts
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
import {useEffect, useState} from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import {clone, makeDiff} from 'reflectx';
|
|
2
|
+
import {useNavigate, useParams} from 'react-router';
|
|
4
3
|
import {Attributes, buildId, createEditStatus, EditStatusConfig, getModelName as getModelName2, hideLoading, initForm, LoadingService, Locale, message, messageByHttpStatus, ResourceService, showLoading, UIService} from './core';
|
|
5
4
|
import {build, createModel as createModel2, EditParameter, GenericService, handleStatus, handleVersion, initPropertyNullInModel, ResultInfo} from './edit';
|
|
6
5
|
import {focusFirstError, readOnly as setReadOnly} from './formutil';
|
|
7
6
|
import {DispatchWithCallback, useMergeState} from './merge';
|
|
8
|
-
import {
|
|
7
|
+
import {clone, makeDiff} from './reflect';
|
|
9
8
|
import {localeOf} from './state';
|
|
10
9
|
import {useUpdate} from './update';
|
|
11
10
|
|
|
12
|
-
function prepareData(data: any): void {
|
|
13
|
-
}
|
|
14
|
-
|
|
15
11
|
export interface BaseEditComponentParam<T, ID> {
|
|
16
12
|
status?: EditStatusConfig;
|
|
17
13
|
backOnSuccess?: boolean;
|
|
@@ -40,9 +36,9 @@ export interface BaseEditComponentParam<T, ID> {
|
|
|
40
36
|
fail?: (result: ResultInfo<T>) => void;
|
|
41
37
|
postSave?: (obj: T, res: number|ResultInfo<T>, version?: string, backOnSave?: boolean) => void;
|
|
42
38
|
handleDuplicateKey?: (result?: ResultInfo<T>) => void;
|
|
43
|
-
load?: (i: ID, callback?: (m: T, showM: (m2: T) => void) => void) => void;
|
|
44
|
-
|
|
45
|
-
prepareCustomData?: (data: any) => void; // need to review
|
|
39
|
+
load?: (i: ID|null, callback?: (m: T, showM: (m2: T) => void) => void) => void;
|
|
40
|
+
doSave?: (obj: T, diff?: T, version?: string, isBack?: boolean) => void;
|
|
41
|
+
// prepareCustomData?: (data: any) => void; // need to review
|
|
46
42
|
}
|
|
47
43
|
export interface HookBaseEditParameter<T, ID, S> extends BaseEditComponentParam<T, ID> {
|
|
48
44
|
refForm: any;
|
|
@@ -57,27 +53,57 @@ export interface HookBaseEditParameter<T, ID, S> extends BaseEditComponentParam<
|
|
|
57
53
|
loading?: LoadingService;
|
|
58
54
|
}
|
|
59
55
|
export interface EditComponentParam<T, ID, S> extends BaseEditComponentParam<T, ID> {
|
|
60
|
-
initialize?: (id: ID, ld: (i: ID, cb?: (m: T, showF: (model: T) => void) => void) => void, setState2: DispatchWithCallback<Partial<S>>, callback?: (m: T, showF: (model: T) => void) => void) => void;
|
|
56
|
+
initialize?: (id: ID|null, ld: (i: ID|null, cb?: (m: T, showF: (model: T) => void) => void) => void, setState2: DispatchWithCallback<Partial<S>>, callback?: (m: T, showF: (model: T) => void) => void) => void;
|
|
61
57
|
callback?: (m: T, showF: (model: T) => void) => void;
|
|
62
58
|
}
|
|
63
|
-
export interface HookPropsEditParameter<T, ID, S, P
|
|
64
|
-
initialize?: (id: ID, ld: (i: ID, cb?: (m: T, showF: (model: T) => void) => void) => void, setState2: DispatchWithCallback<Partial<S>>, callback?: (m: T, showF: (model: T) => void) => void) => void;
|
|
59
|
+
export interface HookPropsEditParameter<T, ID, S, P> extends HookPropsBaseEditParameter<T, ID, S, P> {
|
|
60
|
+
initialize?: (id: ID|null, ld: (i: ID|null, cb?: (m: T, showF: (model: T) => void) => void) => void, setState2: DispatchWithCallback<Partial<S>>, callback?: (m: T, showF: (model: T) => void) => void) => void;
|
|
65
61
|
callback?: (m: T, showF: (model: T) => void) => void;
|
|
66
62
|
}
|
|
67
|
-
export interface HookPropsBaseEditParameter<T, ID, S, P
|
|
63
|
+
export interface HookPropsBaseEditParameter<T, ID, S, P> extends HookBaseEditParameter<T, ID, S> {
|
|
68
64
|
props: P;
|
|
69
|
-
prepareCustomData?: (data: any) => void;
|
|
65
|
+
// prepareCustomData?: (data: any) => void;
|
|
70
66
|
}
|
|
71
67
|
export const useEdit = <T, ID, S>(
|
|
72
68
|
refForm: any,
|
|
73
69
|
initialState: S,
|
|
74
70
|
service: GenericService<T, ID, number|ResultInfo<T>>,
|
|
75
|
-
|
|
76
|
-
|
|
71
|
+
p2: EditParameter,
|
|
72
|
+
p?: EditComponentParam<T, ID, S>
|
|
77
73
|
) => {
|
|
78
|
-
|
|
74
|
+
const params = useParams();
|
|
75
|
+
const baseProps = useCoreEdit(refForm, initialState, service, p2, p);
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (refForm) {
|
|
78
|
+
const registerEvents = (p2.ui ? p2.ui.registerEvents : undefined);
|
|
79
|
+
initForm(baseProps.refForm.current, registerEvents);
|
|
80
|
+
}
|
|
81
|
+
const n = baseProps.getModelName(refForm.current);
|
|
82
|
+
const obj: any = {};
|
|
83
|
+
obj[n] = baseProps.createNewModel();
|
|
84
|
+
baseProps.setState(obj);
|
|
85
|
+
let keys: string[]|undefined;
|
|
86
|
+
if (p && !p.keys && service && service.metadata) {
|
|
87
|
+
const metadata = (p.metadata ? p.metadata : service.metadata());
|
|
88
|
+
if (metadata) {
|
|
89
|
+
const meta = build(metadata);
|
|
90
|
+
keys = (p.keys ? p.keys : (meta ? meta.keys : undefined));
|
|
91
|
+
const version = (p.version ? p.version : (meta ? meta.version : undefined));
|
|
92
|
+
p.keys = keys;
|
|
93
|
+
p.version = version;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const id = buildId<ID>(params, keys);
|
|
97
|
+
if (p && p.initialize) {
|
|
98
|
+
p.initialize(id, baseProps.load, baseProps.setState, p.callback);
|
|
99
|
+
} else {
|
|
100
|
+
baseProps.load(id, p ? p.callback : undefined);
|
|
101
|
+
}
|
|
102
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
103
|
+
}, []);
|
|
104
|
+
return {...baseProps};
|
|
79
105
|
};
|
|
80
|
-
export const useEditProps = <T, ID, S, P
|
|
106
|
+
export const useEditProps = <T, ID, S, P>(
|
|
81
107
|
props: P,
|
|
82
108
|
refForm: any,
|
|
83
109
|
initialState: S,
|
|
@@ -85,7 +111,8 @@ export const useEditProps = <T, ID, S, P extends RouteComponentProps>(
|
|
|
85
111
|
p2: EditParameter,
|
|
86
112
|
p?: EditComponentParam<T, ID, S>
|
|
87
113
|
) => {
|
|
88
|
-
const
|
|
114
|
+
const params = useParams();
|
|
115
|
+
const baseProps = useCoreEdit<T, ID, S, P>(refForm, initialState, service, p2, p, props);
|
|
89
116
|
useEffect(() => {
|
|
90
117
|
if (refForm) {
|
|
91
118
|
const registerEvents = (p2.ui ? p2.ui.registerEvents : undefined);
|
|
@@ -98,36 +125,37 @@ export const useEditProps = <T, ID, S, P extends RouteComponentProps>(
|
|
|
98
125
|
let keys: string[]|undefined;
|
|
99
126
|
if (p && !p.keys && service && service.metadata) {
|
|
100
127
|
const metadata = (p.metadata ? p.metadata : service.metadata());
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const id = buildId<ID>(props, keys);
|
|
108
|
-
if (id) {
|
|
109
|
-
if (p && p.initialize) {
|
|
110
|
-
p.initialize(id, baseProps.load, baseProps.setState, p.callback);
|
|
111
|
-
} else {
|
|
112
|
-
baseProps.load(id, p ? p.callback : undefined);
|
|
128
|
+
if (metadata) {
|
|
129
|
+
const meta = build(metadata);
|
|
130
|
+
keys = (p.keys ? p.keys : (meta ? meta.keys : undefined));
|
|
131
|
+
const version = (p.version ? p.version : (meta ? meta.version : undefined));
|
|
132
|
+
p.keys = keys;
|
|
133
|
+
p.version = version;
|
|
113
134
|
}
|
|
114
135
|
}
|
|
136
|
+
const id = buildId<ID>(params, keys);
|
|
137
|
+
if (p && p.initialize) {
|
|
138
|
+
p.initialize(id, baseProps.load, baseProps.setState, p.callback);
|
|
139
|
+
} else {
|
|
140
|
+
baseProps.load(id, p ? p.callback : undefined);
|
|
141
|
+
}
|
|
142
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
115
143
|
}, []);
|
|
116
144
|
return {...baseProps};
|
|
117
145
|
};
|
|
118
|
-
export const useEditOneProps = <T, ID, S, P
|
|
146
|
+
export const useEditOneProps = <T, ID, S, P>(p: HookPropsEditParameter<T, ID, S, P>) => {
|
|
119
147
|
return useEditProps(p.props, p.refForm, p.initialState, p.service, p, p);
|
|
120
148
|
};
|
|
121
149
|
export const useEditOne = <T, ID, S>(p: HookBaseEditParameter<T, ID, S>) => {
|
|
122
150
|
return useEdit(p.refForm, p.initialState, p.service, p, p);
|
|
123
151
|
};
|
|
124
152
|
export const useCoreEdit = <T, ID, S, P>(
|
|
125
|
-
props: P|undefined,
|
|
126
153
|
refForm: any,
|
|
127
154
|
initialState: S,
|
|
128
155
|
service: GenericService<T, ID, number|ResultInfo<T>>,
|
|
129
156
|
p1: EditParameter,
|
|
130
|
-
p?: BaseEditComponentParam<T, ID
|
|
157
|
+
p?: BaseEditComponentParam<T, ID>,
|
|
158
|
+
props?: P
|
|
131
159
|
) => {
|
|
132
160
|
/*
|
|
133
161
|
const {
|
|
@@ -135,13 +163,13 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
135
163
|
patchable = true,
|
|
136
164
|
addable = true
|
|
137
165
|
} = p; */
|
|
166
|
+
const navigate = useNavigate();
|
|
138
167
|
const addable = (p && p.patchable !== false ? true : undefined);
|
|
139
|
-
const {goBack} = useRouter();
|
|
140
168
|
const back = (event?: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
141
169
|
if (event) {
|
|
142
170
|
event.preventDefault();
|
|
143
171
|
}
|
|
144
|
-
|
|
172
|
+
navigate(-1);
|
|
145
173
|
};
|
|
146
174
|
|
|
147
175
|
const [running, setRunning] = useState<boolean>();
|
|
@@ -154,18 +182,6 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
154
182
|
};
|
|
155
183
|
const baseProps = useUpdate<S>(initialState, getModelName, p1.getLocale);
|
|
156
184
|
|
|
157
|
-
const prepareCustomData = (p && p.prepareCustomData ? p.prepareCustomData : prepareData);
|
|
158
|
-
const updateDateState = (name: string, value: any) => {
|
|
159
|
-
const modelName = getModelName(refForm.current);
|
|
160
|
-
const currentState = (state as any)[modelName];
|
|
161
|
-
if (props && (props as any).setGlobalState) {
|
|
162
|
-
const data = (props as any).shouldBeCustomized ? prepareCustomData({ [name]: value }) : { [name]: value };
|
|
163
|
-
(props as any).setGlobalState({ [modelName]: { ...currentState, ...data } });
|
|
164
|
-
} else {
|
|
165
|
-
setState({[modelName]: {...currentState, [name]: value}} as T);
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
|
|
169
185
|
const { state, setState } = baseProps;
|
|
170
186
|
const [flag, setFlag] = useMergeState({
|
|
171
187
|
newMode: false,
|
|
@@ -222,7 +238,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
222
238
|
};
|
|
223
239
|
const createModel = (p && p.createModel ? p.createModel : _createModel);
|
|
224
240
|
|
|
225
|
-
const
|
|
241
|
+
const create = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
226
242
|
event.preventDefault();
|
|
227
243
|
const obj = createModel();
|
|
228
244
|
resetState(true, obj, undefined);
|
|
@@ -260,7 +276,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
260
276
|
validate(obj, () => {
|
|
261
277
|
const msg = message(p1.resource.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
262
278
|
p1.confirm(msg.message, msg.title, () => {
|
|
263
|
-
|
|
279
|
+
doSave(obj, undefined, version, isBack);
|
|
264
280
|
}, msg.no, msg.yes);
|
|
265
281
|
});
|
|
266
282
|
} else {
|
|
@@ -272,7 +288,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
272
288
|
validate(obj, () => {
|
|
273
289
|
const msg = message(p1.resource.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
274
290
|
p1.confirm(msg.message, msg.title, () => {
|
|
275
|
-
|
|
291
|
+
doSave(obj, diffObj, version, isBack);
|
|
276
292
|
}, msg.no, msg.yes);
|
|
277
293
|
});
|
|
278
294
|
}
|
|
@@ -281,7 +297,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
281
297
|
};
|
|
282
298
|
const onSave = (p && p.onSave ? p.onSave : _onSave);
|
|
283
299
|
|
|
284
|
-
const
|
|
300
|
+
const save = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
285
301
|
event.preventDefault();
|
|
286
302
|
event.persist();
|
|
287
303
|
onSave();
|
|
@@ -387,7 +403,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
387
403
|
};
|
|
388
404
|
const handleDuplicateKey = (p && p.handleDuplicateKey ? p.handleDuplicateKey : _handleDuplicateKey);
|
|
389
405
|
|
|
390
|
-
const
|
|
406
|
+
const _doSave = (obj: T, body?: T, version?: string, isBack?: boolean) => {
|
|
391
407
|
setRunning(true);
|
|
392
408
|
showLoading(p1.loading);
|
|
393
409
|
const isBackO = (isBack == null || isBack === undefined ? true : isBack);
|
|
@@ -402,9 +418,9 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
402
418
|
service.insert(obj).then(result => postSave(obj, result, version, isBackO));
|
|
403
419
|
}
|
|
404
420
|
};
|
|
405
|
-
const
|
|
421
|
+
const doSave = (p && p.doSave ? p.doSave : _doSave);
|
|
406
422
|
|
|
407
|
-
const _load = (_id: ID, callback?: (m: T, showM: (m2: T) => void) => void) => {
|
|
423
|
+
const _load = (_id: ID|null, callback?: (m: T, showM: (m2: T) => void) => void) => {
|
|
408
424
|
const id: any = _id;
|
|
409
425
|
if (id != null && id !== '') {
|
|
410
426
|
setRunning(true);
|
|
@@ -462,16 +478,16 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
462
478
|
flag,
|
|
463
479
|
running,
|
|
464
480
|
setRunning,
|
|
465
|
-
updateDateState,
|
|
466
481
|
showModel,
|
|
467
482
|
getModelName,
|
|
468
483
|
resetState,
|
|
469
484
|
handleNotFound,
|
|
470
485
|
getModel,
|
|
471
486
|
createNewModel: createModel,
|
|
472
|
-
newOnClick,
|
|
473
|
-
|
|
487
|
+
newOnClick: create,
|
|
488
|
+
save,
|
|
474
489
|
onSave,
|
|
490
|
+
// eslint-disable-next-line no-restricted-globals
|
|
475
491
|
confirm,
|
|
476
492
|
validate,
|
|
477
493
|
showMessage: p1.showMessage,
|
|
@@ -480,6 +496,6 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
480
496
|
postSave,
|
|
481
497
|
handleDuplicateKey,
|
|
482
498
|
load,
|
|
483
|
-
|
|
499
|
+
doSave
|
|
484
500
|
};
|
|
485
501
|
};
|
package/src/useSearch.ts
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import {useEffect, useState} from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import {useHistory, useRouteMatch} from 'react-router-dom';
|
|
4
|
-
import {clone} from 'reflectx';
|
|
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';
|
|
2
|
+
import {error, getDecodeFromForm, getName, getRemoveError, getValidateForm, handleToggle, hideLoading, initForm, Locale, PageChange, pageSizes, removeFormError, ResourceService, SearchParameter, SearchResult, SearchService, showLoading} from './core';
|
|
7
3
|
import {DispatchWithCallback, useMergeState} from './merge';
|
|
4
|
+
import {clone} from './reflect';
|
|
8
5
|
import {buildFromUrl} from './route';
|
|
6
|
+
import {addParametersIntoUrl, append, buildMessage, formatResults, getFieldsFromForm, getModel, handleAppend, handleSort, initFilter, mergeFilter as mergeFilter2, Pagination, removeSortStatus, showPaging, Sortable, validate} from './search';
|
|
9
7
|
import {enLocale} from './state';
|
|
10
|
-
import {useUpdate
|
|
8
|
+
import {useUpdate} from './update';
|
|
11
9
|
|
|
12
10
|
export interface Searchable<T> extends Pagination, Sortable {
|
|
13
11
|
nextPageToken?: string;
|
|
14
12
|
excluding?: string[]|number[];
|
|
15
13
|
list?: T[];
|
|
16
14
|
}
|
|
17
|
-
|
|
15
|
+
interface Filter {
|
|
16
|
+
page?: number;
|
|
17
|
+
limit?: number;
|
|
18
|
+
firstLimit?: number;
|
|
19
|
+
fields?: string[];
|
|
20
|
+
sort?: string;
|
|
18
21
|
}
|
|
22
|
+
|
|
19
23
|
export const callSearch = <T, S extends Filter>(se: S, search3: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>, showResults3: (s: S, sr: SearchResult<T>, lc: Locale) => void, searchError3: (err: any) => void, lc: Locale, nextPageToken?: string) => {
|
|
20
24
|
const s = clone(se);
|
|
21
25
|
let page = se.page;
|
|
@@ -24,7 +28,7 @@ export const callSearch = <T, S extends Filter>(se: S, search3: (s: S, limit?: n
|
|
|
24
28
|
}
|
|
25
29
|
let offset: number;
|
|
26
30
|
if (!se.limit || se.limit <= 0) {
|
|
27
|
-
se.limit =
|
|
31
|
+
se.limit = 24;
|
|
28
32
|
}
|
|
29
33
|
if (se.firstLimit && se.firstLimit > 0) {
|
|
30
34
|
offset = se.limit * (page - 2) + se.firstLimit;
|
|
@@ -53,13 +57,19 @@ export interface InitSearchComponentParam<T, M extends Filter, S> extends Search
|
|
|
53
57
|
createFilter?: () => M;
|
|
54
58
|
initialize?: (ld: (s: M, auto?: boolean) => void, setState2: DispatchWithCallback<Partial<S>>, com?: SearchComponentState<T, M>) => void;
|
|
55
59
|
}
|
|
56
|
-
export interface HookPropsSearchParameter<T, S extends Filter, ST, P
|
|
60
|
+
export interface HookPropsSearchParameter<T, S extends Filter, ST, P> extends HookPropsBaseSearchParameter<T, S, ST, P> {
|
|
57
61
|
createFilter?: () => S;
|
|
58
62
|
initialize?: (ld: (s: S, auto?: boolean) => void, setState2: DispatchWithCallback<Partial<ST>>, com?: SearchComponentState<T, S>) => void;
|
|
59
63
|
}
|
|
60
64
|
export interface SearchComponentParam<T, M extends Filter> {
|
|
65
|
+
addable?: boolean;
|
|
66
|
+
editable?: boolean;
|
|
67
|
+
approvable?: boolean;
|
|
68
|
+
deletable?: boolean;
|
|
69
|
+
|
|
61
70
|
keys?: string[];
|
|
62
71
|
sequenceNo?: string;
|
|
72
|
+
hideFilter?: boolean;
|
|
63
73
|
name?: string;
|
|
64
74
|
fields?: string[];
|
|
65
75
|
appendMode?: boolean;
|
|
@@ -77,7 +87,7 @@ export interface SearchComponentParam<T, M extends Filter> {
|
|
|
77
87
|
getFilter?: () => M;
|
|
78
88
|
getFields?: () => string[]|undefined;
|
|
79
89
|
validateSearch?: (se: M, callback: () => void) => void;
|
|
80
|
-
prepareCustomData?: (data: any) => void;
|
|
90
|
+
// prepareCustomData?: (data: any) => void;
|
|
81
91
|
format?(obj: T, locale?: Locale): T;
|
|
82
92
|
showResults?(s: M, sr: SearchResult<T>, lc: Locale): void;
|
|
83
93
|
appendList?(results: T[], list: T[]|undefined, s: DispatchWithCallback<Partial<SearchComponentState<T, M>>>): void;
|
|
@@ -95,7 +105,7 @@ export interface SearchComponentParam<T, M extends Filter> {
|
|
|
95
105
|
export interface HookBaseSearchParameter<T, S extends Filter, ST extends SearchComponentState<T, S>> extends SearchComponentParam<T, S> {
|
|
96
106
|
refForm: any;
|
|
97
107
|
initialState: ST;
|
|
98
|
-
|
|
108
|
+
service: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>;
|
|
99
109
|
resource: ResourceService;
|
|
100
110
|
showMessage: (msg: string) => void;
|
|
101
111
|
showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
|
|
@@ -104,7 +114,7 @@ export interface HookBaseSearchParameter<T, S extends Filter, ST extends SearchC
|
|
|
104
114
|
}
|
|
105
115
|
export interface HookPropsBaseSearchParameter<T, S extends Filter, ST, P> extends HookBaseSearchParameter<T, S, ST> {
|
|
106
116
|
props?: P;
|
|
107
|
-
prepareCustomData?: (data: any) => void;
|
|
117
|
+
// prepareCustomData?: (data: any) => void;
|
|
108
118
|
}
|
|
109
119
|
export interface SearchComponentState<T, S> extends Pagination, Sortable {
|
|
110
120
|
view?: string;
|
|
@@ -121,7 +131,7 @@ export interface SearchComponentState<T, S> extends Pagination, Sortable {
|
|
|
121
131
|
|
|
122
132
|
pageMaxSize?: number;
|
|
123
133
|
pageSizes?: number[];
|
|
124
|
-
excluding?:
|
|
134
|
+
excluding?: string[]|number[];
|
|
125
135
|
hideFilter?: boolean;
|
|
126
136
|
|
|
127
137
|
ignoreUrlParam?: boolean;
|
|
@@ -131,7 +141,6 @@ export interface SearchComponentState<T, S> extends Pagination, Sortable {
|
|
|
131
141
|
approvable?: boolean;
|
|
132
142
|
deletable?: boolean;
|
|
133
143
|
}
|
|
134
|
-
export const pageSizes = [10, 20, 40, 60, 100, 200, 400, 800];
|
|
135
144
|
|
|
136
145
|
function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>): SearchComponentParam<T, S> {
|
|
137
146
|
if (p) {
|
|
@@ -139,7 +148,7 @@ function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>): Search
|
|
|
139
148
|
p.sequenceNo = 'sequenceNo';
|
|
140
149
|
}
|
|
141
150
|
if (!p.pageSize) {
|
|
142
|
-
p.pageSize =
|
|
151
|
+
p.pageSize = 24;
|
|
143
152
|
}
|
|
144
153
|
if (!p.pageSizes) {
|
|
145
154
|
p.pageSizes = pageSizes;
|
|
@@ -147,25 +156,44 @@ function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>): Search
|
|
|
147
156
|
if (!p.pageMaxSize || p.pageMaxSize <= 0) {
|
|
148
157
|
p.pageMaxSize = 7;
|
|
149
158
|
}
|
|
159
|
+
if (p.hideFilter === undefined) {
|
|
160
|
+
p.hideFilter = true;
|
|
161
|
+
}
|
|
162
|
+
if (p.addable === undefined) {
|
|
163
|
+
p.addable = true;
|
|
164
|
+
}
|
|
165
|
+
if (p.editable === undefined) {
|
|
166
|
+
p.editable = true;
|
|
167
|
+
}
|
|
168
|
+
if (p.approvable === undefined) {
|
|
169
|
+
p.approvable = true;
|
|
170
|
+
}
|
|
171
|
+
if (p.deletable === undefined) {
|
|
172
|
+
p.deletable = true;
|
|
173
|
+
}
|
|
150
174
|
return p;
|
|
151
175
|
} else {
|
|
152
176
|
return {
|
|
153
177
|
sequenceNo: 'sequenceNo',
|
|
154
|
-
pageSize:
|
|
178
|
+
pageSize: 24,
|
|
155
179
|
pageSizes,
|
|
156
|
-
pageMaxSize: 7
|
|
180
|
+
pageMaxSize: 7,
|
|
181
|
+
hideFilter: true,
|
|
182
|
+
addable: true,
|
|
183
|
+
editable: true,
|
|
184
|
+
approvable: true,
|
|
185
|
+
deletable: true
|
|
157
186
|
};
|
|
158
187
|
}
|
|
159
188
|
}
|
|
160
189
|
export const useSearch = <T, S extends Filter, ST extends SearchComponentState<T, S>>(
|
|
161
190
|
refForm: any,
|
|
162
191
|
initialState: ST,
|
|
163
|
-
|
|
192
|
+
service: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
164
193
|
p2: SearchParameter,
|
|
165
194
|
p?: InitSearchComponentParam<T, S, ST>,
|
|
166
195
|
) => {
|
|
167
|
-
|
|
168
|
-
const baseProps = useCoreSearch(undefined, refForm, initialState, search, p2, p);
|
|
196
|
+
const baseProps = useCoreSearch(refForm, initialState, service, p2, p);
|
|
169
197
|
|
|
170
198
|
useEffect(() => {
|
|
171
199
|
const { load, setState, component } = baseProps;
|
|
@@ -180,22 +208,23 @@ export const useSearch = <T, S extends Filter, ST extends SearchComponentState<T
|
|
|
180
208
|
const s: any = mergeFilter2(buildFromUrl<S>(), se, component.pageSizes);
|
|
181
209
|
load(s, p2.auto);
|
|
182
210
|
}
|
|
211
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
183
212
|
}, []);
|
|
184
213
|
return { ...baseProps };
|
|
185
214
|
};
|
|
186
|
-
export const useSearchOneProps = <T, S extends Filter, ST extends SearchComponentState<T, S>, P
|
|
187
|
-
return useSearch(p.refForm, p.initialState, p.
|
|
215
|
+
export const useSearchOneProps = <T, S extends Filter, ST extends SearchComponentState<T, S>, P>(p: HookPropsSearchParameter<T, S, ST, P>) => {
|
|
216
|
+
return useSearch(p.refForm, p.initialState, p.service, p, p);
|
|
188
217
|
};
|
|
189
218
|
export const useSearchOne = <T, S extends Filter, ST extends SearchComponentState<T, S>>(p: HookBaseSearchParameter<T, S, ST>) => {
|
|
190
|
-
return useCoreSearch(
|
|
219
|
+
return useCoreSearch(p.refForm, p.initialState, p.service, p, p);
|
|
191
220
|
};
|
|
192
221
|
export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
193
|
-
props: P|undefined,
|
|
194
222
|
refForm: any,
|
|
195
223
|
initialState: ST,
|
|
196
|
-
|
|
224
|
+
service: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
197
225
|
p1: SearchParameter,
|
|
198
|
-
p2?: SearchComponentParam<T, S
|
|
226
|
+
p2?: SearchComponentParam<T, S>,
|
|
227
|
+
props?: P
|
|
199
228
|
) => {
|
|
200
229
|
const p = mergeParam(p2);
|
|
201
230
|
const [running, setRunning] = useState<boolean>();
|
|
@@ -206,38 +235,21 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
206
235
|
const getModelName = (p && p.getModelName ? p.getModelName : _getModelName);
|
|
207
236
|
|
|
208
237
|
// const setState2: <K extends keyof S, P>(st: ((prevState: Readonly<S>, props: Readonly<P>) => (Pick<S, K> | S | null)) | (Pick<S, K> | S | null), cb?: () => void) => void;
|
|
209
|
-
const baseProps =
|
|
238
|
+
const baseProps = useUpdate<ST>(initialState, getModelName, p1.getLocale, getRemoveError(p1));
|
|
210
239
|
const { state, setState } = baseProps;
|
|
211
|
-
const [history, match] = [useHistory(), useRouteMatch()];
|
|
212
240
|
|
|
213
241
|
const _getCurrencyCode = (): string => {
|
|
214
242
|
return refForm && refForm.current ? refForm.current.getAttribute('currency-code') : null;
|
|
215
243
|
};
|
|
216
244
|
const getCurrencyCode = p && p.getCurrencyCode ? p.getCurrencyCode : _getCurrencyCode;
|
|
217
245
|
|
|
218
|
-
const prepareCustomData = (p && p.prepareCustomData ? p.prepareCustomData : prepareData);
|
|
219
|
-
const updateDateState = (name: string, value: any) => {
|
|
220
|
-
const modelName = getModelName();
|
|
221
|
-
const currentState = (state as any)[modelName];
|
|
222
|
-
if (props && (props as any).setGlobalState) {
|
|
223
|
-
const data = (props as any).shouldBeCustomized ? prepareCustomData({ [name]: value }) : { [name]: value };
|
|
224
|
-
(props as any).setGlobalState({ [modelName]: { ...currentState, ...data } });
|
|
225
|
-
} else {
|
|
226
|
-
setState({ [modelName]: { ...currentState, [name]: value } } as T);
|
|
227
|
-
}
|
|
228
|
-
setState({ [modelName]: { ...currentState, [name]: value } } as T);
|
|
229
|
-
};
|
|
230
|
-
|
|
231
246
|
// const p = createSearchComponentState<T, S>(p1);
|
|
232
247
|
const [component, setComponent] = useMergeState<SearchComponentState<T, S>>(p);
|
|
233
248
|
|
|
234
|
-
const toggleFilter = (event:
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
const add = (event: any) => {
|
|
239
|
-
event.preventDefault();
|
|
240
|
-
history.push(match.url + '/add');
|
|
249
|
+
const toggleFilter = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void => {
|
|
250
|
+
const x = !component.hideFilter;
|
|
251
|
+
handleToggle(event.target as HTMLInputElement, !x);
|
|
252
|
+
setComponent({ hideFilter: x });
|
|
241
253
|
};
|
|
242
254
|
|
|
243
255
|
const _getFields = (): string[]|undefined => {
|
|
@@ -253,8 +265,8 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
253
265
|
se = component;
|
|
254
266
|
}
|
|
255
267
|
let keys = p && p.keys ? p.keys : undefined;
|
|
256
|
-
if (!keys && typeof
|
|
257
|
-
keys =
|
|
268
|
+
if (!keys && typeof service !== 'function' && service.keys) {
|
|
269
|
+
keys = service.keys();
|
|
258
270
|
}
|
|
259
271
|
const n = getModelName();
|
|
260
272
|
let fs = p && p.fields;
|
|
@@ -303,10 +315,10 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
303
315
|
addParametersIntoUrl(s, isFirstLoad);
|
|
304
316
|
}
|
|
305
317
|
const lc = p1.getLocale ? p1.getLocale() : enLocale;
|
|
306
|
-
if (typeof
|
|
307
|
-
callSearch<T, S>(s,
|
|
318
|
+
if (typeof service === 'function') {
|
|
319
|
+
callSearch<T, S>(s, service, showResults, searchError, lc, se.nextPageToken);
|
|
308
320
|
} else {
|
|
309
|
-
callSearch<T, S>(s,
|
|
321
|
+
callSearch<T, S>(s, service.search, showResults, searchError, lc, se.nextPageToken);
|
|
310
322
|
}
|
|
311
323
|
});
|
|
312
324
|
};
|
|
@@ -343,7 +355,7 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
343
355
|
}
|
|
344
356
|
};
|
|
345
357
|
|
|
346
|
-
const
|
|
358
|
+
const search = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void => {
|
|
347
359
|
if (event) {
|
|
348
360
|
event.preventDefault();
|
|
349
361
|
}
|
|
@@ -411,11 +423,11 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
411
423
|
const _showResults = (s: S, sr: SearchResult<T>, lc: Locale) => {
|
|
412
424
|
const results = sr.list;
|
|
413
425
|
if (results && results.length > 0) {
|
|
414
|
-
formatResults(results, component.pageIndex, component.pageSize, component.
|
|
426
|
+
formatResults(results, component.pageIndex, component.pageSize, component.pageSize, p ? p.sequenceNo : undefined, p ? p.format : undefined, lc);
|
|
415
427
|
}
|
|
416
428
|
const am = component.appendMode;
|
|
417
429
|
const pi = (s.page && s.page >= 1 ? s.page : 1);
|
|
418
|
-
setComponent({
|
|
430
|
+
setComponent({ total: sr.total, pageIndex: pi, nextPageToken: sr.nextPageToken });
|
|
419
431
|
if (am) {
|
|
420
432
|
let limit = s.limit;
|
|
421
433
|
if ((!s.page || s.page <= 1) && s.firstLimit && s.firstLimit > 0) {
|
|
@@ -456,11 +468,11 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
456
468
|
doSearch(component);
|
|
457
469
|
};
|
|
458
470
|
|
|
459
|
-
const pageChanged = (data:
|
|
460
|
-
const {
|
|
461
|
-
setComponent({ pageIndex:
|
|
462
|
-
component.pageIndex =
|
|
463
|
-
component.pageSize =
|
|
471
|
+
const pageChanged = (data: PageChange) => {
|
|
472
|
+
const { page, size } = data;
|
|
473
|
+
setComponent({ pageIndex: page, pageSize: size, append: false });
|
|
474
|
+
component.pageIndex = page;
|
|
475
|
+
component.pageSize = size;
|
|
464
476
|
component.append = false;
|
|
465
477
|
doSearch(component);
|
|
466
478
|
};
|
|
@@ -470,14 +482,12 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
470
482
|
running,
|
|
471
483
|
setRunning,
|
|
472
484
|
getCurrencyCode,
|
|
473
|
-
updateDateState,
|
|
474
485
|
resource: p1.resource.resource(),
|
|
475
486
|
setComponent,
|
|
476
487
|
component,
|
|
477
488
|
showMessage: p1.showMessage,
|
|
478
489
|
load,
|
|
479
|
-
|
|
480
|
-
searchOnClick,
|
|
490
|
+
search,
|
|
481
491
|
sort,
|
|
482
492
|
changeView,
|
|
483
493
|
showMore,
|