@wizzard-packages/react 0.3.0 → 0.4.0

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/index.d.cts CHANGED
@@ -54,16 +54,17 @@ declare function createWizardFactory<TSchema extends Record<string, any>, StepId
54
54
  useWizardContext: () => any;
55
55
  useWizardValue: <P extends Path<TSchema>>(path: P, options?: {
56
56
  isEqual?: (a: PathValue<TSchema, P>, b: PathValue<TSchema, P>) => boolean;
57
- }) => PathValue<TSchema, P>;
57
+ } | ((a: PathValue<TSchema, P>, b: PathValue<TSchema, P>) => boolean)) => PathValue<TSchema, P>;
58
58
  useWizardField: <P extends Path<TSchema>>(path: P, options?: {
59
59
  isEqual?: (a: PathValue<TSchema, P>, b: PathValue<TSchema, P>) => boolean;
60
- }) => [PathValue<TSchema, P>, (value: PathValue<TSchema, P>) => void];
61
- useWizardSelector: <TSelected>(selector: (state: IWizardContext$1<TSchema, StepId>) => TSelected, options?: {
60
+ } | ((a: PathValue<TSchema, P>, b: PathValue<TSchema, P>) => boolean)) => [PathValue<TSchema, P>, (value: PathValue<TSchema, P>) => void];
61
+ useWizardSelector: <TSelected>(selector: (state: IWizardState<TSchema, StepId>) => TSelected, options?: {
62
62
  isEqual?: (a: TSelected, b: TSelected) => boolean;
63
- }) => TSelected;
63
+ } | ((a: TSelected, b: TSelected) => boolean)) => TSelected;
64
+ useWizardShallowSelector: <TSelected>(selector: (state: IWizardState<TSchema, StepId>) => TSelected) => TSelected;
64
65
  useWizardError: <P extends Path<TSchema>>(path: P) => string | undefined;
65
66
  useWizardActions: () => IWizardActionsTyped<TSchema, StepId>;
66
- useWizardState: () => _wizzard_packages_core.IWizardState<TSchema, StepId>;
67
+ useWizardState: () => IWizardState<TSchema, StepId>;
67
68
  useBreadcrumbs: () => _wizzard_packages_core.IBreadcrumb<StepId>[];
68
69
  createStep: (config: IStepConfig<TSchema, StepId>) => IStepConfig<TSchema, StepId>;
69
70
  };
@@ -90,7 +91,7 @@ declare function useWizardState<T = unknown, StepId extends string = string>():
90
91
  */
91
92
  declare function useWizardValue<TValue = any>(path: string, options?: {
92
93
  isEqual?: (a: TValue, b: TValue) => boolean;
93
- }): TValue;
94
+ } | ((a: TValue, b: TValue) => boolean)): TValue;
94
95
  /**
95
96
  * Returns a value and setter for a path (useState-like API).
96
97
  */
@@ -106,7 +107,7 @@ declare function useWizardError(path: string): string | undefined;
106
107
  */
107
108
  declare function useWizardSelector<TSelected = any>(selector: (state: any) => TSelected, options?: {
108
109
  isEqual?: (a: TSelected, b: TSelected) => boolean;
109
- }): TSelected;
110
+ } | ((a: TSelected, b: TSelected) => boolean)): TSelected;
110
111
  /**
111
112
  * Returns the wizard actions API.
112
113
  */
@@ -189,7 +190,7 @@ declare const useWizardStoreState: <T, StepId extends string = string>(store: IW
189
190
  */
190
191
  declare const useWizardStoreValue: <T, P extends Path<T>>(store: IWizardStore<T, any>, path: P, options?: {
191
192
  isEqual?: (a: PathValue<T, P>, b: PathValue<T, P>) => boolean;
192
- }) => PathValue<T, P>;
193
+ } | ((a: PathValue<T, P>, b: PathValue<T, P>) => boolean)) => PathValue<T, P>;
193
194
  /**
194
195
  * Hook: value + setter for a path without React Context.
195
196
  */
@@ -205,7 +206,7 @@ declare const useWizardStoreError: (store: IWizardStore<any, any>, path: string)
205
206
  */
206
207
  declare const useWizardStoreSelector: <TSelected>(store: IWizardStore<any, any>, selector: (state: any) => TSelected, options?: {
207
208
  isEqual?: (a: TSelected, b: TSelected) => boolean;
208
- }) => TSelected;
209
+ } | ((a: TSelected, b: TSelected) => boolean)) => TSelected;
209
210
  /**
210
211
  * Helper: build store-bound hooks for a single store instance.
211
212
  */
package/dist/index.d.ts CHANGED
@@ -54,16 +54,17 @@ declare function createWizardFactory<TSchema extends Record<string, any>, StepId
54
54
  useWizardContext: () => any;
55
55
  useWizardValue: <P extends Path<TSchema>>(path: P, options?: {
56
56
  isEqual?: (a: PathValue<TSchema, P>, b: PathValue<TSchema, P>) => boolean;
57
- }) => PathValue<TSchema, P>;
57
+ } | ((a: PathValue<TSchema, P>, b: PathValue<TSchema, P>) => boolean)) => PathValue<TSchema, P>;
58
58
  useWizardField: <P extends Path<TSchema>>(path: P, options?: {
59
59
  isEqual?: (a: PathValue<TSchema, P>, b: PathValue<TSchema, P>) => boolean;
60
- }) => [PathValue<TSchema, P>, (value: PathValue<TSchema, P>) => void];
61
- useWizardSelector: <TSelected>(selector: (state: IWizardContext$1<TSchema, StepId>) => TSelected, options?: {
60
+ } | ((a: PathValue<TSchema, P>, b: PathValue<TSchema, P>) => boolean)) => [PathValue<TSchema, P>, (value: PathValue<TSchema, P>) => void];
61
+ useWizardSelector: <TSelected>(selector: (state: IWizardState<TSchema, StepId>) => TSelected, options?: {
62
62
  isEqual?: (a: TSelected, b: TSelected) => boolean;
63
- }) => TSelected;
63
+ } | ((a: TSelected, b: TSelected) => boolean)) => TSelected;
64
+ useWizardShallowSelector: <TSelected>(selector: (state: IWizardState<TSchema, StepId>) => TSelected) => TSelected;
64
65
  useWizardError: <P extends Path<TSchema>>(path: P) => string | undefined;
65
66
  useWizardActions: () => IWizardActionsTyped<TSchema, StepId>;
66
- useWizardState: () => _wizzard_packages_core.IWizardState<TSchema, StepId>;
67
+ useWizardState: () => IWizardState<TSchema, StepId>;
67
68
  useBreadcrumbs: () => _wizzard_packages_core.IBreadcrumb<StepId>[];
68
69
  createStep: (config: IStepConfig<TSchema, StepId>) => IStepConfig<TSchema, StepId>;
69
70
  };
@@ -90,7 +91,7 @@ declare function useWizardState<T = unknown, StepId extends string = string>():
90
91
  */
91
92
  declare function useWizardValue<TValue = any>(path: string, options?: {
92
93
  isEqual?: (a: TValue, b: TValue) => boolean;
93
- }): TValue;
94
+ } | ((a: TValue, b: TValue) => boolean)): TValue;
94
95
  /**
95
96
  * Returns a value and setter for a path (useState-like API).
96
97
  */
@@ -106,7 +107,7 @@ declare function useWizardError(path: string): string | undefined;
106
107
  */
107
108
  declare function useWizardSelector<TSelected = any>(selector: (state: any) => TSelected, options?: {
108
109
  isEqual?: (a: TSelected, b: TSelected) => boolean;
109
- }): TSelected;
110
+ } | ((a: TSelected, b: TSelected) => boolean)): TSelected;
110
111
  /**
111
112
  * Returns the wizard actions API.
112
113
  */
@@ -189,7 +190,7 @@ declare const useWizardStoreState: <T, StepId extends string = string>(store: IW
189
190
  */
190
191
  declare const useWizardStoreValue: <T, P extends Path<T>>(store: IWizardStore<T, any>, path: P, options?: {
191
192
  isEqual?: (a: PathValue<T, P>, b: PathValue<T, P>) => boolean;
192
- }) => PathValue<T, P>;
193
+ } | ((a: PathValue<T, P>, b: PathValue<T, P>) => boolean)) => PathValue<T, P>;
193
194
  /**
194
195
  * Hook: value + setter for a path without React Context.
195
196
  */
@@ -205,7 +206,7 @@ declare const useWizardStoreError: (store: IWizardStore<any, any>, path: string)
205
206
  */
206
207
  declare const useWizardStoreSelector: <TSelected>(store: IWizardStore<any, any>, selector: (state: any) => TSelected, options?: {
207
208
  isEqual?: (a: TSelected, b: TSelected) => boolean;
208
- }) => TSelected;
209
+ } | ((a: TSelected, b: TSelected) => boolean)) => TSelected;
209
210
  /**
210
211
  * Helper: build store-bound hooks for a single store instance.
211
212
  */
package/dist/index.js CHANGED
@@ -1,20 +1,25 @@
1
+ // src/factory.tsx
2
+ import {
3
+ shallowEqual as shallowEqual2
4
+ } from "@wizzard-packages/core";
5
+
1
6
  // src/context/WizardContext.tsx
7
+ import {
8
+ WizardStore,
9
+ getByPath as getByPath2,
10
+ setByPath as setByPath2
11
+ } from "@wizzard-packages/core";
12
+ import { MemoryAdapter } from "@wizzard-packages/persistence";
2
13
  import {
3
14
  createContext,
15
+ useCallback,
4
16
  useContext,
5
17
  useEffect,
6
18
  useMemo,
19
+ useRef,
7
20
  useState,
8
- useCallback,
9
- useSyncExternalStore,
10
- useRef
21
+ useSyncExternalStore
11
22
  } from "react";
12
- import {
13
- WizardStore,
14
- getByPath as getByPath2,
15
- setByPath as setByPath2
16
- } from "@wizzard-packages/core";
17
- import { MemoryAdapter } from "@wizzard-packages/persistence";
18
23
 
19
24
  // src/internal/dependencies.ts
20
25
  import {
@@ -510,22 +515,25 @@ function useWizardState() {
510
515
  function useWizardValue(path, options) {
511
516
  const store = useContext(WizardStoreContext);
512
517
  if (!store) throw new Error("useWizardValue must be used within a WizardProvider");
513
- const lastStateRef = useRef(null);
518
+ const lastStateRef = useRef(UNSET);
514
519
  const lastValueRef = useRef(UNSET);
520
+ const isEqual = typeof options === "function" ? options : options?.isEqual;
521
+ const isEqualRef = useRef(isEqual);
522
+ isEqualRef.current = isEqual;
515
523
  const getSnapshot = useCallback(() => {
516
524
  const data = store.getSnapshot().data;
517
525
  if (data === lastStateRef.current && lastValueRef.current !== UNSET) {
518
526
  return lastValueRef.current;
519
527
  }
520
528
  const value = getByPath2(data, path);
521
- if (lastValueRef.current !== UNSET && (options?.isEqual || Object.is)(lastValueRef.current, value)) {
529
+ if (lastValueRef.current !== UNSET && (isEqualRef.current || Object.is)(lastValueRef.current, value)) {
522
530
  lastStateRef.current = data;
523
531
  return lastValueRef.current;
524
532
  }
525
533
  lastStateRef.current = data;
526
534
  lastValueRef.current = value;
527
535
  return value;
528
- }, [store, path, options?.isEqual]);
536
+ }, [store, path]);
529
537
  return useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);
530
538
  }
531
539
  function useWizardField(path, options) {
@@ -555,16 +563,27 @@ function useWizardError(path) {
555
563
  function useWizardSelector(selector, options) {
556
564
  const store = useContext(WizardStoreContext);
557
565
  if (!store) throw new Error("useWizardSelector must be used within a WizardProvider");
566
+ const selectorRef = useRef(selector);
567
+ selectorRef.current = selector;
568
+ const isEqual = typeof options === "function" ? options : options?.isEqual;
569
+ const isEqualRef = useRef(isEqual);
570
+ isEqualRef.current = isEqual;
571
+ const lastStateRef = useRef(UNSET);
558
572
  const lastResultRef = useRef(UNSET);
559
573
  const getSnapshot = useCallback(() => {
560
574
  const full = store.getSnapshot();
561
- const res = selector(full);
562
- if (lastResultRef.current !== UNSET && (options?.isEqual || Object.is)(lastResultRef.current, res)) {
575
+ if (full === lastStateRef.current && lastResultRef.current !== UNSET) {
576
+ return lastResultRef.current;
577
+ }
578
+ const res = selectorRef.current(full);
579
+ if (lastResultRef.current !== UNSET && (isEqualRef.current || Object.is)(lastResultRef.current, res)) {
580
+ lastStateRef.current = full;
563
581
  return lastResultRef.current;
564
582
  }
583
+ lastStateRef.current = full;
565
584
  lastResultRef.current = res;
566
585
  return res;
567
- }, [store, selector, options?.isEqual]);
586
+ }, [store]);
568
587
  return useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);
569
588
  }
570
589
  function useWizardActions() {
@@ -678,6 +697,9 @@ function createWizardFactory() {
678
697
  const useWizardSelector2 = (selector, options) => {
679
698
  return useWizardSelector(selector, options);
680
699
  };
700
+ const useWizardShallowSelector = (selector) => {
701
+ return useWizardSelector2(selector, shallowEqual2);
702
+ };
681
703
  const useWizardError2 = (path) => {
682
704
  return useWizardError(path);
683
705
  };
@@ -698,6 +720,7 @@ function createWizardFactory() {
698
720
  useWizardValue: useWizardValue2,
699
721
  useWizardField: useWizardField2,
700
722
  useWizardSelector: useWizardSelector2,
723
+ useWizardShallowSelector,
701
724
  useWizardError: useWizardError2,
702
725
  useWizardActions: useWizardActions2,
703
726
  useWizardState: useWizardState2,
@@ -729,13 +752,13 @@ var WizardStepRenderer = ({
729
752
  };
730
753
 
731
754
  // src/store.ts
732
- import { useCallback as useCallback2, useRef as useRef2, useSyncExternalStore as useSyncExternalStore2 } from "react";
733
755
  import {
734
756
  WizardStore as WizardStore2,
735
757
  getByPath as getByPath3,
736
758
  setByPath as setByPath3
737
759
  } from "@wizzard-packages/core";
738
760
  import { MemoryAdapter as MemoryAdapter2 } from "@wizzard-packages/persistence";
761
+ import { useCallback as useCallback2, useRef as useRef2, useSyncExternalStore as useSyncExternalStore2 } from "react";
739
762
  var UNSET2 = /* @__PURE__ */ Symbol("wizard_store_unset");
740
763
  var META_KEY = "__wizzard_meta__";
741
764
  var applyStoredMeta = (store, adapter) => {
@@ -1037,20 +1060,23 @@ var useWizardStoreState = (store) => {
1037
1060
  var useWizardStoreValue = (store, path, options) => {
1038
1061
  const lastStateRef = useRef2(UNSET2);
1039
1062
  const lastValueRef = useRef2(UNSET2);
1063
+ const isEqual = typeof options === "function" ? options : options?.isEqual;
1064
+ const isEqualRef = useRef2(isEqual);
1065
+ isEqualRef.current = isEqual;
1040
1066
  const getSnapshot = useCallback2(() => {
1041
1067
  const data = store.getSnapshot().data;
1042
1068
  if (data === lastStateRef.current && lastValueRef.current !== UNSET2) {
1043
1069
  return lastValueRef.current;
1044
1070
  }
1045
1071
  const value = getByPath3(data, path);
1046
- if (lastValueRef.current !== UNSET2 && (options?.isEqual || Object.is)(lastValueRef.current, value)) {
1072
+ if (lastValueRef.current !== UNSET2 && (isEqualRef.current || Object.is)(lastValueRef.current, value)) {
1047
1073
  lastStateRef.current = data;
1048
1074
  return lastValueRef.current;
1049
1075
  }
1050
1076
  lastStateRef.current = data;
1051
1077
  lastValueRef.current = value;
1052
1078
  return value;
1053
- }, [store, path, options?.isEqual]);
1079
+ }, [store, path]);
1054
1080
  return useSyncExternalStore2(store.subscribe, getSnapshot, getSnapshot);
1055
1081
  };
1056
1082
  var useWizardStoreField = (store, setData, path, options) => {
@@ -1075,16 +1101,27 @@ var useWizardStoreError = (store, path) => {
1075
1101
  return useSyncExternalStore2(store.subscribe, getSnapshot, getSnapshot);
1076
1102
  };
1077
1103
  var useWizardStoreSelector = (store, selector, options) => {
1104
+ const selectorRef = useRef2(selector);
1105
+ selectorRef.current = selector;
1106
+ const isEqual = typeof options === "function" ? options : options?.isEqual;
1107
+ const isEqualRef = useRef2(isEqual);
1108
+ isEqualRef.current = isEqual;
1109
+ const lastStateRef = useRef2(UNSET2);
1078
1110
  const lastResultRef = useRef2(UNSET2);
1079
1111
  const getSnapshot = useCallback2(() => {
1080
1112
  const full = store.getSnapshot();
1081
- const res = selector(full);
1082
- if (lastResultRef.current !== UNSET2 && (options?.isEqual || Object.is)(lastResultRef.current, res)) {
1113
+ if (full === lastStateRef.current && lastResultRef.current !== UNSET2) {
1114
+ return lastResultRef.current;
1115
+ }
1116
+ const res = selectorRef.current(full);
1117
+ if (lastResultRef.current !== UNSET2 && (isEqualRef.current || Object.is)(lastResultRef.current, res)) {
1118
+ lastStateRef.current = full;
1083
1119
  return lastResultRef.current;
1084
1120
  }
1121
+ lastStateRef.current = full;
1085
1122
  lastResultRef.current = res;
1086
1123
  return res;
1087
- }, [store, selector, options?.isEqual]);
1124
+ }, [store]);
1088
1125
  return useSyncExternalStore2(store.subscribe, getSnapshot, getSnapshot);
1089
1126
  };
1090
1127
  var createWizardHooks = (store, actions) => ({