react-hook-core 0.1.15 → 0.1.17
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/com.js +1 -1
- package/lib/merge.js +1 -6
- package/lib/route.js +51 -8
- package/lib/search.js +20 -15
- package/lib/state.js +2 -1
- package/lib/update.js +23 -7
- package/lib/useEdit.js +1 -1
- package/lib/useSearch.js +9 -2
- package/package.json +1 -1
- package/src/com.ts +53 -53
- package/src/components.ts +2 -2
- package/src/merge.ts +23 -28
- package/src/route.ts +86 -47
- package/src/search.ts +20 -13
- package/src/state.ts +9 -1
- package/src/update.ts +86 -74
- package/src/useEdit.ts +6 -5
- package/src/useSearch.ts +521 -511
package/src/search.ts
CHANGED
|
@@ -36,6 +36,7 @@ export interface Sortable {
|
|
|
36
36
|
export interface Pagination {
|
|
37
37
|
initPageSize?: number;
|
|
38
38
|
pageSize?: number;
|
|
39
|
+
limit?: number;
|
|
39
40
|
pageIndex?: number;
|
|
40
41
|
total?: number;
|
|
41
42
|
pages?: number;
|
|
@@ -171,13 +172,19 @@ export function changePage(com: Pagination, pageIndex: number, pageSize: number)
|
|
|
171
172
|
com.append = false;
|
|
172
173
|
}
|
|
173
174
|
export function optimizeFilter<S extends Filter>(obj: S, searchable: Searchable, fields?: string[]): S {
|
|
175
|
+
const sLimit = searchable.limit;
|
|
174
176
|
obj.fields = fields;
|
|
175
177
|
if (searchable.pageIndex && searchable.pageIndex > 1) {
|
|
176
178
|
obj.page = searchable.pageIndex;
|
|
177
179
|
} else {
|
|
178
180
|
delete obj.page;
|
|
179
181
|
}
|
|
180
|
-
|
|
182
|
+
if (sLimit){
|
|
183
|
+
obj.limit = searchable.limit;
|
|
184
|
+
}else{
|
|
185
|
+
obj.limit = searchable.pageSize;
|
|
186
|
+
}
|
|
187
|
+
|
|
181
188
|
if (searchable.appendMode && searchable.initPageSize !== searchable.pageSize) {
|
|
182
189
|
obj.firstLimit = searchable.initPageSize;
|
|
183
190
|
} else {
|
|
@@ -194,21 +201,19 @@ export function optimizeFilter<S extends Filter>(obj: S, searchable: Searchable,
|
|
|
194
201
|
return obj;
|
|
195
202
|
}
|
|
196
203
|
|
|
197
|
-
function mapObjects(
|
|
198
|
-
for (let key in
|
|
199
|
-
if (
|
|
200
|
-
if(Array.isArray(
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
if(arrayObjKeyB && arrayObjKeyB.length > 1) {
|
|
205
|
-
objA[key] = [...arrayObjKeyB];
|
|
204
|
+
function mapObjects(dest: any, src: any): void {
|
|
205
|
+
for (let key in dest) {
|
|
206
|
+
if (src.hasOwnProperty(key) && src[key] !== null && src[key] !== undefined) {
|
|
207
|
+
if(Array.isArray(dest[key]) && typeof src[key] === 'string' && src[key].length > 0) {
|
|
208
|
+
const arrayObjKeySrc = src[key].length > 0 ? (src[key])?.split(',') : [];
|
|
209
|
+
if(arrayObjKeySrc && arrayObjKeySrc.length > 1) {
|
|
210
|
+
dest[key] = [...arrayObjKeySrc];
|
|
206
211
|
} else {
|
|
207
|
-
|
|
212
|
+
dest[key] = [];
|
|
213
|
+
dest[key].push(src[key])
|
|
208
214
|
}
|
|
209
|
-
}
|
|
210
215
|
} else {
|
|
211
|
-
|
|
216
|
+
dest[key] = src[key];
|
|
212
217
|
}
|
|
213
218
|
}
|
|
214
219
|
}
|
|
@@ -576,7 +581,9 @@ export function toggleSortStyle(target: HTMLElement): string {
|
|
|
576
581
|
}
|
|
577
582
|
export function getModel<T, S extends Filter>(state: any, modelName: string, searchable: Searchable, fields?: string[], excluding?: string[]|number[]): S {
|
|
578
583
|
let obj2 = getModelFromState(state, modelName);
|
|
584
|
+
|
|
579
585
|
const obj: any = obj2 ? obj2 : {};
|
|
586
|
+
console.log("getModel optimizeFilter", searchable)
|
|
580
587
|
const obj3 = optimizeFilter(obj, searchable, fields);
|
|
581
588
|
obj3.excluding = excluding;
|
|
582
589
|
return obj3;
|
package/src/state.ts
CHANGED
|
@@ -75,6 +75,7 @@ export function buildState<S, K extends keyof S>(e: any, state: Readonly<S>, ctr
|
|
|
75
75
|
const dataField = ctrl.getAttribute('data-field');
|
|
76
76
|
const field = (dataField ? dataField : ctrl.name);
|
|
77
77
|
const model = Object.assign({}, ex);
|
|
78
|
+
const dType= 'array'
|
|
78
79
|
if (type && type.toLowerCase() === 'checkbox') {
|
|
79
80
|
let value = model[field];
|
|
80
81
|
if (ctrl.id && ctrl.name !== ctrl.id) {
|
|
@@ -82,7 +83,14 @@ export function buildState<S, K extends keyof S>(e: any, state: Readonly<S>, ctr
|
|
|
82
83
|
value = [];
|
|
83
84
|
}
|
|
84
85
|
value.includes(ctrl.value) ? value = value.filter((v: string) => v !== ctrl.value) : value.push(ctrl.value);
|
|
86
|
+
// if (dType == 'array'){
|
|
87
|
+
// if (value === 'string'){
|
|
88
|
+
// value = [value]
|
|
89
|
+
// }
|
|
90
|
+
// }
|
|
85
91
|
model[field] = value;
|
|
92
|
+
// console.log(model, modelName, model, model[field], field, value )
|
|
93
|
+
// setValue(model, field, value);
|
|
86
94
|
} else {
|
|
87
95
|
const v = valueOfCheckbox(ctrl);
|
|
88
96
|
model[field] = v;
|
|
@@ -103,7 +111,7 @@ export function buildState<S, K extends keyof S>(e: any, state: Readonly<S>, ctr
|
|
|
103
111
|
const objSet: any = {};
|
|
104
112
|
try {
|
|
105
113
|
const selectedDate = new Date(ctrl.value);
|
|
106
|
-
setValue(model, field, ctrl.value && ctrl.value!== '' ? selectedDate.toISOString() :
|
|
114
|
+
setValue(model, field, ctrl.value && ctrl.value!== '' ? selectedDate.toISOString() : null);
|
|
107
115
|
objSet[modelName] = model;
|
|
108
116
|
return objSet;
|
|
109
117
|
} catch (error) {
|
package/src/update.ts
CHANGED
|
@@ -1,74 +1,86 @@
|
|
|
1
|
-
import {getModelName as getModelName2, Locale, removePhoneFormat} from
|
|
2
|
-
import {useMergeState} from
|
|
3
|
-
import {buildFlatState, buildState, handleEvent, localeOf} from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
} else {
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
setState(objSet);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
1
|
+
import { getModelName as getModelName2, Locale, removePhoneFormat } from "./core";
|
|
2
|
+
import { useMergeState } from "./merge";
|
|
3
|
+
import { buildFlatState, buildState, handleEvent, localeOf } from "./state";
|
|
4
|
+
import { useEffect, useState } from "react";
|
|
5
|
+
|
|
6
|
+
const m = "model";
|
|
7
|
+
const _getModelName = (f2?: HTMLFormElement | null): string => {
|
|
8
|
+
return getModelName2(f2, m);
|
|
9
|
+
};
|
|
10
|
+
export const useUpdate = <T>(
|
|
11
|
+
initialState: T,
|
|
12
|
+
getName?: ((f?: HTMLFormElement | null) => string) | string,
|
|
13
|
+
getLocale?: (() => Locale) | Locale,
|
|
14
|
+
removeErr?: (ctrl: HTMLInputElement) => void
|
|
15
|
+
) => {
|
|
16
|
+
const [state, setState] = useMergeState<T>(initialState);
|
|
17
|
+
const [rerender, setRerender] = useState(false);
|
|
18
|
+
|
|
19
|
+
// trigger re-render page when change state in useSearch
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
setRerender(!rerender);
|
|
22
|
+
}, [state]);
|
|
23
|
+
|
|
24
|
+
const updatePhoneState = (event: any) => {
|
|
25
|
+
const re = /^[0-9\b]+$/;
|
|
26
|
+
const target = event.currentTarget as HTMLInputElement;
|
|
27
|
+
const value = removePhoneFormat(target.value);
|
|
28
|
+
if (re.test(value) || !value) {
|
|
29
|
+
updateState(event);
|
|
30
|
+
} else {
|
|
31
|
+
const splitArr = value.split("");
|
|
32
|
+
let responseStr = "";
|
|
33
|
+
splitArr.forEach((element) => {
|
|
34
|
+
if (re.test(element)) {
|
|
35
|
+
responseStr += element;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
target.value = responseStr;
|
|
39
|
+
updateState(event);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const getModelName: (f2?: HTMLFormElement | null) => string = typeof getName === "function" ? getName : _getModelName;
|
|
43
|
+
|
|
44
|
+
const updateState = (e: any, callback?: (prev: any) => void, lc?: Locale) => {
|
|
45
|
+
const ctrl = e.currentTarget as HTMLInputElement;
|
|
46
|
+
let mn: string = m;
|
|
47
|
+
if (getName) {
|
|
48
|
+
if (typeof getName === "string") {
|
|
49
|
+
mn = getName;
|
|
50
|
+
} else {
|
|
51
|
+
mn = getName(ctrl.form);
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
mn = _getModelName(ctrl.form);
|
|
55
|
+
}
|
|
56
|
+
const l = localeOf(lc, getLocale);
|
|
57
|
+
handleEvent(e, removeErr);
|
|
58
|
+
const objSet = buildState<T, any>(e, state, ctrl, mn, l);
|
|
59
|
+
if (objSet) {
|
|
60
|
+
if (callback) {
|
|
61
|
+
setState({ ...objSet }, callback);
|
|
62
|
+
} else {
|
|
63
|
+
setState(objSet);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const updateFlatState = (e: any, callback?: () => void, lc?: Locale) => {
|
|
68
|
+
const objSet = buildFlatState<T, any>(e, state, lc);
|
|
69
|
+
if (objSet) {
|
|
70
|
+
if (callback) {
|
|
71
|
+
setState(objSet, callback);
|
|
72
|
+
} else {
|
|
73
|
+
setState(objSet);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
getModelName,
|
|
79
|
+
updateState,
|
|
80
|
+
updatePhoneState,
|
|
81
|
+
updateFlatState,
|
|
82
|
+
getLocale,
|
|
83
|
+
setState,
|
|
84
|
+
state,
|
|
85
|
+
};
|
|
86
|
+
};
|
package/src/useEdit.ts
CHANGED
|
@@ -34,7 +34,7 @@ export interface BaseEditComponentParam<T, ID> {
|
|
|
34
34
|
validate?: (obj: T, callback: (obj2?: T) => void) => void;
|
|
35
35
|
succeed?: (obj: T, msg: string, version?: string, isBack?: boolean, result?: ResultInfo<T>) => void;
|
|
36
36
|
fail?: (result: ResultInfo<T>|ErrorMessage[]) => void;
|
|
37
|
-
postSave?: (obj: T, res: number|ResultInfo<T
|
|
37
|
+
postSave?: (obj: T, res: number|ResultInfo<T>|ErrorMessage[], version?: string, backOnSave?: boolean) => void;
|
|
38
38
|
handleError?: (error: any) => void;
|
|
39
39
|
handleDuplicateKey?: (result?: ResultInfo<T>) => void;
|
|
40
40
|
load?: (i: ID|null, callback?: (m: T, showM: (m2: T) => void) => void) => void;
|
|
@@ -68,7 +68,8 @@ export interface HookPropsBaseEditParameter<T, ID, S, P> extends HookBaseEditPar
|
|
|
68
68
|
export const useEdit = <T, ID, S>(
|
|
69
69
|
refForm: any,
|
|
70
70
|
initialState: S,
|
|
71
|
-
service: GenericService<T, ID, number|ResultInfo<T
|
|
71
|
+
service: GenericService<T, ID, number|ResultInfo<T> | ErrorMessage[]>,
|
|
72
|
+
|
|
72
73
|
p2: EditParameter,
|
|
73
74
|
p?: EditComponentParam<T, ID, S>
|
|
74
75
|
) => {
|
|
@@ -158,7 +159,7 @@ export const useEditOne = <T, ID, S>(p: HookBaseEditParameter<T, ID, S>) => {
|
|
|
158
159
|
export const useCoreEdit = <T, ID, S, P>(
|
|
159
160
|
refForm: any,
|
|
160
161
|
initialState: S,
|
|
161
|
-
service: GenericService<T, ID, number|ResultInfo<T
|
|
162
|
+
service: GenericService<T, ID, number|ResultInfo<T>|ErrorMessage[]>,
|
|
162
163
|
p1: EditParameter,
|
|
163
164
|
p?: BaseEditComponentParam<T, ID>,
|
|
164
165
|
props?: P
|
|
@@ -214,7 +215,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
214
215
|
};
|
|
215
216
|
|
|
216
217
|
const _handleNotFound = (form?: any): void => {
|
|
217
|
-
const msg = message(p1.resource.value, '
|
|
218
|
+
const msg = message(p1.resource.value, 'error_404', 'error');
|
|
218
219
|
if (form) {
|
|
219
220
|
setReadOnly(form);
|
|
220
221
|
}
|
|
@@ -405,7 +406,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
405
406
|
};
|
|
406
407
|
const handleError = (p && p.handleError ? p.handleError : _handleError);
|
|
407
408
|
|
|
408
|
-
const _postSave = (obj: T, r: number | ResultInfo<T
|
|
409
|
+
const _postSave = (obj: T, r: number | ResultInfo<T>|ErrorMessage[], version?: string, backOnSave?: boolean) => {
|
|
409
410
|
setRunning(false);
|
|
410
411
|
hideLoading(p1.loading);
|
|
411
412
|
const x: any = r;
|