react-hook-core 0.2.4 → 0.2.6

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/state.ts CHANGED
@@ -1,194 +1,194 @@
1
- import {Locale, ModelProps} from './core';
2
- import {setValue} from './reflect';
3
- import {valueOf} from './util';
4
-
5
- export const enLocale = {
6
- 'id': 'en-US',
7
- 'countryCode': 'US',
8
- 'dateFormat': 'M/d/yyyy',
9
- 'firstDayOfWeek': 1,
10
- 'decimalSeparator': '.',
11
- 'groupSeparator': ',',
12
- 'decimalDigits': 2,
13
- 'currencyCode': 'USD',
14
- 'currencySymbol': '$',
15
- 'currencyPattern': 0
16
- };
17
- export function localeOf(lc?: Locale, glc?: (() => Locale) | Locale): Locale {
18
- let l: Locale|undefined = lc;
19
- if (!l) {
20
- if (glc) {
21
- if (typeof glc === 'function') {
22
- l = glc();
23
- } else {
24
- l = glc;
25
- }
26
- }
27
- if (!l) {
28
- l = enLocale;
29
- }
30
- }
31
- return l;
32
- }
33
- export function handleEvent(e: any, removeErr?: (ctrl: HTMLInputElement) => void) {
34
- const ctrl = e.currentTarget as HTMLInputElement;
35
- const type = ctrl.getAttribute('type');
36
- const isPreventDefault = type && (['checkbox', 'radio'].indexOf(type.toLowerCase()) >= 0 ? false : true);
37
- if (isPreventDefault) {
38
- e.preventDefault();
39
- }
40
- if (
41
- removeErr &&
42
- ctrl.nodeName === 'SELECT' &&
43
- ctrl.value &&
44
- ctrl.classList.contains('invalid')) {
45
- removeErr(ctrl);
46
- }
47
- }
48
- export function handleProps<P extends ModelProps>(e: any, props: P, ctrl: HTMLInputElement, modelName: string, tloc: Locale, prepareData?: (data: any) => void) {
49
- if (props.setGlobalState) {
50
- const res = valueOf(ctrl, tloc, e.type);
51
- if (res.mustChange) {
52
- const dataField = ctrl.getAttribute('data-field');
53
- const field = (dataField ? dataField : ctrl.name);
54
- const propsDataForm = (props as any)[modelName];
55
- const form = ctrl.form;
56
- if (form) {
57
- const formName = form.name;
58
- if (field.indexOf('.') < 0 && field.indexOf('[') < 0) {
59
- const data = props.shouldBeCustomized && prepareData ? prepareData({ [ctrl.name]: res.value }) : { [ctrl.name]: res.value };
60
- props.setGlobalState({ [formName]: { ...propsDataForm, ...data } });
61
- } else {
62
- setValue(propsDataForm, field, ctrl.value);
63
- props.setGlobalState({ [formName]: { ...propsDataForm } });
64
- }
65
- }
66
- }
67
- }
68
- }
69
- export function buildState<S, K extends keyof S>(e: any, state: Readonly<S>, ctrl: HTMLInputElement, modelName: string, tloc: Locale): K|undefined {
70
- const form = ctrl.form;
71
- if (form) {
72
- if (modelName && modelName !== '') {
73
- const type = ctrl.getAttribute('type');
74
- const ex = (state as any)[modelName];
75
- const dataField = ctrl.getAttribute('data-field');
76
- const field = (dataField ? dataField : ctrl.name);
77
- const model = Object.assign({}, ex);
78
- if (type && type.toLowerCase() === 'checkbox') {
79
- let value = model[field];
80
- if (ctrl.id && ctrl.name !== ctrl.id) {
81
- if (!value || !Array.isArray(value)) {
82
- value = [];
83
- }
84
- value.includes(ctrl.value) ? value = value.filter((v: string) => v !== ctrl.value) : value.push(ctrl.value);
85
- // if (dType == 'array'){
86
- // if (value === 'string'){
87
- // value = [value]
88
- // }
89
- // }
90
- model[field] = value;
91
- // console.log(model, modelName, model, model[field], field, value )
92
- // setValue(model, field, value);
93
- } else {
94
- const v = valueOfCheckbox(ctrl);
95
- model[field] = v;
96
- }
97
- const objSet: any = {};
98
- objSet[modelName] = model;
99
- return objSet;
100
- } else if (type && type.toLowerCase() === 'radio') {
101
- if (field.indexOf('.') < 0 && field.indexOf('[') < 0 ) {
102
- model[field] = ctrl.value;
103
- } else {
104
- setValue(model, field, ctrl.value);
105
- }
106
- const objSet: any = {};
107
- objSet[modelName] = model;
108
- return objSet;
109
- } else if (type && (type.toLowerCase() === 'date' || type.toLowerCase() === 'datetime-local')) {
110
- const objSet: any = {};
111
- try {
112
- const selectedDate = new Date(ctrl.value);
113
- setValue(model, field, ctrl.value && ctrl.value!== '' ? selectedDate.toISOString() : null);
114
- objSet[modelName] = model;
115
- return objSet;
116
- } catch (error) {
117
- console.error('Error occurred while formatting date:', error);
118
- }
119
- return objSet;
120
- } else if (type && (type.toLowerCase() === 'time')) {
121
- const objSet: any = {};
122
- try {
123
- const selectedDate = new Date(ctrl.value);
124
- setValue(model, field, selectedDate.getTime());
125
- objSet[modelName] = model;
126
- return objSet;
127
- } catch (error) {
128
- console.error('Error occurred while formatting time:', error);
129
- }
130
- return objSet;
131
- } else {
132
- if (ctrl.tagName === 'SELECT') {
133
- if (ctrl.value === '' || !ctrl.value) {
134
- ctrl.removeAttribute('data-value');
135
- } else {
136
- ctrl.setAttribute('data-value', 'data-value');
137
- }
138
- }
139
- const data = valueOf(ctrl, tloc, e.type);
140
- if (data.mustChange) {
141
- if (field.indexOf('.') < 0 && field.indexOf('[') < 0) {
142
- model[field] = data.value;
143
- } else {
144
- setValue(model, field, data.value);
145
- }
146
- const objSet: any = {};
147
- objSet[modelName] = model;
148
- return objSet;
149
- }
150
- }
151
- } else {
152
- return buildFlatState(e, state, tloc);
153
- }
154
- } else {
155
- buildFlatState(e, state, tloc);
156
- }
157
- }
158
- export function valueOfCheckbox(ctrl: HTMLInputElement): string|number|boolean {
159
- const ctrlOnValue = ctrl.getAttribute('data-on-value');
160
- const ctrlOffValue = ctrl.getAttribute('data-off-value');
161
- if (ctrlOnValue && ctrlOffValue) {
162
- const onValue = ctrlOnValue ? ctrlOnValue : true;
163
- const offValue = ctrlOffValue ? ctrlOffValue : false;
164
- return ctrl.checked === true ? onValue : offValue;
165
- } else {
166
- return ctrl.checked === true;
167
- }
168
- }
169
- export function buildFlatState<S, K extends keyof S>(e: any, state: Readonly<S>, l?: Locale): K|undefined {
170
- const ctrl = e.currentTarget as HTMLInputElement;
171
- const stateName = ctrl.name;
172
- const objSet: any = {};
173
- const type = ctrl.getAttribute('type');
174
- if (type && type.toLowerCase() === 'checkbox') {
175
- if (ctrl.id && stateName === ctrl.id) {
176
- const v = valueOfCheckbox(ctrl);
177
- objSet[stateName] = v;
178
- return objSet;
179
- } else {
180
- let value = (state as any)[stateName];
181
- value.includes(ctrl.value) ? value = value.filter((v: string) => v !== ctrl.value) : value.push(ctrl.value);
182
- const objSet2: any = {[ctrl.name]: value};
183
- return objSet2;
184
- }
185
- } else {
186
- const data = valueOf(ctrl, l, e.type);
187
- if (data.mustChange) {
188
- objSet[stateName] = data.value;
189
- return objSet;
190
- } else {
191
- return undefined;
192
- }
193
- }
194
- }
1
+ import {Locale, ModelProps} from './core';
2
+ import {setValue} from './reflect';
3
+ import {valueOf} from './util';
4
+
5
+ export const enLocale = {
6
+ 'id': 'en-US',
7
+ 'countryCode': 'US',
8
+ 'dateFormat': 'M/d/yyyy',
9
+ 'firstDayOfWeek': 1,
10
+ 'decimalSeparator': '.',
11
+ 'groupSeparator': ',',
12
+ 'decimalDigits': 2,
13
+ 'currencyCode': 'USD',
14
+ 'currencySymbol': '$',
15
+ 'currencyPattern': 0
16
+ };
17
+ export function localeOf(lc?: Locale, glc?: (() => Locale) | Locale): Locale {
18
+ let l: Locale|undefined = lc;
19
+ if (!l) {
20
+ if (glc) {
21
+ if (typeof glc === 'function') {
22
+ l = glc();
23
+ } else {
24
+ l = glc;
25
+ }
26
+ }
27
+ if (!l) {
28
+ l = enLocale;
29
+ }
30
+ }
31
+ return l;
32
+ }
33
+ export function handleEvent(e: any, removeErr?: (ctrl: HTMLInputElement) => void) {
34
+ const ctrl = e.currentTarget as HTMLInputElement;
35
+ const type = ctrl.getAttribute('type');
36
+ const isPreventDefault = type && (['checkbox', 'radio'].indexOf(type.toLowerCase()) >= 0 ? false : true);
37
+ if (isPreventDefault) {
38
+ e.preventDefault();
39
+ }
40
+ if (
41
+ removeErr &&
42
+ ctrl.nodeName === 'SELECT' &&
43
+ ctrl.value &&
44
+ ctrl.classList.contains('invalid')) {
45
+ removeErr(ctrl);
46
+ }
47
+ }
48
+ export function handleProps<P extends ModelProps>(e: any, props: P, ctrl: HTMLInputElement, modelName: string, tloc: Locale, prepareData?: (data: any) => void) {
49
+ if (props.setGlobalState) {
50
+ const res = valueOf(ctrl, tloc, e.type);
51
+ if (res.mustChange) {
52
+ const dataField = ctrl.getAttribute('data-field');
53
+ const field = (dataField ? dataField : ctrl.name);
54
+ const propsDataForm = (props as any)[modelName];
55
+ const form = ctrl.form;
56
+ if (form) {
57
+ const formName = form.name;
58
+ if (field.indexOf('.') < 0 && field.indexOf('[') < 0) {
59
+ const data = props.shouldBeCustomized && prepareData ? prepareData({ [ctrl.name]: res.value }) : { [ctrl.name]: res.value };
60
+ props.setGlobalState({ [formName]: { ...propsDataForm, ...data } });
61
+ } else {
62
+ setValue(propsDataForm, field, ctrl.value);
63
+ props.setGlobalState({ [formName]: { ...propsDataForm } });
64
+ }
65
+ }
66
+ }
67
+ }
68
+ }
69
+ export function buildState<S, K extends keyof S>(e: any, state: Readonly<S>, ctrl: HTMLInputElement, modelName: string, tloc: Locale): K|undefined {
70
+ const form = ctrl.form;
71
+ if (form) {
72
+ if (modelName && modelName !== '') {
73
+ const type = ctrl.getAttribute('type');
74
+ const ex = (state as any)[modelName];
75
+ const dataField = ctrl.getAttribute('data-field');
76
+ const field = (dataField ? dataField : ctrl.name);
77
+ const model = Object.assign({}, ex);
78
+ if (type && type.toLowerCase() === 'checkbox') {
79
+ let value = model[field];
80
+ if (ctrl.id && ctrl.name !== ctrl.id) {
81
+ if (!value || !Array.isArray(value)) {
82
+ value = [];
83
+ }
84
+ value.includes(ctrl.value) ? value = value.filter((v: string) => v !== ctrl.value) : value.push(ctrl.value);
85
+ // if (dType == 'array'){
86
+ // if (value === 'string'){
87
+ // value = [value]
88
+ // }
89
+ // }
90
+ model[field] = value;
91
+ // console.log(model, modelName, model, model[field], field, value )
92
+ // setValue(model, field, value);
93
+ } else {
94
+ const v = valueOfCheckbox(ctrl);
95
+ model[field] = v;
96
+ }
97
+ const objSet: any = {};
98
+ objSet[modelName] = model;
99
+ return objSet;
100
+ } else if (type && type.toLowerCase() === 'radio') {
101
+ if (field.indexOf('.') < 0 && field.indexOf('[') < 0 ) {
102
+ model[field] = ctrl.value;
103
+ } else {
104
+ setValue(model, field, ctrl.value);
105
+ }
106
+ const objSet: any = {};
107
+ objSet[modelName] = model;
108
+ return objSet;
109
+ } else if (type && (type.toLowerCase() === 'date' || type.toLowerCase() === 'datetime-local')) {
110
+ const objSet: any = {};
111
+ try {
112
+ const selectedDate = new Date(ctrl.value);
113
+ setValue(model, field, ctrl.value && ctrl.value!== '' ? selectedDate.toISOString() : null);
114
+ objSet[modelName] = model;
115
+ return objSet;
116
+ } catch (error) {
117
+ console.error('Error occurred while formatting date:', error);
118
+ }
119
+ return objSet;
120
+ } else if (type && (type.toLowerCase() === 'time')) {
121
+ const objSet: any = {};
122
+ try {
123
+ const selectedDate = new Date(ctrl.value);
124
+ setValue(model, field, selectedDate.getTime());
125
+ objSet[modelName] = model;
126
+ return objSet;
127
+ } catch (error) {
128
+ console.error('Error occurred while formatting time:', error);
129
+ }
130
+ return objSet;
131
+ } else {
132
+ if (ctrl.tagName === 'SELECT') {
133
+ if (ctrl.value === '' || !ctrl.value) {
134
+ ctrl.removeAttribute('data-value');
135
+ } else {
136
+ ctrl.setAttribute('data-value', 'data-value');
137
+ }
138
+ }
139
+ const data = valueOf(ctrl, tloc, e.type);
140
+ if (data.mustChange) {
141
+ if (field.indexOf('.') < 0 && field.indexOf('[') < 0) {
142
+ model[field] = data.value;
143
+ } else {
144
+ setValue(model, field, data.value);
145
+ }
146
+ const objSet: any = {};
147
+ objSet[modelName] = model;
148
+ return objSet;
149
+ }
150
+ }
151
+ } else {
152
+ return buildFlatState(e, state, tloc);
153
+ }
154
+ } else {
155
+ buildFlatState(e, state, tloc);
156
+ }
157
+ }
158
+ export function valueOfCheckbox(ctrl: HTMLInputElement): string|number|boolean {
159
+ const ctrlOnValue = ctrl.getAttribute('data-on-value');
160
+ const ctrlOffValue = ctrl.getAttribute('data-off-value');
161
+ if (ctrlOnValue && ctrlOffValue) {
162
+ const onValue = ctrlOnValue ? ctrlOnValue : true;
163
+ const offValue = ctrlOffValue ? ctrlOffValue : false;
164
+ return ctrl.checked === true ? onValue : offValue;
165
+ } else {
166
+ return ctrl.checked === true;
167
+ }
168
+ }
169
+ export function buildFlatState<S, K extends keyof S>(e: any, state: Readonly<S>, l?: Locale): K|undefined {
170
+ const ctrl = e.currentTarget as HTMLInputElement;
171
+ const stateName = ctrl.name;
172
+ const objSet: any = {};
173
+ const type = ctrl.getAttribute('type');
174
+ if (type && type.toLowerCase() === 'checkbox') {
175
+ if (ctrl.id && stateName === ctrl.id) {
176
+ const v = valueOfCheckbox(ctrl);
177
+ objSet[stateName] = v;
178
+ return objSet;
179
+ } else {
180
+ let value = (state as any)[stateName];
181
+ value.includes(ctrl.value) ? value = value.filter((v: string) => v !== ctrl.value) : value.push(ctrl.value);
182
+ const objSet2: any = {[ctrl.name]: value};
183
+ return objSet2;
184
+ }
185
+ } else {
186
+ const data = valueOf(ctrl, l, e.type);
187
+ if (data.mustChange) {
188
+ objSet[stateName] = data.value;
189
+ return objSet;
190
+ } else {
191
+ return undefined;
192
+ }
193
+ }
194
+ }
package/src/update.ts CHANGED
@@ -1,86 +1,86 @@
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
- };
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
@@ -22,7 +22,7 @@ export interface BaseEditComponentParam<T, ID> {
22
22
  readOnly?: boolean;
23
23
  // deletable?: boolean;
24
24
 
25
- insertSuccessMsg?: string;
25
+ createSuccessMsg?: string;
26
26
  updateSuccessMsg?: string;
27
27
 
28
28
  handleNotFound?: (form?: HTMLFormElement) => void;
@@ -456,7 +456,7 @@ if (running === true) {
456
456
  service.update(obj).then((res: number|T|ErrorMessage[]) => postSave(res, obj, version, false, isBackO)).catch(handleError);
457
457
  }
458
458
  } else {
459
- service.insert(obj).then((res: number|T|ErrorMessage[]) => postSave(res, obj, version, false, isBackO)).catch(handleError);
459
+ service.create(obj).then((res: number|T|ErrorMessage[]) => postSave(res, obj, version, false, isBackO)).catch(handleError);
460
460
  }
461
461
  };
462
462
 
@@ -549,3 +549,13 @@ if (running === true) {
549
549
  doSave
550
550
  };
551
551
  };
552
+ export function isSuccessful<T>(x: number|T|ErrorMessage[]): boolean {
553
+ if (Array.isArray(x)) {
554
+ return false;
555
+ } else if (typeof x === 'object') {
556
+ return true;
557
+ } else if (typeof x === 'number' && x > 0) {
558
+ return true;
559
+ }
560
+ return false;
561
+ }