DemoRenderer.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. console.log("item", item);
  50. if (item) {
  51. todos[index] = { ...item, completed: !item.completed };
  52. }
  53. set("/todos", todos);
  54. },
  55. };
  56. </script>
  57. <template>
  58. <ActionProvider :handlers="handlers">
  59. <VisibilityProvider>
  60. <ValidationProvider>
  61. <Renderer :spec="demoSpec" :registry="registry" />
  62. </ValidationProvider>
  63. </VisibilityProvider>
  64. </ActionProvider>
  65. </template>