cogsbox-state 0.5.435 → 0.5.436

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.
@@ -1,8 +1,9 @@
1
1
  import { CSSProperties, RefObject } from 'react';
2
2
  import { GenericObject } from './utility.js';
3
- import { z } from 'zod';
4
- import { ComponentsType } from './store.js';
3
+ import { ValidationStatus, ComponentsType } from './store.js';
5
4
 
5
+ import * as z3 from 'zod/v3';
6
+ import * as z4 from 'zod/v4';
6
7
  type Prettify<T> = T extends any ? {
7
8
  [K in keyof T]: T[K];
8
9
  } : never;
@@ -101,11 +102,13 @@ export type FormOptsType = {
101
102
  validation?: {
102
103
  hideMessage?: boolean;
103
104
  message?: string;
104
- stretch?: boolean;
105
105
  props?: GenericObject;
106
106
  disable?: boolean;
107
107
  };
108
108
  debounceTime?: number;
109
+ sync?: {
110
+ allowInvalidValues?: boolean;
111
+ };
109
112
  };
110
113
  export type FormControl<T> = (obj: FormElementParams<T>) => JSX.Element;
111
114
  export type UpdateArg<S> = S | ((prevState: S) => S);
@@ -131,6 +134,7 @@ export type EndType<T, IsArrayElement = false> = {
131
134
  _stateKey: string;
132
135
  formElement: (control: FormControl<T>, opts?: FormOptsType) => JSX.Element;
133
136
  get: () => T;
137
+ getState: () => T;
134
138
  $get: () => T;
135
139
  $derive: <R>(fn: EffectFunction<T, R>) => R;
136
140
  _status: 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
@@ -190,11 +194,6 @@ export type UpdateTypeDetail = {
190
194
  };
191
195
  export type ReactivityUnion = 'none' | 'component' | 'deps' | 'all';
192
196
  export type ReactivityType = 'none' | 'component' | 'deps' | 'all' | Array<Prettify<'none' | 'component' | 'deps' | 'all'>>;
193
- type ValidationOptionsType = {
194
- key?: string;
195
- zodSchema?: z.ZodTypeAny;
196
- onBlur?: boolean;
197
- };
198
197
  type SyncApi = {
199
198
  updateState: (data: {
200
199
  operation: any;
@@ -203,6 +202,12 @@ type SyncApi = {
203
202
  clientId: string | null;
204
203
  subscribers: string[];
205
204
  };
205
+ type ValidationOptionsType = {
206
+ key?: string;
207
+ zodSchemaV3?: z3.ZodType<any, any, any>;
208
+ zodSchemaV4?: z4.ZodType<any, any, any>;
209
+ onBlur?: boolean;
210
+ };
206
211
  export type OptionsType<T extends unknown = unknown> = {
207
212
  log?: boolean;
208
213
  componentId?: string;
@@ -240,30 +245,28 @@ export type OptionsType<T extends unknown = unknown> = {
240
245
  key: string | ((state: T) => string);
241
246
  onChange?: (state: T) => void;
242
247
  };
243
- formElements?: FormsElementsType;
248
+ formElements?: FormsElementsType<T>;
244
249
  reactiveDeps?: (state: T) => any[] | true;
245
250
  reactiveType?: ReactivityType;
246
251
  syncUpdate?: Partial<UpdateTypeDetail>;
247
252
  defaultState?: T;
248
253
  dependencies?: any[];
249
254
  };
250
- export type ValidationWrapperOptions<T extends unknown = unknown> = {
251
- children: React.ReactNode;
252
- active: boolean;
253
- stretch?: boolean;
254
- path: string[];
255
- message?: string;
256
- data?: T;
257
- key?: string;
258
- };
259
255
  export type SyncRenderOptions<T extends unknown = unknown> = {
260
256
  children: React.ReactNode;
261
257
  time: number;
262
258
  data?: T;
263
259
  key?: string;
264
260
  };
265
- type FormsElementsType<T extends unknown = unknown> = {
266
- validation?: (options: ValidationWrapperOptions<T>) => React.ReactNode;
261
+ type FormsElementsType<T> = {
262
+ validation?: (options: {
263
+ children: React.ReactNode;
264
+ status: ValidationStatus;
265
+ path: string[];
266
+ message?: string;
267
+ data?: T;
268
+ key?: string;
269
+ }) => React.ReactNode;
267
270
  syncRender?: (options: SyncRenderOptions<T>) => React.ReactNode;
268
271
  };
269
272
  export type InitialStateInnerType<T extends unknown = unknown> = {
@@ -281,13 +284,16 @@ export type TransformedStateType<T> = {
281
284
  [P in keyof T]: T[P] extends CogsInitialState<infer U> ? U : T[P];
282
285
  };
283
286
  export declare function addStateOptions<T extends unknown>(initialState: T, { formElements, validation }: OptionsType<T>): T;
287
+ type UseCogsStateHook<T extends Record<string, any>> = <StateKey extends keyof TransformedStateType<T>>(stateKey: StateKey, options?: Prettify<OptionsType<TransformedStateType<T>[StateKey]>>) => StateObject<TransformedStateType<T>[StateKey]>;
288
+ type SetCogsOptionsFunc<T extends Record<string, any>> = <StateKey extends keyof TransformedStateType<T>>(stateKey: StateKey, options: OptionsType<TransformedStateType<T>[StateKey]>) => void;
289
+ type CogsApi<T extends Record<string, any>> = {
290
+ useCogsState: UseCogsStateHook<T>;
291
+ setCogsOptions: SetCogsOptionsFunc<T>;
292
+ };
284
293
  export declare const createCogsState: <State extends Record<StateKeys, unknown>>(initialState: State, opt?: {
285
- formElements?: FormsElementsType;
294
+ formElements?: FormsElementsType<State>;
286
295
  validation?: ValidationOptionsType;
287
- }) => {
288
- useCogsState: <StateKey extends keyof State>(stateKey: StateKey, options?: OptionsType<TransformedStateType<State>[StateKey]>) => StateObject<TransformedStateType<State>[StateKey]>;
289
- setCogsOptions: <StateKey extends keyof State>(stateKey: StateKey, options: OptionsType<TransformedStateType<State>[StateKey]>) => void;
290
- };
296
+ }) => CogsApi<State>;
291
297
  type LocalStorageData<T> = {
292
298
  state: T;
293
299
  lastUpdated: number;