cogsbox-state 0.5.434 → 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);
@@ -113,7 +116,9 @@ export type InsertParams<S> = S | ((prevState: {
113
116
  state: S;
114
117
  uuid: string;
115
118
  }) => S);
116
- export type UpdateType<T> = (payload: UpdateArg<T>) => void;
119
+ export type UpdateType<T> = (payload: UpdateArg<T>) => {
120
+ synced: () => void;
121
+ };
117
122
  export type InsertType<T> = (payload: InsertParams<T>, index?: number) => void;
118
123
  export type InsertTypeObj<T> = (payload: InsertParams<T>) => void;
119
124
  export type ValidationError = {
@@ -129,6 +134,7 @@ export type EndType<T, IsArrayElement = false> = {
129
134
  _stateKey: string;
130
135
  formElement: (control: FormControl<T>, opts?: FormOptsType) => JSX.Element;
131
136
  get: () => T;
137
+ getState: () => T;
132
138
  $get: () => T;
133
139
  $derive: <R>(fn: EffectFunction<T, R>) => R;
134
140
  _status: 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
@@ -188,15 +194,24 @@ export type UpdateTypeDetail = {
188
194
  };
189
195
  export type ReactivityUnion = 'none' | 'component' | 'deps' | 'all';
190
196
  export type ReactivityType = 'none' | 'component' | 'deps' | 'all' | Array<Prettify<'none' | 'component' | 'deps' | 'all'>>;
197
+ type SyncApi = {
198
+ updateState: (data: {
199
+ operation: any;
200
+ }) => void;
201
+ connected: boolean;
202
+ clientId: string | null;
203
+ subscribers: string[];
204
+ };
191
205
  type ValidationOptionsType = {
192
206
  key?: string;
193
- zodSchema?: z.ZodTypeAny;
207
+ zodSchemaV3?: z3.ZodType<any, any, any>;
208
+ zodSchemaV4?: z4.ZodType<any, any, any>;
194
209
  onBlur?: boolean;
195
210
  };
196
211
  export type OptionsType<T extends unknown = unknown> = {
197
212
  log?: boolean;
198
213
  componentId?: string;
199
- serverSync?: ServerSyncType<T>;
214
+ cogsSync?: (stateObject: StateObject<T>) => SyncApi;
200
215
  validation?: ValidationOptionsType;
201
216
  serverState?: {
202
217
  id?: string | number;
@@ -230,48 +245,28 @@ export type OptionsType<T extends unknown = unknown> = {
230
245
  key: string | ((state: T) => string);
231
246
  onChange?: (state: T) => void;
232
247
  };
233
- formElements?: FormsElementsType;
248
+ formElements?: FormsElementsType<T>;
234
249
  reactiveDeps?: (state: T) => any[] | true;
235
250
  reactiveType?: ReactivityType;
236
251
  syncUpdate?: Partial<UpdateTypeDetail>;
237
252
  defaultState?: T;
238
253
  dependencies?: any[];
239
254
  };
240
- export type ServerSyncType<T> = {
241
- testKey?: string;
242
- syncKey: (({ state }: {
243
- state: T;
244
- }) => string) | string;
245
- syncFunction: ({ state }: {
246
- state: T;
247
- }) => void;
248
- debounce?: number;
249
- snapshot?: {
250
- name: (({ state }: {
251
- state: T;
252
- }) => string) | string;
253
- stateKeys: StateKeys[];
254
- currentUrl: string;
255
- currentParams?: URLSearchParams;
256
- };
257
- };
258
- export type ValidationWrapperOptions<T extends unknown = unknown> = {
259
- children: React.ReactNode;
260
- active: boolean;
261
- stretch?: boolean;
262
- path: string[];
263
- message?: string;
264
- data?: T;
265
- key?: string;
266
- };
267
255
  export type SyncRenderOptions<T extends unknown = unknown> = {
268
256
  children: React.ReactNode;
269
257
  time: number;
270
258
  data?: T;
271
259
  key?: string;
272
260
  };
273
- type FormsElementsType<T extends unknown = unknown> = {
274
- 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;
275
270
  syncRender?: (options: SyncRenderOptions<T>) => React.ReactNode;
276
271
  };
277
272
  export type InitialStateInnerType<T extends unknown = unknown> = {
@@ -289,13 +284,16 @@ export type TransformedStateType<T> = {
289
284
  [P in keyof T]: T[P] extends CogsInitialState<infer U> ? U : T[P];
290
285
  };
291
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
+ };
292
293
  export declare const createCogsState: <State extends Record<StateKeys, unknown>>(initialState: State, opt?: {
293
- formElements?: FormsElementsType;
294
+ formElements?: FormsElementsType<State>;
294
295
  validation?: ValidationOptionsType;
295
- }) => {
296
- useCogsState: <StateKey extends keyof State>(stateKey: StateKey, options?: OptionsType<TransformedStateType<State>[StateKey]>) => StateObject<TransformedStateType<State>[StateKey]>;
297
- setCogsOptions: <StateKey extends keyof State>(stateKey: StateKey, options: OptionsType<TransformedStateType<State>[StateKey]>) => void;
298
- };
296
+ }) => CogsApi<State>;
299
297
  type LocalStorageData<T> = {
300
298
  state: T;
301
299
  lastUpdated: number;
@@ -304,7 +302,7 @@ type LocalStorageData<T> = {
304
302
  stateSource?: 'default' | 'server' | 'localStorage';
305
303
  };
306
304
  export declare const notifyComponent: (stateKey: string, componentId: string) => void;
307
- export declare function useCogsStateFn<TStateObject extends unknown>(stateObject: TStateObject, { stateKey, serverSync, localStorage, formElements, reactiveDeps, reactiveType, componentId, defaultState, syncUpdate, dependencies, serverState, }?: {
305
+ export declare function useCogsStateFn<TStateObject extends unknown>(stateObject: TStateObject, { stateKey, localStorage, formElements, reactiveDeps, reactiveType, componentId, defaultState, syncUpdate, dependencies, serverState, }?: {
308
306
  stateKey?: string;
309
307
  componentId?: string;
310
308
  defaultState?: TStateObject;