@juantroconisf/lib 2.3.4 → 2.3.6

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.js CHANGED
@@ -158,12 +158,7 @@ function useValidation() {
158
158
  const { lang } = useComponentLanguage();
159
159
  const getValidation = (value, isTouched, types, errorMessages) => {
160
160
  const items = Object.entries(types), requiredIndex = items.findIndex(([key]) => key === "required"), isRequired = requiredIndex === -1 ? false : items[requiredIndex][1];
161
- if (isRequired && !isTouched)
162
- return {
163
- isValid: true,
164
- validationProps: nxError()
165
- };
166
- if (value === "" && !isRequired)
161
+ if (value === "" && !isRequired || !isTouched)
167
162
  return { isValid: true, validationProps: nxError() };
168
163
  const validations = items.map(([name, opts]) => {
169
164
  const { validate, msg: msg2 } = errors[name];
@@ -192,22 +187,25 @@ function useFormChange(initialObj) {
192
187
  setTouched(allToValue(initialObj, false));
193
188
  setState(initialObj);
194
189
  };
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
201
- });
202
- return {
203
- id,
204
- onChange,
205
- onBlur,
206
- value,
207
- isInvalid: hasValidations ? !inputValidations.isValid : false,
208
- errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
209
- };
210
- }
190
+ const register = (0, import_react.useCallback)(
191
+ (id, validations, errorMessages) => {
192
+ const [value, isTouched] = [state[id], touched[id]], hasValidations = validations !== void 0, inputValidations = hasValidations ? getValidation(value, isTouched, validations, errorMessages) : {};
193
+ errors2.current = handleNestedChange({
194
+ state: errors2.current,
195
+ id,
196
+ value: hasValidations ? !inputValidations.isValid : false
197
+ });
198
+ return {
199
+ id,
200
+ onChange,
201
+ onBlur,
202
+ value,
203
+ isInvalid: hasValidations ? !inputValidations.isValid : false,
204
+ errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
205
+ };
206
+ },
207
+ [getValidation, onChange, onBlur]
208
+ );
211
209
  const hasInvalidValues = () => {
212
210
  setTouched(allToValue(touched, true));
213
211
  return JSON.stringify(errors2.current).includes(":true");
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, useRef } from "react";
9
+ import { useState, useRef, useCallback } from "react";
10
10
 
11
11
  // types/errors.ts
12
12
  var NextUIError = class {
@@ -140,12 +140,7 @@ function useValidation() {
140
140
  const { lang } = useComponentLanguage();
141
141
  const getValidation = (value, isTouched, types, errorMessages) => {
142
142
  const items = Object.entries(types), requiredIndex = items.findIndex(([key]) => key === "required"), isRequired = requiredIndex === -1 ? false : items[requiredIndex][1];
143
- if (isRequired && !isTouched)
144
- return {
145
- isValid: true,
146
- validationProps: nxError()
147
- };
148
- if (value === "" && !isRequired)
143
+ if (value === "" && !isRequired || !isTouched)
149
144
  return { isValid: true, validationProps: nxError() };
150
145
  const validations = items.map(([name, opts]) => {
151
146
  const { validate, msg: msg2 } = errors[name];
@@ -174,22 +169,25 @@ function useFormChange(initialObj) {
174
169
  setTouched(allToValue(initialObj, false));
175
170
  setState(initialObj);
176
171
  };
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
183
- });
184
- return {
185
- id,
186
- onChange,
187
- onBlur,
188
- value,
189
- isInvalid: hasValidations ? !inputValidations.isValid : false,
190
- errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
191
- };
192
- }
172
+ const register = useCallback(
173
+ (id, validations, errorMessages) => {
174
+ const [value, isTouched] = [state[id], touched[id]], hasValidations = validations !== void 0, inputValidations = hasValidations ? getValidation(value, isTouched, validations, errorMessages) : {};
175
+ errors2.current = handleNestedChange({
176
+ state: errors2.current,
177
+ id,
178
+ value: hasValidations ? !inputValidations.isValid : false
179
+ });
180
+ return {
181
+ id,
182
+ onChange,
183
+ onBlur,
184
+ value,
185
+ isInvalid: hasValidations ? !inputValidations.isValid : false,
186
+ errorMessage: hasValidations ? inputValidations.validationProps.errorMessage : ""
187
+ };
188
+ },
189
+ [getValidation, onChange, onBlur]
190
+ );
193
191
  const hasInvalidValues = () => {
194
192
  setTouched(allToValue(touched, true));
195
193
  return JSON.stringify(errors2.current).includes(":true");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juantroconisf/lib",
3
- "version": "2.3.4",
3
+ "version": "2.3.6",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",