@juantroconisf/lib 2.3.2 → 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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +51 -24
- package/dist/index.mjs +52 -25
- package/package.json +1 -1
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 }
|
|
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 }
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
state
|
|
200
|
-
|
|
201
|
-
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
state
|
|
182
|
-
|
|
183
|
-
|
|
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,
|