@juantroconisf/lib 3.5.8 → 3.6.0

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
@@ -78,6 +78,7 @@ interface FormOptions<O extends StateType> {
78
78
  };
79
79
  }
80
80
  type TouchedType<O extends StateType> = Record<keyof O, boolean>;
81
+ type ErrorsType<O extends StateType> = Record<keyof O, NextUIError>;
81
82
  type ValueChangeFunc<O extends StateType, K extends keyof O> = (id: K, value: O[K]) => void;
82
83
  type BlurFunc<O extends StateType> = (id: keyof O) => void;
83
84
  interface ComponentInputProps<O extends StateType> {
@@ -126,9 +127,22 @@ interface UseFormChangeResponse<O extends StateType> {
126
127
  errors: React.RefObject<TouchedType<O>>;
127
128
  realErrors: React.RefObject<TouchedType<O>>;
128
129
  }
130
+ interface UseFormResponse<O extends StateType> {
131
+ onBlur: BlurFunc<O>;
132
+ onValueChange: ValueChangeFunc<O, keyof O>;
133
+ onSelectionChange: ValueChangeFunc<O, keyof O>;
134
+ state: O;
135
+ setState: React.Dispatch<React.SetStateAction<O>>;
136
+ register: RegisterFunc<O>;
137
+ hasInvalidValues: () => boolean;
138
+ touched: TouchedType<O>;
139
+ resetForm: (presevedKeys?: PreservedKeys<O>[]) => void;
140
+ errors: React.RefObject<ErrorsType<O>>;
141
+ }
142
+ type ValidateFunc<O extends StateType> = (value: O[keyof O], types?: ValidatorParams, errorMessages?: ValidatorErrorMessage) => NextUIError;
129
143
 
130
144
  declare function useFormChange<O extends StateType>({ state: initialState, validations, errorMessages, }: FormChangeState<O>): UseFormChangeResponse<O>;
131
145
 
132
- declare function useForm<O extends StateType>(initialState: O, { validations, errorMessages }?: FormOptions<O>): UseFormChangeResponse<O>;
146
+ declare function useForm<O extends StateType>(initialState: O, { validations, errorMessages }?: FormOptions<O>): UseFormResponse<O>;
133
147
 
134
- export { type BlurFunc, type ComponentInputProps, type FormChangeState, type FormOptions, type GetNestedValueByKey, type GetValidationFunc, type GetValidationResponse, type MapValidator, type NXErrorFunc, type NestedChangeProps, NextUIError, type PreservedKeys, type RegisterFunc, type RegisterHandleFunc, type RequiredValidationFunc, type StateType, type TouchedType, type UXProps, type UseFormChangeResponse, type UseValidationFunc, type ValidateInputFunc, type Validator, type ValidatorErrorMessage$1 as ValidatorErrorMessage, type ValidatorFunction, type ValidatorParams$1 as ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useForm, useFormChange };
148
+ export { type BlurFunc, type ComponentInputProps, type ErrorsType, type FormChangeState, type FormOptions, type GetNestedValueByKey, type GetValidationFunc, type GetValidationResponse, type MapValidator, type NXErrorFunc, type NestedChangeProps, NextUIError, type PreservedKeys, type RegisterFunc, type RegisterHandleFunc, type RequiredValidationFunc, type StateType, type TouchedType, type UXProps, type UseFormChangeResponse, type UseFormResponse, type UseValidationFunc, type ValidateFunc, type ValidateInputFunc, type Validator, type ValidatorErrorMessage$1 as ValidatorErrorMessage, type ValidatorFunction, type ValidatorParams$1 as ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useForm, useFormChange };
package/dist/index.d.ts CHANGED
@@ -78,6 +78,7 @@ interface FormOptions<O extends StateType> {
78
78
  };
79
79
  }
80
80
  type TouchedType<O extends StateType> = Record<keyof O, boolean>;
81
+ type ErrorsType<O extends StateType> = Record<keyof O, NextUIError>;
81
82
  type ValueChangeFunc<O extends StateType, K extends keyof O> = (id: K, value: O[K]) => void;
82
83
  type BlurFunc<O extends StateType> = (id: keyof O) => void;
83
84
  interface ComponentInputProps<O extends StateType> {
@@ -126,9 +127,22 @@ interface UseFormChangeResponse<O extends StateType> {
126
127
  errors: React.RefObject<TouchedType<O>>;
127
128
  realErrors: React.RefObject<TouchedType<O>>;
128
129
  }
130
+ interface UseFormResponse<O extends StateType> {
131
+ onBlur: BlurFunc<O>;
132
+ onValueChange: ValueChangeFunc<O, keyof O>;
133
+ onSelectionChange: ValueChangeFunc<O, keyof O>;
134
+ state: O;
135
+ setState: React.Dispatch<React.SetStateAction<O>>;
136
+ register: RegisterFunc<O>;
137
+ hasInvalidValues: () => boolean;
138
+ touched: TouchedType<O>;
139
+ resetForm: (presevedKeys?: PreservedKeys<O>[]) => void;
140
+ errors: React.RefObject<ErrorsType<O>>;
141
+ }
142
+ type ValidateFunc<O extends StateType> = (value: O[keyof O], types?: ValidatorParams, errorMessages?: ValidatorErrorMessage) => NextUIError;
129
143
 
130
144
  declare function useFormChange<O extends StateType>({ state: initialState, validations, errorMessages, }: FormChangeState<O>): UseFormChangeResponse<O>;
131
145
 
132
- declare function useForm<O extends StateType>(initialState: O, { validations, errorMessages }?: FormOptions<O>): UseFormChangeResponse<O>;
146
+ declare function useForm<O extends StateType>(initialState: O, { validations, errorMessages }?: FormOptions<O>): UseFormResponse<O>;
133
147
 
134
- export { type BlurFunc, type ComponentInputProps, type FormChangeState, type FormOptions, type GetNestedValueByKey, type GetValidationFunc, type GetValidationResponse, type MapValidator, type NXErrorFunc, type NestedChangeProps, NextUIError, type PreservedKeys, type RegisterFunc, type RegisterHandleFunc, type RequiredValidationFunc, type StateType, type TouchedType, type UXProps, type UseFormChangeResponse, type UseValidationFunc, type ValidateInputFunc, type Validator, type ValidatorErrorMessage$1 as ValidatorErrorMessage, type ValidatorFunction, type ValidatorParams$1 as ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useForm, useFormChange };
148
+ export { type BlurFunc, type ComponentInputProps, type ErrorsType, type FormChangeState, type FormOptions, type GetNestedValueByKey, type GetValidationFunc, type GetValidationResponse, type MapValidator, type NXErrorFunc, type NestedChangeProps, NextUIError, type PreservedKeys, type RegisterFunc, type RegisterHandleFunc, type RequiredValidationFunc, type StateType, type TouchedType, type UXProps, type UseFormChangeResponse, type UseFormResponse, type UseValidationFunc, type ValidateFunc, type ValidateInputFunc, type Validator, type ValidatorErrorMessage$1 as ValidatorErrorMessage, type ValidatorFunction, type ValidatorParams$1 as ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useForm, useFormChange };
package/dist/index.js CHANGED
@@ -290,42 +290,61 @@ function useFormChange({
290
290
 
291
291
  // src/hooks/useForm.tsx
292
292
  var import_react2 = require("react");
293
- function useForm(initialState, { validations, errorMessages } = {}) {
294
- const [state, setState] = (0, import_react2.useState)(initialState), falseState = allToValue(state, false), touched = (0, import_react2.useRef)(falseState), errors2 = (0, import_react2.useRef)({ ...falseState }), realErrors = (0, import_react2.useRef)({ ...falseState }), { getValidation } = useValidation(), ux = (0, import_react2.useRef)(
295
- allToValue(state, { isInvalid: false, errorMessage: "" })
296
- );
297
- const validateInput = (id, value, isInputTouched) => {
298
- const isTouched = isInputTouched === void 0 ? touched.current[id] : isInputTouched, requirements = validations?.[id];
299
- const [uiErrors] = [
300
- {
301
- ref: errors2,
302
- touched: isTouched
303
- },
304
- {
305
- ref: realErrors,
306
- touched: true
293
+
294
+ // src/hooks/useValidate.tsx
295
+ var validProps = {
296
+ isInvalid: false,
297
+ errorMessage: ""
298
+ };
299
+ function useValidate() {
300
+ const { lang } = useComponentLanguage();
301
+ const performValidations = (value, validationTypes, errorMessages) => {
302
+ if (!validationTypes) return validProps;
303
+ const items = Object.entries(validationTypes), requiredExist = validationTypes?.required !== void 0, isRequired = requiredExist ? validationTypes.required : false;
304
+ if (value === "" && !isRequired) return validProps;
305
+ let errorFound = validProps;
306
+ for (const [key, opts] of items) {
307
+ const { validate, msg } = errors[key], isInvalid = !validate(value, opts);
308
+ if (isInvalid) {
309
+ errorFound = {
310
+ isInvalid,
311
+ errorMessage: errorMessages?.[key] || lang[msg]
312
+ };
313
+ break;
307
314
  }
308
- ].map((item) => {
309
- const req = getValidation(
310
- value,
311
- item.touched,
312
- requirements,
313
- errorMessages?.[id]
314
- );
315
- item.ref.current = handleNestedChange({
316
- state: item.ref.current,
317
- id,
318
- value: !req.isValid
319
- });
320
- return req;
315
+ }
316
+ return errorFound;
317
+ };
318
+ return { performValidations };
319
+ }
320
+
321
+ // src/hooks/useForm.tsx
322
+ function useForm(initialState, { validations, errorMessages } = {}) {
323
+ const initial = {
324
+ touched: allToValue(initialState, false),
325
+ errors: allToValue(initialState, {
326
+ isInvalid: false,
327
+ errorMessage: ""
328
+ })
329
+ };
330
+ const [state, setState] = (0, import_react2.useState)(initialState), touched = (0, import_react2.useRef)(initial.touched), errors2 = (0, import_react2.useRef)(initial.errors), { performValidations } = useValidate();
331
+ const validateInput = (id, value) => {
332
+ errors2.current = handleNestedChange({
333
+ state: errors2.current,
334
+ id,
335
+ value: performValidations(value, validations?.[id], errorMessages?.[id])
321
336
  });
322
- if (requirements === void 0) return;
323
- ux.current[id] = {
324
- isInvalid: !uiErrors.isValid,
325
- errorMessage: uiErrors.validationProps.errorMessage
326
- };
327
337
  };
328
- const getUXProps = (0, import_react2.useCallback)((id) => ux.current[id], [ux.current]);
338
+ const getUXProps = (0, import_react2.useCallback)(
339
+ (id) => {
340
+ const inputError = errors2.current[id], isTouched = touched.current[id];
341
+ return {
342
+ isInvalid: isTouched && inputError.isInvalid,
343
+ errorMessage: isTouched ? inputError.errorMessage : ""
344
+ };
345
+ },
346
+ [errors2.current, touched.current]
347
+ );
329
348
  const onBlur = (id) => {
330
349
  touched.current = handleNestedChange({
331
350
  state: touched.current,
@@ -355,7 +374,6 @@ function useForm(initialState, { validations, errorMessages } = {}) {
355
374
  setState,
356
375
  register: {
357
376
  input: (id) => ({
358
- // ...ux.current[id],
359
377
  ...getUXProps(id),
360
378
  id,
361
379
  onBlur: () => onBlur(id),
@@ -385,13 +403,12 @@ function useForm(initialState, { validations, errorMessages } = {}) {
385
403
  },
386
404
  hasInvalidValues: () => {
387
405
  touched.current = allToValue(initialState, true);
388
- return JSON.stringify(realErrors.current).includes(":true");
406
+ return JSON.stringify(errors2.current).includes(":true");
389
407
  },
390
408
  touched: touched.current,
391
409
  resetForm: (preservedKeys) => {
392
- const val = allToValue(initialState, false);
393
- touched.current = val;
394
- errors2.current = val;
410
+ touched.current = initial.touched;
411
+ errors2.current = initial.errors;
395
412
  setState(
396
413
  preservedKeys === void 0 ? initialState : preservedKeys.reduce(
397
414
  (acc, key) => ({
@@ -402,8 +419,7 @@ function useForm(initialState, { validations, errorMessages } = {}) {
402
419
  )
403
420
  );
404
421
  },
405
- errors: errors2,
406
- realErrors
422
+ errors: errors2
407
423
  };
408
424
  }
409
425
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.mjs CHANGED
@@ -270,42 +270,61 @@ function useFormChange({
270
270
 
271
271
  // src/hooks/useForm.tsx
272
272
  import { useState as useState2, useRef as useRef2, useCallback as useCallback2 } from "react";
273
- function useForm(initialState, { validations, errorMessages } = {}) {
274
- const [state, setState] = useState2(initialState), falseState = allToValue(state, false), touched = useRef2(falseState), errors2 = useRef2({ ...falseState }), realErrors = useRef2({ ...falseState }), { getValidation } = useValidation(), ux = useRef2(
275
- allToValue(state, { isInvalid: false, errorMessage: "" })
276
- );
277
- const validateInput = (id, value, isInputTouched) => {
278
- const isTouched = isInputTouched === void 0 ? touched.current[id] : isInputTouched, requirements = validations?.[id];
279
- const [uiErrors] = [
280
- {
281
- ref: errors2,
282
- touched: isTouched
283
- },
284
- {
285
- ref: realErrors,
286
- touched: true
273
+
274
+ // src/hooks/useValidate.tsx
275
+ var validProps = {
276
+ isInvalid: false,
277
+ errorMessage: ""
278
+ };
279
+ function useValidate() {
280
+ const { lang } = useComponentLanguage();
281
+ const performValidations = (value, validationTypes, errorMessages) => {
282
+ if (!validationTypes) return validProps;
283
+ const items = Object.entries(validationTypes), requiredExist = validationTypes?.required !== void 0, isRequired = requiredExist ? validationTypes.required : false;
284
+ if (value === "" && !isRequired) return validProps;
285
+ let errorFound = validProps;
286
+ for (const [key, opts] of items) {
287
+ const { validate, msg } = errors[key], isInvalid = !validate(value, opts);
288
+ if (isInvalid) {
289
+ errorFound = {
290
+ isInvalid,
291
+ errorMessage: errorMessages?.[key] || lang[msg]
292
+ };
293
+ break;
287
294
  }
288
- ].map((item) => {
289
- const req = getValidation(
290
- value,
291
- item.touched,
292
- requirements,
293
- errorMessages?.[id]
294
- );
295
- item.ref.current = handleNestedChange({
296
- state: item.ref.current,
297
- id,
298
- value: !req.isValid
299
- });
300
- return req;
295
+ }
296
+ return errorFound;
297
+ };
298
+ return { performValidations };
299
+ }
300
+
301
+ // src/hooks/useForm.tsx
302
+ function useForm(initialState, { validations, errorMessages } = {}) {
303
+ const initial = {
304
+ touched: allToValue(initialState, false),
305
+ errors: allToValue(initialState, {
306
+ isInvalid: false,
307
+ errorMessage: ""
308
+ })
309
+ };
310
+ const [state, setState] = useState2(initialState), touched = useRef2(initial.touched), errors2 = useRef2(initial.errors), { performValidations } = useValidate();
311
+ const validateInput = (id, value) => {
312
+ errors2.current = handleNestedChange({
313
+ state: errors2.current,
314
+ id,
315
+ value: performValidations(value, validations?.[id], errorMessages?.[id])
301
316
  });
302
- if (requirements === void 0) return;
303
- ux.current[id] = {
304
- isInvalid: !uiErrors.isValid,
305
- errorMessage: uiErrors.validationProps.errorMessage
306
- };
307
317
  };
308
- const getUXProps = useCallback2((id) => ux.current[id], [ux.current]);
318
+ const getUXProps = useCallback2(
319
+ (id) => {
320
+ const inputError = errors2.current[id], isTouched = touched.current[id];
321
+ return {
322
+ isInvalid: isTouched && inputError.isInvalid,
323
+ errorMessage: isTouched ? inputError.errorMessage : ""
324
+ };
325
+ },
326
+ [errors2.current, touched.current]
327
+ );
309
328
  const onBlur = (id) => {
310
329
  touched.current = handleNestedChange({
311
330
  state: touched.current,
@@ -335,7 +354,6 @@ function useForm(initialState, { validations, errorMessages } = {}) {
335
354
  setState,
336
355
  register: {
337
356
  input: (id) => ({
338
- // ...ux.current[id],
339
357
  ...getUXProps(id),
340
358
  id,
341
359
  onBlur: () => onBlur(id),
@@ -365,13 +383,12 @@ function useForm(initialState, { validations, errorMessages } = {}) {
365
383
  },
366
384
  hasInvalidValues: () => {
367
385
  touched.current = allToValue(initialState, true);
368
- return JSON.stringify(realErrors.current).includes(":true");
386
+ return JSON.stringify(errors2.current).includes(":true");
369
387
  },
370
388
  touched: touched.current,
371
389
  resetForm: (preservedKeys) => {
372
- const val = allToValue(initialState, false);
373
- touched.current = val;
374
- errors2.current = val;
390
+ touched.current = initial.touched;
391
+ errors2.current = initial.errors;
375
392
  setState(
376
393
  preservedKeys === void 0 ? initialState : preservedKeys.reduce(
377
394
  (acc, key) => ({
@@ -382,8 +399,7 @@ function useForm(initialState, { validations, errorMessages } = {}) {
382
399
  )
383
400
  );
384
401
  },
385
- errors: errors2,
386
- realErrors
402
+ errors: errors2
387
403
  };
388
404
  }
389
405
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juantroconisf/lib",
3
- "version": "3.5.8",
3
+ "version": "3.6.0",
4
4
  "description": "A state and validation library for HeroUI.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",