cogsbox-state 0.5.445 → 0.5.447
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 +16 -18
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +34 -34
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 * as z4 from 'zod/v4';
|
|
40
|
+
import z, * 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,33 +534,46 @@ export function addStateOptions<T extends unknown>(
|
|
|
534
534
|
) {
|
|
535
535
|
return { initialState: initialState, formElements, validation } as T;
|
|
536
536
|
}
|
|
537
|
-
type
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
> = <StateKey extends keyof TransformedStateType<T>>(
|
|
537
|
+
type BasicUseCogsStateHook<T extends Record<string, any>> = <
|
|
538
|
+
StateKey extends keyof TransformedStateType<T>,
|
|
539
|
+
>(
|
|
541
540
|
stateKey: StateKey,
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
>
|
|
541
|
+
// This hook DOES NOT know about apiParams.
|
|
542
|
+
options?: Prettify<OptionsType<TransformedStateType<T>[StateKey]>>
|
|
545
543
|
) => StateObject<TransformedStateType<T>[StateKey]>;
|
|
546
|
-
|
|
547
|
-
// Define the type for the options setter using the Transformed state
|
|
548
544
|
type SetCogsOptionsFunc<T extends Record<string, any>> = <
|
|
549
545
|
StateKey extends keyof TransformedStateType<T>,
|
|
550
546
|
>(
|
|
551
547
|
stateKey: StateKey,
|
|
552
548
|
options: OptionsType<TransformedStateType<T>[StateKey]>
|
|
553
549
|
) => void;
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
type CogsApi<
|
|
557
|
-
T extends Record<string, any>,
|
|
558
|
-
apiParams extends Record<string, any> = never,
|
|
559
|
-
> = {
|
|
560
|
-
useCogsState: UseCogsStateHook<T, apiParams>;
|
|
550
|
+
type BasicCogsApi<T extends Record<string, any>> = {
|
|
551
|
+
useCogsState: BasicUseCogsStateHook<T>;
|
|
561
552
|
setCogsOptions: SetCogsOptionsFunc<T>;
|
|
562
553
|
};
|
|
563
554
|
|
|
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
|
+
};
|
|
564
577
|
// Minimal change - just add a second parameter to detect sync schema
|
|
565
578
|
export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
566
579
|
initialState: State,
|
|
@@ -681,24 +694,11 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
681
694
|
notifyComponents(stateKey as string);
|
|
682
695
|
}
|
|
683
696
|
|
|
684
|
-
return { useCogsState, setCogsOptions } as
|
|
697
|
+
return { useCogsState, setCogsOptions } as BasicCogsApi<State>;
|
|
685
698
|
};
|
|
686
699
|
export function createCogsStateFromSync<
|
|
687
|
-
|
|
688
|
-
|
|
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
|
-
}> {
|
|
700
|
+
TSchema extends { schemas: Record<string, any> }, // Use a simpler generic
|
|
701
|
+
>(syncSchema: TSchema): CogsApi<TSchema> {
|
|
702
702
|
const schemas = syncSchema.schemas;
|
|
703
703
|
const initialState: any = {};
|
|
704
704
|
|
|
@@ -708,7 +708,7 @@ export function createCogsStateFromSync<
|
|
|
708
708
|
initialState[key] = entry?.schemas?.defaultValues || {};
|
|
709
709
|
}
|
|
710
710
|
|
|
711
|
-
return createCogsState(initialState);
|
|
711
|
+
return createCogsState(initialState) as any;
|
|
712
712
|
}
|
|
713
713
|
|
|
714
714
|
const {
|