@juantroconisf/lib 3.5.3 → 3.5.4

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
@@ -69,6 +69,15 @@ interface FormChangeState<O extends StateType> {
69
69
  [key in keyof O]?: ValidatorErrorMessage;
70
70
  };
71
71
  }
72
+ interface FormOptions<O extends StateType> {
73
+ state: O;
74
+ validations?: {
75
+ [key in keyof O]?: ValidatorParams;
76
+ };
77
+ errorMessages?: {
78
+ [key in keyof O]?: ValidatorErrorMessage;
79
+ };
80
+ }
72
81
  type TouchedType<O extends StateType> = Record<keyof O, boolean>;
73
82
  type ValueChangeFunc<O extends StateType, K extends keyof O> = (id: K, value: O[K]) => void;
74
83
  type BlurFunc<O extends StateType> = (id: keyof O) => void;
@@ -78,7 +87,12 @@ interface ComponentInputProps<O extends StateType> {
78
87
  isInvalid: boolean;
79
88
  errorMessage: string;
80
89
  }
90
+ type UXProps<O extends StateType> = Record<keyof O, {
91
+ isInvalid: boolean;
92
+ errorMessage: string;
93
+ }>;
81
94
  type RegisterHandleFunc<O extends StateType> = (id: keyof O) => ComponentInputProps<O>;
95
+ type ValidateInputFunc<O extends StateType> = (id: keyof O, value: O[keyof O]) => any;
82
96
  interface InputRegisterProps<O extends StateType, K extends keyof O, V extends unknown = string> extends ComponentInputProps<O> {
83
97
  onValueChange: (value: O[K]) => void;
84
98
  value: V;
@@ -116,4 +130,6 @@ interface UseFormChangeResponse<O extends StateType> {
116
130
 
117
131
  declare function useFormChange<O extends StateType>({ state: initialState, validations, errorMessages, }: FormChangeState<O>): UseFormChangeResponse<O>;
118
132
 
119
- export { type BlurFunc, type ComponentInputProps, type FormChangeState, 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 UseFormChangeResponse, type UseValidationFunc, type Validator, type ValidatorErrorMessage$1 as ValidatorErrorMessage, type ValidatorFunction, type ValidatorParams$1 as ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useFormChange };
133
+ declare function useForm<O extends StateType>(initialState: O, { validations, errorMessages }: FormOptions<O>): UseFormChangeResponse<O>;
134
+
135
+ 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 };
package/dist/index.d.ts CHANGED
@@ -69,6 +69,15 @@ interface FormChangeState<O extends StateType> {
69
69
  [key in keyof O]?: ValidatorErrorMessage;
70
70
  };
71
71
  }
72
+ interface FormOptions<O extends StateType> {
73
+ state: O;
74
+ validations?: {
75
+ [key in keyof O]?: ValidatorParams;
76
+ };
77
+ errorMessages?: {
78
+ [key in keyof O]?: ValidatorErrorMessage;
79
+ };
80
+ }
72
81
  type TouchedType<O extends StateType> = Record<keyof O, boolean>;
73
82
  type ValueChangeFunc<O extends StateType, K extends keyof O> = (id: K, value: O[K]) => void;
74
83
  type BlurFunc<O extends StateType> = (id: keyof O) => void;
@@ -78,7 +87,12 @@ interface ComponentInputProps<O extends StateType> {
78
87
  isInvalid: boolean;
79
88
  errorMessage: string;
80
89
  }
90
+ type UXProps<O extends StateType> = Record<keyof O, {
91
+ isInvalid: boolean;
92
+ errorMessage: string;
93
+ }>;
81
94
  type RegisterHandleFunc<O extends StateType> = (id: keyof O) => ComponentInputProps<O>;
95
+ type ValidateInputFunc<O extends StateType> = (id: keyof O, value: O[keyof O]) => any;
82
96
  interface InputRegisterProps<O extends StateType, K extends keyof O, V extends unknown = string> extends ComponentInputProps<O> {
83
97
  onValueChange: (value: O[K]) => void;
84
98
  value: V;
@@ -116,4 +130,6 @@ interface UseFormChangeResponse<O extends StateType> {
116
130
 
117
131
  declare function useFormChange<O extends StateType>({ state: initialState, validations, errorMessages, }: FormChangeState<O>): UseFormChangeResponse<O>;
118
132
 
119
- export { type BlurFunc, type ComponentInputProps, type FormChangeState, 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 UseFormChangeResponse, type UseValidationFunc, type Validator, type ValidatorErrorMessage$1 as ValidatorErrorMessage, type ValidatorFunction, type ValidatorParams$1 as ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useFormChange };
133
+ declare function useForm<O extends StateType>(initialState: O, { validations, errorMessages }: FormOptions<O>): UseFormChangeResponse<O>;
134
+
135
+ 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 };
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var index_exports = {};
21
21
  __export(index_exports, {
22
22
  NextUIError: () => NextUIError,
23
+ useForm: () => useForm,
23
24
  useFormChange: () => useFormChange
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
@@ -55,8 +56,8 @@ function handleNestedChange({
55
56
  current[propertyDepth[propertyDepth.length - 1]] = value;
56
57
  return { ...newValues };
57
58
  }
58
- var allToValue = (obj, bool) => Object.keys(obj).reduce((acc, key) => {
59
- acc[key] = bool;
59
+ var allToValue = (obj, value) => Object.keys(obj).reduce((acc, key) => {
60
+ acc[key] = value;
60
61
  return acc;
61
62
  }, {});
62
63
  var nxError = (bool, msg) => new NextUIError(bool, msg);
@@ -286,8 +287,125 @@ function useFormChange({
286
287
  realErrors
287
288
  };
288
289
  }
290
+
291
+ // src/hooks/useForm.tsx
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) => {
298
+ const isTouched = touched.current[id], requirements = validations?.[id];
299
+ const [uiErrors] = [
300
+ {
301
+ ref: errors2,
302
+ touched: isTouched
303
+ },
304
+ {
305
+ ref: realErrors,
306
+ touched: true
307
+ }
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;
321
+ });
322
+ if (requirements === void 0) return;
323
+ ux.current[id] = {
324
+ isInvalid: !uiErrors.isValid,
325
+ errorMessage: uiErrors.validationProps.errorMessage
326
+ };
327
+ };
328
+ const onBlur = (id) => {
329
+ touched.current = handleNestedChange({
330
+ state: touched.current,
331
+ id,
332
+ value: true
333
+ });
334
+ }, onValueChange = (id, value) => {
335
+ setState((prev) => handleNestedChange({ state: prev, id, value }));
336
+ validateInput(id, value);
337
+ }, onSelectionChange = (id, value) => {
338
+ const fixedValue = typeof value === "string" || value === null ? value : Array.from(value);
339
+ setState(
340
+ (prev) => handleNestedChange({
341
+ state: prev,
342
+ id,
343
+ value: fixedValue
344
+ })
345
+ );
346
+ validateInput(id, fixedValue);
347
+ };
348
+ return {
349
+ onBlur,
350
+ onValueChange,
351
+ onSelectionChange,
352
+ state,
353
+ setState,
354
+ register: {
355
+ input: (id) => ({
356
+ ...ux.current[id],
357
+ id,
358
+ onBlur: () => onBlur(id),
359
+ onValueChange: (v) => onValueChange(id, v),
360
+ value: state[id]
361
+ }),
362
+ select: (id) => {
363
+ const isString = typeof state[id] === "string" || state[id] === null;
364
+ return {
365
+ ...ux.current[id],
366
+ id,
367
+ onBlur: () => onBlur(id),
368
+ onSelectionChange: (v) => onSelectionChange(
369
+ id,
370
+ !isString ? v : Array.from(v)[0] || null
371
+ ),
372
+ selectedKeys: state[id] === null ? [] : isString ? [state[id]] : state[id]
373
+ };
374
+ },
375
+ autocomplete: (id) => ({
376
+ ...ux.current[id],
377
+ id,
378
+ onBlur: () => onBlur(id),
379
+ onSelectionChange: (v) => onSelectionChange(id, v),
380
+ selectedKey: state[id]
381
+ })
382
+ },
383
+ hasInvalidValues: () => {
384
+ touched.current = allToValue(initialState, true);
385
+ return JSON.stringify(realErrors.current).includes(":true");
386
+ },
387
+ touched: touched.current,
388
+ resetForm: (preservedKeys) => {
389
+ const val = allToValue(initialState, false);
390
+ touched.current = val;
391
+ errors2.current = val;
392
+ setState(
393
+ preservedKeys === void 0 ? initialState : preservedKeys.reduce(
394
+ (acc, key) => ({
395
+ ...acc,
396
+ [key]: state[key]
397
+ }),
398
+ initialState
399
+ )
400
+ );
401
+ },
402
+ errors: errors2,
403
+ realErrors
404
+ };
405
+ }
289
406
  // Annotate the CommonJS export names for ESM import in node:
290
407
  0 && (module.exports = {
291
408
  NextUIError,
409
+ useForm,
292
410
  useFormChange
293
411
  });
package/dist/index.mjs CHANGED
@@ -36,8 +36,8 @@ function handleNestedChange({
36
36
  current[propertyDepth[propertyDepth.length - 1]] = value;
37
37
  return { ...newValues };
38
38
  }
39
- var allToValue = (obj, bool) => Object.keys(obj).reduce((acc, key) => {
40
- acc[key] = bool;
39
+ var allToValue = (obj, value) => Object.keys(obj).reduce((acc, key) => {
40
+ acc[key] = value;
41
41
  return acc;
42
42
  }, {});
43
43
  var nxError = (bool, msg) => new NextUIError(bool, msg);
@@ -267,7 +267,124 @@ function useFormChange({
267
267
  realErrors
268
268
  };
269
269
  }
270
+
271
+ // src/hooks/useForm.tsx
272
+ import { useState as useState2, useRef as useRef2 } 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) => {
278
+ const isTouched = touched.current[id], requirements = validations?.[id];
279
+ const [uiErrors] = [
280
+ {
281
+ ref: errors2,
282
+ touched: isTouched
283
+ },
284
+ {
285
+ ref: realErrors,
286
+ touched: true
287
+ }
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;
301
+ });
302
+ if (requirements === void 0) return;
303
+ ux.current[id] = {
304
+ isInvalid: !uiErrors.isValid,
305
+ errorMessage: uiErrors.validationProps.errorMessage
306
+ };
307
+ };
308
+ const onBlur = (id) => {
309
+ touched.current = handleNestedChange({
310
+ state: touched.current,
311
+ id,
312
+ value: true
313
+ });
314
+ }, onValueChange = (id, value) => {
315
+ setState((prev) => handleNestedChange({ state: prev, id, value }));
316
+ validateInput(id, value);
317
+ }, onSelectionChange = (id, value) => {
318
+ const fixedValue = typeof value === "string" || value === null ? value : Array.from(value);
319
+ setState(
320
+ (prev) => handleNestedChange({
321
+ state: prev,
322
+ id,
323
+ value: fixedValue
324
+ })
325
+ );
326
+ validateInput(id, fixedValue);
327
+ };
328
+ return {
329
+ onBlur,
330
+ onValueChange,
331
+ onSelectionChange,
332
+ state,
333
+ setState,
334
+ register: {
335
+ input: (id) => ({
336
+ ...ux.current[id],
337
+ id,
338
+ onBlur: () => onBlur(id),
339
+ onValueChange: (v) => onValueChange(id, v),
340
+ value: state[id]
341
+ }),
342
+ select: (id) => {
343
+ const isString = typeof state[id] === "string" || state[id] === null;
344
+ return {
345
+ ...ux.current[id],
346
+ id,
347
+ onBlur: () => onBlur(id),
348
+ onSelectionChange: (v) => onSelectionChange(
349
+ id,
350
+ !isString ? v : Array.from(v)[0] || null
351
+ ),
352
+ selectedKeys: state[id] === null ? [] : isString ? [state[id]] : state[id]
353
+ };
354
+ },
355
+ autocomplete: (id) => ({
356
+ ...ux.current[id],
357
+ id,
358
+ onBlur: () => onBlur(id),
359
+ onSelectionChange: (v) => onSelectionChange(id, v),
360
+ selectedKey: state[id]
361
+ })
362
+ },
363
+ hasInvalidValues: () => {
364
+ touched.current = allToValue(initialState, true);
365
+ return JSON.stringify(realErrors.current).includes(":true");
366
+ },
367
+ touched: touched.current,
368
+ resetForm: (preservedKeys) => {
369
+ const val = allToValue(initialState, false);
370
+ touched.current = val;
371
+ errors2.current = val;
372
+ setState(
373
+ preservedKeys === void 0 ? initialState : preservedKeys.reduce(
374
+ (acc, key) => ({
375
+ ...acc,
376
+ [key]: state[key]
377
+ }),
378
+ initialState
379
+ )
380
+ );
381
+ },
382
+ errors: errors2,
383
+ realErrors
384
+ };
385
+ }
270
386
  export {
271
387
  NextUIError,
388
+ useForm,
272
389
  useFormChange
273
390
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juantroconisf/lib",
3
- "version": "3.5.3",
3
+ "version": "3.5.4",
4
4
  "description": "A state and validation library for HeroUI.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",