cogsbox-state 0.5.447 → 0.5.449
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 -15
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +220 -213
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +47 -33
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -534,46 +534,33 @@ export function addStateOptions<T extends unknown>(
|
|
|
534
534
|
) {
|
|
535
535
|
return { initialState: initialState, formElements, validation } as T;
|
|
536
536
|
}
|
|
537
|
-
type
|
|
538
|
-
|
|
539
|
-
>
|
|
537
|
+
type UseCogsStateHook<
|
|
538
|
+
T extends Record<string, any>,
|
|
539
|
+
apiParams extends Record<string, any> = never,
|
|
540
|
+
> = <StateKey extends keyof TransformedStateType<T>>(
|
|
540
541
|
stateKey: StateKey,
|
|
541
|
-
|
|
542
|
-
|
|
542
|
+
options?: Prettify<
|
|
543
|
+
OptionsType<TransformedStateType<T>[StateKey]> & { apiParams?: apiParams }
|
|
544
|
+
>
|
|
543
545
|
) => StateObject<TransformedStateType<T>[StateKey]>;
|
|
546
|
+
|
|
547
|
+
// Define the type for the options setter using the Transformed state
|
|
544
548
|
type SetCogsOptionsFunc<T extends Record<string, any>> = <
|
|
545
549
|
StateKey extends keyof TransformedStateType<T>,
|
|
546
550
|
>(
|
|
547
551
|
stateKey: StateKey,
|
|
548
552
|
options: OptionsType<TransformedStateType<T>[StateKey]>
|
|
549
553
|
) => void;
|
|
550
|
-
|
|
551
|
-
|
|
554
|
+
|
|
555
|
+
// Define the final API object shape
|
|
556
|
+
type CogsApi<
|
|
557
|
+
T extends Record<string, any>,
|
|
558
|
+
apiParams extends Record<string, any> = never,
|
|
559
|
+
> = {
|
|
560
|
+
useCogsState: UseCogsStateHook<T, apiParams>;
|
|
552
561
|
setCogsOptions: SetCogsOptionsFunc<T>;
|
|
553
562
|
};
|
|
554
563
|
|
|
555
|
-
export type CogsApi<TSchema extends { schemas: Record<string, any> }> = {
|
|
556
|
-
useCogsState: <TStateKey extends keyof TSchema['schemas']>(
|
|
557
|
-
stateKey: TStateKey,
|
|
558
|
-
options?: OptionsType<
|
|
559
|
-
TSchema['schemas'][TStateKey]['schemas']['defaultValues'],
|
|
560
|
-
TSchema['schemas'][TStateKey] extends {
|
|
561
|
-
// This checks for the params schema we create in `createSyncSchema`
|
|
562
|
-
apiParamsSchema: z.ZodObject<any, any>;
|
|
563
|
-
}
|
|
564
|
-
? z.infer<TSchema['schemas'][TStateKey]['apiParamsSchema']>
|
|
565
|
-
: never
|
|
566
|
-
>
|
|
567
|
-
) => StateObject<TSchema['schemas'][TStateKey]['schemas']['defaultValues']>;
|
|
568
|
-
|
|
569
|
-
// You also need the type for the other function
|
|
570
|
-
setCogsOptions: <TStateKey extends keyof TSchema['schemas']>(
|
|
571
|
-
stateKey: TStateKey,
|
|
572
|
-
options: OptionsType<
|
|
573
|
-
TSchema['schemas'][TStateKey]['schemas']['defaultValues']
|
|
574
|
-
>
|
|
575
|
-
) => void;
|
|
576
|
-
};
|
|
577
564
|
// Minimal change - just add a second parameter to detect sync schema
|
|
578
565
|
export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
579
566
|
initialState: State,
|
|
@@ -694,11 +681,21 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
694
681
|
notifyComponents(stateKey as string);
|
|
695
682
|
}
|
|
696
683
|
|
|
697
|
-
return { useCogsState, setCogsOptions } as
|
|
684
|
+
return { useCogsState, setCogsOptions } as CogsApi<State>;
|
|
698
685
|
};
|
|
699
686
|
export function createCogsStateFromSync<
|
|
700
|
-
|
|
701
|
-
|
|
687
|
+
TSyncSchema extends {
|
|
688
|
+
schemas: Record<
|
|
689
|
+
string,
|
|
690
|
+
{
|
|
691
|
+
schemas: { defaultValues: any };
|
|
692
|
+
apiParamsSchema?: z.ZodObject<any>;
|
|
693
|
+
[key: string]: any;
|
|
694
|
+
}
|
|
695
|
+
>;
|
|
696
|
+
notifications: Record<string, any>;
|
|
697
|
+
},
|
|
698
|
+
>(syncSchema: TSyncSchema) {
|
|
702
699
|
const schemas = syncSchema.schemas;
|
|
703
700
|
const initialState: any = {};
|
|
704
701
|
|
|
@@ -708,7 +705,24 @@ export function createCogsStateFromSync<
|
|
|
708
705
|
initialState[key] = entry?.schemas?.defaultValues || {};
|
|
709
706
|
}
|
|
710
707
|
|
|
711
|
-
|
|
708
|
+
// Create the base CogsApi
|
|
709
|
+
const baseApi = createCogsState(initialState, {
|
|
710
|
+
__fromSyncSchema: true,
|
|
711
|
+
__syncNotifications: syncSchema.notifications,
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
// Override the useCogsState function to handle apiParams properly
|
|
715
|
+
const useCogsState = <K extends keyof TSyncSchema['schemas']>(
|
|
716
|
+
stateKey: K,
|
|
717
|
+
options?: any // Just make this flexible
|
|
718
|
+
) => {
|
|
719
|
+
return baseApi.useCogsState(stateKey as any, options);
|
|
720
|
+
};
|
|
721
|
+
|
|
722
|
+
return {
|
|
723
|
+
useCogsState,
|
|
724
|
+
setCogsOptions: baseApi.setCogsOptions,
|
|
725
|
+
};
|
|
712
726
|
}
|
|
713
727
|
|
|
714
728
|
const {
|