@moamc/rn-cli 1.4.2 → 1.4.4
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/package.json +1 -1
- package/templates/hookTemplate.js +9 -14
package/package.json
CHANGED
|
@@ -41,7 +41,7 @@ const generateHookTemplate = (hookName, hasForm = false, formFields = [], eventN
|
|
|
41
41
|
const hasDropdown = formFields.some(f => f.type === 'dropdown');
|
|
42
42
|
const hasDate = formFields.some(f => f.type === 'date');
|
|
43
43
|
|
|
44
|
-
return `import { useState } from 'react';
|
|
44
|
+
return `import { useState, useCallback } from 'react';
|
|
45
45
|
import { showToastNotification } from '../../../../helpers/common';${hasDate ? '\nimport moment from \'moment\';' : ''}
|
|
46
46
|
import { validate${hookName} } from '../validation';
|
|
47
47
|
|
|
@@ -53,22 +53,17 @@ ${formFields.map(f => `\t\t${f.name}: '',`).join('\n')}
|
|
|
53
53
|
\tconst [touched, setTouched] = useState({});
|
|
54
54
|
\tconst [loading, setLoading] = useState(false);${hasDropdown ? '\n\tconst [bottomSheetType, setBottomSheetType] = useState(null);' : ''}${hasDate ? '\n\tconst [openCalendar, setOpenCalendar] = useState(false);' : ''}
|
|
55
55
|
|
|
56
|
-
\tconst handleChange = (value, field) => {
|
|
57
|
-
\t\
|
|
58
|
-
\t\tsetFormData(updatedFormData);
|
|
56
|
+
\tconst handleChange = useCallback((value, field) => {
|
|
57
|
+
\t\tsetFormData(prev => ({ ...prev, [field]: value }));
|
|
59
58
|
\t\tsetTouched(prev => ({ ...prev, [field]: true }));
|
|
60
|
-
\t\
|
|
61
|
-
\t
|
|
62
|
-
\t\tconst validationErrors = validate${hookName}(updatedFormData);
|
|
63
|
-
\t\tsetErrors(validationErrors);
|
|
64
|
-
\t};
|
|
59
|
+
\t\tsetErrors(prev => ({ ...prev, [field]: undefined }));
|
|
60
|
+
\t}, []);
|
|
65
61
|
|
|
66
|
-
\tconst handleSubmit = async () => {
|
|
62
|
+
\tconst handleSubmit = useCallback(async () => {
|
|
67
63
|
\t\tconst validationErrors = validate${hookName}(formData);
|
|
68
64
|
\t\tif (Object.keys(validationErrors).length > 0) {
|
|
69
65
|
\t\t\tsetErrors(validationErrors);
|
|
70
66
|
\t\t\tsetTouched(Object.keys(formData).reduce((acc, key) => ({ ...acc, [key]: true }), {}));
|
|
71
|
-
\t\t\t// Validation errors are shown inline below each field
|
|
72
67
|
\t\t\treturn;
|
|
73
68
|
\t\t}
|
|
74
69
|
|
|
@@ -81,11 +76,11 @@ ${formFields.map(f => `\t\t${f.name}: '',`).join('\n')}
|
|
|
81
76
|
\t\t} finally {
|
|
82
77
|
\t\t\tsetLoading(false);
|
|
83
78
|
\t\t}
|
|
84
|
-
\t};${hasDropdown ? `
|
|
79
|
+
\t}, [formData]);${hasDropdown ? `
|
|
85
80
|
|
|
86
|
-
\tconst openBottomSheet = (type) => {
|
|
81
|
+
\tconst openBottomSheet = useCallback((type) => {
|
|
87
82
|
\t\tsetBottomSheetType(type);
|
|
88
|
-
\t};` : ''}
|
|
83
|
+
\t}, []);` : ''}
|
|
89
84
|
|
|
90
85
|
\treturn {
|
|
91
86
|
\t\tformData,
|