cogsbox-state 0.5.443 → 0.5.445
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/CogsState.d.ts +19 -22
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +4 -3
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +36 -45
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -37,7 +37,7 @@ import { useCogsConfig } from './CogsStateClient.js';
|
|
|
37
37
|
import { Operation } from 'fast-json-patch';
|
|
38
38
|
import { useInView } from 'react-intersection-observer';
|
|
39
39
|
import * as z3 from 'zod/v3';
|
|
40
|
-
import
|
|
40
|
+
import * as z4 from 'zod/v4';
|
|
41
41
|
|
|
42
42
|
type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
|
|
43
43
|
|
|
@@ -534,36 +534,15 @@ export function addStateOptions<T extends unknown>(
|
|
|
534
534
|
) {
|
|
535
535
|
return { initialState: initialState, formElements, validation } as T;
|
|
536
536
|
}
|
|
537
|
-
type
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
apiParamsSchema?: z.ZodObject<any, any>;
|
|
543
|
-
[key: string]: any;
|
|
544
|
-
}
|
|
545
|
-
>;
|
|
546
|
-
notifications: Record<string, any>;
|
|
547
|
-
};
|
|
548
|
-
|
|
549
|
-
type UseCogsStateHook<TSchema extends CogsSyncSchema> = <
|
|
550
|
-
// TStateKey is now DIRECTLY a key of the schemas object. No ambiguity.
|
|
551
|
-
TStateKey extends keyof TSchema['schemas'],
|
|
552
|
-
>(
|
|
553
|
-
stateKey: TStateKey,
|
|
537
|
+
type UseCogsStateHook<
|
|
538
|
+
T extends Record<string, any>,
|
|
539
|
+
apiParams extends Record<string, any> = never,
|
|
540
|
+
> = <StateKey extends keyof TransformedStateType<T>>(
|
|
541
|
+
stateKey: StateKey,
|
|
554
542
|
options?: Prettify<
|
|
555
|
-
OptionsType<
|
|
556
|
-
// The state slice type is derived directly from the schema.
|
|
557
|
-
TSchema['schemas'][TStateKey]['schemas']['defaultValues'],
|
|
558
|
-
// The API params type is also derived directly and safely.
|
|
559
|
-
TSchema['schemas'][TStateKey] extends {
|
|
560
|
-
apiParamsSchema: z.ZodObject<any, any>;
|
|
561
|
-
}
|
|
562
|
-
? z.infer<TSchema['schemas'][TStateKey]['apiParamsSchema']>
|
|
563
|
-
: never
|
|
564
|
-
>
|
|
543
|
+
OptionsType<TransformedStateType<T>[StateKey]> & { apiParams: apiParams }
|
|
565
544
|
>
|
|
566
|
-
) => StateObject<
|
|
545
|
+
) => StateObject<TransformedStateType<T>[StateKey]>;
|
|
567
546
|
|
|
568
547
|
// Define the type for the options setter using the Transformed state
|
|
569
548
|
type SetCogsOptionsFunc<T extends Record<string, any>> = <
|
|
@@ -574,9 +553,12 @@ type SetCogsOptionsFunc<T extends Record<string, any>> = <
|
|
|
574
553
|
) => void;
|
|
575
554
|
|
|
576
555
|
// Define the final API object shape
|
|
577
|
-
type CogsApi<
|
|
578
|
-
|
|
579
|
-
|
|
556
|
+
type CogsApi<
|
|
557
|
+
T extends Record<string, any>,
|
|
558
|
+
apiParams extends Record<string, any> = never,
|
|
559
|
+
> = {
|
|
560
|
+
useCogsState: UseCogsStateHook<T, apiParams>;
|
|
561
|
+
setCogsOptions: SetCogsOptionsFunc<T>;
|
|
580
562
|
};
|
|
581
563
|
|
|
582
564
|
// Minimal change - just add a second parameter to detect sync schema
|
|
@@ -699,25 +681,34 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
699
681
|
notifyComponents(stateKey as string);
|
|
700
682
|
}
|
|
701
683
|
|
|
702
|
-
return { useCogsState, setCogsOptions }
|
|
684
|
+
return { useCogsState, setCogsOptions } as CogsApi<State>;
|
|
703
685
|
};
|
|
704
|
-
export function createCogsStateFromSync<
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
686
|
+
export function createCogsStateFromSync<
|
|
687
|
+
TSyncSchema extends {
|
|
688
|
+
schemas: Record<
|
|
689
|
+
string,
|
|
690
|
+
{
|
|
691
|
+
schemas: { defaultValues: any };
|
|
692
|
+
[key: string]: any;
|
|
693
|
+
}
|
|
694
|
+
>;
|
|
695
|
+
notifications: Record<string, any>;
|
|
696
|
+
},
|
|
697
|
+
>(
|
|
698
|
+
syncSchema: TSyncSchema
|
|
699
|
+
): CogsApi<{
|
|
700
|
+
[K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
|
|
701
|
+
}> {
|
|
713
702
|
const schemas = syncSchema.schemas;
|
|
714
703
|
const initialState: any = {};
|
|
704
|
+
|
|
705
|
+
// Extract defaultValues from each entry
|
|
715
706
|
for (const key in schemas) {
|
|
716
|
-
|
|
707
|
+
const entry = schemas[key];
|
|
708
|
+
initialState[key] = entry?.schemas?.defaultValues || {};
|
|
717
709
|
}
|
|
718
710
|
|
|
719
|
-
|
|
720
|
-
return createCogsState(initialState) as any;
|
|
711
|
+
return createCogsState(initialState);
|
|
721
712
|
}
|
|
722
713
|
|
|
723
714
|
const {
|