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/src/update.ts CHANGED
@@ -1,8 +1,20 @@
1
- import {getModelName as getModelName2, Locale, ModelProps, removePhoneFormat} from './core';
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
- export const useUpdate = <T>(initialState: T, getLocale?: () => Locale, removeErr?: (ctrl: HTMLInputElement) => void, getName?: (f?: HTMLFormElement) => string) => {
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 _getModelName = (f2?: HTMLFormElement) => {
27
- if (f2) {
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
- const modelName = getModelName(ctrl.form);
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
- const objSet = buildState(e, state, ctrl, modelName, l);
42
- if (objSet) {
43
- if (callback) {
44
- setState(objSet, callback);
45
- } else {
46
- setState(objSet);
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,