@juantroconisf/lib 3.5.8 → 3.6.1

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
+ touched: TouchedType<O>;
137
+ errors: ErrorsType<O>;
138
+ register: RegisterFunc<O>;
139
+ hasInvalidValues: () => boolean;
140
+ resetForm: (presevedKeys?: PreservedKeys<O>[]) => void;
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
+ touched: TouchedType<O>;
137
+ errors: ErrorsType<O>;
138
+ register: RegisterFunc<O>;
139
+ hasInvalidValues: () => boolean;
140
+ resetForm: (presevedKeys?: PreservedKeys<O>[]) => void;
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,50 +290,72 @@ 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(
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, setTouched] = (0, import_react2.useState)(initial.touched), [errors2, setErrors] = (0, import_react2.useState)(initial.errors), { performValidations } = useValidate();
331
+ const validateInput = (id, value) => setErrors(
332
+ (prev) => handleNestedChange({
333
+ state: prev,
334
+ id,
335
+ value: performValidations(
310
336
  value,
311
- item.touched,
312
- requirements,
337
+ validations?.[id],
313
338
  errorMessages?.[id]
314
- );
315
- item.ref.current = handleNestedChange({
316
- state: item.ref.current,
317
- id,
318
- value: !req.isValid
319
- });
320
- return req;
321
- });
322
- if (requirements === void 0) return;
323
- ux.current[id] = {
324
- isInvalid: !uiErrors.isValid,
325
- errorMessage: uiErrors.validationProps.errorMessage
326
- };
327
- };
328
- const getUXProps = (0, import_react2.useCallback)((id) => ux.current[id], [ux.current]);
329
- const onBlur = (id) => {
330
- touched.current = handleNestedChange({
331
- state: touched.current,
339
+ )
340
+ })
341
+ );
342
+ const getUXProps = (0, import_react2.useCallback)(
343
+ (id) => {
344
+ const inputError = errors2[id], isTouched = touched[id];
345
+ return {
346
+ isInvalid: isTouched && inputError.isInvalid,
347
+ errorMessage: isTouched ? inputError.errorMessage : ""
348
+ };
349
+ },
350
+ [errors2.current, touched.current]
351
+ );
352
+ const onBlur = (id) => setTouched(
353
+ (prev) => handleNestedChange({
354
+ state: prev,
332
355
  id,
333
356
  value: true
334
- });
335
- validateInput(id, state[id], true);
336
- }, onValueChange = (id, value) => {
357
+ })
358
+ ), onValueChange = (id, value) => {
337
359
  setState((prev) => handleNestedChange({ state: prev, id, value }));
338
360
  validateInput(id, value);
339
361
  }, onSelectionChange = (id, value) => {
@@ -353,9 +375,10 @@ function useForm(initialState, { validations, errorMessages } = {}) {
353
375
  onSelectionChange,
354
376
  state,
355
377
  setState,
378
+ touched,
379
+ errors: errors2,
356
380
  register: {
357
381
  input: (id) => ({
358
- // ...ux.current[id],
359
382
  ...getUXProps(id),
360
383
  id,
361
384
  onBlur: () => onBlur(id),
@@ -384,26 +407,22 @@ function useForm(initialState, { validations, errorMessages } = {}) {
384
407
  })
385
408
  },
386
409
  hasInvalidValues: () => {
387
- touched.current = allToValue(initialState, true);
388
- return JSON.stringify(realErrors.current).includes(":true");
410
+ setTouched((prev) => allToValue(prev, true));
411
+ return JSON.stringify(errors2).includes("isInvalid:true");
389
412
  },
390
- touched: touched.current,
391
413
  resetForm: (preservedKeys) => {
392
- const val = allToValue(initialState, false);
393
- touched.current = val;
394
- errors2.current = val;
414
+ setTouched(initial.touched);
415
+ setErrors(initial.errors);
395
416
  setState(
396
- preservedKeys === void 0 ? initialState : preservedKeys.reduce(
417
+ (prev) => preservedKeys === void 0 ? initialState : preservedKeys.reduce(
397
418
  (acc, key) => ({
398
419
  ...acc,
399
- [key]: state[key]
420
+ [key]: prev[key]
400
421
  }),
401
422
  initialState
402
423
  )
403
424
  );
404
- },
405
- errors: errors2,
406
- realErrors
425
+ }
407
426
  };
408
427
  }
409
428
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.mjs CHANGED
@@ -269,51 +269,73 @@ function useFormChange({
269
269
  }
270
270
 
271
271
  // src/hooks/useForm.tsx
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
272
+ import { useState as useState2, useCallback as useCallback2 } from "react";
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(
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, setTouched] = useState2(initial.touched), [errors2, setErrors] = useState2(initial.errors), { performValidations } = useValidate();
311
+ const validateInput = (id, value) => setErrors(
312
+ (prev) => handleNestedChange({
313
+ state: prev,
314
+ id,
315
+ value: performValidations(
290
316
  value,
291
- item.touched,
292
- requirements,
317
+ validations?.[id],
293
318
  errorMessages?.[id]
294
- );
295
- item.ref.current = handleNestedChange({
296
- state: item.ref.current,
297
- id,
298
- value: !req.isValid
299
- });
300
- return req;
301
- });
302
- if (requirements === void 0) return;
303
- ux.current[id] = {
304
- isInvalid: !uiErrors.isValid,
305
- errorMessage: uiErrors.validationProps.errorMessage
306
- };
307
- };
308
- const getUXProps = useCallback2((id) => ux.current[id], [ux.current]);
309
- const onBlur = (id) => {
310
- touched.current = handleNestedChange({
311
- state: touched.current,
319
+ )
320
+ })
321
+ );
322
+ const getUXProps = useCallback2(
323
+ (id) => {
324
+ const inputError = errors2[id], isTouched = touched[id];
325
+ return {
326
+ isInvalid: isTouched && inputError.isInvalid,
327
+ errorMessage: isTouched ? inputError.errorMessage : ""
328
+ };
329
+ },
330
+ [errors2.current, touched.current]
331
+ );
332
+ const onBlur = (id) => setTouched(
333
+ (prev) => handleNestedChange({
334
+ state: prev,
312
335
  id,
313
336
  value: true
314
- });
315
- validateInput(id, state[id], true);
316
- }, onValueChange = (id, value) => {
337
+ })
338
+ ), onValueChange = (id, value) => {
317
339
  setState((prev) => handleNestedChange({ state: prev, id, value }));
318
340
  validateInput(id, value);
319
341
  }, onSelectionChange = (id, value) => {
@@ -333,9 +355,10 @@ function useForm(initialState, { validations, errorMessages } = {}) {
333
355
  onSelectionChange,
334
356
  state,
335
357
  setState,
358
+ touched,
359
+ errors: errors2,
336
360
  register: {
337
361
  input: (id) => ({
338
- // ...ux.current[id],
339
362
  ...getUXProps(id),
340
363
  id,
341
364
  onBlur: () => onBlur(id),
@@ -364,26 +387,22 @@ function useForm(initialState, { validations, errorMessages } = {}) {
364
387
  })
365
388
  },
366
389
  hasInvalidValues: () => {
367
- touched.current = allToValue(initialState, true);
368
- return JSON.stringify(realErrors.current).includes(":true");
390
+ setTouched((prev) => allToValue(prev, true));
391
+ return JSON.stringify(errors2).includes("isInvalid:true");
369
392
  },
370
- touched: touched.current,
371
393
  resetForm: (preservedKeys) => {
372
- const val = allToValue(initialState, false);
373
- touched.current = val;
374
- errors2.current = val;
394
+ setTouched(initial.touched);
395
+ setErrors(initial.errors);
375
396
  setState(
376
- preservedKeys === void 0 ? initialState : preservedKeys.reduce(
397
+ (prev) => preservedKeys === void 0 ? initialState : preservedKeys.reduce(
377
398
  (acc, key) => ({
378
399
  ...acc,
379
- [key]: state[key]
400
+ [key]: prev[key]
380
401
  }),
381
402
  initialState
382
403
  )
383
404
  );
384
- },
385
- errors: errors2,
386
- realErrors
405
+ }
387
406
  };
388
407
  }
389
408
  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.1",
4
4
  "description": "A state and validation library for HeroUI.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",