@json-render/react-native 0.8.0 → 0.9.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/README.md CHANGED
@@ -214,6 +214,26 @@ const systemPrompt = catalog.prompt({
214
214
  });
215
215
  ```
216
216
 
217
+ ## External Store (Controlled Mode)
218
+
219
+ For full control over state, pass a `StateStore` to bypass the internal state and wire json-render to any state management library (Redux, Zustand, XState, etc.):
220
+
221
+ ```tsx
222
+ import { createStateStore, type StateStore } from "@json-render/react-native";
223
+
224
+ // Use the built-in store outside of React
225
+ const store = createStateStore({ count: 0 });
226
+
227
+ <StateProvider store={store}>
228
+ {children}
229
+ </StateProvider>
230
+
231
+ // Mutate from anywhere — React will re-render automatically:
232
+ store.set("/count", 1);
233
+ ```
234
+
235
+ When `store` is provided, `initialState` and `onStateChange` are ignored. The store is the single source of truth. The same `store` prop is available on `createRenderer`, `JSONUIProvider`, and `StateProvider`.
236
+
217
237
  ## Hooks
218
238
 
219
239
  | Hook | Purpose |
@@ -227,3 +247,4 @@ const systemPrompt = catalog.prompt({
227
247
  | `useAction(name)` | Get a single action dispatch function |
228
248
  | `useUIStream(options)` | Stream specs from an API endpoint |
229
249
  | `createStandardActionHandlers(options)` | Create handlers for standard actions |
250
+ | `createStateStore(initialState)` | Create a framework-agnostic in-memory `StateStore` |
package/dist/index.d.mts CHANGED
@@ -1,56 +1,12 @@
1
+ export { StateContextValue, StateProvider, StateProviderProps, useStateBinding, useStateStore, useStateValue } from '@internal/react-state';
1
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
3
  import { ReactNode, ComponentType } from 'react';
3
- import { StateModel, VisibilityCondition, VisibilityContext, ActionHandler, ResolvedAction, ActionBinding, ActionConfirm, ValidationFunction, ValidationResult, ValidationConfig, Catalog, InferCatalogComponents, InferComponentProps, InferCatalogActions, InferActionParams, UIElement, SchemaDefinition, Spec, FlatElement } from '@json-render/core';
4
- export { Spec, StateModel } from '@json-render/core';
4
+ import { VisibilityCondition, VisibilityContext, ActionHandler, ResolvedAction, ActionBinding, ActionConfirm, ValidationFunction, ValidationResult, ValidationConfig, Catalog, InferCatalogComponents, InferComponentProps, InferCatalogActions, InferActionParams, StateModel, UIElement, SchemaDefinition, Spec, StateStore, FlatElement } from '@json-render/core';
5
+ export { Spec, StateModel, StateStore, createStateStore } from '@json-render/core';
5
6
  export { ElementTreeSchema, ElementTreeSpec, ReactNativeSchema, ReactNativeSpec, elementTreeSchema, schema } from './schema.mjs';
6
7
  export { ActionDefinition, ComponentDefinition, standardActionDefinitions, standardComponentDefinitions } from './catalog.mjs';
7
8
  import 'zod';
8
9
 
9
- /**
10
- * State context value
11
- */
12
- interface StateContextValue {
13
- /** The current state model */
14
- state: StateModel;
15
- /** Get a value by path */
16
- get: (path: string) => unknown;
17
- /** Set a value by path */
18
- set: (path: string, value: unknown) => void;
19
- /** Update multiple values at once */
20
- update: (updates: Record<string, unknown>) => void;
21
- }
22
- /**
23
- * Props for StateProvider
24
- */
25
- interface StateProviderProps {
26
- /** Initial state model */
27
- initialState?: StateModel;
28
- /** Callback when state changes */
29
- onStateChange?: (path: string, value: unknown) => void;
30
- children: ReactNode;
31
- }
32
- /**
33
- * Provider for state model context
34
- */
35
- declare function StateProvider({ initialState, onStateChange, children, }: StateProviderProps): react_jsx_runtime.JSX.Element;
36
- /**
37
- * Hook to access the state context
38
- */
39
- declare function useStateStore(): StateContextValue;
40
- /**
41
- * Hook to get a value from the state model
42
- */
43
- declare function useStateValue<T>(path: string): T | undefined;
44
- /**
45
- * Hook to get and set a value from the state model (like useState).
46
- *
47
- * @deprecated Use {@link useBoundProp} with `$bindState` expressions instead.
48
- * `useStateBinding` takes a raw state path string, while `useBoundProp` works
49
- * with the renderer's `bindings` map and supports both `$bindState` and
50
- * `$bindItem` expressions.
51
- */
52
- declare function useStateBinding<T>(path: string): [T | undefined, (value: T) => void];
53
-
54
10
  /**
55
11
  * Visibility context value
56
12
  */
@@ -391,7 +347,12 @@ interface JSONUIProviderProps {
391
347
  * Custom components are merged with (and override) standard components.
392
348
  */
393
349
  registry?: ComponentRegistry;
394
- /** Initial state model */
350
+ /**
351
+ * External store (controlled mode). When provided, `initialState` and
352
+ * `onStateChange` are ignored.
353
+ */
354
+ store?: StateStore;
355
+ /** Initial state model (uncontrolled mode) */
395
356
  initialState?: Record<string, unknown>;
396
357
  /** Action handlers */
397
358
  handlers?: Record<string, (params: Record<string, unknown>) => Promise<unknown> | unknown>;
@@ -399,14 +360,17 @@ interface JSONUIProviderProps {
399
360
  navigate?: (path: string) => void;
400
361
  /** Custom validation functions */
401
362
  validationFunctions?: Record<string, (value: unknown, args?: Record<string, unknown>) => boolean>;
402
- /** Callback when state changes */
403
- onStateChange?: (path: string, value: unknown) => void;
363
+ /** Callback when state changes (uncontrolled mode) */
364
+ onStateChange?: (changes: Array<{
365
+ path: string;
366
+ value: unknown;
367
+ }>) => void;
404
368
  children: ReactNode;
405
369
  }
406
370
  /**
407
371
  * Combined provider for all JSONUI contexts
408
372
  */
409
- declare function JSONUIProvider({ registry, initialState, handlers, navigate, validationFunctions, onStateChange, children, }: JSONUIProviderProps): react_jsx_runtime.JSX.Element;
373
+ declare function JSONUIProvider({ registry, store, initialState, handlers, navigate, validationFunctions, onStateChange, children, }: JSONUIProviderProps): react_jsx_runtime.JSX.Element;
410
374
  /**
411
375
  * Result returned by defineRegistry
412
376
  */
@@ -463,12 +427,20 @@ declare function defineRegistry<C extends Catalog>(_catalog: C, options: {
463
427
  interface CreateRendererProps {
464
428
  /** The spec to render (AI-generated JSON) */
465
429
  spec: Spec | null;
466
- /** State context for dynamic values */
430
+ /**
431
+ * External store (controlled mode). When provided, `state` and
432
+ * `onStateChange` are ignored.
433
+ */
434
+ store?: StateStore;
435
+ /** State context for dynamic values (uncontrolled mode) */
467
436
  state?: Record<string, unknown>;
468
437
  /** Action handler */
469
438
  onAction?: (actionName: string, params?: Record<string, unknown>) => void;
470
- /** Callback when state changes (e.g., from form inputs) */
471
- onStateChange?: (path: string, value: unknown) => void;
439
+ /** Callback when state changes (uncontrolled mode) */
440
+ onStateChange?: (changes: Array<{
441
+ path: string;
442
+ value: unknown;
443
+ }>) => void;
472
444
  /** Whether the spec is currently loading/streaming */
473
445
  loading?: boolean;
474
446
  /** Fallback component for unknown types */
@@ -587,4 +559,4 @@ declare function useUIStream({ api, onComplete, onError, fetch: fetchFn, validat
587
559
  */
588
560
  declare function flatToTree(elements: FlatElement[]): Spec;
589
561
 
590
- export { type ActionContextValue, type ActionFn, ActionProvider, type ActionProviderProps, type Actions, type ComponentContext, type ComponentFn, type ComponentMap, type ComponentRegistry, type ComponentRenderProps, type ComponentRenderer, type Components, ConfirmDialog, type ConfirmDialogProps, type CreateRendererProps, type DefineRegistryResult, type FieldValidationState, JSONUIProvider, type JSONUIProviderProps, type PendingConfirmation, Renderer, type RendererProps, RepeatScopeProvider, type RepeatScopeValue, type SetState, type StateContextValue, StateProvider, type StateProviderProps, type UseUIStreamOptions, type UseUIStreamReturn, type ValidationContextValue, ValidationProvider, type ValidationProviderProps, type VisibilityContextValue, VisibilityProvider, type VisibilityProviderProps, createRenderer, createStandardActionHandlers, defineRegistry, flatToTree, standardComponents, useAction, useActions, useBoundProp, useFieldValidation, useIsVisible, useRepeatScope, useStateBinding, useStateStore, useStateValue, useUIStream, useValidation, useVisibility };
562
+ export { type ActionContextValue, type ActionFn, ActionProvider, type ActionProviderProps, type Actions, type ComponentContext, type ComponentFn, type ComponentMap, type ComponentRegistry, type ComponentRenderProps, type ComponentRenderer, type Components, ConfirmDialog, type ConfirmDialogProps, type CreateRendererProps, type DefineRegistryResult, type FieldValidationState, JSONUIProvider, type JSONUIProviderProps, type PendingConfirmation, Renderer, type RendererProps, RepeatScopeProvider, type RepeatScopeValue, type SetState, type UseUIStreamOptions, type UseUIStreamReturn, type ValidationContextValue, ValidationProvider, type ValidationProviderProps, type VisibilityContextValue, VisibilityProvider, type VisibilityProviderProps, createRenderer, createStandardActionHandlers, defineRegistry, flatToTree, standardComponents, useAction, useActions, useBoundProp, useFieldValidation, useIsVisible, useRepeatScope, useUIStream, useValidation, useVisibility };
package/dist/index.d.ts CHANGED
@@ -1,56 +1,12 @@
1
+ export { StateContextValue, StateProvider, StateProviderProps, useStateBinding, useStateStore, useStateValue } from '@internal/react-state';
1
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
3
  import { ReactNode, ComponentType } from 'react';
3
- import { StateModel, VisibilityCondition, VisibilityContext, ActionHandler, ResolvedAction, ActionBinding, ActionConfirm, ValidationFunction, ValidationResult, ValidationConfig, Catalog, InferCatalogComponents, InferComponentProps, InferCatalogActions, InferActionParams, UIElement, SchemaDefinition, Spec, FlatElement } from '@json-render/core';
4
- export { Spec, StateModel } from '@json-render/core';
4
+ import { VisibilityCondition, VisibilityContext, ActionHandler, ResolvedAction, ActionBinding, ActionConfirm, ValidationFunction, ValidationResult, ValidationConfig, Catalog, InferCatalogComponents, InferComponentProps, InferCatalogActions, InferActionParams, StateModel, UIElement, SchemaDefinition, Spec, StateStore, FlatElement } from '@json-render/core';
5
+ export { Spec, StateModel, StateStore, createStateStore } from '@json-render/core';
5
6
  export { ElementTreeSchema, ElementTreeSpec, ReactNativeSchema, ReactNativeSpec, elementTreeSchema, schema } from './schema.js';
6
7
  export { ActionDefinition, ComponentDefinition, standardActionDefinitions, standardComponentDefinitions } from './catalog.js';
7
8
  import 'zod';
8
9
 
9
- /**
10
- * State context value
11
- */
12
- interface StateContextValue {
13
- /** The current state model */
14
- state: StateModel;
15
- /** Get a value by path */
16
- get: (path: string) => unknown;
17
- /** Set a value by path */
18
- set: (path: string, value: unknown) => void;
19
- /** Update multiple values at once */
20
- update: (updates: Record<string, unknown>) => void;
21
- }
22
- /**
23
- * Props for StateProvider
24
- */
25
- interface StateProviderProps {
26
- /** Initial state model */
27
- initialState?: StateModel;
28
- /** Callback when state changes */
29
- onStateChange?: (path: string, value: unknown) => void;
30
- children: ReactNode;
31
- }
32
- /**
33
- * Provider for state model context
34
- */
35
- declare function StateProvider({ initialState, onStateChange, children, }: StateProviderProps): react_jsx_runtime.JSX.Element;
36
- /**
37
- * Hook to access the state context
38
- */
39
- declare function useStateStore(): StateContextValue;
40
- /**
41
- * Hook to get a value from the state model
42
- */
43
- declare function useStateValue<T>(path: string): T | undefined;
44
- /**
45
- * Hook to get and set a value from the state model (like useState).
46
- *
47
- * @deprecated Use {@link useBoundProp} with `$bindState` expressions instead.
48
- * `useStateBinding` takes a raw state path string, while `useBoundProp` works
49
- * with the renderer's `bindings` map and supports both `$bindState` and
50
- * `$bindItem` expressions.
51
- */
52
- declare function useStateBinding<T>(path: string): [T | undefined, (value: T) => void];
53
-
54
10
  /**
55
11
  * Visibility context value
56
12
  */
@@ -391,7 +347,12 @@ interface JSONUIProviderProps {
391
347
  * Custom components are merged with (and override) standard components.
392
348
  */
393
349
  registry?: ComponentRegistry;
394
- /** Initial state model */
350
+ /**
351
+ * External store (controlled mode). When provided, `initialState` and
352
+ * `onStateChange` are ignored.
353
+ */
354
+ store?: StateStore;
355
+ /** Initial state model (uncontrolled mode) */
395
356
  initialState?: Record<string, unknown>;
396
357
  /** Action handlers */
397
358
  handlers?: Record<string, (params: Record<string, unknown>) => Promise<unknown> | unknown>;
@@ -399,14 +360,17 @@ interface JSONUIProviderProps {
399
360
  navigate?: (path: string) => void;
400
361
  /** Custom validation functions */
401
362
  validationFunctions?: Record<string, (value: unknown, args?: Record<string, unknown>) => boolean>;
402
- /** Callback when state changes */
403
- onStateChange?: (path: string, value: unknown) => void;
363
+ /** Callback when state changes (uncontrolled mode) */
364
+ onStateChange?: (changes: Array<{
365
+ path: string;
366
+ value: unknown;
367
+ }>) => void;
404
368
  children: ReactNode;
405
369
  }
406
370
  /**
407
371
  * Combined provider for all JSONUI contexts
408
372
  */
409
- declare function JSONUIProvider({ registry, initialState, handlers, navigate, validationFunctions, onStateChange, children, }: JSONUIProviderProps): react_jsx_runtime.JSX.Element;
373
+ declare function JSONUIProvider({ registry, store, initialState, handlers, navigate, validationFunctions, onStateChange, children, }: JSONUIProviderProps): react_jsx_runtime.JSX.Element;
410
374
  /**
411
375
  * Result returned by defineRegistry
412
376
  */
@@ -463,12 +427,20 @@ declare function defineRegistry<C extends Catalog>(_catalog: C, options: {
463
427
  interface CreateRendererProps {
464
428
  /** The spec to render (AI-generated JSON) */
465
429
  spec: Spec | null;
466
- /** State context for dynamic values */
430
+ /**
431
+ * External store (controlled mode). When provided, `state` and
432
+ * `onStateChange` are ignored.
433
+ */
434
+ store?: StateStore;
435
+ /** State context for dynamic values (uncontrolled mode) */
467
436
  state?: Record<string, unknown>;
468
437
  /** Action handler */
469
438
  onAction?: (actionName: string, params?: Record<string, unknown>) => void;
470
- /** Callback when state changes (e.g., from form inputs) */
471
- onStateChange?: (path: string, value: unknown) => void;
439
+ /** Callback when state changes (uncontrolled mode) */
440
+ onStateChange?: (changes: Array<{
441
+ path: string;
442
+ value: unknown;
443
+ }>) => void;
472
444
  /** Whether the spec is currently loading/streaming */
473
445
  loading?: boolean;
474
446
  /** Fallback component for unknown types */
@@ -587,4 +559,4 @@ declare function useUIStream({ api, onComplete, onError, fetch: fetchFn, validat
587
559
  */
588
560
  declare function flatToTree(elements: FlatElement[]): Spec;
589
561
 
590
- export { type ActionContextValue, type ActionFn, ActionProvider, type ActionProviderProps, type Actions, type ComponentContext, type ComponentFn, type ComponentMap, type ComponentRegistry, type ComponentRenderProps, type ComponentRenderer, type Components, ConfirmDialog, type ConfirmDialogProps, type CreateRendererProps, type DefineRegistryResult, type FieldValidationState, JSONUIProvider, type JSONUIProviderProps, type PendingConfirmation, Renderer, type RendererProps, RepeatScopeProvider, type RepeatScopeValue, type SetState, type StateContextValue, StateProvider, type StateProviderProps, type UseUIStreamOptions, type UseUIStreamReturn, type ValidationContextValue, ValidationProvider, type ValidationProviderProps, type VisibilityContextValue, VisibilityProvider, type VisibilityProviderProps, createRenderer, createStandardActionHandlers, defineRegistry, flatToTree, standardComponents, useAction, useActions, useBoundProp, useFieldValidation, useIsVisible, useRepeatScope, useStateBinding, useStateStore, useStateValue, useUIStream, useValidation, useVisibility };
562
+ export { type ActionContextValue, type ActionFn, ActionProvider, type ActionProviderProps, type Actions, type ComponentContext, type ComponentFn, type ComponentMap, type ComponentRegistry, type ComponentRenderProps, type ComponentRenderer, type Components, ConfirmDialog, type ConfirmDialogProps, type CreateRendererProps, type DefineRegistryResult, type FieldValidationState, JSONUIProvider, type JSONUIProviderProps, type PendingConfirmation, Renderer, type RendererProps, RepeatScopeProvider, type RepeatScopeValue, type SetState, type UseUIStreamOptions, type UseUIStreamReturn, type ValidationContextValue, ValidationProvider, type ValidationProviderProps, type VisibilityContextValue, VisibilityProvider, type VisibilityProviderProps, createRenderer, createStandardActionHandlers, defineRegistry, flatToTree, standardComponents, useAction, useActions, useBoundProp, useFieldValidation, useIsVisible, useRepeatScope, useUIStream, useValidation, useVisibility };