react-hook-core 0.1.0 → 0.1.4
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 +47 -37
- package/lib/core.js +14 -0
- package/lib/index.js +31 -0
- package/lib/useEdit.js +20 -21
- package/lib/useSearch.js +83 -53
- package/package.json +2 -2
- package/src/components.ts +126 -115
- package/src/core.ts +20 -14
- package/src/index.ts +39 -1
- package/src/update.ts +1 -1
- package/src/useEdit.ts +26 -26
- package/src/useSearch.ts +99 -63
- package/tsconfig.json +2 -1
package/src/core.ts
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
import * as H from 'history';
|
|
2
1
|
import {RouteComponentProps} from 'react-router';
|
|
3
|
-
import {match} from 'react-router-dom';
|
|
4
2
|
import {focusFirstElement} from './formutil';
|
|
5
3
|
|
|
6
|
-
export
|
|
7
|
-
location: H.Location;
|
|
8
|
-
history: H.History;
|
|
9
|
-
match?: match;
|
|
10
|
-
}
|
|
4
|
+
export const pageSizes = [12, 24, 60, 100, 120, 180, 300, 600];
|
|
11
5
|
export interface ModelMap {
|
|
12
6
|
[key: string]: any;
|
|
13
7
|
}
|
|
8
|
+
export interface PageChange {
|
|
9
|
+
page: number; // currentPage
|
|
10
|
+
size: number; // itemsPerPage
|
|
11
|
+
}
|
|
14
12
|
export interface ModelProps {
|
|
15
13
|
setGlobalState?: (m: ModelMap) => void;
|
|
16
14
|
shouldBeCustomized?: boolean;
|
|
@@ -99,9 +97,6 @@ export interface SearchState<T, S extends Filter> {
|
|
|
99
97
|
export interface SearchService<T, S extends Filter> {
|
|
100
98
|
keys?(): string[];
|
|
101
99
|
search(s: S, limit?: number, offset?: number|string, fields?: string[]): Promise<SearchResult<T>>;
|
|
102
|
-
}
|
|
103
|
-
export interface ModelHistoryProps extends HistoryProps, ModelProps {
|
|
104
|
-
|
|
105
100
|
}
|
|
106
101
|
export interface ViewParameter {
|
|
107
102
|
resource: ResourceService;
|
|
@@ -110,9 +105,9 @@ export interface ViewParameter {
|
|
|
110
105
|
loading?: LoadingService;
|
|
111
106
|
}
|
|
112
107
|
export interface ViewService<T, ID> {
|
|
113
|
-
metadata?(): Attributes;
|
|
108
|
+
metadata?(): Attributes|undefined;
|
|
114
109
|
keys?(): string[];
|
|
115
|
-
load(id: ID, ctx?: any): Promise<T>;
|
|
110
|
+
load(id: ID, ctx?: any): Promise<T|null>;
|
|
116
111
|
}
|
|
117
112
|
|
|
118
113
|
export interface DiffParameter {
|
|
@@ -241,7 +236,7 @@ export interface ErrorMessage {
|
|
|
241
236
|
message?: string;
|
|
242
237
|
}
|
|
243
238
|
export interface UIService {
|
|
244
|
-
getValue(el: HTMLInputElement, locale?: Locale, currencyCode?: string): string|number|boolean;
|
|
239
|
+
getValue(el: HTMLInputElement, locale?: Locale, currencyCode?: string): string|number|boolean|null|undefined;
|
|
245
240
|
decodeFromForm(form: HTMLFormElement, locale?: Locale, currencyCode?: string|null): any;
|
|
246
241
|
|
|
247
242
|
validateForm(form?: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean): boolean;
|
|
@@ -385,7 +380,7 @@ export function formatCurrency(currency: string|number, locale?: Locale, currenc
|
|
|
385
380
|
}
|
|
386
381
|
*/
|
|
387
382
|
|
|
388
|
-
export function initForm(form
|
|
383
|
+
export function initForm(form?: HTMLFormElement, initMat?: (f: HTMLFormElement) => void): HTMLFormElement|undefined {
|
|
389
384
|
if (form) {
|
|
390
385
|
setTimeout(() => {
|
|
391
386
|
if (initMat) {
|
|
@@ -501,3 +496,14 @@ export function getDecodeFromForm(u?: UIParameter, d?: (form: HTMLFormElement, l
|
|
|
501
496
|
}
|
|
502
497
|
return (u && u.ui ? u.ui.decodeFromForm : undefined);
|
|
503
498
|
}
|
|
499
|
+
export function handleToggle(target?: HTMLInputElement, on?: boolean): void {
|
|
500
|
+
if (target) {
|
|
501
|
+
if (on) {
|
|
502
|
+
if (!target.classList.contains('on')) {
|
|
503
|
+
target.classList.add('on');
|
|
504
|
+
}
|
|
505
|
+
} else {
|
|
506
|
+
target.classList.remove('on');
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -16,7 +16,45 @@ export * from './useEdit';
|
|
|
16
16
|
export * from './useSearch';
|
|
17
17
|
export * from './useMessage';
|
|
18
18
|
|
|
19
|
+
export function checked(s: string[]|string|undefined, v: string): boolean|undefined {
|
|
20
|
+
if (s) {
|
|
21
|
+
if (Array.isArray(s)) {
|
|
22
|
+
return s.includes(v);
|
|
23
|
+
} else {
|
|
24
|
+
return s === v;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
export function value<T>(obj?: T): T {
|
|
30
|
+
return (obj ? obj : {} as any);
|
|
31
|
+
}
|
|
19
32
|
export const withDefaultProps = (Component: any) => (props: RouteComponentProps) => {
|
|
20
33
|
// return <Component props={props} history={props.history} />;
|
|
21
|
-
return React.createElement(Component, { props
|
|
34
|
+
return React.createElement(Component, { props, history: props.history });
|
|
35
|
+
};
|
|
36
|
+
export interface LoadingProps {
|
|
37
|
+
error?: any;
|
|
38
|
+
}
|
|
39
|
+
export const Loading = (props: LoadingProps) => {
|
|
40
|
+
const loadingStyle = {
|
|
41
|
+
top: '30%',
|
|
42
|
+
backgroundColor: 'white',
|
|
43
|
+
border: 'none',
|
|
44
|
+
'WebkitBoxShadow': 'none',
|
|
45
|
+
'boxShadow': 'none'
|
|
46
|
+
};
|
|
47
|
+
if (props.error) {
|
|
48
|
+
return React.createElement('div', null, 'Error Load Module!'); // return (<div>Error Load Module!</div>);
|
|
49
|
+
} else {
|
|
50
|
+
return (React.createElement('div', { className: 'loader-wrapper' }, React.createElement('div', { className: 'loader-sign', style: loadingStyle }, React.createElement('div', { className: 'loader' }))));
|
|
51
|
+
/*
|
|
52
|
+
return (
|
|
53
|
+
<div className='loader-wrapper'>
|
|
54
|
+
<div className='loader-sign' style={loadingStyle}>
|
|
55
|
+
<div className='loader' />
|
|
56
|
+
</div>
|
|
57
|
+
</div>
|
|
58
|
+
);*/
|
|
59
|
+
}
|
|
22
60
|
};
|
package/src/update.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {getModelName as getModelName2, Locale,
|
|
1
|
+
import {getModelName as getModelName2, Locale, removePhoneFormat} from './core';
|
|
2
2
|
import {useMergeState} from './merge';
|
|
3
3
|
import {buildFlatState, buildState, handleEvent, handleProps, localeOf} from './state';
|
|
4
4
|
|
package/src/useEdit.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {useEffect, useState} from 'react';
|
|
2
2
|
import {RouteComponentProps} from 'react-router';
|
|
3
3
|
import {clone, makeDiff} from 'reflectx';
|
|
4
|
-
import {Attributes, buildId, createEditStatus, EditStatusConfig, getModelName as getModelName2, hideLoading, initForm, LoadingService, Locale, message, messageByHttpStatus,
|
|
4
|
+
import {Attributes, buildId, createEditStatus, EditStatusConfig, getModelName as getModelName2, hideLoading, initForm, LoadingService, Locale, message, messageByHttpStatus, ResourceService, showLoading, UIService} from './core';
|
|
5
5
|
import {build, createModel as createModel2, EditParameter, GenericService, handleStatus, handleVersion, initPropertyNullInModel, ResultInfo} from './edit';
|
|
6
6
|
import {focusFirstError, readOnly as setReadOnly} from './formutil';
|
|
7
7
|
import {DispatchWithCallback, useMergeState} from './merge';
|
|
@@ -40,8 +40,8 @@ export interface BaseEditComponentParam<T, ID> {
|
|
|
40
40
|
fail?: (result: ResultInfo<T>) => void;
|
|
41
41
|
postSave?: (obj: T, res: number|ResultInfo<T>, version?: string, backOnSave?: boolean) => void;
|
|
42
42
|
handleDuplicateKey?: (result?: ResultInfo<T>) => void;
|
|
43
|
-
load?: (i: ID, callback?: (m: T, showM: (m2: T) => void) => void) => void;
|
|
44
|
-
|
|
43
|
+
load?: (i: ID|null, callback?: (m: T, showM: (m2: T) => void) => void) => void;
|
|
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> {
|
|
@@ -57,11 +57,11 @@ export interface HookBaseEditParameter<T, ID, S> extends BaseEditComponentParam<
|
|
|
57
57
|
loading?: LoadingService;
|
|
58
58
|
}
|
|
59
59
|
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;
|
|
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;
|
|
61
61
|
callback?: (m: T, showF: (model: T) => void) => void;
|
|
62
62
|
}
|
|
63
63
|
export interface HookPropsEditParameter<T, ID, S, P extends RouteComponentProps> extends HookPropsBaseEditParameter<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;
|
|
64
|
+
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
65
|
callback?: (m: T, showF: (model: T) => void) => void;
|
|
66
66
|
}
|
|
67
67
|
export interface HookPropsBaseEditParameter<T, ID, S, P extends RouteComponentProps> extends HookBaseEditParameter<T, ID, S> {
|
|
@@ -98,19 +98,19 @@ export const useEditProps = <T, ID, S, P extends RouteComponentProps>(
|
|
|
98
98
|
let keys: string[]|undefined;
|
|
99
99
|
if (p && !p.keys && service && service.metadata) {
|
|
100
100
|
const metadata = (p.metadata ? p.metadata : service.metadata());
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
if (metadata) {
|
|
102
|
+
const meta = build(metadata);
|
|
103
|
+
keys = (p.keys ? p.keys : (meta ? meta.keys : undefined));
|
|
104
|
+
const version = (p.version ? p.version : (meta ? meta.version : undefined));
|
|
105
|
+
p.keys = keys;
|
|
106
|
+
p.version = version;
|
|
107
|
+
}
|
|
106
108
|
}
|
|
107
109
|
const id = buildId<ID>(props, keys);
|
|
108
|
-
if (
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
baseProps.load(id, p ? p.callback : undefined);
|
|
113
|
-
}
|
|
110
|
+
if (p && p.initialize) {
|
|
111
|
+
p.initialize(id, baseProps.load, baseProps.setState, p.callback);
|
|
112
|
+
} else {
|
|
113
|
+
baseProps.load(id, p ? p.callback : undefined);
|
|
114
114
|
}
|
|
115
115
|
}, []);
|
|
116
116
|
return {...baseProps};
|
|
@@ -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,9 +402,9 @@ 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
|
-
const _load = (_id: ID, callback?: (m: T, showM: (m2: T) => void) => void) => {
|
|
407
|
+
const _load = (_id: ID|null, callback?: (m: T, showM: (m2: T) => void) => void) => {
|
|
408
408
|
const id: any = _id;
|
|
409
409
|
if (id != null && id !== '') {
|
|
410
410
|
setRunning(true);
|
|
@@ -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
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {useEffect, useState} from 'react';
|
|
2
|
+
import {RouteComponentProps} from 'react-router';
|
|
2
3
|
import {useHistory, useRouteMatch} from 'react-router-dom';
|
|
3
4
|
import {clone} from 'reflectx';
|
|
4
5
|
import {addParametersIntoUrl, append, buildMessage, Filter, formatResults, getFieldsFromForm, getModel, handleAppend, handleSort, initFilter, mergeFilter as mergeFilter2, Pagination, removeSortStatus, showPaging, Sortable, validate} from 'search-core';
|
|
5
|
-
import {error, getDecodeFromForm, getName, getRemoveError, getValidateForm, hideLoading, initForm,
|
|
6
|
+
import {error, getDecodeFromForm, getName, getRemoveError, getValidateForm, handleToggle, hideLoading, initForm, Locale, PageChange, pageSizes, removeFormError, ResourceService, SearchParameter, SearchResult, SearchService, showLoading} from './core';
|
|
6
7
|
import {DispatchWithCallback, useMergeState} from './merge';
|
|
7
8
|
import {buildFromUrl} from './route';
|
|
8
9
|
import {enLocale} from './state';
|
|
@@ -23,7 +24,7 @@ export const callSearch = <T, S extends Filter>(se: S, search3: (s: S, limit?: n
|
|
|
23
24
|
}
|
|
24
25
|
let offset: number;
|
|
25
26
|
if (!se.limit || se.limit <= 0) {
|
|
26
|
-
se.limit =
|
|
27
|
+
se.limit = 24;
|
|
27
28
|
}
|
|
28
29
|
if (se.firstLimit && se.firstLimit > 0) {
|
|
29
30
|
offset = se.limit * (page - 2) + se.firstLimit;
|
|
@@ -52,13 +53,19 @@ export interface InitSearchComponentParam<T, M extends Filter, S> extends Search
|
|
|
52
53
|
createFilter?: () => M;
|
|
53
54
|
initialize?: (ld: (s: M, auto?: boolean) => void, setState2: DispatchWithCallback<Partial<S>>, com?: SearchComponentState<T, M>) => void;
|
|
54
55
|
}
|
|
55
|
-
export interface HookPropsSearchParameter<T, S extends Filter, ST, P extends
|
|
56
|
+
export interface HookPropsSearchParameter<T, S extends Filter, ST, P extends RouteComponentProps> extends HookPropsBaseSearchParameter<T, S, ST, P> {
|
|
56
57
|
createFilter?: () => S;
|
|
57
58
|
initialize?: (ld: (s: S, auto?: boolean) => void, setState2: DispatchWithCallback<Partial<ST>>, com?: SearchComponentState<T, S>) => void;
|
|
58
59
|
}
|
|
59
60
|
export interface SearchComponentParam<T, M extends Filter> {
|
|
61
|
+
addable?: boolean;
|
|
62
|
+
editable?: boolean;
|
|
63
|
+
approvable?: boolean;
|
|
64
|
+
deletable?: boolean;
|
|
65
|
+
|
|
60
66
|
keys?: string[];
|
|
61
67
|
sequenceNo?: string;
|
|
68
|
+
hideFilter?: boolean;
|
|
62
69
|
name?: string;
|
|
63
70
|
fields?: string[];
|
|
64
71
|
appendMode?: boolean;
|
|
@@ -94,7 +101,7 @@ export interface SearchComponentParam<T, M extends Filter> {
|
|
|
94
101
|
export interface HookBaseSearchParameter<T, S extends Filter, ST extends SearchComponentState<T, S>> extends SearchComponentParam<T, S> {
|
|
95
102
|
refForm: any;
|
|
96
103
|
initialState: ST;
|
|
97
|
-
|
|
104
|
+
service: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>;
|
|
98
105
|
resource: ResourceService;
|
|
99
106
|
showMessage: (msg: string) => void;
|
|
100
107
|
showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
|
|
@@ -109,7 +116,7 @@ export interface SearchComponentState<T, S> extends Pagination, Sortable {
|
|
|
109
116
|
view?: string;
|
|
110
117
|
nextPageToken?: string;
|
|
111
118
|
keys?: string[];
|
|
112
|
-
|
|
119
|
+
filter?: S;
|
|
113
120
|
list?: T[];
|
|
114
121
|
|
|
115
122
|
format?: (obj: T, locale: Locale) => T;
|
|
@@ -120,7 +127,7 @@ export interface SearchComponentState<T, S> extends Pagination, Sortable {
|
|
|
120
127
|
|
|
121
128
|
pageMaxSize?: number;
|
|
122
129
|
pageSizes?: number[];
|
|
123
|
-
excluding?:
|
|
130
|
+
excluding?: string[]|number[];
|
|
124
131
|
hideFilter?: boolean;
|
|
125
132
|
|
|
126
133
|
ignoreUrlParam?: boolean;
|
|
@@ -130,15 +137,14 @@ export interface SearchComponentState<T, S> extends Pagination, Sortable {
|
|
|
130
137
|
approvable?: boolean;
|
|
131
138
|
deletable?: boolean;
|
|
132
139
|
}
|
|
133
|
-
export const pageSizes = [10, 20, 40, 60, 100, 200, 400, 800];
|
|
134
140
|
|
|
135
|
-
function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>):
|
|
141
|
+
function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>): SearchComponentParam<T, S> {
|
|
136
142
|
if (p) {
|
|
137
143
|
if (!p.sequenceNo) {
|
|
138
144
|
p.sequenceNo = 'sequenceNo';
|
|
139
145
|
}
|
|
140
146
|
if (!p.pageSize) {
|
|
141
|
-
p.pageSize =
|
|
147
|
+
p.pageSize = 24;
|
|
142
148
|
}
|
|
143
149
|
if (!p.pageSizes) {
|
|
144
150
|
p.pageSizes = pageSizes;
|
|
@@ -146,16 +152,44 @@ function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>): void {
|
|
|
146
152
|
if (!p.pageMaxSize || p.pageMaxSize <= 0) {
|
|
147
153
|
p.pageMaxSize = 7;
|
|
148
154
|
}
|
|
155
|
+
if (p.hideFilter === undefined) {
|
|
156
|
+
p.hideFilter = true;
|
|
157
|
+
}
|
|
158
|
+
if (p.addable === undefined) {
|
|
159
|
+
p.addable = true;
|
|
160
|
+
}
|
|
161
|
+
if (p.editable === undefined) {
|
|
162
|
+
p.editable = true;
|
|
163
|
+
}
|
|
164
|
+
if (p.approvable === undefined) {
|
|
165
|
+
p.approvable = true;
|
|
166
|
+
}
|
|
167
|
+
if (p.deletable === undefined) {
|
|
168
|
+
p.deletable = true;
|
|
169
|
+
}
|
|
170
|
+
return p;
|
|
171
|
+
} else {
|
|
172
|
+
return {
|
|
173
|
+
sequenceNo: 'sequenceNo',
|
|
174
|
+
pageSize: 24,
|
|
175
|
+
pageSizes,
|
|
176
|
+
pageMaxSize: 7,
|
|
177
|
+
hideFilter: true,
|
|
178
|
+
addable: true,
|
|
179
|
+
editable: true,
|
|
180
|
+
approvable: true,
|
|
181
|
+
deletable: true
|
|
182
|
+
};
|
|
149
183
|
}
|
|
150
184
|
}
|
|
151
185
|
export const useSearch = <T, S extends Filter, ST extends SearchComponentState<T, S>>(
|
|
152
186
|
refForm: any,
|
|
153
187
|
initialState: ST,
|
|
154
|
-
|
|
188
|
+
service: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
155
189
|
p2: SearchParameter,
|
|
156
190
|
p?: InitSearchComponentParam<T, S, ST>,
|
|
157
191
|
) => {
|
|
158
|
-
const baseProps = useCoreSearch(undefined, refForm, initialState,
|
|
192
|
+
const baseProps = useCoreSearch(undefined, refForm, initialState, service, p2, p);
|
|
159
193
|
|
|
160
194
|
useEffect(() => {
|
|
161
195
|
const { load, setState, component } = baseProps;
|
|
@@ -173,39 +207,39 @@ export const useSearch = <T, S extends Filter, ST extends SearchComponentState<T
|
|
|
173
207
|
}, []);
|
|
174
208
|
return { ...baseProps };
|
|
175
209
|
};
|
|
176
|
-
export const useSearchOneProps = <T, S extends Filter, ST extends SearchComponentState<T, S>, P>(p: HookPropsSearchParameter<T, S, ST, P>) => {
|
|
177
|
-
return useSearch(p.refForm, p.initialState, p.
|
|
210
|
+
export const useSearchOneProps = <T, S extends Filter, ST extends SearchComponentState<T, S>, P extends RouteComponentProps>(p: HookPropsSearchParameter<T, S, ST, P>) => {
|
|
211
|
+
return useSearch(p.refForm, p.initialState, p.service, p, p);
|
|
178
212
|
};
|
|
179
213
|
export const useSearchOne = <T, S extends Filter, ST extends SearchComponentState<T, S>>(p: HookBaseSearchParameter<T, S, ST>) => {
|
|
180
|
-
return useCoreSearch(undefined, p.refForm, p.initialState, p.
|
|
214
|
+
return useCoreSearch(undefined, p.refForm, p.initialState, p.service, p, p);
|
|
181
215
|
};
|
|
182
216
|
export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
183
217
|
props: P|undefined,
|
|
184
218
|
refForm: any,
|
|
185
219
|
initialState: ST,
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
220
|
+
service: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
221
|
+
p1: SearchParameter,
|
|
222
|
+
p2?: SearchComponentParam<T, S>
|
|
189
223
|
) => {
|
|
190
|
-
mergeParam(
|
|
224
|
+
const p = mergeParam(p2);
|
|
191
225
|
const [running, setRunning] = useState<boolean>();
|
|
192
226
|
|
|
193
227
|
const _getModelName = (): string => {
|
|
194
|
-
return getName('filter',
|
|
228
|
+
return getName('filter', p && p.name ? p.name : undefined);
|
|
195
229
|
};
|
|
196
|
-
const getModelName = (
|
|
230
|
+
const getModelName = (p && p.getModelName ? p.getModelName : _getModelName);
|
|
197
231
|
|
|
198
232
|
// 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;
|
|
199
|
-
const baseProps = (props ? useUpdateWithProps<ST, P>(props, initialState, getModelName,
|
|
233
|
+
const baseProps = (props ? useUpdateWithProps<ST, P>(props, initialState, getModelName, p1.getLocale, getRemoveError(p1), p ? p.prepareCustomData : undefined) : useUpdate<ST>(initialState, getModelName, p1.getLocale, getRemoveError(p1)));
|
|
200
234
|
const { state, setState } = baseProps;
|
|
201
235
|
const [history, match] = [useHistory(), useRouteMatch()];
|
|
202
236
|
|
|
203
237
|
const _getCurrencyCode = (): string => {
|
|
204
238
|
return refForm && refForm.current ? refForm.current.getAttribute('currency-code') : null;
|
|
205
239
|
};
|
|
206
|
-
const getCurrencyCode =
|
|
240
|
+
const getCurrencyCode = p && p.getCurrencyCode ? p.getCurrencyCode : _getCurrencyCode;
|
|
207
241
|
|
|
208
|
-
const prepareCustomData = (
|
|
242
|
+
const prepareCustomData = (p && p.prepareCustomData ? p.prepareCustomData : prepareData);
|
|
209
243
|
const updateDateState = (name: string, value: any) => {
|
|
210
244
|
const modelName = getModelName();
|
|
211
245
|
const currentState = (state as any)[modelName];
|
|
@@ -219,10 +253,12 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
219
253
|
};
|
|
220
254
|
|
|
221
255
|
// const p = createSearchComponentState<T, S>(p1);
|
|
222
|
-
const [component, setComponent] = useMergeState<SearchComponentState<T, S>>(
|
|
256
|
+
const [component, setComponent] = useMergeState<SearchComponentState<T, S>>(p);
|
|
223
257
|
|
|
224
|
-
const toggleFilter = (event:
|
|
225
|
-
|
|
258
|
+
const toggleFilter = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void => {
|
|
259
|
+
const x = !component.hideFilter;
|
|
260
|
+
handleToggle(event.target as HTMLInputElement, !x);
|
|
261
|
+
setComponent({ hideFilter: x });
|
|
226
262
|
};
|
|
227
263
|
|
|
228
264
|
const add = (event: any) => {
|
|
@@ -236,24 +272,24 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
236
272
|
setComponent({ fields: fs, initFields: true });
|
|
237
273
|
return fs;
|
|
238
274
|
};
|
|
239
|
-
const getFields =
|
|
275
|
+
const getFields = p && p.getFields ? p.getFields : _getFields;
|
|
240
276
|
|
|
241
277
|
const getFilter = (se?: Searchable<T>): S => {
|
|
242
278
|
if (!se) {
|
|
243
279
|
se = component;
|
|
244
280
|
}
|
|
245
|
-
let keys =
|
|
246
|
-
if (!keys && typeof
|
|
247
|
-
keys =
|
|
281
|
+
let keys = p && p.keys ? p.keys : undefined;
|
|
282
|
+
if (!keys && typeof service !== 'function' && service.keys) {
|
|
283
|
+
keys = service.keys();
|
|
248
284
|
}
|
|
249
285
|
const n = getModelName();
|
|
250
|
-
let fs =
|
|
286
|
+
let fs = p && p.fields;
|
|
251
287
|
if (!fs || fs.length <= 0) {
|
|
252
288
|
fs = getFields();
|
|
253
289
|
}
|
|
254
|
-
const lc = (
|
|
290
|
+
const lc = (p1.getLocale ? p1.getLocale() : enLocale);
|
|
255
291
|
const cc = getCurrencyCode();
|
|
256
|
-
const obj3 = getModel<T, S>(state, n, se, fs, se.excluding, keys, se.list, refForm.current, getDecodeFromForm(
|
|
292
|
+
const obj3 = getModel<T, S>(state, n, se, fs, se.excluding, keys, se.list, refForm.current, getDecodeFromForm(p1), lc, cc);
|
|
257
293
|
return obj3;
|
|
258
294
|
};
|
|
259
295
|
const _setFilter = (s: S): void => {
|
|
@@ -263,7 +299,7 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
263
299
|
setState(objSet);
|
|
264
300
|
};
|
|
265
301
|
|
|
266
|
-
const setFilter =
|
|
302
|
+
const setFilter = p && p.setFilter ? p.setFilter : _setFilter;
|
|
267
303
|
|
|
268
304
|
const _load = (s: S, auto?: boolean): void => {
|
|
269
305
|
const com = Object.assign({}, component);
|
|
@@ -277,10 +313,10 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
277
313
|
}, 0);
|
|
278
314
|
}
|
|
279
315
|
};
|
|
280
|
-
const load =
|
|
316
|
+
const load = p && p.load ? p.load : _load;
|
|
281
317
|
|
|
282
318
|
const doSearch = (se: Searchable<T>, isFirstLoad?: boolean) => {
|
|
283
|
-
removeFormError(
|
|
319
|
+
removeFormError(p1, refForm.current);
|
|
284
320
|
const s = getFilter(se);
|
|
285
321
|
const isStillRunning = running;
|
|
286
322
|
validateSearch(s, () => {
|
|
@@ -288,23 +324,23 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
288
324
|
return;
|
|
289
325
|
}
|
|
290
326
|
setRunning(true);
|
|
291
|
-
showLoading(
|
|
292
|
-
if (
|
|
327
|
+
showLoading(p1.loading);
|
|
328
|
+
if (p && !p.ignoreUrlParam) {
|
|
293
329
|
addParametersIntoUrl(s, isFirstLoad);
|
|
294
330
|
}
|
|
295
|
-
const lc =
|
|
296
|
-
if (typeof
|
|
297
|
-
callSearch<T, S>(s,
|
|
331
|
+
const lc = p1.getLocale ? p1.getLocale() : enLocale;
|
|
332
|
+
if (typeof service === 'function') {
|
|
333
|
+
callSearch<T, S>(s, service, showResults, searchError, lc, se.nextPageToken);
|
|
298
334
|
} else {
|
|
299
|
-
callSearch<T, S>(s,
|
|
335
|
+
callSearch<T, S>(s, service.search, showResults, searchError, lc, se.nextPageToken);
|
|
300
336
|
}
|
|
301
337
|
});
|
|
302
338
|
};
|
|
303
339
|
|
|
304
340
|
const _validateSearch = (se: S, callback: () => void) => {
|
|
305
|
-
validate(se, callback, refForm.current, (
|
|
341
|
+
validate(se, callback, refForm.current, (p1.getLocale ? p1.getLocale() : undefined), getValidateForm(p1));
|
|
306
342
|
};
|
|
307
|
-
const validateSearch =
|
|
343
|
+
const validateSearch = p && p.validateSearch ? p.validateSearch : _validateSearch;
|
|
308
344
|
|
|
309
345
|
const pageSizeChanged = (event: any) => {
|
|
310
346
|
const size = parseInt(event.currentTarget.value, 10);
|
|
@@ -333,7 +369,7 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
333
369
|
}
|
|
334
370
|
};
|
|
335
371
|
|
|
336
|
-
const
|
|
372
|
+
const search = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void => {
|
|
337
373
|
if (event) {
|
|
338
374
|
event.preventDefault();
|
|
339
375
|
}
|
|
@@ -394,18 +430,18 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
394
430
|
|
|
395
431
|
const searchError = (err: any): void => {
|
|
396
432
|
setComponent({ pageIndex: component.tmpPageIndex });
|
|
397
|
-
error(err,
|
|
433
|
+
error(err, p1.resource.value, p1.showError);
|
|
398
434
|
};
|
|
399
|
-
const appendList = (
|
|
400
|
-
const setList = (
|
|
435
|
+
const appendList = (p && p.appendList ? p.appendList : appendListOfState);
|
|
436
|
+
const setList = (p && p.setList ? p.setList : setListOfState);
|
|
401
437
|
const _showResults = (s: S, sr: SearchResult<T>, lc: Locale) => {
|
|
402
438
|
const results = sr.list;
|
|
403
439
|
if (results && results.length > 0) {
|
|
404
|
-
formatResults(results, component.pageIndex, component.pageSize, component.
|
|
440
|
+
formatResults(results, component.pageIndex, component.pageSize, component.pageSize, p ? p.sequenceNo : undefined, p ? p.format : undefined, lc);
|
|
405
441
|
}
|
|
406
442
|
const am = component.appendMode;
|
|
407
443
|
const pi = (s.page && s.page >= 1 ? s.page : 1);
|
|
408
|
-
setComponent({
|
|
444
|
+
setComponent({ total: sr.total, pageIndex: pi, nextPageToken: sr.nextPageToken });
|
|
409
445
|
if (am) {
|
|
410
446
|
let limit = s.limit;
|
|
411
447
|
if ((!s.page || s.page <= 1) && s.firstLimit && s.firstLimit > 0) {
|
|
@@ -422,18 +458,18 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
422
458
|
setList(results, setState as any);
|
|
423
459
|
setComponent({ tmpPageIndex: s.page });
|
|
424
460
|
if (s.limit) {
|
|
425
|
-
const m1 = buildMessage(
|
|
426
|
-
|
|
461
|
+
const m1 = buildMessage(p1.resource, s.page, s.limit, sr.list, sr.total);
|
|
462
|
+
p1.showMessage(m1);
|
|
427
463
|
}
|
|
428
464
|
}
|
|
429
465
|
setRunning(false);
|
|
430
|
-
hideLoading(
|
|
466
|
+
hideLoading(p1.loading);
|
|
431
467
|
if (component.triggerSearch) {
|
|
432
468
|
setComponent({ triggerSearch: false });
|
|
433
469
|
resetAndSearch();
|
|
434
470
|
}
|
|
435
471
|
};
|
|
436
|
-
const showResults = (
|
|
472
|
+
const showResults = (p && p.showResults ? p.showResults : _showResults);
|
|
437
473
|
|
|
438
474
|
const showMore = (event: any) => {
|
|
439
475
|
event.preventDefault();
|
|
@@ -446,11 +482,11 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
446
482
|
doSearch(component);
|
|
447
483
|
};
|
|
448
484
|
|
|
449
|
-
const pageChanged = (data:
|
|
450
|
-
const {
|
|
451
|
-
setComponent({ pageIndex:
|
|
452
|
-
component.pageIndex =
|
|
453
|
-
component.pageSize =
|
|
485
|
+
const pageChanged = (data: PageChange) => {
|
|
486
|
+
const { page, size } = data;
|
|
487
|
+
setComponent({ pageIndex: page, pageSize: size, append: false });
|
|
488
|
+
component.pageIndex = page;
|
|
489
|
+
component.pageSize = size;
|
|
454
490
|
component.append = false;
|
|
455
491
|
doSearch(component);
|
|
456
492
|
};
|
|
@@ -461,13 +497,13 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
461
497
|
setRunning,
|
|
462
498
|
getCurrencyCode,
|
|
463
499
|
updateDateState,
|
|
464
|
-
resource:
|
|
500
|
+
resource: p1.resource.resource(),
|
|
465
501
|
setComponent,
|
|
466
502
|
component,
|
|
467
|
-
showMessage:
|
|
503
|
+
showMessage: p1.showMessage,
|
|
468
504
|
load,
|
|
469
505
|
add,
|
|
470
|
-
|
|
506
|
+
search,
|
|
471
507
|
sort,
|
|
472
508
|
changeView,
|
|
473
509
|
showMore,
|
|
@@ -479,6 +515,6 @@ export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
|
479
515
|
showResults,
|
|
480
516
|
getFields,
|
|
481
517
|
getModelName,
|
|
482
|
-
format:
|
|
518
|
+
format: p ? p.format : undefined
|
|
483
519
|
};
|
|
484
520
|
};
|