@juantroconisf/lib 2.3.3 → 2.3.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/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,61 +185,33 @@ 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]);
204
- const register = (0, import_react.useCallback)(
205
- (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
- );
218
- return {
219
- id,
220
- onChange,
221
- onBlur,
222
- value,
223
- isInvalid: hasValidations ? !inputValidations.isValid : false,
224
- errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
225
- };
226
- },
227
- [state, touched, getValidation]
228
- );
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;
194
+ };
195
+ function register(id, validations, errorMessages) {
196
+ const [value, isTouched] = [state[id], touched[id]], hasValidations = validations !== void 0, inputValidations = hasValidations ? getValidation(value, isTouched, validations, errorMessages) : {};
197
+ errors2.current = handleNestedChange({
198
+ state: errors2.current,
199
+ id,
200
+ value: hasValidations ? !inputValidations.isValid : false
240
201
  });
241
- return JSON.stringify(errors2).includes(":true");
242
- }, [errors2]);
202
+ return {
203
+ id,
204
+ onChange,
205
+ onBlur,
206
+ value,
207
+ isInvalid: hasValidations ? !inputValidations.isValid : false,
208
+ errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
209
+ };
210
+ }
211
+ const hasInvalidValues = () => {
212
+ setTouched(allToValue(touched, true));
213
+ return JSON.stringify(errors2.current).includes(":true");
214
+ };
243
215
  return {
244
216
  onChange,
245
217
  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 } from "react";
10
10
 
11
11
  // types/errors.ts
12
12
  var NextUIError = class {
@@ -167,61 +167,33 @@ 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]);
186
- const register = useCallback(
187
- (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
- );
200
- return {
201
- id,
202
- onChange,
203
- onBlur,
204
- value,
205
- isInvalid: hasValidations ? !inputValidations.isValid : false,
206
- errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
207
- };
208
- },
209
- [state, touched, getValidation]
210
- );
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;
176
+ };
177
+ function register(id, validations, errorMessages) {
178
+ const [value, isTouched] = [state[id], touched[id]], hasValidations = validations !== void 0, inputValidations = hasValidations ? getValidation(value, isTouched, validations, errorMessages) : {};
179
+ errors2.current = handleNestedChange({
180
+ state: errors2.current,
181
+ id,
182
+ value: hasValidations ? !inputValidations.isValid : false
222
183
  });
223
- return JSON.stringify(errors2).includes(":true");
224
- }, [errors2]);
184
+ return {
185
+ id,
186
+ onChange,
187
+ onBlur,
188
+ value,
189
+ isInvalid: hasValidations ? !inputValidations.isValid : false,
190
+ errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
191
+ };
192
+ }
193
+ const hasInvalidValues = () => {
194
+ setTouched(allToValue(touched, true));
195
+ return JSON.stringify(errors2.current).includes(":true");
196
+ };
225
197
  return {
226
198
  onChange,
227
199
  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.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",