@juantroconisf/lib 4.2.0 → 5.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/README.md +99 -2
- package/dist/index.d.mts +122 -17
- package/dist/index.d.ts +122 -17
- package/dist/index.js +421 -49
- package/dist/index.mjs +422 -50
- package/package.json +37 -36
package/README.md
CHANGED
|
@@ -1,3 +1,100 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @juantroconisf/lib
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A powerful, type-safe form management and validation library optimized for **HeroUI** (formerly NextUI).
|
|
4
|
+
|
|
5
|
+
## Key Features
|
|
6
|
+
|
|
7
|
+
- 🎯 **Polymorphic `on` API**: Single consistent interface for `input`, `select`, and `autocomplete`.
|
|
8
|
+
- 🧩 **Deep Nesting**: Effortlessly manage complex objects with dot-notation support (e.g., `address.city`).
|
|
9
|
+
- 🔢 **ID-Based Arrays**: Stable state management for dynamic lists. Items are tracked by unique identifiers, preventing state loss during reordering or deletions.
|
|
10
|
+
- ⚡ **O(1) Performance**: Internal mapping for array items ensures lightning-fast updates regardless of list size.
|
|
11
|
+
- 🛡️ **Total Type Safety**: Best-in-class Intellisense for paths, types, and array identifiers.
|
|
12
|
+
- 🎨 **HeroUI Optimized**: Returns props ready to be spread directly onto HeroUI components (`isInvalid`, `errorMessage`, `onBlur`, etc.).
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pnpm add @juantroconisf/lib
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { useForm } from "@juantroconisf/lib";
|
|
28
|
+
|
|
29
|
+
const MyForm = () => {
|
|
30
|
+
const { on, state, helpers } = useForm({
|
|
31
|
+
name: "Juan",
|
|
32
|
+
lines: [
|
|
33
|
+
{ id: 1, amount: 10 }
|
|
34
|
+
]
|
|
35
|
+
}, {
|
|
36
|
+
rules: {
|
|
37
|
+
name: { required: true }
|
|
38
|
+
},
|
|
39
|
+
arrayRules: {
|
|
40
|
+
lines: {
|
|
41
|
+
amount: (item, index) => ({ min: 1 })
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<form>
|
|
48
|
+
{/* Scalar/Nested Registration */}
|
|
49
|
+
<Input {...on.input("name")} label="Name" />
|
|
50
|
+
|
|
51
|
+
{/* Array Registration (By ID) */}
|
|
52
|
+
{state.lines.map((line) => (
|
|
53
|
+
<Input
|
|
54
|
+
key={line.id}
|
|
55
|
+
{...on.input("lines", line.id, "amount")}
|
|
56
|
+
label="Amount"
|
|
57
|
+
/>
|
|
58
|
+
))}
|
|
59
|
+
|
|
60
|
+
<Button onClick={() => helpers.removeById("lines", 1)}>
|
|
61
|
+
Remove First
|
|
62
|
+
</Button>
|
|
63
|
+
</form>
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## API Reference
|
|
71
|
+
|
|
72
|
+
### `useForm(initialState, options)`
|
|
73
|
+
|
|
74
|
+
#### Options
|
|
75
|
+
- **`rules`**: Validation rules for root/nested fields.
|
|
76
|
+
- **`messages`**: Custom error messages for root/nested fields.
|
|
77
|
+
- **`arrayRules`**: Validation rules for array items. Receives `(item, index)`.
|
|
78
|
+
- **`arrayMessages`**: Custom error messages for array items.
|
|
79
|
+
- **`arrayIdentifiers`**: Mapping of array keys to their unique ID property (defaults to `"id"`).
|
|
80
|
+
|
|
81
|
+
### The `on` Object
|
|
82
|
+
Provides methods to register components with the form state. Support both scalar/nested paths and array tracking.
|
|
83
|
+
|
|
84
|
+
- **`on.input(path)`** / **`on.input(arrayKey, itemId, field)`**
|
|
85
|
+
- **`on.select(path)`** / **`on.select(arrayKey, itemId, field)`**
|
|
86
|
+
- **`on.autocomplete(path)`** / **`on.autocomplete(arrayKey, itemId, field)`**
|
|
87
|
+
|
|
88
|
+
### Helpers
|
|
89
|
+
Utilities for common state manipulations:
|
|
90
|
+
- **`addItem(arrayKey, item, index?)`**
|
|
91
|
+
- **`removeItem(arrayKey, index)`**
|
|
92
|
+
- **`removeById(arrayKey, itemId)`**
|
|
93
|
+
- **`moveItem(arrayKey, from, to)`**
|
|
94
|
+
- **`moveById(arrayKey, fromId, toId)`**
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
ISC © Juan T
|
package/dist/index.d.mts
CHANGED
|
@@ -38,20 +38,57 @@ type ValidatorErrorMessage = {
|
|
|
38
38
|
[K in keyof ValidatorTypes]?: string;
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
+
/**
|
|
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.
|
|
45
|
+
*/
|
|
46
|
+
type ArrayRules<O extends StateType, K extends keyof O = keyof O> = {
|
|
47
|
+
[F in FieldPaths<ArrayElement<O[K]>>]?: ValidatorParams | ((item: ArrayElement<O[K]>, index: number) => ValidatorParams);
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Custom error messages for individual fields in an array item.
|
|
51
|
+
* @template O The state type.
|
|
52
|
+
* @template K The key of the array in the state.
|
|
53
|
+
*/
|
|
54
|
+
type ArrayMessages<O extends StateType, K extends keyof O = keyof O> = {
|
|
55
|
+
[F in FieldPaths<ArrayElement<O[K]>>]?: ValidatorErrorMessage;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Options for the useForm hook.
|
|
59
|
+
* @template O The state type.
|
|
60
|
+
*/
|
|
41
61
|
interface FormOptions<O extends StateType> {
|
|
42
|
-
|
|
62
|
+
/** Validation rules for top-level fields. */
|
|
63
|
+
rules?: {
|
|
43
64
|
[key in keyof O]?: ValidatorParams;
|
|
44
65
|
};
|
|
45
|
-
|
|
66
|
+
/** Custom error messages for top-level fields. */
|
|
67
|
+
messages?: {
|
|
46
68
|
[key in keyof O]?: ValidatorErrorMessage;
|
|
47
69
|
};
|
|
70
|
+
/** Validation rules for items within arrays. */
|
|
71
|
+
arrayRules?: {
|
|
72
|
+
[K in ArrayKeys<O>]?: ArrayRules<O, K>;
|
|
73
|
+
};
|
|
74
|
+
/** Custom error messages for items within arrays. */
|
|
75
|
+
arrayMessages?: {
|
|
76
|
+
[K in ArrayKeys<O>]?: ArrayMessages<O, K>;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Custom property names used as unique identifiers for items in specific arrays.
|
|
80
|
+
* Default is "id".
|
|
81
|
+
*/
|
|
82
|
+
arrayIdentifiers?: Partial<Record<ArrayKeys<O>, string>>;
|
|
83
|
+
/** General configuration for the form behavior. */
|
|
84
|
+
config?: {};
|
|
48
85
|
}
|
|
49
86
|
type TouchedType<O extends StateType> = Record<keyof O, boolean>;
|
|
50
87
|
type ErrorsType<O extends StateType> = Record<keyof O, NextUIError>;
|
|
51
88
|
type ValueChangeFunc<O extends StateType, K extends keyof O> = (id: K, value: O[K]) => void;
|
|
52
89
|
type BlurFunc<O extends StateType> = (id: keyof O) => void;
|
|
53
90
|
interface ComponentInputProps<O extends StateType> {
|
|
54
|
-
id:
|
|
91
|
+
id: string;
|
|
55
92
|
onBlur: () => void;
|
|
56
93
|
isInvalid: boolean;
|
|
57
94
|
errorMessage: string;
|
|
@@ -60,26 +97,87 @@ type UXProps<O extends StateType> = Record<keyof O, {
|
|
|
60
97
|
isInvalid: boolean;
|
|
61
98
|
errorMessage: string;
|
|
62
99
|
}>;
|
|
63
|
-
|
|
64
|
-
|
|
100
|
+
/** Props returned by on.input() */
|
|
101
|
+
interface ItemInputProps<V = any> extends ComponentInputProps<any> {
|
|
102
|
+
onValueChange: (value: V) => void;
|
|
65
103
|
value: V;
|
|
66
104
|
}
|
|
67
|
-
|
|
105
|
+
/** Props returned by on.select() */
|
|
106
|
+
interface ItemSelectProps extends ComponentInputProps<any> {
|
|
68
107
|
onSelectionChange: SelectProps["onSelectionChange"];
|
|
69
108
|
selectedKeys: string[] | number[] | string | null;
|
|
70
109
|
}
|
|
71
|
-
|
|
110
|
+
/** Props returned by on.autocomplete() */
|
|
111
|
+
interface ItemAutocompleteProps extends ComponentInputProps<any> {
|
|
72
112
|
onSelectionChange: AutocompleteProps["onSelectionChange"];
|
|
73
113
|
selectedKey: SingleSelection["selectedKey"];
|
|
74
114
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Interface for the polymorphic 'on' method handlers.
|
|
117
|
+
*/
|
|
118
|
+
interface OnMethods<O extends StateType> {
|
|
119
|
+
/**
|
|
120
|
+
* Registers a standard or nested field for an input component.
|
|
121
|
+
*/
|
|
122
|
+
input<P extends AllPaths<O>>(id: P): ItemInputProps<NestedFieldValue<O, P & string>>;
|
|
123
|
+
/**
|
|
124
|
+
* Registers an array item field for an input component using its unique ID.
|
|
125
|
+
*/
|
|
126
|
+
input<K extends ArrayKeys<O>, F extends FieldPaths<ArrayElement<O[K]>>>(arrayKey: K, itemId: ItemIdType<O, K>, field: F): ItemInputProps<NestedFieldValue<ArrayElement<O[K]>, F & string>>;
|
|
127
|
+
/**
|
|
128
|
+
* Registers a standard or nested field for a select component.
|
|
129
|
+
*/
|
|
130
|
+
select<P extends AllPaths<O>>(id: P): ItemSelectProps;
|
|
131
|
+
/**
|
|
132
|
+
* Registers an array item field for a select component using its unique ID.
|
|
133
|
+
*/
|
|
134
|
+
select<K extends ArrayKeys<O>>(arrayKey: K, itemId: ItemIdType<O, K>, field: FieldPaths<ArrayElement<O[K]>>): ItemSelectProps;
|
|
135
|
+
/**
|
|
136
|
+
* Registers a standard or nested field for an autocomplete component.
|
|
137
|
+
*/
|
|
138
|
+
autocomplete<P extends AllPaths<O>>(id: P): ItemAutocompleteProps;
|
|
139
|
+
/**
|
|
140
|
+
* Registers an array item field for an autocomplete component using its unique ID.
|
|
141
|
+
*/
|
|
142
|
+
autocomplete<K extends ArrayKeys<O>>(arrayKey: K, itemId: ItemIdType<O, K>, field: FieldPaths<ArrayElement<O[K]>>): ItemAutocompleteProps;
|
|
143
|
+
}
|
|
144
|
+
type ArrayKeys<O extends StateType> = {
|
|
145
|
+
[K in keyof O]: O[K] extends any[] ? K : never;
|
|
146
|
+
}[keyof O];
|
|
147
|
+
type ArrayElement<T> = T extends (infer E)[] ? E : never;
|
|
148
|
+
/** Resolves the type of the identifier field for an array element (defaults to "id"). */
|
|
149
|
+
type ItemIdType<O extends StateType, K extends ArrayKeys<O>> = "id" extends keyof ArrayElement<O[K]> ? ArrayElement<O[K]>["id"] : string | number;
|
|
150
|
+
type NestedFieldValue<T, F extends string> = F extends `${infer First}.${infer Rest}` ? First extends keyof T ? NestedFieldValue<T[First], Rest> : any : F extends keyof T ? T[F] : any;
|
|
151
|
+
type FieldPaths<T> = T extends Record<string, any> ? {
|
|
152
|
+
[K in keyof T & string]: T[K] extends any[] ? K : T[K] extends Record<string, any> ? `${K}.${FieldPaths<T[K]>}` : K;
|
|
153
|
+
}[keyof T & string] : never;
|
|
154
|
+
type NestedObjectPaths<O extends StateType> = {
|
|
155
|
+
[K in keyof O & string]: O[K] extends any[] ? never : O[K] extends Record<string, any> ? `${K}.${FieldPaths<O[K]>}` : never;
|
|
156
|
+
}[keyof O & string];
|
|
157
|
+
/** Keys whose values are not arrays and not plain objects (i.e. scalar/primitive fields). */
|
|
158
|
+
type ScalarKeys<O extends StateType> = {
|
|
159
|
+
[K in keyof O]: O[K] extends any[] ? never : O[K] extends Record<string, any> ? never : K;
|
|
160
|
+
}[keyof O];
|
|
161
|
+
type AllPaths<O extends StateType> = ScalarKeys<O> | NestedObjectPaths<O>;
|
|
162
|
+
/**
|
|
163
|
+
* Array helper functions.
|
|
164
|
+
*/
|
|
165
|
+
interface HelpersFunc<O extends StateType> {
|
|
166
|
+
/** Adds a new item to an array. */
|
|
167
|
+
addItem: <K extends ArrayKeys<O>>(arrayKey: K, item: ArrayElement<O[K]>, index?: number) => void;
|
|
168
|
+
/** Removes an item from an array by its index. */
|
|
169
|
+
removeItem: <K extends ArrayKeys<O>>(arrayKey: K, index: number) => void;
|
|
170
|
+
/** Removes an item from an array by its unique identifier. */
|
|
171
|
+
removeById: <K extends ArrayKeys<O>>(arrayKey: K, itemId: string | number) => void;
|
|
172
|
+
/** Moves an item within an array using indices. */
|
|
173
|
+
moveItem: <K extends ArrayKeys<O>>(arrayKey: K, from: number, to: number) => void;
|
|
174
|
+
/** Moves an item within an array using unique identifiers. */
|
|
175
|
+
moveById: <K extends ArrayKeys<O>>(arrayKey: K, fromId: string | number, toId: string | number) => void;
|
|
82
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* The response object from the useForm hook.
|
|
179
|
+
* @template O The state type.
|
|
180
|
+
*/
|
|
83
181
|
interface UseFormResponse<O extends StateType> {
|
|
84
182
|
onBlur: BlurFunc<O>;
|
|
85
183
|
onValueChange: ValueChangeFunc<O, keyof O>;
|
|
@@ -88,12 +186,19 @@ interface UseFormResponse<O extends StateType> {
|
|
|
88
186
|
setState: React.Dispatch<React.SetStateAction<O>>;
|
|
89
187
|
touched: TouchedType<O>;
|
|
90
188
|
errors: ErrorsType<O>;
|
|
91
|
-
|
|
189
|
+
/** Main object to bind form elements to the state. */
|
|
190
|
+
on: OnMethods<O>;
|
|
191
|
+
/** Array manipulation helpers. */
|
|
192
|
+
helpers: HelpersFunc<O>;
|
|
193
|
+
isDirty: boolean;
|
|
194
|
+
/** Manually triggers all validations and marks all fields as touched. Returns true if any error is found. */
|
|
92
195
|
hasInvalidValues: () => boolean;
|
|
196
|
+
/** Resets the form state and metadata. */
|
|
93
197
|
resetForm: (preservedKeys?: (keyof O)[]) => void;
|
|
198
|
+
/** Resets the touched metadata. */
|
|
94
199
|
resetTouched: (preservedKeys?: (keyof O)[]) => void;
|
|
95
200
|
}
|
|
96
201
|
|
|
97
|
-
declare function useForm<O extends StateType>(initialState: O, {
|
|
202
|
+
declare function useForm<O extends StateType>(initialState: O, { rules, messages, arrayRules, arrayMessages, arrayIdentifiers, config, }?: FormOptions<O>): UseFormResponse<O>;
|
|
98
203
|
|
|
99
|
-
export { type BlurFunc, type ErrorsType, type FormOptions, type NestedChangeProps, NextUIError, type
|
|
204
|
+
export { type ArrayMessages, type ArrayRules, type BlurFunc, type ErrorsType, type FormOptions, type HelpersFunc, type NestedChangeProps, NextUIError, type OnMethods, type StateType, type TouchedType, type UXProps, type UseFormResponse, type Validator, type ValidatorErrorMessage, type ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useForm };
|
package/dist/index.d.ts
CHANGED
|
@@ -38,20 +38,57 @@ type ValidatorErrorMessage = {
|
|
|
38
38
|
[K in keyof ValidatorTypes]?: string;
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
+
/**
|
|
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.
|
|
45
|
+
*/
|
|
46
|
+
type ArrayRules<O extends StateType, K extends keyof O = keyof O> = {
|
|
47
|
+
[F in FieldPaths<ArrayElement<O[K]>>]?: ValidatorParams | ((item: ArrayElement<O[K]>, index: number) => ValidatorParams);
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Custom error messages for individual fields in an array item.
|
|
51
|
+
* @template O The state type.
|
|
52
|
+
* @template K The key of the array in the state.
|
|
53
|
+
*/
|
|
54
|
+
type ArrayMessages<O extends StateType, K extends keyof O = keyof O> = {
|
|
55
|
+
[F in FieldPaths<ArrayElement<O[K]>>]?: ValidatorErrorMessage;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Options for the useForm hook.
|
|
59
|
+
* @template O The state type.
|
|
60
|
+
*/
|
|
41
61
|
interface FormOptions<O extends StateType> {
|
|
42
|
-
|
|
62
|
+
/** Validation rules for top-level fields. */
|
|
63
|
+
rules?: {
|
|
43
64
|
[key in keyof O]?: ValidatorParams;
|
|
44
65
|
};
|
|
45
|
-
|
|
66
|
+
/** Custom error messages for top-level fields. */
|
|
67
|
+
messages?: {
|
|
46
68
|
[key in keyof O]?: ValidatorErrorMessage;
|
|
47
69
|
};
|
|
70
|
+
/** Validation rules for items within arrays. */
|
|
71
|
+
arrayRules?: {
|
|
72
|
+
[K in ArrayKeys<O>]?: ArrayRules<O, K>;
|
|
73
|
+
};
|
|
74
|
+
/** Custom error messages for items within arrays. */
|
|
75
|
+
arrayMessages?: {
|
|
76
|
+
[K in ArrayKeys<O>]?: ArrayMessages<O, K>;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Custom property names used as unique identifiers for items in specific arrays.
|
|
80
|
+
* Default is "id".
|
|
81
|
+
*/
|
|
82
|
+
arrayIdentifiers?: Partial<Record<ArrayKeys<O>, string>>;
|
|
83
|
+
/** General configuration for the form behavior. */
|
|
84
|
+
config?: {};
|
|
48
85
|
}
|
|
49
86
|
type TouchedType<O extends StateType> = Record<keyof O, boolean>;
|
|
50
87
|
type ErrorsType<O extends StateType> = Record<keyof O, NextUIError>;
|
|
51
88
|
type ValueChangeFunc<O extends StateType, K extends keyof O> = (id: K, value: O[K]) => void;
|
|
52
89
|
type BlurFunc<O extends StateType> = (id: keyof O) => void;
|
|
53
90
|
interface ComponentInputProps<O extends StateType> {
|
|
54
|
-
id:
|
|
91
|
+
id: string;
|
|
55
92
|
onBlur: () => void;
|
|
56
93
|
isInvalid: boolean;
|
|
57
94
|
errorMessage: string;
|
|
@@ -60,26 +97,87 @@ type UXProps<O extends StateType> = Record<keyof O, {
|
|
|
60
97
|
isInvalid: boolean;
|
|
61
98
|
errorMessage: string;
|
|
62
99
|
}>;
|
|
63
|
-
|
|
64
|
-
|
|
100
|
+
/** Props returned by on.input() */
|
|
101
|
+
interface ItemInputProps<V = any> extends ComponentInputProps<any> {
|
|
102
|
+
onValueChange: (value: V) => void;
|
|
65
103
|
value: V;
|
|
66
104
|
}
|
|
67
|
-
|
|
105
|
+
/** Props returned by on.select() */
|
|
106
|
+
interface ItemSelectProps extends ComponentInputProps<any> {
|
|
68
107
|
onSelectionChange: SelectProps["onSelectionChange"];
|
|
69
108
|
selectedKeys: string[] | number[] | string | null;
|
|
70
109
|
}
|
|
71
|
-
|
|
110
|
+
/** Props returned by on.autocomplete() */
|
|
111
|
+
interface ItemAutocompleteProps extends ComponentInputProps<any> {
|
|
72
112
|
onSelectionChange: AutocompleteProps["onSelectionChange"];
|
|
73
113
|
selectedKey: SingleSelection["selectedKey"];
|
|
74
114
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Interface for the polymorphic 'on' method handlers.
|
|
117
|
+
*/
|
|
118
|
+
interface OnMethods<O extends StateType> {
|
|
119
|
+
/**
|
|
120
|
+
* Registers a standard or nested field for an input component.
|
|
121
|
+
*/
|
|
122
|
+
input<P extends AllPaths<O>>(id: P): ItemInputProps<NestedFieldValue<O, P & string>>;
|
|
123
|
+
/**
|
|
124
|
+
* Registers an array item field for an input component using its unique ID.
|
|
125
|
+
*/
|
|
126
|
+
input<K extends ArrayKeys<O>, F extends FieldPaths<ArrayElement<O[K]>>>(arrayKey: K, itemId: ItemIdType<O, K>, field: F): ItemInputProps<NestedFieldValue<ArrayElement<O[K]>, F & string>>;
|
|
127
|
+
/**
|
|
128
|
+
* Registers a standard or nested field for a select component.
|
|
129
|
+
*/
|
|
130
|
+
select<P extends AllPaths<O>>(id: P): ItemSelectProps;
|
|
131
|
+
/**
|
|
132
|
+
* Registers an array item field for a select component using its unique ID.
|
|
133
|
+
*/
|
|
134
|
+
select<K extends ArrayKeys<O>>(arrayKey: K, itemId: ItemIdType<O, K>, field: FieldPaths<ArrayElement<O[K]>>): ItemSelectProps;
|
|
135
|
+
/**
|
|
136
|
+
* Registers a standard or nested field for an autocomplete component.
|
|
137
|
+
*/
|
|
138
|
+
autocomplete<P extends AllPaths<O>>(id: P): ItemAutocompleteProps;
|
|
139
|
+
/**
|
|
140
|
+
* Registers an array item field for an autocomplete component using its unique ID.
|
|
141
|
+
*/
|
|
142
|
+
autocomplete<K extends ArrayKeys<O>>(arrayKey: K, itemId: ItemIdType<O, K>, field: FieldPaths<ArrayElement<O[K]>>): ItemAutocompleteProps;
|
|
143
|
+
}
|
|
144
|
+
type ArrayKeys<O extends StateType> = {
|
|
145
|
+
[K in keyof O]: O[K] extends any[] ? K : never;
|
|
146
|
+
}[keyof O];
|
|
147
|
+
type ArrayElement<T> = T extends (infer E)[] ? E : never;
|
|
148
|
+
/** Resolves the type of the identifier field for an array element (defaults to "id"). */
|
|
149
|
+
type ItemIdType<O extends StateType, K extends ArrayKeys<O>> = "id" extends keyof ArrayElement<O[K]> ? ArrayElement<O[K]>["id"] : string | number;
|
|
150
|
+
type NestedFieldValue<T, F extends string> = F extends `${infer First}.${infer Rest}` ? First extends keyof T ? NestedFieldValue<T[First], Rest> : any : F extends keyof T ? T[F] : any;
|
|
151
|
+
type FieldPaths<T> = T extends Record<string, any> ? {
|
|
152
|
+
[K in keyof T & string]: T[K] extends any[] ? K : T[K] extends Record<string, any> ? `${K}.${FieldPaths<T[K]>}` : K;
|
|
153
|
+
}[keyof T & string] : never;
|
|
154
|
+
type NestedObjectPaths<O extends StateType> = {
|
|
155
|
+
[K in keyof O & string]: O[K] extends any[] ? never : O[K] extends Record<string, any> ? `${K}.${FieldPaths<O[K]>}` : never;
|
|
156
|
+
}[keyof O & string];
|
|
157
|
+
/** Keys whose values are not arrays and not plain objects (i.e. scalar/primitive fields). */
|
|
158
|
+
type ScalarKeys<O extends StateType> = {
|
|
159
|
+
[K in keyof O]: O[K] extends any[] ? never : O[K] extends Record<string, any> ? never : K;
|
|
160
|
+
}[keyof O];
|
|
161
|
+
type AllPaths<O extends StateType> = ScalarKeys<O> | NestedObjectPaths<O>;
|
|
162
|
+
/**
|
|
163
|
+
* Array helper functions.
|
|
164
|
+
*/
|
|
165
|
+
interface HelpersFunc<O extends StateType> {
|
|
166
|
+
/** Adds a new item to an array. */
|
|
167
|
+
addItem: <K extends ArrayKeys<O>>(arrayKey: K, item: ArrayElement<O[K]>, index?: number) => void;
|
|
168
|
+
/** Removes an item from an array by its index. */
|
|
169
|
+
removeItem: <K extends ArrayKeys<O>>(arrayKey: K, index: number) => void;
|
|
170
|
+
/** Removes an item from an array by its unique identifier. */
|
|
171
|
+
removeById: <K extends ArrayKeys<O>>(arrayKey: K, itemId: string | number) => void;
|
|
172
|
+
/** Moves an item within an array using indices. */
|
|
173
|
+
moveItem: <K extends ArrayKeys<O>>(arrayKey: K, from: number, to: number) => void;
|
|
174
|
+
/** Moves an item within an array using unique identifiers. */
|
|
175
|
+
moveById: <K extends ArrayKeys<O>>(arrayKey: K, fromId: string | number, toId: string | number) => void;
|
|
82
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* The response object from the useForm hook.
|
|
179
|
+
* @template O The state type.
|
|
180
|
+
*/
|
|
83
181
|
interface UseFormResponse<O extends StateType> {
|
|
84
182
|
onBlur: BlurFunc<O>;
|
|
85
183
|
onValueChange: ValueChangeFunc<O, keyof O>;
|
|
@@ -88,12 +186,19 @@ interface UseFormResponse<O extends StateType> {
|
|
|
88
186
|
setState: React.Dispatch<React.SetStateAction<O>>;
|
|
89
187
|
touched: TouchedType<O>;
|
|
90
188
|
errors: ErrorsType<O>;
|
|
91
|
-
|
|
189
|
+
/** Main object to bind form elements to the state. */
|
|
190
|
+
on: OnMethods<O>;
|
|
191
|
+
/** Array manipulation helpers. */
|
|
192
|
+
helpers: HelpersFunc<O>;
|
|
193
|
+
isDirty: boolean;
|
|
194
|
+
/** Manually triggers all validations and marks all fields as touched. Returns true if any error is found. */
|
|
92
195
|
hasInvalidValues: () => boolean;
|
|
196
|
+
/** Resets the form state and metadata. */
|
|
93
197
|
resetForm: (preservedKeys?: (keyof O)[]) => void;
|
|
198
|
+
/** Resets the touched metadata. */
|
|
94
199
|
resetTouched: (preservedKeys?: (keyof O)[]) => void;
|
|
95
200
|
}
|
|
96
201
|
|
|
97
|
-
declare function useForm<O extends StateType>(initialState: O, {
|
|
202
|
+
declare function useForm<O extends StateType>(initialState: O, { rules, messages, arrayRules, arrayMessages, arrayIdentifiers, config, }?: FormOptions<O>): UseFormResponse<O>;
|
|
98
203
|
|
|
99
|
-
export { type BlurFunc, type ErrorsType, type FormOptions, type NestedChangeProps, NextUIError, type
|
|
204
|
+
export { type ArrayMessages, type ArrayRules, type BlurFunc, type ErrorsType, type FormOptions, type HelpersFunc, type NestedChangeProps, NextUIError, type OnMethods, type StateType, type TouchedType, type UXProps, type UseFormResponse, type Validator, type ValidatorErrorMessage, type ValidatorParams, type ValidatorTypes, type ValueChangeFunc, useForm };
|