@juantroconisf/lib 2.3.1 → 2.3.3

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
@@ -24,7 +24,7 @@ type ValidatorParams = {
24
24
  [K in keyof ValidatorTypes]?: ValidatorTypes[K] extends Validator<any, infer U> ? U : never;
25
25
  };
26
26
  type ValidatorErrorMessage = {
27
- [K in keyof ValidatorTypes]: string;
27
+ [K in keyof ValidatorTypes]?: string;
28
28
  };
29
29
 
30
30
  interface ValidationProps {
@@ -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
@@ -24,7 +24,7 @@ type ValidatorParams = {
24
24
  [K in keyof ValidatorTypes]?: ValidatorTypes[K] extends Validator<any, infer U> ? U : never;
25
25
  };
26
26
  type ValidatorErrorMessage = {
27
- [K in keyof ValidatorTypes]: string;
27
+ [K in keyof ValidatorTypes]?: string;
28
28
  };
29
29
 
30
30
  interface ValidationProps {
@@ -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,34 +185,61 @@ 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();
189
- let errors2 = allToValue(initialObj, false);
190
- const onChange = ({
191
- target: { id, value }
192
- }) => setState(handleNestedChange({ state, id, value })), onBlur = ({ target: { id } }) => setTouched(handleNestedChange({ state: touched, id, value: true })), resetForm = () => {
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)(() => {
193
200
  setTouched(allToValue(initialObj, false));
194
201
  setState(initialObj);
195
- };
196
- function register(id, validations, errorMessages) {
197
- const [value, isTouched] = [state[id], touched[id]], hasValidations = validations !== void 0, inputValidations = hasValidations ? getValidation(value, isTouched, validations, errorMessages) : {};
198
- errors2 = handleNestedChange({
199
- state: errors2,
200
- id,
201
- value: hasValidations ? !inputValidations.isValid : false
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;
202
240
  });
203
- return {
204
- id,
205
- onChange,
206
- onBlur,
207
- value,
208
- isInvalid: hasValidations ? !inputValidations.isValid : false,
209
- errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
210
- };
211
- }
212
- const hasInvalidValues = () => {
213
- setTouched(allToValue(touched, true));
214
241
  return JSON.stringify(errors2).includes(":true");
215
- };
242
+ }, [errors2]);
216
243
  return {
217
244
  onChange,
218
245
  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 } from "react";
9
+ import { useState, useCallback, useMemo } from "react";
10
10
 
11
11
  // types/errors.ts
12
12
  var NextUIError = class {
@@ -167,34 +167,61 @@ 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();
171
- let errors2 = allToValue(initialObj, false);
172
- const onChange = ({
173
- target: { id, value }
174
- }) => setState(handleNestedChange({ state, id, value })), onBlur = ({ target: { id } }) => setTouched(handleNestedChange({ state: touched, id, value: true })), resetForm = () => {
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(() => {
175
182
  setTouched(allToValue(initialObj, false));
176
183
  setState(initialObj);
177
- };
178
- function register(id, validations, errorMessages) {
179
- const [value, isTouched] = [state[id], touched[id]], hasValidations = validations !== void 0, inputValidations = hasValidations ? getValidation(value, isTouched, validations, errorMessages) : {};
180
- errors2 = handleNestedChange({
181
- state: errors2,
182
- id,
183
- value: hasValidations ? !inputValidations.isValid : false
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;
184
222
  });
185
- return {
186
- id,
187
- onChange,
188
- onBlur,
189
- value,
190
- isInvalid: hasValidations ? !inputValidations.isValid : false,
191
- errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
192
- };
193
- }
194
- const hasInvalidValues = () => {
195
- setTouched(allToValue(touched, true));
196
223
  return JSON.stringify(errors2).includes(":true");
197
- };
224
+ }, [errors2]);
198
225
  return {
199
226
  onChange,
200
227
  onBlur,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juantroconisf/lib",
3
- "version": "2.3.1",
3
+ "version": "2.3.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",