react-hook-core 0.1.13 → 0.1.15

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,187 +1,187 @@
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
- model[field] = value;
86
- } else {
87
- const v = valueOfCheckbox(ctrl);
88
- model[field] = v;
89
- }
90
- const objSet: any = {};
91
- objSet[modelName] = model;
92
- return objSet;
93
- } else if (type && type.toLowerCase() === 'radio') {
94
- if (field.indexOf('.') < 0 && field.indexOf('[') < 0 ) {
95
- model[field] = ctrl.value;
96
- } else {
97
- setValue(model, field, ctrl.value);
98
- }
99
- const objSet: any = {};
100
- objSet[modelName] = model;
101
- return objSet;
102
- } else if (type && (type.toLowerCase() === 'date' || type.toLowerCase() === 'datetime-local')) {
103
- const objSet: any = {};
104
- try {
105
- const selectedDate = new Date(ctrl.value);
106
- setValue(model, field, selectedDate.toISOString());
107
- objSet[modelName] = model;
108
- return objSet;
109
- } catch (error) {
110
- console.error('Error occurred while formatting date:', error);
111
- }
112
- return objSet;
113
- } else if (type && (type.toLowerCase() === 'time')) {
114
- const objSet: any = {};
115
- try {
116
- const selectedDate = new Date(ctrl.value);
117
- setValue(model, field, selectedDate.getTime());
118
- objSet[modelName] = model;
119
- return objSet;
120
- } catch (error) {
121
- console.error('Error occurred while formatting time:', error);
122
- }
123
- return objSet;
124
- } else {
125
- if (ctrl.tagName === 'SELECT') {
126
- if (ctrl.value === '' || !ctrl.value) {
127
- ctrl.removeAttribute('data-value');
128
- } else {
129
- ctrl.setAttribute('data-value', 'data-value');
130
- }
131
- }
132
- const data = valueOf(ctrl, tloc, e.type);
133
- if (data.mustChange) {
134
- if (field.indexOf('.') < 0 && field.indexOf('[') < 0) {
135
- model[field] = data.value;
136
- } else {
137
- setValue(model, field, data.value);
138
- }
139
- const objSet: any = {};
140
- objSet[modelName] = model;
141
- return objSet;
142
- }
143
- }
144
- } else {
145
- return buildFlatState(e, state, tloc);
146
- }
147
- } else {
148
- buildFlatState(e, state, tloc);
149
- }
150
- }
151
- export function valueOfCheckbox(ctrl: HTMLInputElement): string|number|boolean {
152
- const ctrlOnValue = ctrl.getAttribute('data-on-value');
153
- const ctrlOffValue = ctrl.getAttribute('data-off-value');
154
- if (ctrlOnValue && ctrlOffValue) {
155
- const onValue = ctrlOnValue ? ctrlOnValue : true;
156
- const offValue = ctrlOffValue ? ctrlOffValue : false;
157
- return ctrl.checked === true ? onValue : offValue;
158
- } else {
159
- return ctrl.checked === true;
160
- }
161
- }
162
- export function buildFlatState<S, K extends keyof S>(e: any, state: Readonly<S>, l?: Locale): K|undefined {
163
- const ctrl = e.currentTarget as HTMLInputElement;
164
- const stateName = ctrl.name;
165
- const objSet: any = {};
166
- const type = ctrl.getAttribute('type');
167
- if (type && type.toLowerCase() === 'checkbox') {
168
- if (ctrl.id && stateName === ctrl.id) {
169
- const v = valueOfCheckbox(ctrl);
170
- objSet[stateName] = v;
171
- return objSet;
172
- } else {
173
- let value = (state as any)[stateName];
174
- value.includes(ctrl.value) ? value = value.filter((v: string) => v !== ctrl.value) : value.push(ctrl.value);
175
- const objSet2: any = {[ctrl.name]: value};
176
- return objSet2;
177
- }
178
- } else {
179
- const data = valueOf(ctrl, l, e.type);
180
- if (data.mustChange) {
181
- objSet[stateName] = data.value;
182
- return objSet;
183
- } else {
184
- return undefined;
185
- }
186
- }
187
- }
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
+ model[field] = value;
86
+ } else {
87
+ const v = valueOfCheckbox(ctrl);
88
+ model[field] = v;
89
+ }
90
+ const objSet: any = {};
91
+ objSet[modelName] = model;
92
+ return objSet;
93
+ } else if (type && type.toLowerCase() === 'radio') {
94
+ if (field.indexOf('.') < 0 && field.indexOf('[') < 0 ) {
95
+ model[field] = ctrl.value;
96
+ } else {
97
+ setValue(model, field, ctrl.value);
98
+ }
99
+ const objSet: any = {};
100
+ objSet[modelName] = model;
101
+ return objSet;
102
+ } else if (type && (type.toLowerCase() === 'date' || type.toLowerCase() === 'datetime-local')) {
103
+ const objSet: any = {};
104
+ try {
105
+ const selectedDate = new Date(ctrl.value);
106
+ setValue(model, field, ctrl.value && ctrl.value!== '' ? selectedDate.toISOString() : '');
107
+ objSet[modelName] = model;
108
+ return objSet;
109
+ } catch (error) {
110
+ console.error('Error occurred while formatting date:', error);
111
+ }
112
+ return objSet;
113
+ } else if (type && (type.toLowerCase() === 'time')) {
114
+ const objSet: any = {};
115
+ try {
116
+ const selectedDate = new Date(ctrl.value);
117
+ setValue(model, field, selectedDate.getTime());
118
+ objSet[modelName] = model;
119
+ return objSet;
120
+ } catch (error) {
121
+ console.error('Error occurred while formatting time:', error);
122
+ }
123
+ return objSet;
124
+ } else {
125
+ if (ctrl.tagName === 'SELECT') {
126
+ if (ctrl.value === '' || !ctrl.value) {
127
+ ctrl.removeAttribute('data-value');
128
+ } else {
129
+ ctrl.setAttribute('data-value', 'data-value');
130
+ }
131
+ }
132
+ const data = valueOf(ctrl, tloc, e.type);
133
+ if (data.mustChange) {
134
+ if (field.indexOf('.') < 0 && field.indexOf('[') < 0) {
135
+ model[field] = data.value;
136
+ } else {
137
+ setValue(model, field, data.value);
138
+ }
139
+ const objSet: any = {};
140
+ objSet[modelName] = model;
141
+ return objSet;
142
+ }
143
+ }
144
+ } else {
145
+ return buildFlatState(e, state, tloc);
146
+ }
147
+ } else {
148
+ buildFlatState(e, state, tloc);
149
+ }
150
+ }
151
+ export function valueOfCheckbox(ctrl: HTMLInputElement): string|number|boolean {
152
+ const ctrlOnValue = ctrl.getAttribute('data-on-value');
153
+ const ctrlOffValue = ctrl.getAttribute('data-off-value');
154
+ if (ctrlOnValue && ctrlOffValue) {
155
+ const onValue = ctrlOnValue ? ctrlOnValue : true;
156
+ const offValue = ctrlOffValue ? ctrlOffValue : false;
157
+ return ctrl.checked === true ? onValue : offValue;
158
+ } else {
159
+ return ctrl.checked === true;
160
+ }
161
+ }
162
+ export function buildFlatState<S, K extends keyof S>(e: any, state: Readonly<S>, l?: Locale): K|undefined {
163
+ const ctrl = e.currentTarget as HTMLInputElement;
164
+ const stateName = ctrl.name;
165
+ const objSet: any = {};
166
+ const type = ctrl.getAttribute('type');
167
+ if (type && type.toLowerCase() === 'checkbox') {
168
+ if (ctrl.id && stateName === ctrl.id) {
169
+ const v = valueOfCheckbox(ctrl);
170
+ objSet[stateName] = v;
171
+ return objSet;
172
+ } else {
173
+ let value = (state as any)[stateName];
174
+ value.includes(ctrl.value) ? value = value.filter((v: string) => v !== ctrl.value) : value.push(ctrl.value);
175
+ const objSet2: any = {[ctrl.name]: value};
176
+ return objSet2;
177
+ }
178
+ } else {
179
+ const data = valueOf(ctrl, l, e.type);
180
+ if (data.mustChange) {
181
+ objSet[stateName] = data.value;
182
+ return objSet;
183
+ } else {
184
+ return undefined;
185
+ }
186
+ }
187
+ }