react-hook-core 0.0.1 → 0.1.0
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 +370 -486
- package/lib/core.js +89 -27
- package/lib/diff.js +3 -0
- package/lib/edit.js +35 -33
- package/lib/formutil.js +5 -2
- package/lib/index.js +7 -0
- package/lib/merge.js +1 -1
- package/lib/state.js +17 -10
- package/lib/update.js +36 -55
- package/lib/useEdit.js +181 -327
- package/lib/useMessage.js +25 -0
- package/lib/useSearch.js +118 -238
- package/lib/useView.js +70 -161
- package/lib/util.js +6 -2
- package/package.json +7 -9
- package/src/components.ts +345 -301
- package/src/core.ts +110 -49
- package/src/diff.ts +4 -1
- package/src/edit.ts +44 -42
- package/src/formutil.ts +7 -4
- package/src/index.ts +8 -0
- package/src/merge.ts +3 -3
- package/src/state.ts +25 -19
- package/src/update.ts +35 -58
- package/src/useEdit.ts +165 -244
- package/src/useMessage.ts +37 -0
- package/src/useSearch.ts +145 -225
- package/src/useView.ts +64 -101
- package/src/util.ts +8 -4
- package/tsconfig.json +1 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
interface ErrorMessage {
|
|
4
|
+
field: string;
|
|
5
|
+
code: string;
|
|
6
|
+
param?: string|number|Date;
|
|
7
|
+
message?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface MessageState {
|
|
11
|
+
message?: string;
|
|
12
|
+
alertClass?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const useMessage = (initialState: MessageState) => {
|
|
16
|
+
const [msg, setMessage] = React.useState<MessageState>(initialState);
|
|
17
|
+
|
|
18
|
+
const hideMessage = () => {
|
|
19
|
+
setMessage({alertClass: '', message: ''});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const showMessage = (ms: string) => {
|
|
23
|
+
setMessage({alertClass: 'alert alert-info', message: ms});
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const showError = (ms: string|ErrorMessage[]) => {
|
|
27
|
+
if (typeof ms === 'string') {
|
|
28
|
+
setMessage({alertClass: 'alert alert-error', message: ms});
|
|
29
|
+
} else if (Array.isArray(ms) && ms.length > 0) {
|
|
30
|
+
setMessage({alertClass: 'alert alert-error', message: ms[0].message});
|
|
31
|
+
} else {
|
|
32
|
+
const x = JSON.stringify(ms);
|
|
33
|
+
setMessage({alertClass: 'alert alert-error', message: x});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
return {msg, showError, showMessage, hideMessage};
|
|
37
|
+
};
|
package/src/useSearch.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {useEffect, useState} from 'react';
|
|
2
2
|
import {useHistory, useRouteMatch} from 'react-router-dom';
|
|
3
3
|
import {clone} from 'reflectx';
|
|
4
|
-
import {addParametersIntoUrl, append,
|
|
5
|
-
import {error, initForm, LoadingService, Locale, ModelProps, ResourceService, SearchParameter,
|
|
4
|
+
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, LoadingService, Locale, ModelProps, removeFormError, ResourceService, SearchParameter, SearchResult, SearchService, showLoading, UIService} from './core';
|
|
6
6
|
import {DispatchWithCallback, useMergeState} from './merge';
|
|
7
7
|
import {buildFromUrl} from './route';
|
|
8
|
+
import {enLocale} from './state';
|
|
8
9
|
import {useUpdate, useUpdateWithProps} from './update';
|
|
9
10
|
|
|
10
11
|
export interface Searchable<T> extends Pagination, Sortable {
|
|
@@ -14,54 +15,54 @@ export interface Searchable<T> extends Pagination, Sortable {
|
|
|
14
15
|
}
|
|
15
16
|
function prepareData(data: any): void {
|
|
16
17
|
}
|
|
17
|
-
export const callSearch =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const next = (nextPageToken && nextPageToken.length > 0 ? nextPageToken : offset);
|
|
32
|
-
const fields = se.fields;
|
|
33
|
-
delete se['page'];
|
|
34
|
-
delete se['fields'];
|
|
35
|
-
delete se['limit'];
|
|
36
|
-
delete se['firstLimit'];
|
|
37
|
-
const sr = await search3(s, limit, next, fields);
|
|
38
|
-
showResults3(s, sr, lc);
|
|
39
|
-
} catch (err) {
|
|
40
|
-
searchError3(err);
|
|
18
|
+
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) => {
|
|
19
|
+
const s = clone(se);
|
|
20
|
+
let page = se.page;
|
|
21
|
+
if (!page || page < 1) {
|
|
22
|
+
page = 1;
|
|
23
|
+
}
|
|
24
|
+
let offset: number;
|
|
25
|
+
if (!se.limit || se.limit <= 0) {
|
|
26
|
+
se.limit = 20;
|
|
27
|
+
}
|
|
28
|
+
if (se.firstLimit && se.firstLimit > 0) {
|
|
29
|
+
offset = se.limit * (page - 2) + se.firstLimit;
|
|
30
|
+
} else {
|
|
31
|
+
offset = se.limit * (page - 1);
|
|
41
32
|
}
|
|
33
|
+
const limit = (page <= 1 && se.firstLimit && se.firstLimit > 0 ? se.firstLimit : se.limit);
|
|
34
|
+
const next = (nextPageToken && nextPageToken.length > 0 ? nextPageToken : offset);
|
|
35
|
+
const fields = se.fields;
|
|
36
|
+
delete se['page'];
|
|
37
|
+
delete se['fields'];
|
|
38
|
+
delete se['limit'];
|
|
39
|
+
delete se['firstLimit'];
|
|
40
|
+
search3(s, limit, next, fields).then(sr => {
|
|
41
|
+
showResults3(s, sr, lc);
|
|
42
|
+
}).catch(err => searchError3(err));
|
|
42
43
|
};
|
|
43
|
-
const appendListOfState = <T, S extends
|
|
44
|
+
const appendListOfState = <T, S extends Filter>(results: T[], list: T[]|undefined, setState2: DispatchWithCallback<Partial<SearchComponentState<T, S>>>) => {
|
|
44
45
|
const arr = append(list, results);
|
|
45
46
|
setState2({ list: arr } as any);
|
|
46
47
|
};
|
|
47
|
-
const setListOfState = <T, S extends
|
|
48
|
+
const setListOfState = <T, S extends Filter>(list: T[], setState2: DispatchWithCallback<Partial<SearchComponentState<T, S>>>) => {
|
|
48
49
|
setState2({ list } as any);
|
|
49
50
|
};
|
|
50
|
-
export interface InitSearchComponentParam<T, M extends
|
|
51
|
-
|
|
51
|
+
export interface InitSearchComponentParam<T, M extends Filter, S> extends SearchComponentParam<T, M> {
|
|
52
|
+
createFilter?: () => M;
|
|
52
53
|
initialize?: (ld: (s: M, auto?: boolean) => void, setState2: DispatchWithCallback<Partial<S>>, com?: SearchComponentState<T, M>) => void;
|
|
53
54
|
}
|
|
54
|
-
export interface HookPropsSearchParameter<T, S extends
|
|
55
|
-
|
|
55
|
+
export interface HookPropsSearchParameter<T, S extends Filter, ST, P extends ModelProps> extends HookPropsBaseSearchParameter<T, S, ST, P> {
|
|
56
|
+
createFilter?: () => S;
|
|
56
57
|
initialize?: (ld: (s: S, auto?: boolean) => void, setState2: DispatchWithCallback<Partial<ST>>, com?: SearchComponentState<T, S>) => void;
|
|
57
58
|
}
|
|
58
|
-
export interface SearchComponentParam<T, M extends
|
|
59
|
+
export interface SearchComponentParam<T, M extends Filter> {
|
|
59
60
|
keys?: string[];
|
|
60
61
|
sequenceNo?: string;
|
|
61
62
|
name?: string;
|
|
63
|
+
fields?: string[];
|
|
62
64
|
appendMode?: boolean;
|
|
63
65
|
pageSizes?: number[];
|
|
64
|
-
displayFields?: string[];
|
|
65
66
|
pageIndex?: number;
|
|
66
67
|
pageSize?: number;
|
|
67
68
|
initPageSize?: number;
|
|
@@ -71,15 +72,16 @@ export interface SearchComponentParam<T, M extends SearchModel> {
|
|
|
71
72
|
load?: (s: M, auto?: boolean) => void;
|
|
72
73
|
getModelName?: () => string;
|
|
73
74
|
getCurrencyCode?: () => string;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
getFields?: () => string[];
|
|
75
|
+
setFilter?: (s: M) => void;
|
|
76
|
+
getFilter?: () => M;
|
|
77
|
+
getFields?: () => string[]|undefined;
|
|
77
78
|
validateSearch?: (se: M, callback: () => void) => void;
|
|
78
79
|
prepareCustomData?: (data: any) => void;
|
|
79
|
-
format?(obj: T, locale
|
|
80
|
+
format?(obj: T, locale?: Locale): T;
|
|
80
81
|
showResults?(s: M, sr: SearchResult<T>, lc: Locale): void;
|
|
81
|
-
appendList?(results: T[], list: T[], s: DispatchWithCallback<Partial<SearchComponentState<T, M>>>): void;
|
|
82
|
+
appendList?(results: T[], list: T[]|undefined, s: DispatchWithCallback<Partial<SearchComponentState<T, M>>>): void;
|
|
82
83
|
setList?(list: T[], s: DispatchWithCallback<Partial<SearchComponentState<T, M>>>): void;
|
|
84
|
+
/*
|
|
83
85
|
showLoading?(firstTime?: boolean): void;
|
|
84
86
|
hideLoading?(): void;
|
|
85
87
|
decodeFromForm?(form: HTMLFormElement, locale?: Locale, currencyCode?: string): any;
|
|
@@ -87,8 +89,9 @@ export interface SearchComponentParam<T, M extends SearchModel> {
|
|
|
87
89
|
validateForm?(form: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean): boolean;
|
|
88
90
|
removeFormError?(form: HTMLFormElement): void;
|
|
89
91
|
removeError?(el: HTMLInputElement): void;
|
|
92
|
+
*/
|
|
90
93
|
}
|
|
91
|
-
export interface HookBaseSearchParameter<T, S extends
|
|
94
|
+
export interface HookBaseSearchParameter<T, S extends Filter, ST extends SearchComponentState<T, S>> extends SearchComponentParam<T, S> {
|
|
92
95
|
refForm: any;
|
|
93
96
|
initialState: ST;
|
|
94
97
|
search: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>;
|
|
@@ -98,18 +101,19 @@ export interface HookBaseSearchParameter<T, S extends SearchModel, ST extends Se
|
|
|
98
101
|
getLocale?: () => Locale;
|
|
99
102
|
autoSearch?: boolean;
|
|
100
103
|
}
|
|
101
|
-
export interface HookPropsBaseSearchParameter<T, S extends
|
|
104
|
+
export interface HookPropsBaseSearchParameter<T, S extends Filter, ST, P> extends HookBaseSearchParameter<T, S, ST> {
|
|
102
105
|
props?: P;
|
|
103
106
|
prepareCustomData?: (data: any) => void;
|
|
104
107
|
}
|
|
105
108
|
export interface SearchComponentState<T, S> extends Pagination, Sortable {
|
|
109
|
+
view?: string;
|
|
106
110
|
nextPageToken?: string;
|
|
107
111
|
keys?: string[];
|
|
108
112
|
model?: S;
|
|
109
113
|
list?: T[];
|
|
110
114
|
|
|
111
115
|
format?: (obj: T, locale: Locale) => T;
|
|
112
|
-
|
|
116
|
+
fields?: string[];
|
|
113
117
|
initFields?: boolean;
|
|
114
118
|
triggerSearch?: boolean;
|
|
115
119
|
tmpPageIndex?: number;
|
|
@@ -128,185 +132,94 @@ export interface SearchComponentState<T, S> extends Pagination, Sortable {
|
|
|
128
132
|
}
|
|
129
133
|
export const pageSizes = [10, 20, 40, 60, 100, 200, 400, 800];
|
|
130
134
|
|
|
131
|
-
function
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
pageSize: p.pageSize,
|
|
136
|
-
initPageSize: p.initPageSize,
|
|
137
|
-
pageSizes: p.pageSizes,
|
|
138
|
-
appendMode: p.appendMode,
|
|
139
|
-
displayFields: p.displayFields,
|
|
140
|
-
pageMaxSize: (p.pageMaxSize && p.pageMaxSize > 0 ? p.pageMaxSize : 7)
|
|
141
|
-
};
|
|
142
|
-
if (p2) {
|
|
143
|
-
p3.viewable = p2.viewable;
|
|
144
|
-
p3.addable = p2.addable;
|
|
145
|
-
p3.editable = p2.editable;
|
|
146
|
-
p3.deletable = p2.deletable;
|
|
147
|
-
p3.approvable = p2.approvable;
|
|
148
|
-
} else {
|
|
149
|
-
p3.viewable = true;
|
|
150
|
-
p3.addable = true;
|
|
151
|
-
p3.editable = true;
|
|
152
|
-
}
|
|
153
|
-
return p3;
|
|
154
|
-
}
|
|
155
|
-
function mergeParam<T, S extends SearchModel>(p: SearchComponentParam<T, S>, ui?: UIService, loading?: LoadingService): void {
|
|
156
|
-
if (!p.sequenceNo) {
|
|
157
|
-
p.sequenceNo = 'sequenceNo';
|
|
158
|
-
}
|
|
159
|
-
if (!p.pageIndex || p.pageIndex < 1) {
|
|
160
|
-
p.pageIndex = 1;
|
|
161
|
-
}
|
|
162
|
-
if (!p.pageSize) {
|
|
163
|
-
p.pageSize = 20;
|
|
164
|
-
}
|
|
165
|
-
if (!p.initPageSize) {
|
|
166
|
-
p.initPageSize = p.pageSize;
|
|
167
|
-
}
|
|
168
|
-
if (!p.pageSizes) {
|
|
169
|
-
p.pageSizes = pageSizes;
|
|
170
|
-
}
|
|
171
|
-
if (!p.pageMaxSize) {
|
|
172
|
-
p.pageMaxSize = 7;
|
|
173
|
-
}
|
|
174
|
-
if (ui) {
|
|
175
|
-
if (!p.decodeFromForm) {
|
|
176
|
-
p.decodeFromForm = ui.decodeFromForm;
|
|
177
|
-
}
|
|
178
|
-
if (!p.registerEvents) {
|
|
179
|
-
p.registerEvents = ui.registerEvents;
|
|
135
|
+
function mergeParam<T, S extends Filter>(p?: SearchComponentParam<T, S>): void {
|
|
136
|
+
if (p) {
|
|
137
|
+
if (!p.sequenceNo) {
|
|
138
|
+
p.sequenceNo = 'sequenceNo';
|
|
180
139
|
}
|
|
181
|
-
if (!p.
|
|
182
|
-
p.
|
|
140
|
+
if (!p.pageSize) {
|
|
141
|
+
p.pageSize = 20;
|
|
183
142
|
}
|
|
184
|
-
if (!p.
|
|
185
|
-
p.
|
|
143
|
+
if (!p.pageSizes) {
|
|
144
|
+
p.pageSizes = pageSizes;
|
|
186
145
|
}
|
|
187
|
-
if (!p.
|
|
188
|
-
p.
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
if (loading) {
|
|
192
|
-
if (!p.showLoading) {
|
|
193
|
-
p.showLoading = loading.showLoading;
|
|
194
|
-
}
|
|
195
|
-
if (!p.hideLoading) {
|
|
196
|
-
p.hideLoading = loading.hideLoading;
|
|
146
|
+
if (!p.pageMaxSize || p.pageMaxSize <= 0) {
|
|
147
|
+
p.pageMaxSize = 7;
|
|
197
148
|
}
|
|
198
149
|
}
|
|
199
150
|
}
|
|
200
|
-
export const useSearch = <T, S extends
|
|
151
|
+
export const useSearch = <T, S extends Filter, ST extends SearchComponentState<T, S>>(
|
|
201
152
|
refForm: any,
|
|
202
153
|
initialState: ST,
|
|
203
154
|
search: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
204
|
-
p1: InitSearchComponentParam<T, S, ST>,
|
|
205
155
|
p2: SearchParameter,
|
|
206
|
-
|
|
156
|
+
p?: InitSearchComponentParam<T, S, ST>,
|
|
207
157
|
) => {
|
|
208
|
-
const baseProps =
|
|
158
|
+
const baseProps = useCoreSearch(undefined, refForm, initialState, search, p2, p);
|
|
209
159
|
|
|
210
160
|
useEffect(() => {
|
|
211
161
|
const { load, setState, component } = baseProps;
|
|
212
162
|
if (refForm) {
|
|
213
|
-
const registerEvents = (p2.ui ? p2.ui.registerEvents :
|
|
163
|
+
const registerEvents = (p2.ui ? p2.ui.registerEvents : undefined);
|
|
214
164
|
initForm(refForm.current, registerEvents);
|
|
215
165
|
}
|
|
216
|
-
if (
|
|
217
|
-
p1.initialize(load, setState, component);
|
|
218
|
-
} else {
|
|
219
|
-
const se: S = (p1.createSearchModel ? p1.createSearchModel() : null);
|
|
220
|
-
const s: any = mergeSearchModel2(buildFromUrl<S>(), se, component.pageSizes);
|
|
221
|
-
load(s, p2.auto);
|
|
222
|
-
}
|
|
223
|
-
}, []);
|
|
224
|
-
return { ...baseProps };
|
|
225
|
-
};
|
|
226
|
-
export const useSearchOneWithProps = <T, S extends SearchModel, ST extends SearchComponentState<T, S>, P>(p: HookPropsSearchParameter<T, S, ST, P>) => {
|
|
227
|
-
const baseProps = useBaseSearchOne(p);
|
|
228
|
-
/*
|
|
229
|
-
useEffect(() => {
|
|
230
|
-
if (!component.isFirstTime) {
|
|
231
|
-
doSearch();
|
|
232
|
-
}
|
|
233
|
-
}, [component.pageSize, component.pageIndex]);
|
|
234
|
-
*/
|
|
235
|
-
useEffect(() => {
|
|
236
|
-
const { load, setState, component } = baseProps;
|
|
237
|
-
if (p.refForm) {
|
|
238
|
-
initForm(p.refForm.current, p.registerEvents);
|
|
239
|
-
}
|
|
240
|
-
if (p.initialize) {
|
|
166
|
+
if (p && p.initialize) {
|
|
241
167
|
p.initialize(load, setState, component);
|
|
242
168
|
} else {
|
|
243
|
-
const se: S = (p.
|
|
244
|
-
const s: any =
|
|
245
|
-
load(s,
|
|
169
|
+
const se: S|undefined = (p && p.createFilter ? p.createFilter() : undefined);
|
|
170
|
+
const s: any = mergeFilter2(buildFromUrl<S>(), se, component.pageSizes);
|
|
171
|
+
load(s, p2.auto);
|
|
246
172
|
}
|
|
247
173
|
}, []);
|
|
248
|
-
|
|
249
174
|
return { ...baseProps };
|
|
250
175
|
};
|
|
251
|
-
export const
|
|
252
|
-
return
|
|
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.search, p, p);
|
|
253
178
|
};
|
|
254
|
-
export const
|
|
255
|
-
refForm
|
|
256
|
-
initialState: ST,
|
|
257
|
-
search: ((s: S, ctx?: any) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
258
|
-
p1: SearchComponentParam<T, S>,
|
|
259
|
-
p2: SearchParameter,
|
|
260
|
-
p3?: SearchPermission
|
|
261
|
-
) => {
|
|
262
|
-
return useBaseSearchWithProps(null, refForm, initialState, search, p1, p2, p3);
|
|
179
|
+
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.search, p, p);
|
|
263
181
|
};
|
|
264
|
-
export const
|
|
265
|
-
props: P,
|
|
182
|
+
export const useCoreSearch = <T, S extends Filter, ST, P>(
|
|
183
|
+
props: P|undefined,
|
|
266
184
|
refForm: any,
|
|
267
185
|
initialState: ST,
|
|
268
186
|
search: ((s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
269
|
-
p1: SearchComponentParam<T, S>,
|
|
270
187
|
p2: SearchParameter,
|
|
271
|
-
|
|
188
|
+
p1?: SearchComponentParam<T, S>
|
|
272
189
|
) => {
|
|
273
|
-
mergeParam(p1
|
|
274
|
-
const [running, setRunning] = useState(
|
|
190
|
+
mergeParam(p1);
|
|
191
|
+
const [running, setRunning] = useState<boolean>();
|
|
275
192
|
|
|
276
193
|
const _getModelName = (): string => {
|
|
277
|
-
|
|
278
|
-
return p1.name;
|
|
279
|
-
} else {
|
|
280
|
-
return 'model';
|
|
281
|
-
}
|
|
194
|
+
return getName('filter', p1 && p1.name ? p1.name : undefined);
|
|
282
195
|
};
|
|
283
|
-
const getModelName = (p1.getModelName ? p1.getModelName : _getModelName);
|
|
196
|
+
const getModelName = (p1 && p1.getModelName ? p1.getModelName : _getModelName);
|
|
284
197
|
|
|
285
198
|
// 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;
|
|
286
|
-
const baseProps = (props ? useUpdateWithProps<ST, P>(props, initialState, p2.getLocale,
|
|
199
|
+
const baseProps = (props ? useUpdateWithProps<ST, P>(props, initialState, getModelName, p2.getLocale, getRemoveError(p2), p1 ? p1.prepareCustomData : undefined) : useUpdate<ST>(initialState, getModelName, p2.getLocale, getRemoveError(p2)));
|
|
287
200
|
const { state, setState } = baseProps;
|
|
288
201
|
const [history, match] = [useHistory(), useRouteMatch()];
|
|
289
202
|
|
|
290
203
|
const _getCurrencyCode = (): string => {
|
|
291
204
|
return refForm && refForm.current ? refForm.current.getAttribute('currency-code') : null;
|
|
292
205
|
};
|
|
293
|
-
const getCurrencyCode = p1.getCurrencyCode ? p1.getCurrencyCode : _getCurrencyCode;
|
|
206
|
+
const getCurrencyCode = p1 && p1.getCurrencyCode ? p1.getCurrencyCode : _getCurrencyCode;
|
|
294
207
|
|
|
295
|
-
const prepareCustomData = (p1.prepareCustomData ? p1.prepareCustomData : prepareData);
|
|
208
|
+
const prepareCustomData = (p1 && p1.prepareCustomData ? p1.prepareCustomData : prepareData);
|
|
296
209
|
const updateDateState = (name: string, value: any) => {
|
|
297
210
|
const modelName = getModelName();
|
|
298
|
-
const currentState = state[modelName];
|
|
299
|
-
if (props.setGlobalState) {
|
|
300
|
-
const data = props.shouldBeCustomized ? prepareCustomData({ [name]: value }) : { [name]: value };
|
|
301
|
-
props.setGlobalState({ [modelName]: { ...currentState, ...data } });
|
|
211
|
+
const currentState = (state as any)[modelName];
|
|
212
|
+
if (props && (props as any).setGlobalState) {
|
|
213
|
+
const data = (props as any).shouldBeCustomized ? prepareCustomData({ [name]: value }) : { [name]: value };
|
|
214
|
+
(props as any).setGlobalState({ [modelName]: { ...currentState, ...data } });
|
|
302
215
|
} else {
|
|
303
216
|
setState({ [modelName]: { ...currentState, [name]: value } } as T);
|
|
304
217
|
}
|
|
305
218
|
setState({ [modelName]: { ...currentState, [name]: value } } as T);
|
|
306
219
|
};
|
|
307
220
|
|
|
308
|
-
const p = createSearchComponentState<T, S>(p1
|
|
309
|
-
const [component, setComponent] = useMergeState<SearchComponentState<T, S>>(
|
|
221
|
+
// const p = createSearchComponentState<T, S>(p1);
|
|
222
|
+
const [component, setComponent] = useMergeState<SearchComponentState<T, S>>(p1);
|
|
310
223
|
|
|
311
224
|
const toggleFilter = (event: any): void => {
|
|
312
225
|
setComponent({ hideFilter: !component.hideFilter });
|
|
@@ -317,46 +230,46 @@ export const useBaseSearchWithProps = <T, S extends SearchModel, ST, P extends M
|
|
|
317
230
|
history.push(match.url + '/add');
|
|
318
231
|
};
|
|
319
232
|
|
|
320
|
-
const _getFields = (): string[] => {
|
|
321
|
-
const {
|
|
322
|
-
const fs =
|
|
323
|
-
setComponent({
|
|
233
|
+
const _getFields = (): string[]|undefined => {
|
|
234
|
+
const { fields, initFields } = component;
|
|
235
|
+
const fs = getFieldsFromForm(fields, initFields, refForm.current);
|
|
236
|
+
setComponent({ fields: fs, initFields: true });
|
|
324
237
|
return fs;
|
|
325
238
|
};
|
|
326
|
-
const getFields = p1.getFields ? p1.getFields : _getFields;
|
|
239
|
+
const getFields = p1 && p1.getFields ? p1.getFields : _getFields;
|
|
327
240
|
|
|
328
|
-
const
|
|
241
|
+
const getFilter = (se?: Searchable<T>): S => {
|
|
329
242
|
if (!se) {
|
|
330
243
|
se = component;
|
|
331
244
|
}
|
|
332
|
-
let keys = p1.keys;
|
|
333
|
-
if (!keys && typeof search !== 'function') {
|
|
245
|
+
let keys = p1 && p1.keys ? p1.keys : undefined;
|
|
246
|
+
if (!keys && typeof search !== 'function' && search.keys) {
|
|
334
247
|
keys = search.keys();
|
|
335
248
|
}
|
|
336
249
|
const n = getModelName();
|
|
337
|
-
let fs = p1.
|
|
250
|
+
let fs = p1 && p1.fields;
|
|
338
251
|
if (!fs || fs.length <= 0) {
|
|
339
252
|
fs = getFields();
|
|
340
253
|
}
|
|
341
|
-
const lc = p2.getLocale();
|
|
254
|
+
const lc = (p2.getLocale ? p2.getLocale() : enLocale);
|
|
342
255
|
const cc = getCurrencyCode();
|
|
343
|
-
const obj3 = getModel<T, S>(state, n, se, fs, se.excluding, keys, se.list, refForm.current,
|
|
256
|
+
const obj3 = getModel<T, S>(state, n, se, fs, se.excluding, keys, se.list, refForm.current, getDecodeFromForm(p2), lc, cc);
|
|
344
257
|
return obj3;
|
|
345
258
|
};
|
|
346
|
-
const
|
|
259
|
+
const _setFilter = (s: S): void => {
|
|
347
260
|
const objSet: any = {};
|
|
348
261
|
const n = getModelName();
|
|
349
262
|
objSet[n] = s;
|
|
350
263
|
setState(objSet);
|
|
351
264
|
};
|
|
352
265
|
|
|
353
|
-
const
|
|
266
|
+
const setFilter = p1 && p1.setFilter ? p1.setFilter : _setFilter;
|
|
354
267
|
|
|
355
268
|
const _load = (s: S, auto?: boolean): void => {
|
|
356
269
|
const com = Object.assign({}, component);
|
|
357
|
-
const obj2 =
|
|
270
|
+
const obj2 = initFilter(s, com);
|
|
358
271
|
setComponent(com);
|
|
359
|
-
|
|
272
|
+
setFilter(obj2);
|
|
360
273
|
const runSearch = doSearch;
|
|
361
274
|
if (auto) {
|
|
362
275
|
setTimeout(() => {
|
|
@@ -364,27 +277,22 @@ export const useBaseSearchWithProps = <T, S extends SearchModel, ST, P extends M
|
|
|
364
277
|
}, 0);
|
|
365
278
|
}
|
|
366
279
|
};
|
|
367
|
-
const load = p1.load ? p1.load : _load;
|
|
280
|
+
const load = p1 && p1.load ? p1.load : _load;
|
|
368
281
|
|
|
369
282
|
const doSearch = (se: Searchable<T>, isFirstLoad?: boolean) => {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
p1.removeFormError(f);
|
|
373
|
-
}
|
|
374
|
-
const s = getSearchModel(se);
|
|
283
|
+
removeFormError(p2, refForm.current);
|
|
284
|
+
const s = getFilter(se);
|
|
375
285
|
const isStillRunning = running;
|
|
376
286
|
validateSearch(s, () => {
|
|
377
287
|
if (isStillRunning === true) {
|
|
378
288
|
return;
|
|
379
289
|
}
|
|
380
290
|
setRunning(true);
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}
|
|
384
|
-
if (!p1.ignoreUrlParam) {
|
|
291
|
+
showLoading(p2.loading);
|
|
292
|
+
if (p1 && !p1.ignoreUrlParam) {
|
|
385
293
|
addParametersIntoUrl(s, isFirstLoad);
|
|
386
294
|
}
|
|
387
|
-
const lc = p2.getLocale();
|
|
295
|
+
const lc = p2.getLocale ? p2.getLocale() : enLocale;
|
|
388
296
|
if (typeof search === 'function') {
|
|
389
297
|
callSearch<T, S>(s, search, showResults, searchError, lc, se.nextPageToken);
|
|
390
298
|
} else {
|
|
@@ -394,9 +302,9 @@ export const useBaseSearchWithProps = <T, S extends SearchModel, ST, P extends M
|
|
|
394
302
|
};
|
|
395
303
|
|
|
396
304
|
const _validateSearch = (se: S, callback: () => void) => {
|
|
397
|
-
validate(se, callback, refForm.current, p2.getLocale(),
|
|
305
|
+
validate(se, callback, refForm.current, (p2.getLocale ? p2.getLocale() : undefined), getValidateForm(p2));
|
|
398
306
|
};
|
|
399
|
-
const validateSearch = p1.validateSearch ? p1.validateSearch : _validateSearch;
|
|
307
|
+
const validateSearch = p1 && p1.validateSearch ? p1.validateSearch : _validateSearch;
|
|
400
308
|
|
|
401
309
|
const pageSizeChanged = (event: any) => {
|
|
402
310
|
const size = parseInt(event.currentTarget.value, 10);
|
|
@@ -411,10 +319,10 @@ export const useBaseSearchWithProps = <T, S extends SearchModel, ST, P extends M
|
|
|
411
319
|
doSearch(component);
|
|
412
320
|
};
|
|
413
321
|
|
|
414
|
-
const
|
|
322
|
+
const clearQ = (event?: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
415
323
|
const n = getModelName();
|
|
416
324
|
if (n && n.length > 0) {
|
|
417
|
-
const m = state[n];
|
|
325
|
+
const m = (state as any)[n];
|
|
418
326
|
if (m) {
|
|
419
327
|
m.keyword = '';
|
|
420
328
|
const setObj: any = {};
|
|
@@ -452,6 +360,17 @@ export const useBaseSearchWithProps = <T, S extends SearchModel, ST, P extends M
|
|
|
452
360
|
resetAndSearch();
|
|
453
361
|
}
|
|
454
362
|
};
|
|
363
|
+
const changeView = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>, view?: string) => {
|
|
364
|
+
if (view && view.length > 0) {
|
|
365
|
+
setComponent({view});
|
|
366
|
+
} else if (event && event.target) {
|
|
367
|
+
const target = event.target as any;
|
|
368
|
+
const v: string = target.getAttribute('data-view');
|
|
369
|
+
if (v && v.length > 0) {
|
|
370
|
+
setComponent({view: v});
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
};
|
|
455
374
|
|
|
456
375
|
const resetAndSearch = () => {
|
|
457
376
|
if (running === true) {
|
|
@@ -461,13 +380,13 @@ export const useBaseSearchWithProps = <T, S extends SearchModel, ST, P extends M
|
|
|
461
380
|
setComponent({ pageIndex: 1, tmpPageIndex: 1 });
|
|
462
381
|
removeSortStatus(component.sortTarget);
|
|
463
382
|
setComponent({
|
|
464
|
-
sortTarget:
|
|
465
|
-
sortField:
|
|
383
|
+
sortTarget: undefined,
|
|
384
|
+
sortField: undefined,
|
|
466
385
|
append: false,
|
|
467
386
|
pageIndex: 1
|
|
468
387
|
});
|
|
469
|
-
component.sortTarget =
|
|
470
|
-
component.sortField =
|
|
388
|
+
component.sortTarget = undefined;
|
|
389
|
+
component.sortField = undefined;
|
|
471
390
|
component.append = false;
|
|
472
391
|
component.pageIndex = 1;
|
|
473
392
|
doSearch(component);
|
|
@@ -477,48 +396,48 @@ export const useBaseSearchWithProps = <T, S extends SearchModel, ST, P extends M
|
|
|
477
396
|
setComponent({ pageIndex: component.tmpPageIndex });
|
|
478
397
|
error(err, p2.resource.value, p2.showError);
|
|
479
398
|
};
|
|
480
|
-
const appendList = (p1.appendList ? p1.appendList : appendListOfState);
|
|
481
|
-
const setList = (p1.setList ? p1.setList : setListOfState);
|
|
399
|
+
const appendList = (p1 && p1.appendList ? p1.appendList : appendListOfState);
|
|
400
|
+
const setList = (p1 && p1.setList ? p1.setList : setListOfState);
|
|
482
401
|
const _showResults = (s: S, sr: SearchResult<T>, lc: Locale) => {
|
|
483
402
|
const results = sr.list;
|
|
484
403
|
if (results && results.length > 0) {
|
|
485
|
-
formatResults(results, component.pageIndex, component.pageSize, component.initPageSize, p1.sequenceNo, p1.format, lc);
|
|
404
|
+
formatResults(results, component.pageIndex, component.pageSize, component.initPageSize, p1 ? p1.sequenceNo : undefined, p1 ? p1.format : undefined, lc);
|
|
486
405
|
}
|
|
487
406
|
const am = component.appendMode;
|
|
488
407
|
const pi = (s.page && s.page >= 1 ? s.page : 1);
|
|
489
408
|
setComponent({ itemTotal: sr.total, pageIndex: pi, nextPageToken: sr.nextPageToken });
|
|
490
409
|
if (am) {
|
|
491
410
|
let limit = s.limit;
|
|
492
|
-
if (s.page <= 1 && s.firstLimit && s.firstLimit > 0) {
|
|
411
|
+
if ((!s.page || s.page <= 1) && s.firstLimit && s.firstLimit > 0) {
|
|
493
412
|
limit = s.firstLimit;
|
|
494
413
|
}
|
|
495
|
-
handleAppend(component,
|
|
496
|
-
if (component.append && s.page > 1) {
|
|
497
|
-
appendList(results, component.list, setState);
|
|
414
|
+
handleAppend(component, sr.list, limit, sr.nextPageToken);
|
|
415
|
+
if (component.append && (s.page && s.page > 1)) {
|
|
416
|
+
appendList(results, component.list, setState as any);
|
|
498
417
|
} else {
|
|
499
|
-
setList(results, setState);
|
|
418
|
+
setList(results, setState as any);
|
|
500
419
|
}
|
|
501
420
|
} else {
|
|
502
|
-
showPaging(component,
|
|
503
|
-
setList(results, setState);
|
|
421
|
+
showPaging(component, sr.list, s.limit, sr.total);
|
|
422
|
+
setList(results, setState as any);
|
|
504
423
|
setComponent({ tmpPageIndex: s.page });
|
|
505
|
-
|
|
506
|
-
|
|
424
|
+
if (s.limit) {
|
|
425
|
+
const m1 = buildMessage(p2.resource, s.page, s.limit, sr.list, sr.total);
|
|
426
|
+
p2.showMessage(m1);
|
|
427
|
+
}
|
|
507
428
|
}
|
|
508
429
|
setRunning(false);
|
|
509
|
-
|
|
510
|
-
p1.hideLoading();
|
|
511
|
-
}
|
|
430
|
+
hideLoading(p2.loading);
|
|
512
431
|
if (component.triggerSearch) {
|
|
513
432
|
setComponent({ triggerSearch: false });
|
|
514
433
|
resetAndSearch();
|
|
515
434
|
}
|
|
516
435
|
};
|
|
517
|
-
const showResults = (p1.showResults ? p1.showResults : _showResults);
|
|
436
|
+
const showResults = (p1 && p1.showResults ? p1.showResults : _showResults);
|
|
518
437
|
|
|
519
438
|
const showMore = (event: any) => {
|
|
520
439
|
event.preventDefault();
|
|
521
|
-
const n = component.pageIndex + 1;
|
|
440
|
+
const n = component.pageIndex ? component.pageIndex + 1 : 2;
|
|
522
441
|
const m = component.pageIndex;
|
|
523
442
|
setComponent({ tmpPageIndex: m, pageIndex: n, append: true });
|
|
524
443
|
component.tmpPageIndex = m;
|
|
@@ -527,7 +446,7 @@ export const useBaseSearchWithProps = <T, S extends SearchModel, ST, P extends M
|
|
|
527
446
|
doSearch(component);
|
|
528
447
|
};
|
|
529
448
|
|
|
530
|
-
const pageChanged = (data) => {
|
|
449
|
+
const pageChanged = (data: any) => {
|
|
531
450
|
const { currentPage, itemsPerPage } = data;
|
|
532
451
|
setComponent({ pageIndex: currentPage, pageSize: itemsPerPage, append: false });
|
|
533
452
|
component.pageIndex = currentPage;
|
|
@@ -550,15 +469,16 @@ export const useBaseSearchWithProps = <T, S extends SearchModel, ST, P extends M
|
|
|
550
469
|
add,
|
|
551
470
|
searchOnClick,
|
|
552
471
|
sort,
|
|
472
|
+
changeView,
|
|
553
473
|
showMore,
|
|
554
474
|
toggleFilter,
|
|
555
475
|
doSearch,
|
|
556
476
|
pageChanged,
|
|
557
477
|
pageSizeChanged,
|
|
558
|
-
clearKeyworkOnClick,
|
|
478
|
+
clearKeyworkOnClick: clearQ,
|
|
559
479
|
showResults,
|
|
560
480
|
getFields,
|
|
561
481
|
getModelName,
|
|
562
|
-
format: p1.format
|
|
482
|
+
format: p1 ? p1.format : undefined
|
|
563
483
|
};
|
|
564
484
|
};
|