page.mdx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. export const metadata = { title: "@json-render/react API" }
  2. # @json-render/react
  3. React components, providers, and hooks.
  4. ## Providers
  5. ### StateProvider
  6. ```tsx
  7. <StateProvider initialState={object}>
  8. {children}
  9. </StateProvider>
  10. ```
  11. ### ActionProvider
  12. ```tsx
  13. <ActionProvider handlers={Record<string, ActionHandler>}>
  14. {children}
  15. </ActionProvider>
  16. type ActionHandler = (params: Record<string, unknown>) => void | Promise<void>;
  17. ```
  18. ### VisibilityProvider
  19. ```tsx
  20. <VisibilityProvider auth={AuthState}>
  21. {children}
  22. </VisibilityProvider>
  23. interface AuthState {
  24. isSignedIn: boolean;
  25. roles?: string[];
  26. }
  27. ```
  28. ### ValidationProvider
  29. ```tsx
  30. <ValidationProvider functions={Record<string, ValidatorFn>}>
  31. {children}
  32. </ValidationProvider>
  33. type ValidatorFn = (value: unknown, args?: object) => boolean | Promise<boolean>;
  34. ```
  35. ## defineRegistry
  36. Create a type-safe component registry from a catalog. Components receive `props`, `children`, `onAction`, and `loading` with catalog-inferred types.
  37. ```tsx
  38. import { defineRegistry } from '@json-render/react';
  39. const { registry } = defineRegistry(catalog, {
  40. components: {
  41. Card: ({ props, children }) => <div>{props.title}{children}</div>,
  42. Button: ({ props, onAction }) => (
  43. <button onClick={() => onAction?.({ name: props.action })}>
  44. {props.label}
  45. </button>
  46. ),
  47. },
  48. });
  49. // Pass to <Renderer>
  50. <Renderer spec={spec} registry={registry} />
  51. ```
  52. ## Components
  53. ### Renderer
  54. ```tsx
  55. <Renderer
  56. spec={Spec} // The UI spec to render
  57. registry={Registry} // Component registry (from defineRegistry)
  58. loading={boolean} // Optional loading state
  59. fallback={Component} // Optional fallback for unknown types
  60. />
  61. type Registry = Record<string, React.ComponentType<ComponentRenderProps>>;
  62. ```
  63. ### Component Props (via defineRegistry)
  64. ```tsx
  65. interface ComponentContext<P> {
  66. props: P; // Typed props from catalog
  67. children?: React.ReactNode; // Rendered children (for slot components)
  68. onAction?: (action: { name: string; params?: object }) => void;
  69. loading?: boolean;
  70. }
  71. ```
  72. ## Hooks
  73. ### useUIStream
  74. ```typescript
  75. const {
  76. spec, // Spec | null - current UI state
  77. isStreaming, // boolean - true while streaming
  78. error, // Error | null
  79. send, // (prompt: string, context?: Record<string, unknown>) => Promise<void>
  80. clear, // () => void - reset spec and error
  81. } = useUIStream({
  82. api: string, // API endpoint URL
  83. onComplete?: (spec: Spec) => void, // Called when streaming completes
  84. onError?: (error: Error) => void, // Called when an error occurs
  85. });
  86. ```
  87. ### useStateStore
  88. ```typescript
  89. const {
  90. data, // Record<string, unknown>
  91. setState, // (data: object) => void
  92. getValue, // (path: string) => unknown
  93. setValue, // (path: string, value: unknown) => void
  94. } = useStateStore();
  95. ```
  96. ### useStateValue
  97. ```typescript
  98. const value = useStateValue(path: string);
  99. ```
  100. ### useStateBinding
  101. ```typescript
  102. const [value, setValue] = useStateBinding(path: string);
  103. ```
  104. ### useActions
  105. ```typescript
  106. const { dispatch } = useActions();
  107. // dispatch(actionName: string, params: object)
  108. ```
  109. ### useAction
  110. ```typescript
  111. const submitForm = useAction('submit_form');
  112. // submitForm(params: object)
  113. ```
  114. ### useIsVisible
  115. ```typescript
  116. const isVisible = useIsVisible(condition?: VisibilityCondition);
  117. ```
  118. ### useFieldValidation
  119. ```typescript
  120. const {
  121. value, // unknown
  122. setValue, // (value: unknown) => void
  123. errors, // string[]
  124. validate, // () => Promise<boolean>
  125. isValid, // boolean
  126. } = useFieldValidation(path: string, checks: ValidationCheck[]);
  127. ```