@juantroconisf/lib 2.5.4 → 3.0.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
@@ -1,7 +1,25 @@
1
- import { SharedSelection, InputProps } from '@heroui/react';
2
- import { ChangeEvent } from 'react';
1
+ import { SharedSelection, InputProps, SelectProps } from '@heroui/react';
3
2
 
4
3
  type StateType = Record<string, any>;
4
+ type GetNestedValueByKey<O extends StateType> = (obj: O, nestedKey: string) => O[keyof O];
5
+ interface NestedChangeProps<O extends StateType> {
6
+ state: O;
7
+ id: keyof O | string;
8
+ value: any;
9
+ hasNestedValues?: boolean;
10
+ }
11
+ declare class NextUIError {
12
+ isInvalid: boolean;
13
+ errorMessage: string;
14
+ constructor(bool?: boolean, msg?: string);
15
+ }
16
+ type NXErrorFunc = (bool?: boolean, msg?: string) => NextUIError;
17
+ interface MapValidator {
18
+ string: (val: string) => boolean;
19
+ number: (val: string | number) => boolean;
20
+ object: (val: object) => boolean;
21
+ }
22
+ type RequiredValidationFunc = (value: any[] | any) => boolean;
5
23
  type ValidatorFunction<T, U> = (val: T, ...args: U[]) => boolean;
6
24
  interface Validator<T, U = any> {
7
25
  validate: ValidatorFunction<T, U>;
@@ -20,6 +38,12 @@ interface ValidatorTypes {
20
38
  password: Validator<string, boolean>;
21
39
  custom: Validator<any, boolean>;
22
40
  }
41
+ type ValidatorParams$1 = {
42
+ [K in keyof ValidatorTypes]?: ValidatorTypes[K] extends Validator<any, infer U> ? U : never;
43
+ };
44
+ type ValidatorErrorMessage$1 = {
45
+ [K in keyof ValidatorTypes]?: string;
46
+ };
23
47
 
24
48
  type ValidatorParams = {
25
49
  [K in keyof ValidatorTypes]?: ValidatorTypes[K] extends Validator<any, infer U> ? U : never;
@@ -27,51 +51,57 @@ type ValidatorParams = {
27
51
  type ValidatorErrorMessage = {
28
52
  [K in keyof ValidatorTypes]?: string;
29
53
  };
30
- interface ValidationProps {
31
- isInvalid: InputProps["isInvalid"];
32
- errorMessage: InputProps["errorMessage"];
54
+ type GetValidationResponse = {
55
+ isValid: boolean;
56
+ validationProps: NextUIError;
57
+ };
58
+ type GetValidationFunc<V extends unknown> = (value: V, isTouched: boolean, types: ValidatorParams, errorMessages?: ValidatorErrorMessage) => GetValidationResponse;
59
+ interface UseValidationFunc {
60
+ getValidation: GetValidationFunc<unknown>;
33
61
  }
34
62
  interface FormChangeState<O extends StateType> {
35
63
  state: O;
36
64
  validations?: {
37
65
  [key in keyof O]?: ValidatorParams;
38
66
  };
39
- }
40
- type TouchedType = {
41
- [key: string]: boolean;
42
- };
43
- interface TargetChangeProps<O extends StateType> {
44
- target: {
45
- id: keyof O;
46
- value?: O[keyof O];
67
+ errorMessages?: {
68
+ [key in keyof O]?: ValidatorErrorMessage;
47
69
  };
48
70
  }
49
- type InputChangeFunc<O extends StateType> = (props: TargetChangeProps<O> | ChangeEvent<HTMLInputElement>) => void;
71
+ type TouchedType<O extends StateType> = Record<keyof O, boolean>;
50
72
  type ValueChangeFunc<O extends StateType, V extends unknown> = (id: keyof O, value: V) => void;
51
- interface ComponentInputProps<O extends StateType, V extends unknown> extends ValidationProps {
73
+ type BlurFunc<O extends StateType> = (id: keyof O) => void;
74
+ interface ComponentInputProps<O extends StateType> {
52
75
  id: keyof O;
53
- onBlur: InputChangeFunc<O>;
54
- value: V;
76
+ onBlur: InputProps["onBlur"];
77
+ isInvalid: InputProps["isInvalid"];
78
+ errorMessage: InputProps["errorMessage"];
79
+ }
80
+ type RegisterHandleFunc<O extends StateType> = (id: keyof O) => ComponentInputProps<O>;
81
+ interface InputRegisterProps<O extends StateType> extends ComponentInputProps<O> {
82
+ onValueChange: InputProps["onValueChange"];
83
+ value: InputProps["value"];
84
+ }
85
+ interface SelectRegisterProps<O extends StateType> extends ComponentInputProps<O> {
86
+ onSelectionChange: SelectProps["onSelectionChange"];
87
+ selectedKeys: SelectProps["selectedKeys"];
55
88
  }
56
- interface RegisterInputProps<O extends StateType> extends ComponentInputProps<O, string | number> {
57
- onChange: InputChangeFunc<O>;
89
+ interface RegisterFunc<O extends StateType> {
90
+ input: (id: keyof O) => InputRegisterProps<O>;
91
+ select: (id: keyof O) => SelectRegisterProps<O>;
58
92
  }
59
- type RegisterFunc<O extends StateType> = (id: keyof O, errorMessages?: ValidatorErrorMessage) => RegisterInputProps<O>;
60
- type HasInvalidValuesFunc = () => boolean;
61
- type ResetFormFunc = () => void;
62
93
  interface UseFormChangeResponse<O extends StateType> {
63
- onChange: InputChangeFunc<O>;
64
- onBlur: InputChangeFunc<O>;
94
+ onBlur: BlurFunc<O>;
65
95
  onValueChange: ValueChangeFunc<O, string | number>;
66
96
  onSelectionChange: ValueChangeFunc<O, SharedSelection>;
67
97
  state: O;
68
98
  setState: React.Dispatch<React.SetStateAction<O>>;
69
99
  register: RegisterFunc<O>;
70
- hasInvalidValues: HasInvalidValuesFunc;
71
- touched: TouchedType;
72
- resetForm: ResetFormFunc;
100
+ hasInvalidValues: () => boolean;
101
+ touched: TouchedType<O>;
102
+ resetForm: () => void;
73
103
  }
74
104
 
75
- declare function useFormChange<O extends StateType>({ state: newState, validations, }: FormChangeState<O>): UseFormChangeResponse<O>;
105
+ declare function useFormChange<O extends StateType>({ state: newState, validations, errorMessages, }: FormChangeState<O>): UseFormChangeResponse<O>;
76
106
 
77
- export { useFormChange };
107
+ export { type BlurFunc, type FormChangeState, type GetNestedValueByKey, type GetValidationFunc, type MapValidator, type NXErrorFunc, type NestedChangeProps, NextUIError, 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 };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,25 @@
1
- import { SharedSelection, InputProps } from '@heroui/react';
2
- import { ChangeEvent } from 'react';
1
+ import { SharedSelection, InputProps, SelectProps } from '@heroui/react';
3
2
 
4
3
  type StateType = Record<string, any>;
4
+ type GetNestedValueByKey<O extends StateType> = (obj: O, nestedKey: string) => O[keyof O];
5
+ interface NestedChangeProps<O extends StateType> {
6
+ state: O;
7
+ id: keyof O | string;
8
+ value: any;
9
+ hasNestedValues?: boolean;
10
+ }
11
+ declare class NextUIError {
12
+ isInvalid: boolean;
13
+ errorMessage: string;
14
+ constructor(bool?: boolean, msg?: string);
15
+ }
16
+ type NXErrorFunc = (bool?: boolean, msg?: string) => NextUIError;
17
+ interface MapValidator {
18
+ string: (val: string) => boolean;
19
+ number: (val: string | number) => boolean;
20
+ object: (val: object) => boolean;
21
+ }
22
+ type RequiredValidationFunc = (value: any[] | any) => boolean;
5
23
  type ValidatorFunction<T, U> = (val: T, ...args: U[]) => boolean;
6
24
  interface Validator<T, U = any> {
7
25
  validate: ValidatorFunction<T, U>;
@@ -20,6 +38,12 @@ interface ValidatorTypes {
20
38
  password: Validator<string, boolean>;
21
39
  custom: Validator<any, boolean>;
22
40
  }
41
+ type ValidatorParams$1 = {
42
+ [K in keyof ValidatorTypes]?: ValidatorTypes[K] extends Validator<any, infer U> ? U : never;
43
+ };
44
+ type ValidatorErrorMessage$1 = {
45
+ [K in keyof ValidatorTypes]?: string;
46
+ };
23
47
 
24
48
  type ValidatorParams = {
25
49
  [K in keyof ValidatorTypes]?: ValidatorTypes[K] extends Validator<any, infer U> ? U : never;
@@ -27,51 +51,57 @@ type ValidatorParams = {
27
51
  type ValidatorErrorMessage = {
28
52
  [K in keyof ValidatorTypes]?: string;
29
53
  };
30
- interface ValidationProps {
31
- isInvalid: InputProps["isInvalid"];
32
- errorMessage: InputProps["errorMessage"];
54
+ type GetValidationResponse = {
55
+ isValid: boolean;
56
+ validationProps: NextUIError;
57
+ };
58
+ type GetValidationFunc<V extends unknown> = (value: V, isTouched: boolean, types: ValidatorParams, errorMessages?: ValidatorErrorMessage) => GetValidationResponse;
59
+ interface UseValidationFunc {
60
+ getValidation: GetValidationFunc<unknown>;
33
61
  }
34
62
  interface FormChangeState<O extends StateType> {
35
63
  state: O;
36
64
  validations?: {
37
65
  [key in keyof O]?: ValidatorParams;
38
66
  };
39
- }
40
- type TouchedType = {
41
- [key: string]: boolean;
42
- };
43
- interface TargetChangeProps<O extends StateType> {
44
- target: {
45
- id: keyof O;
46
- value?: O[keyof O];
67
+ errorMessages?: {
68
+ [key in keyof O]?: ValidatorErrorMessage;
47
69
  };
48
70
  }
49
- type InputChangeFunc<O extends StateType> = (props: TargetChangeProps<O> | ChangeEvent<HTMLInputElement>) => void;
71
+ type TouchedType<O extends StateType> = Record<keyof O, boolean>;
50
72
  type ValueChangeFunc<O extends StateType, V extends unknown> = (id: keyof O, value: V) => void;
51
- interface ComponentInputProps<O extends StateType, V extends unknown> extends ValidationProps {
73
+ type BlurFunc<O extends StateType> = (id: keyof O) => void;
74
+ interface ComponentInputProps<O extends StateType> {
52
75
  id: keyof O;
53
- onBlur: InputChangeFunc<O>;
54
- value: V;
76
+ onBlur: InputProps["onBlur"];
77
+ isInvalid: InputProps["isInvalid"];
78
+ errorMessage: InputProps["errorMessage"];
79
+ }
80
+ type RegisterHandleFunc<O extends StateType> = (id: keyof O) => ComponentInputProps<O>;
81
+ interface InputRegisterProps<O extends StateType> extends ComponentInputProps<O> {
82
+ onValueChange: InputProps["onValueChange"];
83
+ value: InputProps["value"];
84
+ }
85
+ interface SelectRegisterProps<O extends StateType> extends ComponentInputProps<O> {
86
+ onSelectionChange: SelectProps["onSelectionChange"];
87
+ selectedKeys: SelectProps["selectedKeys"];
55
88
  }
56
- interface RegisterInputProps<O extends StateType> extends ComponentInputProps<O, string | number> {
57
- onChange: InputChangeFunc<O>;
89
+ interface RegisterFunc<O extends StateType> {
90
+ input: (id: keyof O) => InputRegisterProps<O>;
91
+ select: (id: keyof O) => SelectRegisterProps<O>;
58
92
  }
59
- type RegisterFunc<O extends StateType> = (id: keyof O, errorMessages?: ValidatorErrorMessage) => RegisterInputProps<O>;
60
- type HasInvalidValuesFunc = () => boolean;
61
- type ResetFormFunc = () => void;
62
93
  interface UseFormChangeResponse<O extends StateType> {
63
- onChange: InputChangeFunc<O>;
64
- onBlur: InputChangeFunc<O>;
94
+ onBlur: BlurFunc<O>;
65
95
  onValueChange: ValueChangeFunc<O, string | number>;
66
96
  onSelectionChange: ValueChangeFunc<O, SharedSelection>;
67
97
  state: O;
68
98
  setState: React.Dispatch<React.SetStateAction<O>>;
69
99
  register: RegisterFunc<O>;
70
- hasInvalidValues: HasInvalidValuesFunc;
71
- touched: TouchedType;
72
- resetForm: ResetFormFunc;
100
+ hasInvalidValues: () => boolean;
101
+ touched: TouchedType<O>;
102
+ resetForm: () => void;
73
103
  }
74
104
 
75
- declare function useFormChange<O extends StateType>({ state: newState, validations, }: FormChangeState<O>): UseFormChangeResponse<O>;
105
+ declare function useFormChange<O extends StateType>({ state: newState, validations, errorMessages, }: FormChangeState<O>): UseFormChangeResponse<O>;
76
106
 
77
- export { useFormChange };
107
+ export { type BlurFunc, type FormChangeState, type GetNestedValueByKey, type GetValidationFunc, type MapValidator, type NXErrorFunc, type NestedChangeProps, NextUIError, 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 };