cogsbox-state 0.5.486 → 0.5.487
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/README.md +70 -7
- package/dist/CogsState.d.ts +19 -0
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.js +381 -355
- package/dist/CogsState.js.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +82 -17
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -58,11 +58,26 @@ import { ZodType } from 'zod/v4';
|
|
|
58
58
|
|
|
59
59
|
export type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
|
|
60
60
|
|
|
61
|
-
export type SyncInfo = {
|
|
62
|
-
timeStamp: number;
|
|
63
|
-
userId: number;
|
|
64
|
-
};
|
|
65
|
-
|
|
61
|
+
export type SyncInfo = {
|
|
62
|
+
timeStamp: number;
|
|
63
|
+
userId: number;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type ValidationFieldSummary = {
|
|
67
|
+
status: ValidationStatus;
|
|
68
|
+
severity: ValidationSeverity;
|
|
69
|
+
hasErrors: boolean;
|
|
70
|
+
hasWarnings: boolean;
|
|
71
|
+
message: string;
|
|
72
|
+
errors: string[];
|
|
73
|
+
warnings: string[];
|
|
74
|
+
allErrors: Array<ValidationError & { path: string[] }>;
|
|
75
|
+
path: string[];
|
|
76
|
+
getData: () => unknown;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
type ValidationSummaryArray = ValidationFieldSummary[];
|
|
80
|
+
|
|
66
81
|
export type FormElementParams<T> = StateObject<T> & {
|
|
67
82
|
$inputProps: {
|
|
68
83
|
ref?: RefObject<any>;
|
|
@@ -193,8 +208,14 @@ export type EndType<
|
|
|
193
208
|
$$derive: <R>(fn: EffectFunction<T, R>) => R;
|
|
194
209
|
$_status: 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
|
|
195
210
|
$getStatus: () => 'fresh' | 'dirty' | 'synced' | 'restored' | 'unknown';
|
|
196
|
-
$showValidationErrors: () => string[];
|
|
197
|
-
$
|
|
211
|
+
$showValidationErrors: () => string[];
|
|
212
|
+
$validationErrors: {
|
|
213
|
+
(): ValidationSummaryArray;
|
|
214
|
+
<K extends Extract<keyof NonNullable<T>, string>>(
|
|
215
|
+
keys: readonly K[]
|
|
216
|
+
): ValidationSummaryArray;
|
|
217
|
+
};
|
|
218
|
+
$setValidation: (ctx: string) => void;
|
|
198
219
|
$removeValidation: (ctx: string) => void;
|
|
199
220
|
$isSelected: boolean;
|
|
200
221
|
$setSelected: (value: boolean) => void;
|
|
@@ -2623,11 +2644,11 @@ function createProxyHandler<
|
|
|
2623
2644
|
return result;
|
|
2624
2645
|
};
|
|
2625
2646
|
}
|
|
2626
|
-
if (prop === '$showValidationErrors') {
|
|
2627
|
-
return () => {
|
|
2628
|
-
const { shadowMeta } = getScopedData(stateKey, path, meta);
|
|
2629
|
-
if (
|
|
2630
|
-
shadowMeta?.validation?.status === 'INVALID' &&
|
|
2647
|
+
if (prop === '$showValidationErrors') {
|
|
2648
|
+
return () => {
|
|
2649
|
+
const { shadowMeta } = getScopedData(stateKey, path, meta);
|
|
2650
|
+
if (
|
|
2651
|
+
shadowMeta?.validation?.status === 'INVALID' &&
|
|
2631
2652
|
shadowMeta.validation.errors.length > 0
|
|
2632
2653
|
) {
|
|
2633
2654
|
// Return only error-severity messages (not warnings)
|
|
@@ -2635,11 +2656,55 @@ function createProxyHandler<
|
|
|
2635
2656
|
.filter((err) => err.severity === 'error')
|
|
2636
2657
|
.map((err) => err.message);
|
|
2637
2658
|
}
|
|
2638
|
-
return [];
|
|
2639
|
-
};
|
|
2640
|
-
}
|
|
2641
|
-
|
|
2642
|
-
|
|
2659
|
+
return [];
|
|
2660
|
+
};
|
|
2661
|
+
}
|
|
2662
|
+
if (prop === '$validationErrors') {
|
|
2663
|
+
return (keys?: readonly string[]) => {
|
|
2664
|
+
const store = getGlobalStore.getState();
|
|
2665
|
+
const summarizePath = (targetPath: string[]) => {
|
|
2666
|
+
const validationState =
|
|
2667
|
+
store.getShadowMetadata(stateKey, targetPath)?.validation;
|
|
2668
|
+
const allErrors = (validationState?.errors || []).map((err) => ({
|
|
2669
|
+
...err,
|
|
2670
|
+
path: targetPath,
|
|
2671
|
+
}));
|
|
2672
|
+
const errors = allErrors.filter((err) => err.severity === 'error');
|
|
2673
|
+
const warnings = allErrors.filter(
|
|
2674
|
+
(err) => err.severity === 'warning'
|
|
2675
|
+
);
|
|
2676
|
+
const severity: ValidationSeverity =
|
|
2677
|
+
errors.length > 0
|
|
2678
|
+
? 'error'
|
|
2679
|
+
: warnings.length > 0
|
|
2680
|
+
? 'warning'
|
|
2681
|
+
: undefined;
|
|
2682
|
+
|
|
2683
|
+
return {
|
|
2684
|
+
status: validationState?.status || 'NOT_VALIDATED',
|
|
2685
|
+
severity,
|
|
2686
|
+
hasErrors: errors.length > 0,
|
|
2687
|
+
hasWarnings: warnings.length > 0,
|
|
2688
|
+
message: errors[0]?.message || warnings[0]?.message || '',
|
|
2689
|
+
errors: errors.map((err) => err.message),
|
|
2690
|
+
warnings: warnings.map((err) => err.message),
|
|
2691
|
+
allErrors,
|
|
2692
|
+
path: targetPath,
|
|
2693
|
+
getData: () => store.getShadowValue(stateKey, targetPath),
|
|
2694
|
+
} satisfies ValidationFieldSummary;
|
|
2695
|
+
};
|
|
2696
|
+
|
|
2697
|
+
const childKeys =
|
|
2698
|
+
keys ??
|
|
2699
|
+
Object.keys(store.getShadowNode(stateKey, path) ?? {}).filter(
|
|
2700
|
+
(key) => key !== '_meta'
|
|
2701
|
+
);
|
|
2702
|
+
|
|
2703
|
+
return childKeys.map((key) => summarizePath([...path, key]));
|
|
2704
|
+
};
|
|
2705
|
+
}
|
|
2706
|
+
|
|
2707
|
+
if (prop === '$getSelected') {
|
|
2643
2708
|
return () => {
|
|
2644
2709
|
const arrayKey = [stateKey, ...path].join('.');
|
|
2645
2710
|
registerComponentDependency(stateKey, componentId, [
|