react-hook-core 0.0.1 → 0.1.3
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 +404 -511
- package/lib/core.js +90 -27
- package/lib/diff.js +3 -0
- package/lib/edit.js +35 -33
- package/lib/formutil.js +5 -2
- package/lib/index.js +22 -0
- package/lib/merge.js +1 -1
- package/lib/state.js +17 -10
- package/lib/update.js +36 -55
- package/lib/useEdit.js +186 -333
- package/lib/useMessage.js +25 -0
- package/lib/useSearch.js +140 -253
- package/lib/useView.js +70 -161
- package/lib/util.js +6 -2
- package/package.json +7 -9
- package/src/components.ts +430 -376
- package/src/core.ts +115 -59
- package/src/diff.ts +4 -1
- package/src/edit.ts +44 -42
- package/src/formutil.ts +7 -4
- package/src/index.ts +33 -0
- package/src/merge.ts +3 -3
- package/src/state.ts +25 -19
- package/src/update.ts +36 -59
- package/src/useEdit.ts +171 -250
- package/src/useMessage.ts +37 -0
- package/src/useSearch.ts +173 -244
- package/src/useView.ts +64 -101
- package/src/util.ts +8 -4
- package/tsconfig.json +1 -0
package/src/update.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
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
|
|
|
5
|
-
|
|
5
|
+
const m = 'model';
|
|
6
|
+
const _getModelName = (f2?: HTMLFormElement|null): string => {
|
|
7
|
+
return getModelName2(f2, m);
|
|
8
|
+
};
|
|
9
|
+
export const useUpdate = <T>(initialState: T, getName?: ((f?: HTMLFormElement|null) => string) | string, getLocale?: (() => Locale) | Locale, removeErr?: (ctrl: HTMLInputElement) => void) => {
|
|
10
|
+
return useUpdateWithProps<T, any>(undefined, initialState, getName, getLocale, removeErr);
|
|
11
|
+
};
|
|
12
|
+
function prepareData(data: any): void {
|
|
13
|
+
}
|
|
14
|
+
export const useUpdateWithProps = <T, P>(props: P|undefined, initialState: T, getName?: ((f?: HTMLFormElement|null) => string) | string, getLocale?: (() => Locale) | Locale, removeErr?: (ctrl: HTMLInputElement) => void, prepareCustomData?: (d: any) => void) => {
|
|
15
|
+
if (!prepareCustomData) {
|
|
16
|
+
prepareCustomData = prepareData;
|
|
17
|
+
}
|
|
6
18
|
const [state, setState] = useMergeState<T>(initialState);
|
|
7
19
|
|
|
8
20
|
const updatePhoneState = (event: any) => {
|
|
@@ -23,31 +35,35 @@ export const useUpdate = <T>(initialState: T, getLocale?: () => Locale, removeEr
|
|
|
23
35
|
updateState(event);
|
|
24
36
|
}
|
|
25
37
|
};
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const a = getModelName2(f2);
|
|
29
|
-
if (a && a.length > 0) {
|
|
30
|
-
return a;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return 'model';
|
|
34
|
-
};
|
|
35
|
-
const getModelName = getName ? getName : _getModelName;
|
|
38
|
+
const getModelName: (f2?: HTMLFormElement|null) => string = (typeof getName === 'function' ? getName : _getModelName);
|
|
39
|
+
|
|
36
40
|
const updateState = (e: any, callback?: () => void, lc?: Locale) => {
|
|
37
41
|
const ctrl = e.currentTarget as HTMLInputElement;
|
|
38
|
-
|
|
42
|
+
let mn: string = m;
|
|
43
|
+
if (getName) {
|
|
44
|
+
if (typeof getName === 'string') {
|
|
45
|
+
mn = getName;
|
|
46
|
+
} else {
|
|
47
|
+
mn = getName(ctrl.form);
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
mn = _getModelName(ctrl.form);
|
|
51
|
+
}
|
|
39
52
|
const l = localeOf(lc, getLocale);
|
|
40
53
|
handleEvent(e, removeErr);
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
54
|
+
if (props && (props as any).setGlobalState) {
|
|
55
|
+
handleProps<P>(e, props, ctrl, mn, l, prepareCustomData);
|
|
56
|
+
} else {
|
|
57
|
+
const objSet = buildState(e, state, ctrl, mn, l);
|
|
58
|
+
if (objSet) {
|
|
59
|
+
if (callback) {
|
|
60
|
+
setState(objSet, callback);
|
|
61
|
+
} else {
|
|
62
|
+
setState(objSet);
|
|
63
|
+
}
|
|
47
64
|
}
|
|
48
65
|
}
|
|
49
66
|
};
|
|
50
|
-
|
|
51
67
|
const updateFlatState = (e: any, callback?: () => void, lc?: Locale) => {
|
|
52
68
|
const objSet = buildFlatState(e, state, lc);
|
|
53
69
|
if (objSet) {
|
|
@@ -58,45 +74,6 @@ export const useUpdate = <T>(initialState: T, getLocale?: () => Locale, removeEr
|
|
|
58
74
|
}
|
|
59
75
|
}
|
|
60
76
|
};
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
getModelName,
|
|
64
|
-
updateState,
|
|
65
|
-
updatePhoneState,
|
|
66
|
-
updateFlatState,
|
|
67
|
-
getLocale,
|
|
68
|
-
setState,
|
|
69
|
-
state
|
|
70
|
-
};
|
|
71
|
-
};
|
|
72
|
-
function prepareData(data: any): void {
|
|
73
|
-
}
|
|
74
|
-
export const useUpdateWithProps = <T, P extends ModelProps>(props: P, initialState: T, gl?: () => Locale, removeErr?: (ctrl: HTMLInputElement) => void, getName?: (f?: HTMLFormElement) => string, prepareCustomData?: (d: any) => void) => {
|
|
75
|
-
if (!prepareCustomData) {
|
|
76
|
-
prepareCustomData = prepareData;
|
|
77
|
-
}
|
|
78
|
-
const baseProps = useUpdate<T>(initialState, gl, removeErr, getName);
|
|
79
|
-
const {getModelName, updatePhoneState, updateFlatState, getLocale, state, setState} = baseProps;
|
|
80
|
-
|
|
81
|
-
const updateState = (e: any, callback?: () => void, lc?: Locale) => {
|
|
82
|
-
const ctrl = e.currentTarget as HTMLInputElement;
|
|
83
|
-
const modelName = getModelName(ctrl.form);
|
|
84
|
-
const l = localeOf(lc, gl);
|
|
85
|
-
handleEvent(e, removeErr);
|
|
86
|
-
if (props.setGlobalState) {
|
|
87
|
-
handleProps<P>(e, props, ctrl, modelName, l, prepareCustomData);
|
|
88
|
-
} else {
|
|
89
|
-
const objSet = buildState(e, state, ctrl, modelName, l);
|
|
90
|
-
if (objSet) {
|
|
91
|
-
if (callback) {
|
|
92
|
-
setState(objSet, callback);
|
|
93
|
-
} else {
|
|
94
|
-
setState(objSet);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
77
|
return {
|
|
101
78
|
getModelName,
|
|
102
79
|
updateState,
|