@juantroconisf/lib 5.1.1 → 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 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;
@@ -161,6 +165,8 @@ interface HelpersFunc<O extends StateType> {
161
165
  moveItem: <K extends ArrayKeys<O>>(arrayKey: K, from: number, to: number) => void;
162
166
  /** Moves an item within an array using unique identifiers. */
163
167
  moveById: <K extends ArrayKeys<O>>(arrayKey: K, fromId: string | number, toId: string | number) => void;
168
+ /** Gets an item from an array by its unique identifier (O(1) via indexMap). */
169
+ getItem: <K extends ArrayKeys<O>>(arrayKey: K, itemId: ItemIdType<O, K>) => ArrayElement<O[K]> | undefined;
164
170
  }
165
171
  /**
166
172
  * The response object from the useForm hook.
@@ -168,7 +174,10 @@ interface HelpersFunc<O extends StateType> {
168
174
  */
169
175
  interface UseFormResponse<O extends StateType> {
170
176
  onBlur: BlurFunc<O>;
171
- onValueChange: ValueChangeFunc<O, keyof O>;
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;
172
181
  onSelectionChange: ValueChangeFunc<O, keyof O>;
173
182
  state: O;
174
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;
@@ -161,6 +165,8 @@ interface HelpersFunc<O extends StateType> {
161
165
  moveItem: <K extends ArrayKeys<O>>(arrayKey: K, from: number, to: number) => void;
162
166
  /** Moves an item within an array using unique identifiers. */
163
167
  moveById: <K extends ArrayKeys<O>>(arrayKey: K, fromId: string | number, toId: string | number) => void;
168
+ /** Gets an item from an array by its unique identifier (O(1) via indexMap). */
169
+ getItem: <K extends ArrayKeys<O>>(arrayKey: K, itemId: ItemIdType<O, K>) => ArrayElement<O[K]> | undefined;
164
170
  }
165
171
  /**
166
172
  * The response object from the useForm hook.
@@ -168,7 +174,10 @@ interface HelpersFunc<O extends StateType> {
168
174
  */
169
175
  interface UseFormResponse<O extends StateType> {
170
176
  onBlur: BlurFunc<O>;
171
- onValueChange: ValueChangeFunc<O, keyof O>;
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;
172
181
  onSelectionChange: ValueChangeFunc<O, keyof O>;
173
182
  state: O;
174
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 = (id, value) => {
298
- setState((prev) => handleNestedChange({ state: prev, id, value }));
299
- validateInput(id, value);
300
- }, onSelectionChange = (id, value) => {
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({
@@ -754,6 +792,13 @@ function useForm(initialState, {
754
792
  arr[index] = value;
755
793
  return { ...prev, [arrayKey]: arr };
756
794
  });
795
+ },
796
+ getItem: (arrayKey, itemId) => {
797
+ const index = getIndex(String(arrayKey), itemId);
798
+ if (index !== void 0) {
799
+ return state[arrayKey][index];
800
+ }
801
+ return void 0;
757
802
  }
758
803
  },
759
804
  isDirty: JSON.stringify(state) !== JSON.stringify(initialState),
package/dist/index.mjs CHANGED
@@ -275,10 +275,48 @@ function useForm(initialState, {
275
275
  value: true
276
276
  })
277
277
  );
278
- }, onValueChange = (id, value) => {
279
- setState((prev) => handleNestedChange({ state: prev, id, value }));
280
- validateInput(id, value);
281
- }, onSelectionChange = (id, value) => {
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({
@@ -735,6 +773,13 @@ function useForm(initialState, {
735
773
  arr[index] = value;
736
774
  return { ...prev, [arrayKey]: arr };
737
775
  });
776
+ },
777
+ getItem: (arrayKey, itemId) => {
778
+ const index = getIndex(String(arrayKey), itemId);
779
+ if (index !== void 0) {
780
+ return state[arrayKey][index];
781
+ }
782
+ return void 0;
738
783
  }
739
784
  },
740
785
  isDirty: JSON.stringify(state) !== JSON.stringify(initialState),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juantroconisf/lib",
3
- "version": "5.1.1",
3
+ "version": "5.3.0",
4
4
  "description": "A form validation library for HeroUI.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",