@juantroconisf/lib 5.2.0 → 5.3.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.mts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +42 -4
- package/dist/index.mjs +42 -4
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -130,6 +130,10 @@ interface OnMethods<O extends StateType> {
|
|
|
130
130
|
type ArrayKeys<O extends StateType> = {
|
|
131
131
|
[K in keyof O]: O[K] extends any[] ? K : never;
|
|
132
132
|
}[keyof O];
|
|
133
|
+
/** Keys whose values are arrays of objects (not primitives). */
|
|
134
|
+
type ObjectArrayKeys<O extends StateType> = {
|
|
135
|
+
[K in keyof O]: O[K] extends Record<string, any>[] ? K : never;
|
|
136
|
+
}[keyof O];
|
|
133
137
|
type ArrayElement<T> = T extends (infer E)[] ? E : never;
|
|
134
138
|
/** Resolves the type of the identifier field for an array element (defaults to "id"). */
|
|
135
139
|
type ItemIdType<O extends StateType, K extends keyof O> = "id" extends keyof ArrayElement<O[K]> ? ArrayElement<O[K]>["id"] : string | number;
|
|
@@ -170,7 +174,10 @@ interface HelpersFunc<O extends StateType> {
|
|
|
170
174
|
*/
|
|
171
175
|
interface UseFormResponse<O extends StateType> {
|
|
172
176
|
onBlur: BlurFunc<O>;
|
|
173
|
-
|
|
177
|
+
/** Updates an object array element's field by ID. */
|
|
178
|
+
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;
|
|
179
|
+
/** Updates a scalar or nested object field. */
|
|
180
|
+
onValueChange<P extends AllPaths<O>>(id: P, value: NestedFieldValue<O, P & string>): void;
|
|
174
181
|
onSelectionChange: ValueChangeFunc<O, keyof O>;
|
|
175
182
|
state: O;
|
|
176
183
|
setState: React.Dispatch<React.SetStateAction<O>>;
|
package/dist/index.d.ts
CHANGED
|
@@ -130,6 +130,10 @@ interface OnMethods<O extends StateType> {
|
|
|
130
130
|
type ArrayKeys<O extends StateType> = {
|
|
131
131
|
[K in keyof O]: O[K] extends any[] ? K : never;
|
|
132
132
|
}[keyof O];
|
|
133
|
+
/** Keys whose values are arrays of objects (not primitives). */
|
|
134
|
+
type ObjectArrayKeys<O extends StateType> = {
|
|
135
|
+
[K in keyof O]: O[K] extends Record<string, any>[] ? K : never;
|
|
136
|
+
}[keyof O];
|
|
133
137
|
type ArrayElement<T> = T extends (infer E)[] ? E : never;
|
|
134
138
|
/** Resolves the type of the identifier field for an array element (defaults to "id"). */
|
|
135
139
|
type ItemIdType<O extends StateType, K extends keyof O> = "id" extends keyof ArrayElement<O[K]> ? ArrayElement<O[K]>["id"] : string | number;
|
|
@@ -170,7 +174,10 @@ interface HelpersFunc<O extends StateType> {
|
|
|
170
174
|
*/
|
|
171
175
|
interface UseFormResponse<O extends StateType> {
|
|
172
176
|
onBlur: BlurFunc<O>;
|
|
173
|
-
|
|
177
|
+
/** Updates an object array element's field by ID. */
|
|
178
|
+
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;
|
|
179
|
+
/** Updates a scalar or nested object field. */
|
|
180
|
+
onValueChange<P extends AllPaths<O>>(id: P, value: NestedFieldValue<O, P & string>): void;
|
|
174
181
|
onSelectionChange: ValueChangeFunc<O, keyof O>;
|
|
175
182
|
state: O;
|
|
176
183
|
setState: React.Dispatch<React.SetStateAction<O>>;
|
package/dist/index.js
CHANGED
|
@@ -294,10 +294,48 @@ function useForm(initialState, {
|
|
|
294
294
|
value: true
|
|
295
295
|
})
|
|
296
296
|
);
|
|
297
|
-
}, onValueChange = (
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
297
|
+
}, onValueChange = ((...args) => {
|
|
298
|
+
if (args.length === 2) {
|
|
299
|
+
const [id, value] = args;
|
|
300
|
+
setState((prev) => handleNestedChange({ state: prev, id, value }));
|
|
301
|
+
validateInput(id, value);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (args.length === 3) {
|
|
305
|
+
const [arrayKey, index, value] = args;
|
|
306
|
+
setState((prev) => {
|
|
307
|
+
const arr = [...prev[arrayKey]];
|
|
308
|
+
arr[index] = value;
|
|
309
|
+
return { ...prev, [arrayKey]: arr };
|
|
310
|
+
});
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (args.length === 4) {
|
|
314
|
+
const [arrayKey, itemId, field, value] = args;
|
|
315
|
+
const index = getIndex(String(arrayKey), itemId);
|
|
316
|
+
if (index === void 0) return;
|
|
317
|
+
setState(
|
|
318
|
+
(prev) => handleArrayItemChange({ state: prev, arrayKey, index, field, value })
|
|
319
|
+
);
|
|
320
|
+
validateItemInput(arrayKey, itemId, field, value);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (args.length === 5) {
|
|
324
|
+
const [parentKey, parentId, field, index, value] = args;
|
|
325
|
+
const parentIndex = getIndex(String(parentKey), parentId);
|
|
326
|
+
if (parentIndex === void 0) return;
|
|
327
|
+
setState((prev) => {
|
|
328
|
+
const parentArr = [...prev[parentKey]];
|
|
329
|
+
const item = { ...parentArr[parentIndex] };
|
|
330
|
+
const nestedArr = [...getNestedValue(item, field) || []];
|
|
331
|
+
nestedArr[index] = value;
|
|
332
|
+
const updatedItem = setNestedValue(item, field, nestedArr);
|
|
333
|
+
parentArr[parentIndex] = updatedItem;
|
|
334
|
+
return { ...prev, [parentKey]: parentArr };
|
|
335
|
+
});
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
}), onSelectionChange = (id, value) => {
|
|
301
339
|
const fixedValue = typeof value === "string" || value === null ? value : Array.from(value);
|
|
302
340
|
setState(
|
|
303
341
|
(prev) => handleNestedChange({
|
package/dist/index.mjs
CHANGED
|
@@ -275,10 +275,48 @@ function useForm(initialState, {
|
|
|
275
275
|
value: true
|
|
276
276
|
})
|
|
277
277
|
);
|
|
278
|
-
}, onValueChange = (
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
278
|
+
}, onValueChange = ((...args) => {
|
|
279
|
+
if (args.length === 2) {
|
|
280
|
+
const [id, value] = args;
|
|
281
|
+
setState((prev) => handleNestedChange({ state: prev, id, value }));
|
|
282
|
+
validateInput(id, value);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (args.length === 3) {
|
|
286
|
+
const [arrayKey, index, value] = args;
|
|
287
|
+
setState((prev) => {
|
|
288
|
+
const arr = [...prev[arrayKey]];
|
|
289
|
+
arr[index] = value;
|
|
290
|
+
return { ...prev, [arrayKey]: arr };
|
|
291
|
+
});
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (args.length === 4) {
|
|
295
|
+
const [arrayKey, itemId, field, value] = args;
|
|
296
|
+
const index = getIndex(String(arrayKey), itemId);
|
|
297
|
+
if (index === void 0) return;
|
|
298
|
+
setState(
|
|
299
|
+
(prev) => handleArrayItemChange({ state: prev, arrayKey, index, field, value })
|
|
300
|
+
);
|
|
301
|
+
validateItemInput(arrayKey, itemId, field, value);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (args.length === 5) {
|
|
305
|
+
const [parentKey, parentId, field, index, value] = args;
|
|
306
|
+
const parentIndex = getIndex(String(parentKey), parentId);
|
|
307
|
+
if (parentIndex === void 0) return;
|
|
308
|
+
setState((prev) => {
|
|
309
|
+
const parentArr = [...prev[parentKey]];
|
|
310
|
+
const item = { ...parentArr[parentIndex] };
|
|
311
|
+
const nestedArr = [...getNestedValue(item, field) || []];
|
|
312
|
+
nestedArr[index] = value;
|
|
313
|
+
const updatedItem = setNestedValue(item, field, nestedArr);
|
|
314
|
+
parentArr[parentIndex] = updatedItem;
|
|
315
|
+
return { ...prev, [parentKey]: parentArr };
|
|
316
|
+
});
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
}), onSelectionChange = (id, value) => {
|
|
282
320
|
const fixedValue = typeof value === "string" || value === null ? value : Array.from(value);
|
|
283
321
|
setState(
|
|
284
322
|
(prev) => handleNestedChange({
|