DemoRenderer.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <script setup lang="ts">
  2. import {
  3. ActionProvider,
  4. ValidationProvider,
  5. VisibilityProvider,
  6. Renderer,
  7. defineRegistry,
  8. useStateStore,
  9. } from "@json-render/vue";
  10. import { catalog } from "./lib/catalog";
  11. import { components } from "./lib/registry";
  12. import { demoSpec } from "./lib/spec";
  13. // Access the state store provided by the parent StateProvider
  14. const { get, set } = useStateStore();
  15. // Build registry — include stub actions to satisfy catalog types.
  16. // The actual logic runs in the handlers below, which have direct
  17. // access to the state store.
  18. const { registry } = defineRegistry(catalog, {
  19. components,
  20. actions: {
  21. increment: async () => {},
  22. decrement: async () => {},
  23. reset: async () => {},
  24. toggleItem: async () => {},
  25. },
  26. });
  27. // Action handlers — close over the state store's get/set so they
  28. // can read and write state directly without needing an external store.
  29. const handlers = {
  30. increment: async () => {
  31. set("/count", Number(get("/count") || 0) + 1);
  32. },
  33. decrement: async () => {
  34. set("/count", Math.max(0, Number(get("/count") || 0) - 1));
  35. },
  36. reset: async () => {
  37. set("/count", 0);
  38. },
  39. toggleItem: async (params: Record<string, unknown>) => {
  40. const index = params.index as number;
  41. const todos = (
  42. get("/todos") as Array<{
  43. id: number;
  44. title: string;
  45. completed: boolean;
  46. }>
  47. ).slice();
  48. const item = todos[index];
  49. if (item) {
  50. todos[index] = { ...item, completed: !item.completed };
  51. }
  52. set("/todos", todos);
  53. },
  54. };
  55. </script>
  56. <template>
  57. <ActionProvider :handlers="handlers">
  58. <VisibilityProvider>
  59. <ValidationProvider>
  60. <Renderer :spec="demoSpec" :registry="registry" />
  61. </ValidationProvider>
  62. </VisibilityProvider>
  63. </ActionProvider>
  64. </template>