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/useView.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import {useEffect, useState} from 'react';
2
- import {buildId, getModelName as getModelName2, HistoryProps, initForm, LoadingService, Locale, message, messageByHttpStatus, ResourceService, ViewParameter, ViewService} from './core';
2
+ import {RouteComponentProps} from 'react-router';
3
+ import {buildId, getModelName as getModelName2, hideLoading, initForm, LoadingService, Locale, message, messageByHttpStatus, ResourceService, showLoading, ViewParameter, ViewService} from './core';
3
4
  import {readOnly} from './formutil';
4
5
  import {DispatchWithCallback, useMergeState} from './merge';
5
6
  import {useRouter} from './router';
@@ -15,7 +16,7 @@ export interface HookBaseViewParameter<T, ID, S> extends BaseViewComponentParam<
15
16
  refForm: any;
16
17
  initialState: S;
17
18
  service: ((id: ID, ctx?: any) => Promise<T>)|ViewService<T, ID>;
18
- resourceService: ResourceService;
19
+ resource: ResourceService;
19
20
  showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
20
21
  getLocale?: () => Locale;
21
22
  loading?: LoadingService;
@@ -25,88 +26,29 @@ export interface ViewComponentParam<T, ID, S> extends BaseViewComponentParam<T,
25
26
  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;
26
27
  callback?: (m: T, showF: (model: T) => void) => void;
27
28
  }
28
- export interface HookPropsViewParameter<T, ID, S, P extends HistoryProps> extends HookPropsBaseViewParameter<T, ID, S, P> {
29
+ export interface HookPropsViewParameter<T, ID, S, P extends RouteComponentProps> extends HookPropsBaseViewParameter<T, ID, S, P> {
29
30
  keys?: string[];
30
31
  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;
31
32
  callback?: (m: T, showF: (model: T) => void) => void;
32
33
  }
33
- export interface HookPropsBaseViewParameter<T, ID, S, P extends HistoryProps> extends HookBaseViewParameter<T, ID, S> {
34
- props?: P;
34
+ export interface HookPropsBaseViewParameter<T, ID, S, P> extends HookBaseViewParameter<T, ID, S> {
35
+ props: P;
35
36
  }
36
- export const useBaseView = <T, ID, S, P extends HistoryProps>(
37
- props: P,
38
- refForm: any,
39
- initialState: S,
40
- service: ((id: ID, ctx?: any) => Promise<T>)|ViewService<T, ID>,
41
- p1: ViewParameter,
42
- p2?: BaseViewComponentParam<T, ID>
43
- ) => {
44
- const p4: BaseViewComponentParam<T, ID> = (p2 ? p2 : {} as any);
45
- const p6: HookPropsBaseViewParameter<T, ID, S, P> = {
46
- props,
47
- refForm,
48
- initialState,
49
- service,
50
- resourceService: p1.resource,
51
- showError: p1.showError,
52
- getLocale: p1.getLocale,
53
- loading: p1.loading,
54
- name: p4.name,
55
- handleNotFound: p4.handleNotFound,
56
- getModelName: p4.getModelName,
57
- showModel: p4.showModel,
58
- load: p4.load
59
- };
60
- return useBaseViewOne(p6);
37
+ export const useViewOneProps = <T, ID, S, P extends RouteComponentProps>(p: HookPropsViewParameter<T, ID, S, P>) => {
38
+ return useViewProps(p.props, p.refForm, p.initialState, p.service, p, p);
61
39
  };
62
- export const useView = <T, ID, S, P extends HistoryProps>(
63
- props: P,
40
+ export const useViewOne = <T, ID, S>(p: HookBaseViewParameter<T, ID, S>) => {
41
+ return useView(p.refForm, p.initialState, p.service, p, p);
42
+ };
43
+ export const useView = <T, ID, S>(
64
44
  refForm: any,
65
45
  initialState: S,
66
46
  service: ((id: ID, ctx?: any) => Promise<T>)|ViewService<T, ID>,
67
47
  p1: ViewParameter,
68
- p2?: ViewComponentParam<T, ID, S>
48
+ p?: ViewComponentParam<T, ID, S>
69
49
  ) => {
70
- const p4: ViewComponentParam<T, ID, S> = (p2 ? p2 : {} as any);
71
- const p: HookPropsViewParameter<T, ID, S, P> = {
72
- props,
73
- refForm,
74
- keys: p4.keys,
75
- initialize: p4.initialize,
76
- callback: p4.callback,
77
- initialState,
78
- service,
79
- resourceService: p1.resource,
80
- showError: p1.showError,
81
- getLocale: p1.getLocale,
82
- loading: p1.loading,
83
- name: p4.name,
84
- handleNotFound: p4.handleNotFound,
85
- getModelName: p4.getModelName,
86
- showModel: p4.showModel,
87
- load: p4.load
88
- };
89
- return useViewOne(p);
90
- };
91
- export const useViewOne = <T, ID, S, P extends HistoryProps>(p: HookPropsViewParameter<T, ID, S, P>) => {
92
- const baseProps = useBaseViewOne(p);
93
- const [state, setState] = useMergeState<S>(p.initialState);
94
- useEffect(() => {
95
- if (baseProps.refForm) {
96
- initForm(baseProps.refForm.current);
97
- }
98
- const id = buildId<ID>(p.props, p.keys);
99
- if (p && p.initialize) {
100
- p.initialize(id, baseProps.load, setState, p.callback);
101
- } else {
102
- baseProps.load(id, p.callback);
103
- }
104
- }, []);
105
- return {...baseProps};
106
- };
107
- export const useBaseViewOne = <T, ID, S, P extends HistoryProps>(p: HookPropsBaseViewParameter<T, ID, S, P>) => {
108
- const [state, setState] = useMergeState<S>(p.initialState);
109
- const [running, setRunning] = useState(undefined);
50
+ const [state, setState] = useMergeState<S>(initialState);
51
+ const [running, setRunning] = useState<boolean>();
110
52
  const {goBack} = useRouter();
111
53
 
112
54
  const back = (event: any) => {
@@ -117,40 +59,37 @@ export const useBaseViewOne = <T, ID, S, P extends HistoryProps>(p: HookPropsBas
117
59
  };
118
60
 
119
61
  const getModelName = (f?: HTMLFormElement) => {
120
- if (p.name) {
62
+ if (p && p.name) {
121
63
  return p.name;
122
64
  }
123
- return getModelName2(f);
65
+ return getModelName2(f, 'model');
124
66
  };
125
67
 
126
68
  const showModel = (model: T) => {
127
- const n: string = getModelName(p.refForm.current);
69
+ const n: string = getModelName(refForm.current);
128
70
  const objSet: any = {};
129
71
  objSet[n] = model;
130
72
  setState(objSet);
131
73
  };
132
74
 
133
75
  const _handleNotFound = (form?: any): void => {
134
- const msg = message(p.resourceService.value, 'error_not_found', 'error');
76
+ const msg = message(p1.resource.value, 'error_not_found', 'error');
135
77
  if (form) {
136
78
  readOnly(form);
137
79
  }
138
- p.showError(msg.message, msg.title);
80
+ p1.showError(msg.message, msg.title);
139
81
  };
140
- const handleNotFound = (p.handleNotFound ? p.handleNotFound : _handleNotFound);
82
+ const handleNotFound = (p && p.handleNotFound ? p.handleNotFound : _handleNotFound);
141
83
 
142
- const _load = async (_id: ID, callback?: (m: T, showM: (m2: T) => void) => void) => {
84
+ const _load = (_id: ID, callback?: (m: T, showM: (m2: T) => void) => void) => {
143
85
  const id: any = _id;
144
86
  if (id != null && id !== '') {
145
- try {
146
- let obj: T;
147
- if (typeof p.service === 'function') {
148
- obj = await p.service(id);
149
- } else {
150
- obj = await p.service.load(id);
151
- }
87
+ setRunning(true);
88
+ showLoading(p1.loading);
89
+ const fn = (typeof service === 'function' ? service : service.load);
90
+ fn(id).then(obj => {
152
91
  if (!obj) {
153
- handleNotFound(p.refForm.current);
92
+ handleNotFound(refForm.current);
154
93
  } else {
155
94
  if (callback) {
156
95
  callback(obj, showModel);
@@ -158,35 +97,34 @@ export const useBaseViewOne = <T, ID, S, P extends HistoryProps>(p: HookPropsBas
158
97
  showModel(obj);
159
98
  }
160
99
  }
161
- } catch (err) {
100
+ setRunning(false);
101
+ hideLoading(p1.loading);
102
+ }).catch(err => {
162
103
  const data = (err && err.response) ? err.response : err;
163
- const r = p.resourceService;
104
+ const r = p1.resource;
164
105
  const title = r.value('error');
165
106
  let msg = r.value('error_internal');
166
107
  if (data && data.status === 404) {
167
- handleNotFound(p.refForm.current);
108
+ handleNotFound(refForm.current);
168
109
  } else {
169
110
  if (data && data.status) {
170
111
  msg = messageByHttpStatus(data.status, r.value);
171
112
  }
172
- readOnly(p.refForm.current);
173
- p.showError(msg, title);
113
+ readOnly(refForm.current);
114
+ p1.showError(msg, title);
174
115
  }
175
- } finally {
176
116
  setRunning(false);
177
- if (p.loading) {
178
- p.loading.hideLoading();
179
- }
180
- }
117
+ hideLoading(p1.loading);
118
+ });
181
119
  }
182
120
  };
183
- const load = (p.load ? p.load : _load);
121
+ const load = (p && p.load ? p.load : _load);
184
122
 
185
123
  return {
186
124
  state,
187
125
  setState,
188
- refForm: p.refForm,
189
- resource: p.resourceService.resource(),
126
+ refForm,
127
+ resource: p1.resource.resource(),
190
128
  running,
191
129
  setRunning,
192
130
  showModel,
@@ -196,3 +134,28 @@ export const useBaseViewOne = <T, ID, S, P extends HistoryProps>(p: HookPropsBas
196
134
  back
197
135
  };
198
136
  };
137
+ export const useViewProps = <T, ID, S, P extends RouteComponentProps>(
138
+ props: P,
139
+ refForm: any,
140
+ initialState: S,
141
+ service: ((id: ID, ctx?: any) => Promise<T>)|ViewService<T, ID>,
142
+ p1: ViewParameter,
143
+ p?: ViewComponentParam<T, ID, S>
144
+ ) => {
145
+ const baseProps = useView(refForm, initialState, service, p1, p);
146
+ const [state, setState] = useMergeState<S>(initialState);
147
+ useEffect(() => {
148
+ if (baseProps.refForm) {
149
+ initForm(baseProps.refForm.current);
150
+ }
151
+ const id = buildId<ID>(props, p ? p.keys : undefined);
152
+ if (id) {
153
+ if (p && p.initialize) {
154
+ p.initialize(id, baseProps.load, setState, p.callback);
155
+ } else {
156
+ baseProps.load(id, p ? p.callback : undefined);
157
+ }
158
+ }
159
+ }, []);
160
+ return {...baseProps};
161
+ };
package/src/util.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import {Locale} from './core';
2
+ import {enLocale} from './state';
2
3
 
3
4
  const r1 = / |,|\$|€|£|¥|'|٬|،| /g;
4
5
  const r2 = / |\.|\$|€|£|¥|'|٬|،| /g;
@@ -26,10 +27,10 @@ export function valueOf(ctrl: HTMLInputElement, locale?: Locale, eventType?: str
26
27
  } else {
27
28
  v = ctrl.value.replace(r1, '');
28
29
  }
29
- return isNaN(v) ? { mustChange: false } : { mustChange: true, value: parseFloat(v) };
30
+ return isNaN(v as any) ? { mustChange: false } : { mustChange: true, value: parseFloat(v) };
30
31
  } else if (datatype === 'currency' || datatype === 'string-currency') {
31
32
  const ml = ctrl.getAttribute('maxlength');
32
- const nm = (isNaN(ml as any) ? null : parseInt(ml, 10));
33
+ const nm = (ml != null && !isNaN(ml as any) ? parseInt(ml, 10) : undefined);
33
34
  const res: any = getStringCurrency(ctrl.value, datatype, locale, nm, eventType === 'blur');
34
35
  return res;
35
36
  } else {
@@ -38,7 +39,7 @@ export function valueOf(ctrl: HTMLInputElement, locale?: Locale, eventType?: str
38
39
  }
39
40
  }
40
41
 
41
- function getStringCurrency(value: string, datatype: string, locale: Locale, maxLength?: number, isOnBlur?: boolean): { mustChange: any, value?: string } {
42
+ function getStringCurrency(value: string, datatype: string, locale?: Locale, maxLength?: number, isOnBlur?: boolean): { mustChange: any, value?: string } {
42
43
  if (locale && locale.decimalSeparator !== '.') {
43
44
  value = value.replace(r2, '');
44
45
  if (value.indexOf(locale.decimalSeparator) >= 0) {
@@ -57,6 +58,9 @@ function getStringCurrency(value: string, datatype: string, locale: Locale, maxL
57
58
  return { mustChange: true, value: value.substring(1) };
58
59
  }
59
60
 
61
+ if (!locale) {
62
+ locale = enLocale;
63
+ }
60
64
  const decimalDigits = locale ? locale.decimalDigits : 2;
61
65
  const groupDigits = 3; // TODO in database locale don't have data
62
66
  const decimalSeparator = locale.decimalSeparator; // '.'
@@ -86,7 +90,7 @@ function getStringCurrency(value: string, datatype: string, locale: Locale, maxL
86
90
  afterDot = afterDot.substr(0, decimalDigits);
87
91
  }
88
92
  }
89
- if (beforeDot.length > maxLength - (decimalDigits + 1)) {
93
+ if (maxLength && beforeDot.length > maxLength - (decimalDigits + 1)) {
90
94
  return { mustChange: false };
91
95
  }
92
96
 
package/tsconfig.json CHANGED
@@ -6,6 +6,7 @@
6
6
  "outDir": "./lib",
7
7
  "module": "commonjs",
8
8
  "moduleResolution": "node",
9
+ "strict": true,
9
10
  "pretty": true,
10
11
  "sourceMap": false,
11
12
  "declaration": false,