@juantroconisf/lib 11.8.0 → 11.10.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/README.md CHANGED
@@ -25,6 +25,7 @@ Managing forms in React often involves manual state syncing and verbose validati
25
25
  - [4. Manual Updates](#4-performing-manual-updates)
26
26
  - [Reference](#reference)
27
27
  - [useForm API](#useformschema-options)
28
+ - [Validation Results](#validation-results)
28
29
  - [on.* API](#the-on-api)
29
30
  - [Array Helpers](#array-helpers-helpers)
30
31
  - [Explanation](#explanation)
@@ -139,6 +140,36 @@ onArrayItemChange({ at: "users.name", id: userId, value: "Alice" });
139
140
  | `resetOnSubmit` | `boolean` | Reset form after success (default: `false`) |
140
141
  | `keepValues` | `(keyof State)[]` | Fields to exclude from reset |
141
142
 
143
+ ### Validation Results
144
+
145
+ The `validateAll`, `validateItem`, and the `helpers` validation methods return a `ValidationResponse` object:
146
+
147
+ ```ts
148
+ // Top level
149
+ const { isValid, results } = await validateAll();
150
+ const { isValid, results } = await validateFields(["firstName", "lastName"]);
151
+
152
+ // Within helpers
153
+ const { isValid, results } = await helpers.validateItem("users", userId);
154
+ ```
155
+ isValid: boolean;
156
+ errors: string[]; // List of composite IDs with errors
157
+ results: ErrorResult[]; // Detailed error objects
158
+ }
159
+
160
+ interface ErrorResult {
161
+ id: string; // e.g. "users.0.name"
162
+ label: string; // resolved from DOM (e.g. "User Name") or prettified ID
163
+ message: string; // validation error message
164
+ }
165
+ ```
166
+
167
+ #### Automatic Label Capture
168
+ The library lazily resolves labels the first time a field is blurred or validated. It checks in priority:
169
+ 1. `aria-labelledby` element's text.
170
+ 2. Standard `<label for="...">` text.
171
+ 3. Fallback: Prettified last segment of the ID (capitalized).
172
+
142
173
  ### The `on.*` API
143
174
  Methods that bridge schema logic to HeroUI components.
144
175
 
package/dist/index.d.mts CHANGED
@@ -58,8 +58,20 @@ type FieldMetadata = {
58
58
  isTouched: boolean;
59
59
  isInvalid: boolean;
60
60
  errorMessage: string;
61
+ /** Captured label or placeholder for the field. */
62
+ label?: string;
61
63
  };
62
64
  type MetadataType = Map<string, FieldMetadata>;
65
+ type ErrorResult = {
66
+ id: string;
67
+ label: string;
68
+ message: string;
69
+ };
70
+ type ValidationResponse = {
71
+ isValid: boolean;
72
+ errors: string[];
73
+ results: ErrorResult[];
74
+ };
63
75
  type FormSubmitHandler<O extends StateType> = (data: O, e: React.FormEvent) => void;
64
76
  /**
65
77
  * A utility type for inferring the submit handler's signature directly from a Yup schema object.
@@ -239,7 +251,11 @@ interface HelpersFunc<O extends StateType> {
239
251
  /** Gets an item from an array by its unique identifier (O(1) via indexMap). */
240
252
  getItemById: <K extends ArrayPaths<O>>(arrayKey: K, itemId: string | number) => (NestedFieldValue<O, K> extends (infer E)[] ? E : never) | undefined;
241
253
  /** Validates a single item in an array by its unique identifier. */
242
- validateItem: <K extends ArrayPaths<O>>(arrayKey: K, itemId: string | number) => Promise<boolean>;
254
+ validateItem: <K extends ArrayPaths<O>>(arrayKey: K, itemId: string | number) => Promise<ValidationResponse>;
255
+ /** Validates all fields in the form. */
256
+ validateAll: () => Promise<ValidationResponse>;
257
+ /** Validates a specific set of fields by their paths. */
258
+ validateFields: (paths: (keyof O | string)[]) => Promise<ValidationResponse>;
243
259
  }
244
260
  /**
245
261
  * The response object from the useForm hook.
@@ -287,15 +303,19 @@ interface UseFormResponse<O extends StateType> {
287
303
  * If validation fails, identifying errors and touching fields.
288
304
  * If validation succeeds, calls the provided handler with the current state.
289
305
  */
290
- onFormSubmit: (fn: FormSubmitHandler<O>) => (e: React.FormEvent) => void;
306
+ onFormSubmit: (fn: FormSubmitHandler<O>) => (e: React.FormEvent) => Promise<void>;
291
307
  /**
292
308
  * A controlled form component that handles submission and validation.
293
309
  */
294
310
  ControlledForm: React.ComponentType<Omit<FormProps, "onSubmit"> & {
295
311
  onSubmit?: (data: O, e: React.FormEvent<HTMLFormElement>) => void;
296
312
  }>;
313
+ /** Validates all fields in the form. */
314
+ validateAll: () => Promise<ValidationResponse>;
315
+ /** Validates a specific set of fields by their paths. */
316
+ validateFields: (paths: (keyof O | string)[]) => Promise<ValidationResponse>;
297
317
  }
298
318
 
299
319
  declare function useForm<S extends Record<string, ConfigType>>(schema: S, { arrayIdentifiers, onFormSubmit: onFormSubmitProp, resetOnSubmit, keepValues: keepValuesProp, }?: FormOptions<InferState<S>>): UseFormResponse<InferState<S>>;
300
320
 
301
- export { type BlurFunc, type ComponentInputProps, type ConfigType, type FieldMetadata, type FormOptions, type FormResetOptions, type FormSubmit, type FormSubmitHandler, type HelpersFunc, type InferState, type InferSubmitHandler, type ItemAutocompleteProps, type ItemInputProps, type ItemNumberInputProps, type ItemRadioGroupProps, type ItemSelectProps, type ItemToggleProps, type MetadataType, type NestedChangeProps, NextUIError, type OnMethods, type StateType, type UseFormResponse, type ValueChangeFunc, useForm };
321
+ export { type BlurFunc, type ComponentInputProps, type ConfigType, type ErrorResult, type FieldMetadata, type FormOptions, type FormResetOptions, type FormSubmit, type FormSubmitHandler, type HelpersFunc, type InferState, type InferSubmitHandler, type ItemAutocompleteProps, type ItemInputProps, type ItemNumberInputProps, type ItemRadioGroupProps, type ItemSelectProps, type ItemToggleProps, type MetadataType, type NestedChangeProps, NextUIError, type OnMethods, type StateType, type UseFormResponse, type ValidationResponse, type ValueChangeFunc, useForm };
package/dist/index.d.ts CHANGED
@@ -58,8 +58,20 @@ type FieldMetadata = {
58
58
  isTouched: boolean;
59
59
  isInvalid: boolean;
60
60
  errorMessage: string;
61
+ /** Captured label or placeholder for the field. */
62
+ label?: string;
61
63
  };
62
64
  type MetadataType = Map<string, FieldMetadata>;
65
+ type ErrorResult = {
66
+ id: string;
67
+ label: string;
68
+ message: string;
69
+ };
70
+ type ValidationResponse = {
71
+ isValid: boolean;
72
+ errors: string[];
73
+ results: ErrorResult[];
74
+ };
63
75
  type FormSubmitHandler<O extends StateType> = (data: O, e: React.FormEvent) => void;
64
76
  /**
65
77
  * A utility type for inferring the submit handler's signature directly from a Yup schema object.
@@ -239,7 +251,11 @@ interface HelpersFunc<O extends StateType> {
239
251
  /** Gets an item from an array by its unique identifier (O(1) via indexMap). */
240
252
  getItemById: <K extends ArrayPaths<O>>(arrayKey: K, itemId: string | number) => (NestedFieldValue<O, K> extends (infer E)[] ? E : never) | undefined;
241
253
  /** Validates a single item in an array by its unique identifier. */
242
- validateItem: <K extends ArrayPaths<O>>(arrayKey: K, itemId: string | number) => Promise<boolean>;
254
+ validateItem: <K extends ArrayPaths<O>>(arrayKey: K, itemId: string | number) => Promise<ValidationResponse>;
255
+ /** Validates all fields in the form. */
256
+ validateAll: () => Promise<ValidationResponse>;
257
+ /** Validates a specific set of fields by their paths. */
258
+ validateFields: (paths: (keyof O | string)[]) => Promise<ValidationResponse>;
243
259
  }
244
260
  /**
245
261
  * The response object from the useForm hook.
@@ -287,15 +303,19 @@ interface UseFormResponse<O extends StateType> {
287
303
  * If validation fails, identifying errors and touching fields.
288
304
  * If validation succeeds, calls the provided handler with the current state.
289
305
  */
290
- onFormSubmit: (fn: FormSubmitHandler<O>) => (e: React.FormEvent) => void;
306
+ onFormSubmit: (fn: FormSubmitHandler<O>) => (e: React.FormEvent) => Promise<void>;
291
307
  /**
292
308
  * A controlled form component that handles submission and validation.
293
309
  */
294
310
  ControlledForm: React.ComponentType<Omit<FormProps, "onSubmit"> & {
295
311
  onSubmit?: (data: O, e: React.FormEvent<HTMLFormElement>) => void;
296
312
  }>;
313
+ /** Validates all fields in the form. */
314
+ validateAll: () => Promise<ValidationResponse>;
315
+ /** Validates a specific set of fields by their paths. */
316
+ validateFields: (paths: (keyof O | string)[]) => Promise<ValidationResponse>;
297
317
  }
298
318
 
299
319
  declare function useForm<S extends Record<string, ConfigType>>(schema: S, { arrayIdentifiers, onFormSubmit: onFormSubmitProp, resetOnSubmit, keepValues: keepValuesProp, }?: FormOptions<InferState<S>>): UseFormResponse<InferState<S>>;
300
320
 
301
- export { type BlurFunc, type ComponentInputProps, type ConfigType, type FieldMetadata, type FormOptions, type FormResetOptions, type FormSubmit, type FormSubmitHandler, type HelpersFunc, type InferState, type InferSubmitHandler, type ItemAutocompleteProps, type ItemInputProps, type ItemNumberInputProps, type ItemRadioGroupProps, type ItemSelectProps, type ItemToggleProps, type MetadataType, type NestedChangeProps, NextUIError, type OnMethods, type StateType, type UseFormResponse, type ValueChangeFunc, useForm };
321
+ export { type BlurFunc, type ComponentInputProps, type ConfigType, type ErrorResult, type FieldMetadata, type FormOptions, type FormResetOptions, type FormSubmit, type FormSubmitHandler, type HelpersFunc, type InferState, type InferSubmitHandler, type ItemAutocompleteProps, type ItemInputProps, type ItemNumberInputProps, type ItemRadioGroupProps, type ItemSelectProps, type ItemToggleProps, type MetadataType, type NestedChangeProps, NextUIError, type OnMethods, type StateType, type UseFormResponse, type ValidationResponse, type ValueChangeFunc, useForm };