@juantroconisf/lib 7.0.0 → 8.0.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.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { SelectProps, AutocompleteProps } from '@heroui/react';
2
2
  import { SingleSelection } from '@react-types/shared';
3
+ import { Schema, InferType } from 'yup';
3
4
 
4
5
  type StateType = Record<string, any>;
5
6
  interface NestedChangeProps<O extends StateType> {
@@ -13,78 +14,63 @@ declare class NextUIError {
13
14
  errorMessage: string;
14
15
  constructor(bool?: boolean, msg?: string);
15
16
  }
16
- type ValidatorFunction<T, U> = (val: T, ...args: U[]) => boolean;
17
- interface Validator<T, U = any> {
18
- validate: ValidatorFunction<T, U>;
19
- msg: string;
20
- }
21
- interface ValidatorTypes {
22
- required: Validator<string | number, boolean>;
23
- min: Validator<number | string, number | string>;
24
- max: Validator<number | string, number | string>;
25
- minLength: Validator<string, number>;
26
- maxLength: Validator<string, number>;
27
- pattern: Validator<string, RegExp>;
28
- equal: Validator<string, string>;
29
- numberIsEqual: Validator<number, number>;
30
- email: Validator<string, boolean>;
31
- password: Validator<string, boolean>;
32
- custom: Validator<any, boolean>;
33
- }
34
- type ValidatorParams = {
35
- [K in keyof ValidatorTypes]?: ValidatorTypes[K] extends Validator<any, infer U> ? U : never;
36
- };
37
- type ValidatorErrorMessage = {
38
- [K in keyof ValidatorTypes]?: string;
39
- };
40
17
 
41
18
  /**
42
- * Validation rules for individual fields in an array item.
43
- * @template O The state type.
44
- * @template K The key of the array in the state.
19
+ * Valid types for the initial state configuration.
20
+ * Can be a raw value or a Yup schema.
45
21
  */
46
- type ValidatorRule<item = any> = ValidatorParams | ((item: item, ...args: any[]) => ValidatorParams);
47
- type RulePath<T> = T extends object ? {
48
- [K in keyof T & string]: NonNullable<T[K]> extends (infer E)[] ? K | (E extends object ? `${K}.${RulePath<E>}` : never) : NonNullable<T[K]> extends object ? NonNullable<T[K]> extends Date ? K : K | `${K}.${RulePath<NonNullable<T[K]>>}` : K;
49
- }[keyof T & string] : never;
22
+ type ConfigType = any | Schema<any>;
23
+ /**
24
+ * Helper to infer the state type from the configuration object.
25
+ * If a value is a Yup schema, infer its return type.
26
+ * Otherwise, use the value's type directly.
27
+ */
28
+ type InferState<S extends Record<string, ConfigType>> = {
29
+ [K in keyof S]: S[K] extends Schema<any, any, any, any> ? InferType<S[K]> : S[K];
30
+ };
50
31
  /**
51
32
  * Options for the useForm hook.
52
33
  * @template O The state type.
53
34
  */
54
35
  interface FormOptions<O extends StateType> {
55
- /** Validation rules for top-level fields. */
56
- rules?: Partial<Record<RulePath<O>, ValidatorRule>>;
57
- /** Custom error messages for top-level fields. */
58
- messages?: Partial<Record<RulePath<O>, ValidatorErrorMessage>>;
59
36
  /**
60
37
  * Custom property names used as unique identifiers for items in specific arrays.
61
38
  * Default is "id".
62
39
  */
63
40
  arrayIdentifiers?: Partial<Record<ArrayPaths<O>, string>>;
64
41
  }
65
- type TouchedType = Map<string, boolean>;
66
- type ErrorsType = Map<string, NextUIError>;
42
+ type FieldMetadata = {
43
+ isTouched: boolean;
44
+ isInvalid: boolean;
45
+ errorMessage: string;
46
+ };
47
+ type MetadataType = Map<string, FieldMetadata>;
48
+ type SubmitHandler<O extends StateType> = (data: O, e: React.FormEvent) => void;
49
+ interface ResetOptions<O> {
50
+ preservedStateKeys?: (keyof O)[];
51
+ preserveTouched?: boolean;
52
+ }
67
53
  type ValueChangeFunc<O extends StateType, K extends keyof O> = (id: K, value: O[K]) => void;
68
54
  type BlurFunc<O extends StateType> = (id: keyof O) => void;
69
- interface ComponentInputProps<O extends StateType> {
55
+ interface ComponentInputProps {
70
56
  id: string;
71
57
  onBlur: () => void;
72
58
  isInvalid: boolean;
73
59
  errorMessage: string;
74
60
  }
75
61
  /** Props returned by on.input() */
76
- interface ItemInputProps<V = any> extends ComponentInputProps<any> {
62
+ interface ItemInputProps<V = any> extends ComponentInputProps {
77
63
  onValueChange: (value: V) => void;
78
64
  value: V;
79
65
  }
80
66
  /** Props returned by on.select() */
81
- interface ItemSelectProps extends ComponentInputProps<any> {
82
- onSelectionChange: SelectProps["onSelectionChange"];
67
+ interface ItemSelectProps extends ComponentInputProps {
68
+ onSelectionChange: NonNullable<SelectProps["onSelectionChange"]>;
83
69
  selectedKeys: string[] | number[] | string | null;
84
70
  }
85
71
  /** Props returned by on.autocomplete() */
86
- interface ItemAutocompleteProps extends ComponentInputProps<any> {
87
- onSelectionChange: AutocompleteProps["onSelectionChange"];
72
+ interface ItemAutocompleteProps extends ComponentInputProps {
73
+ onSelectionChange: NonNullable<AutocompleteProps["onSelectionChange"]>;
88
74
  selectedKey: SingleSelection["selectedKey"];
89
75
  }
90
76
  /**
@@ -100,8 +86,9 @@ interface OnMethods<O extends StateType> {
100
86
  input<P extends ObjectArrayFieldPaths<O>>(compositePath: P, itemId: ItemIdType<O, GetArrayKeyFromPath<O, P>>): ItemInputProps<any>;
101
87
  /** Registers a scalar or nested object field. */
102
88
  select<P extends AllPaths<O>>(id: P): ItemSelectProps;
89
+ /** Registers a complete array field for multi-selection. */
90
+ select<K extends ArrayKeys<O>>(arrayKey: K): ItemSelectProps;
103
91
  /** Registers an array element — adapts to primitive arrays (by index) or object arrays (by ID + field). */
104
- /** Registers an array element — adapts to primitive arrays (by index). */
105
92
  select<K extends ArrayKeys<O>>(arrayKey: K, index: number): ItemSelectProps;
106
93
  /** Registers an object array element's field using composite syntax "array.field". */
107
94
  select<P extends ObjectArrayFieldPaths<O>>(compositePath: P, itemId: ItemIdType<O, GetArrayKeyFromPath<O, P>>): ItemSelectProps;
@@ -176,29 +163,33 @@ interface HelpersFunc<O extends StateType> {
176
163
  * @template O The state type.
177
164
  */
178
165
  interface UseFormResponse<O extends StateType> {
179
- onBlur: BlurFunc<O>;
166
+ onFieldBlur: BlurFunc<O>;
180
167
  /** Updates an object array element's field by ID. */
181
- onValueChange<K extends ObjectArrayKeys<O>, F extends keyof ArrayElement<O[K]> & string>(arrayKey: K, itemId: ItemIdType<O, K>, field: F, value: ArrayElement<O[K]>[F]): void;
168
+ onFieldChange<K extends ObjectArrayKeys<O>, F extends keyof ArrayElement<O[K]> & string>(arrayKey: K, itemId: ItemIdType<O, K>, field: F, value: ArrayElement<O[K]>[F]): void;
182
169
  /** Updates a scalar or nested object field. */
183
- onValueChange<P extends AllPaths<O>>(id: P, value: NestedFieldValue<O, P & string>): void;
170
+ onFieldChange<P extends AllPaths<O>>(id: P, value: NestedFieldValue<O, P & string>): void;
184
171
  onSelectionChange: ValueChangeFunc<O, keyof O>;
185
172
  state: O;
186
173
  setState: React.Dispatch<React.SetStateAction<O>>;
187
- touched: TouchedType;
188
- errors: ErrorsType;
174
+ metadata: MetadataType;
189
175
  /** Main object to bind form elements to the state. */
190
176
  on: OnMethods<O>;
191
177
  /** Array manipulation helpers. */
192
178
  helpers: HelpersFunc<O>;
193
179
  isDirty: boolean;
194
- /** Manually triggers all validations and marks all fields as touched. Returns true if any error is found. */
195
- hasInvalidValues: () => boolean;
196
- /** Resets the form state and metadata. */
197
- resetForm: (preservedKeys?: (keyof O)[]) => void;
198
- /** Resets the touched metadata. */
199
- resetTouched: (preservedKeys?: (keyof O)[]) => void;
180
+ /**
181
+ * Resets the form state and metadata.
182
+ * @param options Configuration for the reset (preserve keys, preserve touched).
183
+ */
184
+ reset: (options?: ResetOptions<O>) => void;
185
+ /**
186
+ * Wraps a form submission handler to automatically validate the form.
187
+ * If validation fails, identifying errors and touching fields.
188
+ * If validation succeeds, calls the provided handler with the current state.
189
+ */
190
+ onSubmit: (fn: SubmitHandler<O>) => (e: React.FormEvent) => void;
200
191
  }
201
192
 
202
- declare function useForm<O extends StateType>(initialState: O, { rules, messages, arrayIdentifiers }?: FormOptions<O>): UseFormResponse<O>;
193
+ declare function useForm<S extends Record<string, ConfigType>>(schema: S, { arrayIdentifiers }?: FormOptions<InferState<S>>): UseFormResponse<InferState<S>>;
203
194
 
204
- export { type BlurFunc, type ErrorsType, type FormOptions, type HelpersFunc, type NestedChangeProps, NextUIError, type OnMethods, type StateType, type TouchedType, type UseFormResponse, type Validator, type ValidatorErrorMessage, type ValidatorParams, type ValidatorRule, type ValidatorTypes, type ValueChangeFunc, useForm };
195
+ export { type BlurFunc, type ConfigType, type FieldMetadata, type FormOptions, type HelpersFunc, type InferState, type MetadataType, type NestedChangeProps, NextUIError, type OnMethods, type StateType, type SubmitHandler, type UseFormResponse, type ValueChangeFunc, useForm };