@juantroconisf/lib 2.3.3 → 2.3.5

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/dist/index.d.mts CHANGED
@@ -39,7 +39,7 @@ interface RegisterInputProps extends ValidationProps {
39
39
  }
40
40
 
41
41
  declare function useFormChange<InitialObj extends object>(initialObj: InitialObj): {
42
- onChange: ({ target: { id, value } }: ChangeEvent<HTMLInputElement>) => void;
42
+ onChange: ({ target: { id, value }, }: ChangeEvent<HTMLInputElement>) => void;
43
43
  onBlur: ({ target: { id } }: FocusEvent<HTMLInputElement>) => void;
44
44
  state: InitialObj;
45
45
  setState: react.Dispatch<react.SetStateAction<InitialObj>>;
package/dist/index.d.ts CHANGED
@@ -39,7 +39,7 @@ interface RegisterInputProps extends ValidationProps {
39
39
  }
40
40
 
41
41
  declare function useFormChange<InitialObj extends object>(initialObj: InitialObj): {
42
- onChange: ({ target: { id, value } }: ChangeEvent<HTMLInputElement>) => void;
42
+ onChange: ({ target: { id, value }, }: ChangeEvent<HTMLInputElement>) => void;
43
43
  onBlur: ({ target: { id } }: FocusEvent<HTMLInputElement>) => void;
44
44
  state: InitialObj;
45
45
  setState: react.Dispatch<react.SetStateAction<InitialObj>>;
package/dist/index.js CHANGED
@@ -185,36 +185,21 @@ function useValidation() {
185
185
 
186
186
  // src/hooks/useFormChange.tsx
187
187
  function useFormChange(initialObj) {
188
- const [state, setState] = (0, import_react.useState)(initialObj), [touched, setTouched] = (0, import_react.useState)(allToValue(initialObj, false)), { getValidation } = useValidation(), [errors2, setErrors] = (0, import_react.useState)(allToValue(initialObj, false));
189
- const onChange = (0, import_react.useCallback)(
190
- ({ target: { id, value } }) => setState(
191
- (prevState) => handleNestedChange({ state: prevState, id, value })
192
- ),
193
- []
194
- ), onBlur = (0, import_react.useCallback)(
195
- ({ target: { id } }) => setTouched(
196
- (prevTouched) => handleNestedChange({ state: prevTouched, id, value: true })
197
- ),
198
- []
199
- ), resetForm = (0, import_react.useCallback)(() => {
188
+ const [state, setState] = (0, import_react.useState)(initialObj), [touched, setTouched] = (0, import_react.useState)(allToValue(initialObj, false)), { getValidation } = useValidation(), errors2 = (0, import_react.useRef)(allToValue(initialObj, false));
189
+ const onChange = ({
190
+ target: { id, value }
191
+ }) => setState(handleNestedChange({ state, id, value })), onBlur = ({ target: { id } }) => setTouched(handleNestedChange({ state: touched, id, value: true })), resetForm = () => {
200
192
  setTouched(allToValue(initialObj, false));
201
193
  setState(initialObj);
202
- setErrors(allToValue(initialObj, false));
203
- }, [initialObj]);
194
+ };
204
195
  const register = (0, import_react.useCallback)(
205
196
  (id, validations, errorMessages) => {
206
- const [value, isTouched] = [state[id], touched[id]], hasValidations = validations !== void 0;
207
- const inputValidations = (0, import_react.useMemo)(
208
- () => hasValidations ? getValidation(value, isTouched, validations, errorMessages) : {},
209
- [value, isTouched, validations, errorMessages]
210
- );
211
- setErrors(
212
- (prevErrors) => handleNestedChange({
213
- state: prevErrors,
214
- id,
215
- value: hasValidations ? !inputValidations.isValid : false
216
- })
217
- );
197
+ const [value, isTouched] = [state[id], touched[id]], hasValidations = validations !== void 0, inputValidations = hasValidations ? getValidation(value, isTouched, validations, errorMessages) : {};
198
+ errors2.current = handleNestedChange({
199
+ state: errors2.current,
200
+ id,
201
+ value: hasValidations ? !inputValidations.isValid : false
202
+ });
218
203
  return {
219
204
  id,
220
205
  onChange,
@@ -224,22 +209,12 @@ function useFormChange(initialObj) {
224
209
  errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
225
210
  };
226
211
  },
227
- [state, touched, getValidation]
212
+ [getValidation, onChange, onBlur]
228
213
  );
229
- const hasInvalidValues = (0, import_react.useCallback)(() => {
230
- setTouched((prevTouched) => {
231
- const newTouched = allToValue(prevTouched, true);
232
- setErrors((prevErrors) => {
233
- const newErrors = { ...prevErrors };
234
- for (const key in newTouched) {
235
- if (newTouched.hasOwnProperty(key)) newErrors[key] = true;
236
- }
237
- return newErrors;
238
- });
239
- return newTouched;
240
- });
241
- return JSON.stringify(errors2).includes(":true");
242
- }, [errors2]);
214
+ const hasInvalidValues = () => {
215
+ setTouched(allToValue(touched, true));
216
+ return JSON.stringify(errors2.current).includes(":true");
217
+ };
243
218
  return {
244
219
  onChange,
245
220
  onBlur,
package/dist/index.mjs CHANGED
@@ -6,7 +6,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
6
6
  });
7
7
 
8
8
  // src/hooks/useFormChange.tsx
9
- import { useState, useCallback, useMemo } from "react";
9
+ import { useState, useRef, useCallback } from "react";
10
10
 
11
11
  // types/errors.ts
12
12
  var NextUIError = class {
@@ -167,36 +167,21 @@ function useValidation() {
167
167
 
168
168
  // src/hooks/useFormChange.tsx
169
169
  function useFormChange(initialObj) {
170
- const [state, setState] = useState(initialObj), [touched, setTouched] = useState(allToValue(initialObj, false)), { getValidation } = useValidation(), [errors2, setErrors] = useState(allToValue(initialObj, false));
171
- const onChange = useCallback(
172
- ({ target: { id, value } }) => setState(
173
- (prevState) => handleNestedChange({ state: prevState, id, value })
174
- ),
175
- []
176
- ), onBlur = useCallback(
177
- ({ target: { id } }) => setTouched(
178
- (prevTouched) => handleNestedChange({ state: prevTouched, id, value: true })
179
- ),
180
- []
181
- ), resetForm = useCallback(() => {
170
+ const [state, setState] = useState(initialObj), [touched, setTouched] = useState(allToValue(initialObj, false)), { getValidation } = useValidation(), errors2 = useRef(allToValue(initialObj, false));
171
+ const onChange = ({
172
+ target: { id, value }
173
+ }) => setState(handleNestedChange({ state, id, value })), onBlur = ({ target: { id } }) => setTouched(handleNestedChange({ state: touched, id, value: true })), resetForm = () => {
182
174
  setTouched(allToValue(initialObj, false));
183
175
  setState(initialObj);
184
- setErrors(allToValue(initialObj, false));
185
- }, [initialObj]);
176
+ };
186
177
  const register = useCallback(
187
178
  (id, validations, errorMessages) => {
188
- const [value, isTouched] = [state[id], touched[id]], hasValidations = validations !== void 0;
189
- const inputValidations = useMemo(
190
- () => hasValidations ? getValidation(value, isTouched, validations, errorMessages) : {},
191
- [value, isTouched, validations, errorMessages]
192
- );
193
- setErrors(
194
- (prevErrors) => handleNestedChange({
195
- state: prevErrors,
196
- id,
197
- value: hasValidations ? !inputValidations.isValid : false
198
- })
199
- );
179
+ const [value, isTouched] = [state[id], touched[id]], hasValidations = validations !== void 0, inputValidations = hasValidations ? getValidation(value, isTouched, validations, errorMessages) : {};
180
+ errors2.current = handleNestedChange({
181
+ state: errors2.current,
182
+ id,
183
+ value: hasValidations ? !inputValidations.isValid : false
184
+ });
200
185
  return {
201
186
  id,
202
187
  onChange,
@@ -206,22 +191,12 @@ function useFormChange(initialObj) {
206
191
  errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
207
192
  };
208
193
  },
209
- [state, touched, getValidation]
194
+ [getValidation, onChange, onBlur]
210
195
  );
211
- const hasInvalidValues = useCallback(() => {
212
- setTouched((prevTouched) => {
213
- const newTouched = allToValue(prevTouched, true);
214
- setErrors((prevErrors) => {
215
- const newErrors = { ...prevErrors };
216
- for (const key in newTouched) {
217
- if (newTouched.hasOwnProperty(key)) newErrors[key] = true;
218
- }
219
- return newErrors;
220
- });
221
- return newTouched;
222
- });
223
- return JSON.stringify(errors2).includes(":true");
224
- }, [errors2]);
196
+ const hasInvalidValues = () => {
197
+ setTouched(allToValue(touched, true));
198
+ return JSON.stringify(errors2.current).includes(":true");
199
+ };
225
200
  return {
226
201
  onChange,
227
202
  onBlur,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juantroconisf/lib",
3
- "version": "2.3.3",
3
+ "version": "2.3.5",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",