cogsbox-state 0.5.467 → 0.5.469

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.
@@ -8,7 +8,12 @@ import React, {
8
8
  useRef,
9
9
  useState,
10
10
  } from 'react';
11
- import { formRefStore, getGlobalStore, ValidationError } from './store';
11
+ import {
12
+ formRefStore,
13
+ getGlobalStore,
14
+ ValidationError,
15
+ ValidationSeverity,
16
+ } from './store';
12
17
  import { useInView } from 'react-intersection-observer';
13
18
  import { v4 as uuidv4 } from 'uuid';
14
19
  import { isDeepEqual } from './utility';
@@ -60,7 +65,12 @@ export function ValidationWrapper({
60
65
 
61
66
  // Use first error, or first warning if no errors
62
67
  const message = errorMessages[0] || warningMessages[0];
63
-
68
+ const primarySeverity: ValidationSeverity =
69
+ errorMessages.length > 0
70
+ ? 'error'
71
+ : warningMessages.length > 0
72
+ ? 'warning'
73
+ : undefined;
64
74
  return (
65
75
  <>
66
76
  {thisStateOpts?.formElements?.validation &&
@@ -73,7 +83,7 @@ export function ValidationWrapper({
73
83
  message: formOpts?.validation?.hideMessage
74
84
  ? ''
75
85
  : formOpts?.validation?.message || message || '',
76
-
86
+ severity: primarySeverity,
77
87
  hasErrors: errorMessages.length > 0,
78
88
  hasWarnings: warningMessages.length > 0,
79
89
  allErrors: errors,
@@ -445,6 +455,7 @@ export function FormElementWrapper({
445
455
  const baseState = rebuildStateShape({
446
456
  path: path,
447
457
  componentId: componentId,
458
+ meta: undefined,
448
459
  });
449
460
 
450
461
  const stateWithInputProps = new Proxy(baseState, {
@@ -539,3 +550,44 @@ const useImageLoaded = (ref: RefObject<HTMLElement>): boolean => {
539
550
 
540
551
  return loaded;
541
552
  };
553
+ // Components.tsx
554
+
555
+ // Generic isolated component wrapper
556
+ export function IsolatedComponentWrapper({
557
+ stateKey,
558
+ path,
559
+ rebuildStateShape,
560
+ renderFn,
561
+ }: {
562
+ stateKey: string;
563
+ path: string[];
564
+ rebuildStateShape: (options: {
565
+ path: string[];
566
+ componentId: string;
567
+ meta?: any;
568
+ }) => any;
569
+ renderFn: (state: any) => React.ReactNode;
570
+ }) {
571
+ const [componentId] = useState(() => uuidv4());
572
+ const [, forceUpdate] = useState({});
573
+
574
+ const stateKeyPathKey = [stateKey, ...path].join('.');
575
+ useRegisterComponent(stateKey, componentId, forceUpdate);
576
+
577
+ useEffect(() => {
578
+ const unsubscribe = getGlobalStore
579
+ .getState()
580
+ .subscribeToPath(stateKeyPathKey, () => {
581
+ forceUpdate({});
582
+ });
583
+ return () => unsubscribe();
584
+ }, [stateKeyPathKey]);
585
+
586
+ const baseState = rebuildStateShape({
587
+ path: path,
588
+ componentId: componentId,
589
+ meta: undefined,
590
+ });
591
+ console.log('baseState', baseState?.$get());
592
+ return <>{renderFn(baseState)}</>;
593
+ }
package/src/store.ts CHANGED
@@ -85,11 +85,11 @@ export type ValidationStatus =
85
85
  | 'VALIDATING'
86
86
  | 'VALID'
87
87
  | 'INVALID';
88
-
88
+ export type ValidationSeverity = 'warning' | 'error' | undefined;
89
89
  export type ValidationError = {
90
90
  source: 'client' | 'sync_engine' | 'api';
91
91
  message: string;
92
- severity: 'warning' | 'error';
92
+ severity: ValidationSeverity;
93
93
  code?: string;
94
94
  };
95
95