@easyops-cn/a2ui-react 0.0.5 → 0.0.7

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
@@ -22,10 +22,11 @@ First, use the `@source` directive to tell Tailwind to scan the library code for
22
22
  @source "../node_modules/@easyops-cn/a2ui-react";
23
23
  ```
24
24
 
25
- Next, use the `A2UIRenderer` component to render A2UI messages:
25
+ Next, use `A2UIProvider` and `A2UIRenderer` to render A2UI messages:
26
26
 
27
27
  ```tsx
28
28
  import {
29
+ A2UIProvider,
29
30
  A2UIRenderer,
30
31
  type A2UIMessage,
31
32
  type A2UIAction,
@@ -38,19 +39,24 @@ function App() {
38
39
  console.log('Action received:', action)
39
40
  }
40
41
 
41
- return <A2UIRenderer messages={messages} onAction={handleAction} />
42
+ return (
43
+ <A2UIProvider messages={messages}>
44
+ <A2UIRenderer onAction={handleAction} />
45
+ </A2UIProvider>
46
+ )
42
47
  }
43
48
  ```
44
49
 
45
50
  ### Custom components
46
51
 
47
- You can override default components or add new custom components via the `components` prop, which takes a `Map<string, React.ComponentType>`.
52
+ You can override default components or add new custom components via the `components` prop on `A2UIProvider`, which takes a `Map<string, React.ComponentType>`.
48
53
 
49
54
  ```tsx
50
55
  import {
56
+ A2UIProvider,
51
57
  A2UIRenderer,
52
- A2UIMessage,
53
- A2UIAction,
58
+ type A2UIMessage,
59
+ type A2UIAction,
54
60
  } from '@easyops-cn/a2ui-react/0.8'
55
61
 
56
62
  const ComponentsMap = new Map<string, React.ComponentType<any>>([
@@ -63,11 +69,9 @@ const ComponentsMap = new Map<string, React.ComponentType<any>>([
63
69
 
64
70
  function App() {
65
71
  return (
66
- <A2UIRenderer
67
- components={ComponentsMap}
68
- messages={messages}
69
- onAction={handleAction}
70
- />
72
+ <A2UIProvider components={ComponentsMap} messages={messages}>
73
+ <A2UIRenderer onAction={handleAction} />
74
+ </A2UIProvider>
71
75
  )
72
76
  }
73
77
  ```
@@ -1,49 +1,31 @@
1
- import { ComponentType } from 'react';
2
- import { A2UIMessage, ActionPayload, BaseComponentProps } from './types';
1
+ import { ActionHandler } from './types';
3
2
  /**
4
- * Type for custom component map.
3
+ * Props for A2UIRenderer.
5
4
  */
6
- export type ComponentsMap = Map<string, ComponentType<BaseComponentProps & Record<string, unknown>>>;
7
- /**
8
- * Props for A2UIRenderer component.
9
- */
10
- export interface A2UIRenderProps {
11
- /** Array of A2UI messages to render */
12
- messages: A2UIMessage[];
5
+ export interface A2UIRendererProps {
13
6
  /** Callback when an action is dispatched */
14
- onAction?: (action: ActionPayload) => void;
15
- /** Custom component overrides */
16
- components?: ComponentsMap;
7
+ onAction?: ActionHandler;
17
8
  }
18
9
  /**
19
- * Main entry component for rendering A2UI messages.
20
- *
21
- * Processes an array of A2UIMessage objects and renders the resulting UI.
22
- * Supports custom component overrides via the components prop.
10
+ * Component for rendering A2UI surfaces.
23
11
  *
24
- * @param props - Component props
25
- * @param props.messages - Array of A2UI messages to render
26
- * @param props.onAction - Optional callback when an action is dispatched
27
- * @param props.components - Optional custom component overrides
12
+ * Renders all surfaces from the A2UI context. Must be used within an A2UIProvider.
28
13
  *
29
14
  * @example
30
15
  * ```tsx
31
16
  * // Basic usage
32
- * <A2UIRenderer messages={messages} onAction={handleAction} />
17
+ * <A2UIProvider messages={messages}>
18
+ * <A2UIRenderer onAction={handleAction} />
19
+ * </A2UIProvider>
33
20
  *
34
- * // With custom components
35
- * const customComponents = new Map([
36
- * ['Button', CustomButton],
37
- * ['Switch', CustomSwitch],
38
- * ])
39
- * <A2UIRenderer
40
- * messages={messages}
41
- * onAction={handleAction}
42
- * components={customComponents}
43
- * />
21
+ * // With custom middleware for hooks access
22
+ * <A2UIProvider messages={messages}>
23
+ * <MyCustomComponent />
24
+ * <A2UIRenderer onAction={handleAction} />
25
+ * </A2UIProvider>
44
26
  * ```
45
27
  */
46
- export declare function A2UIRenderer({ messages, onAction, components, }: A2UIRenderProps): import("react/jsx-runtime").JSX.Element;
28
+ export declare function A2UIRenderer({ onAction }: A2UIRendererProps): import("react/jsx-runtime").JSX.Element | null;
47
29
  export declare namespace A2UIRenderer {
48
30
  var displayName: string;
49
31
  }
@@ -1,40 +1,19 @@
1
- import { jsx as e, Fragment as f } from "react/jsx-runtime";
2
- import { useEffect as p } from "react";
3
- import { A2UIProvider as c } from "./contexts/A2UIProvider.js";
4
- import { ComponentsMapProvider as a } from "./contexts/ComponentsMapContext.js";
5
- import { useSurfaceContext as u } from "./contexts/SurfaceContext.js";
6
- import { useA2UIMessageHandler as l } from "./hooks/useA2UIMessageHandler.js";
7
- import { componentRegistry as d, ComponentRenderer as g } from "./components/ComponentRenderer.js";
8
- function A({ messages: r }) {
9
- const { processMessages: n, clear: o } = l(), { surfaces: t } = u();
10
- p(() => {
11
- o(), r && r.length > 0 && n(r);
12
- }, [r, n, o]);
13
- const s = Array.from(t.entries());
14
- return s.length === 0 ? null : /* @__PURE__ */ e(f, { children: s.map(([i, m]) => m.root ? /* @__PURE__ */ e(
15
- g,
1
+ import { jsx as n } from "react/jsx-runtime";
2
+ import { useSurfaceContext as m } from "./contexts/SurfaceContext.js";
3
+ import { ActionProvider as f } from "./contexts/ActionContext.js";
4
+ import { ComponentRenderer as p } from "./components/ComponentRenderer.js";
5
+ function u({ onAction: t }) {
6
+ const { surfaces: i } = m(), r = Array.from(i.entries());
7
+ return r.length === 0 ? null : /* @__PURE__ */ n(f, { onAction: t, children: r.map(([e, o]) => o.root ? /* @__PURE__ */ n(
8
+ p,
16
9
  {
17
- surfaceId: i,
18
- componentId: m.root
10
+ surfaceId: e,
11
+ componentId: o.root
19
12
  },
20
- i
13
+ e
21
14
  ) : null) });
22
15
  }
23
- function I({
24
- messages: r,
25
- onAction: n,
26
- components: o
27
- }) {
28
- return /* @__PURE__ */ e(c, { onAction: n, children: /* @__PURE__ */ e(
29
- a,
30
- {
31
- components: o,
32
- defaultComponents: d,
33
- children: /* @__PURE__ */ e(A, { messages: r ?? [] })
34
- }
35
- ) });
36
- }
37
- I.displayName = "A2UI.Render";
16
+ u.displayName = "A2UI.Renderer";
38
17
  export {
39
- I as A2UIRenderer
18
+ u as A2UIRenderer
40
19
  };
@@ -1,11 +1,17 @@
1
- import { ReactNode } from 'react';
2
- import { ActionHandler } from '../types';
1
+ import { ReactNode, ComponentType } from 'react';
2
+ import { A2UIMessage, BaseComponentProps } from '../types';
3
+ /**
4
+ * Type for custom component map.
5
+ */
6
+ export type ComponentsMap = Map<string, ComponentType<BaseComponentProps & Record<string, unknown>>>;
3
7
  /**
4
8
  * Props for A2UIProvider.
5
9
  */
6
10
  export interface A2UIProviderProps {
7
- /** Callback when an action is dispatched */
8
- onAction?: ActionHandler;
11
+ /** Array of A2UI messages to render */
12
+ messages: A2UIMessage[];
13
+ /** Custom component overrides */
14
+ components?: ComponentsMap;
9
15
  children: ReactNode;
10
16
  }
11
17
  /**
@@ -14,21 +20,37 @@ export interface A2UIProviderProps {
14
20
  * Provides:
15
21
  * - SurfaceContext: Component tree management
16
22
  * - DataModelContext: Data model state
17
- * - ActionContext: Action dispatching
23
+ * - ComponentsMapContext: Custom component overrides
24
+ *
25
+ * @param props - Component props
26
+ * @param props.messages - Array of A2UI messages to render
27
+ * @param props.components - Optional custom component overrides
28
+ * @param props.children - Child components (typically A2UIRenderer)
18
29
  *
19
30
  * @example
20
31
  * ```tsx
21
- * function App() {
22
- * const handleAction = (action) => {
23
- * console.log('Action:', action);
24
- * };
32
+ * // Basic usage
33
+ * <A2UIProvider messages={messages}>
34
+ * <A2UIRenderer onAction={handleAction} />
35
+ * </A2UIProvider>
36
+ *
37
+ * // With custom components
38
+ * const customComponents = new Map([
39
+ * ['Button', CustomButton],
40
+ * ['Switch', CustomSwitch],
41
+ * ])
42
+ * <A2UIProvider
43
+ * messages={messages}
44
+ * components={customComponents}
45
+ * >
46
+ * <A2UIRenderer onAction={handleAction} />
47
+ * </A2UIProvider>
25
48
  *
26
- * return (
27
- * <A2UIProvider onAction={handleAction}>
28
- * <A2UIReactRenderer messages={messages} />
29
- * </A2UIProvider>
30
- * );
31
- * }
49
+ * // With custom middleware for hooks access
50
+ * <A2UIProvider messages={messages}>
51
+ * <MyCustomComponent />
52
+ * <A2UIRenderer onAction={handleAction} />
53
+ * </A2UIProvider>
32
54
  * ```
33
55
  */
34
- export declare function A2UIProvider({ onAction, children }: A2UIProviderProps): import("react/jsx-runtime").JSX.Element;
56
+ export declare function A2UIProvider({ messages, components, children, }: A2UIProviderProps): import("react/jsx-runtime").JSX.Element;
@@ -1,10 +1,33 @@
1
- import { jsx as r } from "react/jsx-runtime";
2
- import { SurfaceProvider as e } from "./SurfaceContext.js";
3
- import { DataModelProvider as t } from "./DataModelContext.js";
4
- import { ActionProvider as m } from "./ActionContext.js";
5
- function p({ onAction: o, children: i }) {
6
- return /* @__PURE__ */ r(e, { children: /* @__PURE__ */ r(t, { children: /* @__PURE__ */ r(m, { onAction: o, children: i }) }) });
1
+ import { jsx as e, Fragment as n } from "react/jsx-runtime";
2
+ import { useEffect as i } from "react";
3
+ import { SurfaceProvider as f } from "./SurfaceContext.js";
4
+ import { DataModelProvider as m } from "./DataModelContext.js";
5
+ import { ComponentsMapProvider as a } from "./ComponentsMapContext.js";
6
+ import { componentRegistry as c } from "../components/ComponentRenderer.js";
7
+ import { useA2UIMessageHandler as p } from "../hooks/useA2UIMessageHandler.js";
8
+ function d({
9
+ messages: r,
10
+ children: t
11
+ }) {
12
+ const { processMessages: o, clear: s } = p();
13
+ return i(() => {
14
+ s(), r && r.length > 0 && o(r);
15
+ }, [r, o, s]), /* @__PURE__ */ e(n, { children: t });
16
+ }
17
+ function A({
18
+ messages: r,
19
+ components: t,
20
+ children: o
21
+ }) {
22
+ return /* @__PURE__ */ e(f, { children: /* @__PURE__ */ e(m, { children: /* @__PURE__ */ e(
23
+ a,
24
+ {
25
+ components: t,
26
+ defaultComponents: c,
27
+ children: /* @__PURE__ */ e(d, { messages: r ?? [], children: o })
28
+ }
29
+ ) }) });
7
30
  }
8
31
  export {
9
- p as A2UIProvider
32
+ A as A2UIProvider
10
33
  };
@@ -1,8 +1,8 @@
1
1
  import { jsx as m } from "react/jsx-runtime";
2
- import { createContext as x, useContext as C, useCallback as p, useMemo as f } from "react";
2
+ import { useContext as x, createContext as C, useCallback as p, useMemo as f } from "react";
3
3
  import { useDataModelContext as v } from "./DataModelContext.js";
4
4
  import { resolveActionContext as A } from "../utils/dataBinding.js";
5
- const s = x(null);
5
+ const s = C(null);
6
6
  function P({ onAction: t, children: c }) {
7
7
  const { getDataModel: o } = v(), e = p(
8
8
  (n, a, r) => {
@@ -29,7 +29,7 @@ function P({ onAction: t, children: c }) {
29
29
  return /* @__PURE__ */ m(s.Provider, { value: i, children: c });
30
30
  }
31
31
  function g() {
32
- const t = C(s);
32
+ const t = x(s);
33
33
  if (!t)
34
34
  throw new Error("useActionContext must be used within an ActionProvider");
35
35
  return t;
@@ -6,19 +6,38 @@
6
6
  *
7
7
  * @example
8
8
  * ```tsx
9
- * import { A2UIRenderer, A2UIMessage, A2UIAction } from '@easyops-cn/a2ui-react/0.8'
9
+ * import { A2UIProvider, A2UIRenderer, A2UIMessage, A2UIAction } from '@easyops-cn/a2ui-react/0.8'
10
10
  *
11
11
  * function App() {
12
12
  * const messages: A2UIMessage[] = [...]
13
13
  * const handleAction = (action: A2UIAction) => {
14
14
  * console.log('Action:', action)
15
15
  * }
16
- * return <A2UIRenderer messages={messages} onAction={handleAction} />
16
+ * return (
17
+ * <A2UIProvider messages={messages} onAction={handleAction}>
18
+ * <A2UIRenderer />
19
+ * </A2UIProvider>
20
+ * )
21
+ * }
22
+ *
23
+ * // With custom middleware component that uses hooks
24
+ * function AppWithMiddleware() {
25
+ * return (
26
+ * <A2UIProvider messages={messages} onAction={handleAction}>
27
+ * <MyCustomMiddleware>
28
+ * <A2UIRenderer />
29
+ * </MyCustomMiddleware>
30
+ * </A2UIProvider>
31
+ * )
17
32
  * }
18
33
  * ```
19
34
  */
20
35
  export type { A2UIMessage, ActionPayload as A2UIAction, Action, ValueSource, } from './types';
36
+ export type { A2UIProviderProps, ComponentsMap } from './contexts/A2UIProvider';
37
+ export type { A2UIRendererProps } from './A2UIRenderer';
38
+ export { A2UIProvider } from './contexts/A2UIProvider';
21
39
  export { A2UIRenderer } from './A2UIRenderer';
22
40
  export { ComponentRenderer } from './components/ComponentRenderer';
23
41
  export { useDispatchAction } from './hooks/useDispatchAction';
24
42
  export { useDataBinding, useFormBinding } from './hooks/useDataBinding';
43
+ export { useA2UIMessageHandler } from './hooks/useA2UIMessageHandler';
package/dist/0.8/index.js CHANGED
@@ -1,11 +1,15 @@
1
- import { A2UIRenderer as o } from "./A2UIRenderer.js";
2
- import { ComponentRenderer as t } from "./components/ComponentRenderer.js";
3
- import { useDispatchAction as m } from "./hooks/useDispatchAction.js";
4
- import { useDataBinding as d, useFormBinding as f } from "./hooks/useDataBinding.js";
1
+ import { A2UIProvider as o } from "./contexts/A2UIProvider.js";
2
+ import { A2UIRenderer as t } from "./A2UIRenderer.js";
3
+ import { ComponentRenderer as p } from "./components/ComponentRenderer.js";
4
+ import { useDispatchAction as s } from "./hooks/useDispatchAction.js";
5
+ import { useDataBinding as f, useFormBinding as x } from "./hooks/useDataBinding.js";
6
+ import { useA2UIMessageHandler as u } from "./hooks/useA2UIMessageHandler.js";
5
7
  export {
6
- o as A2UIRenderer,
7
- t as ComponentRenderer,
8
- d as useDataBinding,
9
- m as useDispatchAction,
10
- f as useFormBinding
8
+ o as A2UIProvider,
9
+ t as A2UIRenderer,
10
+ p as ComponentRenderer,
11
+ u as useA2UIMessageHandler,
12
+ f as useDataBinding,
13
+ s as useDispatchAction,
14
+ x as useFormBinding
11
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easyops-cn/a2ui-react",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "A2UI React Renderer",
5
5
  "homepage": "https://easyops-cn.github.io/a2ui-react/",
6
6
  "repository": {
@@ -1,6 +0,0 @@
1
- /**
2
- * A2UIRenderer Tests
3
- *
4
- * Tests for the main A2UIRenderer component.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * ComponentRenderer Tests
3
- *
4
- * Tests for the ComponentRenderer component that routes rendering based on type.
5
- */
6
- export {};
@@ -1,7 +0,0 @@
1
- /**
2
- * Display Components Tests
3
- *
4
- * Tests for TextComponent, ImageComponent, IconComponent, VideoComponent,
5
- * AudioPlayerComponent, and DividerComponent.
6
- */
7
- export {};
@@ -1,7 +0,0 @@
1
- /**
2
- * Interactive Components Tests
3
- *
4
- * Tests for ButtonComponent, CheckBoxComponent, TextFieldComponent,
5
- * DateTimeInputComponent, MultipleChoiceComponent, and SliderComponent.
6
- */
7
- export {};
@@ -1,7 +0,0 @@
1
- /**
2
- * Layout Components Tests
3
- *
4
- * Tests for RowComponent, ColumnComponent, ListComponent, CardComponent,
5
- * TabsComponent, and ModalComponent.
6
- */
7
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * A2UIProvider Tests
3
- *
4
- * Tests for the combined A2UI provider component.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * ActionContext Tests
3
- *
4
- * Tests for the Action context provider and hook.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * DataModelContext Tests
3
- *
4
- * Tests for the DataModel context provider and hook.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * SurfaceContext Tests
3
- *
4
- * Tests for the Surface context provider and hook.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * useA2UIMessageHandler Tests
3
- *
4
- * Tests for the useA2UIMessageHandler hook.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * useComponent Tests
3
- *
4
- * Tests for the useComponent hook.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * useDataBinding Tests
3
- *
4
- * Tests for the useDataBinding, useDataModel, and useFormBinding hooks.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * useDispatchAction Tests
3
- *
4
- * Tests for the useDispatchAction hooks.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * useSurface Tests
3
- *
4
- * Tests for the useSurface hook.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * dataBinding Tests
3
- *
4
- * Tests for data binding utility functions used in A2UI.
5
- */
6
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * pathUtils Tests
3
- *
4
- * Tests for path utility functions used in A2UI data model operations.
5
- */
6
- export {};