@chobantonov/jsonforms-react-extended-renderers 0.0.0-stage → 0.1.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.
Files changed (193) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +17 -2
  3. package/lib/AgGridControlRenderer.impl-BO-Z1x0Y.js +2 -0
  4. package/lib/AgGridControlRenderer.impl-BO-Z1x0Y.js.map +1 -0
  5. package/lib/AgGridControlRenderer.impl-BZS8_ocu.mjs +429 -0
  6. package/lib/AgGridControlRenderer.impl-BZS8_ocu.mjs.map +1 -0
  7. package/lib/DynamicJSXRenderer-Cyw40Kz0.js +2 -0
  8. package/lib/DynamicJSXRenderer-Cyw40Kz0.js.map +1 -0
  9. package/lib/DynamicJSXRenderer-DyB3ZpZV.mjs +109 -0
  10. package/lib/DynamicJSXRenderer-DyB3ZpZV.mjs.map +1 -0
  11. package/lib/MarkupLabelRenderer.impl-CWfdcUuH.mjs +12 -0
  12. package/lib/MarkupLabelRenderer.impl-CWfdcUuH.mjs.map +1 -0
  13. package/lib/MarkupLabelRenderer.impl-DnuzIPQg.js +2 -0
  14. package/lib/MarkupLabelRenderer.impl-DnuzIPQg.js.map +1 -0
  15. package/lib/MonacoControlRenderer.impl-BwgLGNMK.js +3 -0
  16. package/lib/MonacoControlRenderer.impl-BwgLGNMK.js.map +1 -0
  17. package/lib/MonacoControlRenderer.impl-CyjMHbOd.mjs +186 -0
  18. package/lib/MonacoControlRenderer.impl-CyjMHbOd.mjs.map +1 -0
  19. package/lib/RactiveTemplate-7L44A38e.js +2 -0
  20. package/lib/RactiveTemplate-7L44A38e.js.map +1 -0
  21. package/lib/RactiveTemplate-Cat7CxUp.mjs +74 -0
  22. package/lib/RactiveTemplate-Cat7CxUp.mjs.map +1 -0
  23. package/lib/ajv-localizers.cjs.js +2 -0
  24. package/lib/ajv-localizers.cjs.js.map +1 -0
  25. package/lib/ajv-localizers.esm.js +7 -0
  26. package/lib/ajv-localizers.esm.js.map +1 -0
  27. package/lib/ajvI18n-2UTyJv5g.js +2 -0
  28. package/lib/ajvI18n-2UTyJv5g.js.map +1 -0
  29. package/lib/ajvI18n-DO8FFvx1.mjs +227 -0
  30. package/lib/ajvI18n-DO8FFvx1.mjs.map +1 -0
  31. package/lib/celTemplate-Cl0alaCz.js +3 -0
  32. package/lib/celTemplate-Cl0alaCz.js.map +1 -0
  33. package/lib/celTemplate-D_AGhaIL.mjs +137 -0
  34. package/lib/celTemplate-D_AGhaIL.mjs.map +1 -0
  35. package/lib/components/DynamicJSXRenderer.d.ts +7 -0
  36. package/lib/components/ExtendedJsonForms.d.ts +80 -0
  37. package/lib/components/GenericAdditionalProperties.d.ts +4 -0
  38. package/lib/components/InterpolatedText.d.ts +34 -0
  39. package/lib/components/RactiveTemplate.d.ts +37 -0
  40. package/lib/components/editorRendererTypes.d.ts +139 -0
  41. package/lib/components/elementRender.d.ts +15 -0
  42. package/lib/configNamespaces-BFclHtB8.mjs +1 -0
  43. package/lib/configNamespaces-sboXDudL.js +1 -0
  44. package/lib/core/ajv.d.ts +82 -0
  45. package/lib/core/ajvI18n/bg.d.ts +2 -0
  46. package/lib/core/ajvI18n/index.d.ts +118 -0
  47. package/lib/core/ajvI18n/localizers.d.ts +20 -0
  48. package/lib/core/dynamicDefaults.d.ts +34 -0
  49. package/lib/core/keywords.d.ts +24 -0
  50. package/lib/core/transform.d.ts +29 -0
  51. package/lib/core/uiSchema.d.ts +48 -0
  52. package/lib/elementRender-Dbp2e-Cc.js +2 -0
  53. package/lib/elementRender-Dbp2e-Cc.js.map +1 -0
  54. package/lib/elementRender-y8MK9CCE.mjs +5 -0
  55. package/lib/elementRender-y8MK9CCE.mjs.map +1 -0
  56. package/lib/index.d.ts +41 -0
  57. package/lib/jsonforms-react-extended.cjs.js +16 -0
  58. package/lib/jsonforms-react-extended.cjs.js.map +1 -0
  59. package/lib/jsonforms-react-extended.esm.js +2335 -0
  60. package/lib/jsonforms-react-extended.esm.js.map +1 -0
  61. package/lib/locale/bg.d.ts +14 -0
  62. package/lib/markdown-C_y73Q0w.mjs +123 -0
  63. package/lib/markdown-C_y73Q0w.mjs.map +1 -0
  64. package/lib/markdown-CuqtpGVd.js +2 -0
  65. package/lib/markdown-CuqtpGVd.js.map +1 -0
  66. package/lib/objectWithoutProperties-6zedhz76.js +2 -0
  67. package/lib/objectWithoutProperties-6zedhz76.js.map +1 -0
  68. package/lib/objectWithoutProperties-yjQMyZ0y.mjs +58 -0
  69. package/lib/objectWithoutProperties-yjQMyZ0y.mjs.map +1 -0
  70. package/lib/renderers/AgGridControlRenderer.d.ts +10 -0
  71. package/lib/renderers/AgGridControlRenderer.impl.d.ts +4 -0
  72. package/lib/renderers/ButtonRenderer.d.ts +83 -0
  73. package/lib/renderers/GenericMixedRenderer.d.ts +9 -0
  74. package/lib/renderers/HorizontalLayoutRenderer.d.ts +5 -0
  75. package/lib/renderers/ImageViewRenderer.d.ts +48 -0
  76. package/lib/renderers/LinkRenderer.d.ts +23 -0
  77. package/lib/renderers/MarkupLabelRenderer.d.ts +35 -0
  78. package/lib/renderers/MarkupLabelRenderer.impl.d.ts +17 -0
  79. package/lib/renderers/MonacoControlRenderer.d.ts +8 -0
  80. package/lib/renderers/MonacoControlRenderer.impl.d.ts +4 -0
  81. package/lib/renderers/RactiveTemplateLayoutRenderer.d.ts +6 -0
  82. package/lib/renderers/SeparatorRenderer.d.ts +17 -0
  83. package/lib/renderers/SlotRenderer.d.ts +5 -0
  84. package/lib/renderers/SpacerRenderer.d.ts +5 -0
  85. package/lib/renderers/SplitLayoutRenderer.d.ts +18 -0
  86. package/lib/renderers/TemplateLayoutRenderer.d.ts +13 -0
  87. package/lib/renderers/TemplateRenderer.d.ts +17 -0
  88. package/lib/renderers/TsxTemplateLayoutRenderer.d.ts +111 -0
  89. package/lib/renderers/index.d.ts +13 -0
  90. package/lib/renderers/templateEngines.d.ts +43 -0
  91. package/lib/util/actionContext.d.ts +23 -0
  92. package/lib/util/additionalErrors.d.ts +77 -0
  93. package/lib/util/celEnvironment.d.ts +23 -0
  94. package/lib/util/celTemplate.d.ts +47 -0
  95. package/lib/util/childNames.d.ts +46 -0
  96. package/lib/util/configNamespaces.d.ts +1 -0
  97. package/lib/util/cron.d.ts +122 -0
  98. package/lib/util/editorControls.d.ts +8 -0
  99. package/lib/util/editorDiagnostics.d.ts +56 -0
  100. package/lib/util/extendedControls.d.ts +4 -0
  101. package/lib/util/extendedLocale.d.ts +32 -0
  102. package/lib/util/fileFilterValue.d.ts +3 -0
  103. package/lib/util/formContext.d.ts +54 -0
  104. package/lib/util/gridColumns.d.ts +6 -0
  105. package/lib/util/horizontalLayout.d.ts +10 -0
  106. package/lib/util/i18nDefaults.d.ts +45 -0
  107. package/lib/util/interpolate.d.ts +105 -0
  108. package/lib/util/labelFilterValue.d.ts +2 -0
  109. package/lib/util/lazyControl.d.ts +36 -0
  110. package/lib/util/lazyTemplate.d.ts +18 -0
  111. package/lib/util/markdown.d.ts +55 -0
  112. package/lib/util/markdownEscape.d.ts +27 -0
  113. package/lib/util/markup.d.ts +74 -0
  114. package/lib/util/maskControls.d.ts +2 -0
  115. package/lib/util/templateLang.d.ts +87 -0
  116. package/lib/util/templateUrls.d.ts +40 -0
  117. package/lib/util/urlPolicy.d.ts +42 -0
  118. package/lib/util/useCronPicker.d.ts +24 -0
  119. package/lib/util/useDurationControl.d.ts +99 -0
  120. package/lib/util/useEditorAppearance.d.ts +21 -0
  121. package/lib/util/useExtendedTranslator.d.ts +10 -0
  122. package/lib/util/useMonacoShadowStyles.d.ts +2 -0
  123. package/package.json +110 -3
  124. package/src/components/DynamicJSXRenderer.tsx +274 -0
  125. package/src/components/ExtendedJsonForms.tsx +329 -0
  126. package/src/components/GenericAdditionalProperties.tsx +362 -0
  127. package/src/components/InterpolatedText.tsx +114 -0
  128. package/src/components/RactiveTemplate.tsx +161 -0
  129. package/src/components/editorRendererTypes.ts +135 -0
  130. package/src/components/elementRender.ts +15 -0
  131. package/src/core/ajv.ts +177 -0
  132. package/src/core/ajvI18n/bg.ts +203 -0
  133. package/src/core/ajvI18n/index.ts +267 -0
  134. package/src/core/ajvI18n/localizers.ts +25 -0
  135. package/src/core/dynamicDefaults.ts +170 -0
  136. package/src/core/keywords.ts +71 -0
  137. package/src/core/transform.ts +137 -0
  138. package/src/core/uiSchema.ts +53 -0
  139. package/src/index.tsx +196 -0
  140. package/src/locale/bg.ts +51 -0
  141. package/src/renderers/AgGridControlRenderer.impl.tsx +788 -0
  142. package/src/renderers/AgGridControlRenderer.tsx +24 -0
  143. package/src/renderers/ButtonRenderer.tsx +328 -0
  144. package/src/renderers/GenericMixedRenderer.tsx +224 -0
  145. package/src/renderers/HorizontalLayoutRenderer.tsx +93 -0
  146. package/src/renderers/ImageViewRenderer.tsx +328 -0
  147. package/src/renderers/LinkRenderer.tsx +92 -0
  148. package/src/renderers/MarkupLabelRenderer.impl.tsx +26 -0
  149. package/src/renderers/MarkupLabelRenderer.tsx +327 -0
  150. package/src/renderers/MonacoControlRenderer.impl.tsx +351 -0
  151. package/src/renderers/MonacoControlRenderer.tsx +24 -0
  152. package/src/renderers/RactiveTemplateLayoutRenderer.tsx +172 -0
  153. package/src/renderers/SeparatorRenderer.tsx +55 -0
  154. package/src/renderers/SlotRenderer.tsx +34 -0
  155. package/src/renderers/SpacerRenderer.tsx +69 -0
  156. package/src/renderers/SplitLayoutRenderer.tsx +220 -0
  157. package/src/renderers/TemplateLayoutRenderer.tsx +394 -0
  158. package/src/renderers/TemplateRenderer.tsx +71 -0
  159. package/src/renderers/TsxTemplateLayoutRenderer.tsx +258 -0
  160. package/src/renderers/index.ts +14 -0
  161. package/src/renderers/templateEngines.tsx +100 -0
  162. package/src/util/actionContext.ts +29 -0
  163. package/src/util/additionalErrors.tsx +391 -0
  164. package/src/util/celEnvironment.ts +300 -0
  165. package/src/util/celTemplate.ts +267 -0
  166. package/src/util/childNames.ts +135 -0
  167. package/src/util/configNamespaces.ts +6 -0
  168. package/src/util/cron.ts +433 -0
  169. package/src/util/editorControls.ts +58 -0
  170. package/src/util/editorDiagnostics.ts +103 -0
  171. package/src/util/extendedControls.ts +34 -0
  172. package/src/util/extendedLocale.ts +67 -0
  173. package/src/util/fileFilterValue.ts +28 -0
  174. package/src/util/formContext.ts +74 -0
  175. package/src/util/gridColumns.ts +80 -0
  176. package/src/util/horizontalLayout.ts +59 -0
  177. package/src/util/i18nDefaults.ts +80 -0
  178. package/src/util/interpolate.ts +218 -0
  179. package/src/util/labelFilterValue.ts +49 -0
  180. package/src/util/lazyControl.tsx +66 -0
  181. package/src/util/lazyTemplate.tsx +110 -0
  182. package/src/util/markdown.tsx +345 -0
  183. package/src/util/markdownEscape.ts +29 -0
  184. package/src/util/markup.ts +144 -0
  185. package/src/util/maskControls.ts +6 -0
  186. package/src/util/templateLang.ts +145 -0
  187. package/src/util/templateUrls.ts +274 -0
  188. package/src/util/urlPolicy.ts +115 -0
  189. package/src/util/useCronPicker.ts +102 -0
  190. package/src/util/useDurationControl.ts +272 -0
  191. package/src/util/useEditorAppearance.ts +86 -0
  192. package/src/util/useExtendedTranslator.ts +25 -0
  193. package/src/util/useMonacoShadowStyles.ts +47 -0
@@ -0,0 +1,24 @@
1
+ import type { ControlProps } from '@jsonforms/core';
2
+ import { type CronField, type CronPeriod } from './cron';
3
+ /** Keeps the cron expression, picker draft and displayed period in sync. */
4
+ export declare const useCronPicker: (props: ControlProps) => {
5
+ label: (key: import("@chobantonov/jsonforms-react-renderer-common").I18nKey, values?: Record<string, unknown>) => string;
6
+ expression: string;
7
+ open: boolean;
8
+ setOpen: import("react").Dispatch<import("react").SetStateAction<boolean>>;
9
+ text: string;
10
+ period: CronPeriod | undefined;
11
+ options: any;
12
+ showActions: boolean;
13
+ disabled: boolean;
14
+ localError: string;
15
+ draft: readonly string[];
16
+ changeField: (field: CronField, value: string) => void;
17
+ changeText: (next: string) => void;
18
+ changePeriod: (next: CronPeriod) => void;
19
+ editedProblem: string | undefined;
20
+ shownFields: ("month" | "second" | "minute" | "hour" | "dayOfMonth" | "dayOfWeek")[];
21
+ hasCustomField: boolean;
22
+ openOn: (next: boolean) => void;
23
+ accept: () => void;
24
+ };
@@ -0,0 +1,99 @@
1
+ import { ControlProps } from '@jsonforms/core';
2
+ /**
3
+ * The picker's shape, and why it is not one field per component.
4
+ *
5
+ * ISO 8601 gives seven components, and showing all seven means five rows of
6
+ * `0` for a value like "2 days, 3 hours" - the two that carry information are
7
+ * found by scanning past the five that do not. So the picker shows only the
8
+ * units **in play**, with the rest reachable through an add control.
9
+ *
10
+ * Weeks is the other half of it. ISO forbids combining `W` with anything else,
11
+ * which the previous picker enforced by greying the Weeks row whenever another
12
+ * field was non-zero. That is correct and unexplained: a user who wants weeks
13
+ * has to work out that five other fields must be zeroed first. It is a
14
+ * **mode**, not a component, so it is modelled as one.
15
+ */
16
+ export declare const emptyDurationParts: {
17
+ weeks: number;
18
+ years: number;
19
+ months: number;
20
+ days: number;
21
+ hours: number;
22
+ minutes: number;
23
+ seconds: number;
24
+ };
25
+ export type ExtendedDurationParts = typeof emptyDurationParts;
26
+ export declare const durationFields: Array<keyof ExtendedDurationParts>;
27
+ /** The components, in magnitude order. Weeks is a mode and is not among them. */
28
+ export declare const durationComponentFields: Array<Exclude<keyof ExtendedDurationParts, "weeks">>;
29
+ export type DurationMode = 'weeks' | 'components';
30
+ /**
31
+ * Whether a value is written in the weeks form.
32
+ *
33
+ * Not `parts.weeks > 0`: `P0W` is a valid duration - RFC 3339's
34
+ * `dur-week = 1*DIGIT "W"` admits `0` - and it parses to a draft of all
35
+ * zeros, which is indistinguishable from `P0D`. The **text** is what says
36
+ * which form was written.
37
+ */
38
+ export declare const isWeeksDuration: (value: unknown) => boolean;
39
+ /**
40
+ * The largest value a component can hold - and it is not 59.
41
+ *
42
+ * A duration's components are **quantities, not clock fields**. ISO 8601
43
+ * bounds none of them: `PT90M`, `PT3600S`, `P18M`, `P400D` and `PT1H591212M`
44
+ * are all valid, and Ajv's `duration` format accepts every one of them. The
45
+ * picker used to cap months at 11 and hours, minutes and seconds at 23 or 59,
46
+ * which is the shape of a *time of day* and has nothing to do with a length
47
+ * of time.
48
+ *
49
+ * The cap was not only wrong, it was **destructive**: `changePart` clamped to
50
+ * it, so typing 90 into Minutes silently became 59. And `PT90M` is not
51
+ * interchangeable with `PT1H30M` as stored data even though the two are the
52
+ * same length - rewriting one into the other is the normalisation §32.1a
53
+ * already forbids.
54
+ *
55
+ * What remains is the only real bound: the range in which an integer is
56
+ * exact. Past it the formatted string would no longer say what was typed.
57
+ */
58
+ export declare const durationFieldMax: number;
59
+ export declare const parseExtendedDuration: (value: unknown) => ExtendedDurationParts | null;
60
+ export declare const formatExtendedDuration: (parts: ExtendedDurationParts) => string;
61
+ export declare const useDurationControl: (props: ControlProps) => {
62
+ open: boolean;
63
+ draft: {
64
+ weeks: number;
65
+ years: number;
66
+ months: number;
67
+ days: number;
68
+ hours: number;
69
+ minutes: number;
70
+ seconds: number;
71
+ };
72
+ options: any;
73
+ disabled: boolean;
74
+ value: string;
75
+ mode: DurationMode;
76
+ /** The rows to draw, and the order to draw them in. */
77
+ activeFields: ("seconds" | "minutes" | "hours" | "days" | "months" | "years" | "weeks")[];
78
+ /** What the add control may offer. Empty in weeks mode. */
79
+ addableFields: ("seconds" | "minutes" | "hours" | "days" | "months" | "years")[];
80
+ error: string;
81
+ showActions: boolean;
82
+ openPicker: () => void;
83
+ close: () => void;
84
+ apply: () => void;
85
+ changeText: (next: string) => void;
86
+ /**
87
+ * Switches between the two representations.
88
+ *
89
+ * They cannot coexist - `P2W3D` is not a duration - so switching clears
90
+ * the other side rather than pretending to carry it across. The draft is
91
+ * only committed on Apply, so Cancel still restores what was there.
92
+ */
93
+ setMode: (next: DurationMode) => void;
94
+ /** Reveals a unit at zero, in magnitude order. */
95
+ addField: (key: keyof ExtendedDurationParts) => void;
96
+ /** Hides a unit *and clears it*, so the value matches what is on screen. */
97
+ removeField: (key: keyof ExtendedDurationParts) => void;
98
+ changePart: (key: keyof ExtendedDurationParts, value: number) => void;
99
+ };
@@ -0,0 +1,21 @@
1
+ import { RefObject } from 'react';
2
+ export type EditorAppearance = {
3
+ /** Resolved light/dark for the control, whatever the source. */
4
+ isDark: boolean;
5
+ /**
6
+ * A theme name the uischema supplied verbatim (i.e. not light/dark/system).
7
+ * Only meaningful to renderers that understand named themes, such as Monaco.
8
+ */
9
+ customTheme?: string;
10
+ };
11
+ export declare const useEditorAppearance: (ref: RefObject<HTMLElement>, explicitTheme?: string,
12
+ /**
13
+ * Authoritative light/dark from the host design system (e.g. antd's active
14
+ * theme algorithm). When supplied it wins over the DOM luminance heuristic
15
+ * below, which only guesses and silently falls back to prefers-color-scheme
16
+ * when no ancestor paints an opaque background.
17
+ */
18
+ isDark?: boolean) => {
19
+ customTheme: string | undefined;
20
+ isDark: boolean;
21
+ };
@@ -0,0 +1,10 @@
1
+ import { ExtendedI18nKey } from './i18nDefaults';
2
+ /**
3
+ * Translates this package's own strings, and substitutes {name} placeholders.
4
+ *
5
+ * Three sources, in order: the form's own catalog, the locale bundle
6
+ * registered for the current locale, then English. The bundle's string is
7
+ * passed as the translator's *default message*, which is what puts the form's
8
+ * catalog first - see `extendedLocale.ts`.
9
+ */
10
+ export declare const useExtendedTranslator: () => (key: ExtendedI18nKey, values?: Record<string, unknown>) => string;
@@ -0,0 +1,2 @@
1
+ import { RefObject } from 'react';
2
+ export declare const useMonacoShadowStyles: (ref: RefObject<HTMLElement>) => void;
package/package.json CHANGED
@@ -1,6 +1,113 @@
1
1
  {
2
2
  "name": "@chobantonov/jsonforms-react-extended-renderers",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Extended Renderer Set for JSON Forms",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/kchobantonov/jsonforms-react-renderers.git",
8
+ "directory": "packages/jsonforms-react-extended-renderers"
9
+ },
10
+ "bugs": "https://github.com/kchobantonov/jsonforms-react-renderers/issues",
11
+ "homepage": "https://github.com/kchobantonov/jsonforms-react-renderers",
12
+ "license": "MIT",
13
+ "directories": {
14
+ "src": "src"
15
+ },
16
+ "files": [
17
+ "lib",
18
+ "src"
19
+ ],
20
+ "keywords": [
21
+ "extended",
22
+ "renderer",
23
+ "form",
24
+ "forms",
25
+ "json",
26
+ "jsonforms",
27
+ "frontend",
28
+ "generator",
29
+ "input",
30
+ "renderengine",
31
+ "jsonschema",
32
+ "schema",
33
+ "uischema",
34
+ "layout",
35
+ "customization"
36
+ ],
37
+ "main": "lib/jsonforms-react-extended.cjs.js",
38
+ "module": "lib/jsonforms-react-extended.esm.js",
39
+ "typings": "lib/index.d.ts",
40
+ "dependencies": {
41
+ "@marcbachmann/cel-js": "^8.0.0",
42
+ "@monaco-editor/react": "^4.6.0",
43
+ "ag-grid-community": "36.1.0",
44
+ "ag-grid-react": "36.1.0",
45
+ "ajv": "^8.6.1",
46
+ "ajv-errors": "^3.0.0",
47
+ "ajv-i18n": "^4.2.0",
48
+ "ajv-keywords": "^5.1.0",
49
+ "dayjs": "^1.11.11",
50
+ "lodash": "^4.18.1",
51
+ "markdown-it": "^15.0.3",
52
+ "ractive": "^1.4.4",
53
+ "sucrase": "^3.35.1",
54
+ "valtio": "^1.11.2",
55
+ "@chobantonov/jsonforms-react-renderer-common": "0.1.0"
56
+ },
57
+ "peerDependencies": {
58
+ "@jsonforms/core": "3.9.0-alpha.4",
59
+ "@jsonforms/react": "3.9.0-alpha.4",
60
+ "react": "^16.12.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
61
+ "react-dom": "^16.12.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
62
+ },
63
+ "devDependencies": {
64
+ "@jsonforms/core": "3.9.0-alpha.4",
65
+ "@jsonforms/react": "3.9.0-alpha.4",
66
+ "@types/lodash": "^4.17.27",
67
+ "@types/react": "^18.3.23",
68
+ "@types/react-dom": "^18.3.7",
69
+ "@vitest/coverage-v8": "5.1.1",
70
+ "jsdom": "^22.0.0",
71
+ "react": "^18.3.1",
72
+ "react-dom": "^18.3.1",
73
+ "rimraf": "^6.1.4",
74
+ "typedoc": "~0.28.20",
75
+ "typescript": "~6.0.3",
76
+ "vite": "npm:@voidzero-dev/vite-plus-core@8.3.8001",
77
+ "vitest": "5.1.1",
78
+ "@types/node": "^22.0.0",
79
+ "@chobantonov/jsonforms-extended-spec": "0.2.0"
80
+ },
81
+ "exports": {
82
+ ".": {
83
+ "types": "./lib/index.d.ts",
84
+ "import": "./lib/jsonforms-react-extended.esm.js",
85
+ "require": "./lib/jsonforms-react-extended.cjs.js"
86
+ },
87
+ "./ajv-localizers": {
88
+ "types": "./lib/core/ajvI18n/localizers.d.ts",
89
+ "import": "./lib/ajv-localizers.esm.js",
90
+ "require": "./lib/ajv-localizers.cjs.js"
91
+ },
92
+ "./package.json": "./package.json"
93
+ },
94
+ "typesVersions": {
95
+ "*": {
96
+ "ajv-localizers": [
97
+ "lib/core/ajvI18n/localizers.d.ts"
98
+ ]
99
+ }
100
+ },
101
+ "publishConfig": {
102
+ "access": "public"
103
+ },
104
+ "scripts": {
105
+ "build": "vp build && tsc --emitDeclarationOnly",
106
+ "lint": "vp lint .",
107
+ "lint:fix": "vp run lint --fix",
108
+ "test": "vp test run",
109
+ "clean": "rimraf lib coverage dist .nyc_output 2> /dev/null",
110
+ "typecheck": "tsc --noEmit -p tsconfig.json",
111
+ "test-cov": "vp test run --coverage"
112
+ }
6
113
  }
@@ -0,0 +1,274 @@
1
+ import { useI18n } from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
2
+ import * as Sucrase from 'sucrase';
3
+ import React, { useMemo } from 'react';
4
+
5
+ import { useSnapshot } from 'valtio';
6
+ import { ElementRender } from './elementRender';
7
+ import { sanitizeUrlProps } from '../util/templateUrls';
8
+ import { UrlPolicy, defaultUrlPolicy } from '../util/urlPolicy';
9
+
10
+ /**
11
+ * Unwrap the proxy container for templates
12
+ */
13
+ function unwrapSafeProxy(proxyObj: any) {
14
+ // If it's a special proxy wrapper, return the inner value
15
+ if (proxyObj && typeof proxyObj === 'object' && '__val__' in proxyObj) {
16
+ return proxyObj.__val__;
17
+ }
18
+ return proxyObj;
19
+ }
20
+
21
+ /**
22
+ * Custom hook that only re-renders when accessed properties change.
23
+ * Supports special proxies: objects, arrays, or { _value: primitive }.
24
+ */
25
+ export function useTrackedSnapshot(proxy: any) {
26
+ return unwrapSafeProxy(useSnapshot(proxy));
27
+ }
28
+
29
+ /**
30
+ * Builds the JSX pragma, bound to a URL policy.
31
+ *
32
+ * A factory rather than a module-level "current policy" cell, because two
33
+ * templates with different `config` can be on screen at once and a shared cell
34
+ * would hand one of them the other's policy. Reading through `getPolicy` also
35
+ * means the pragma itself stays identity-stable, so the compiled component is
36
+ * not rebuilt - and the controls it slots are not remounted - when config
37
+ * changes.
38
+ */
39
+ const makeCreateElement = (getPolicy: () => UrlPolicy) =>
40
+ function createElement(type: any, props: any, ...children: any[]) {
41
+ return createElementWithPolicy(getPolicy(), type, props, ...children);
42
+ };
43
+
44
+ /**
45
+ * Custom createElement: automatically renders objects with [ElementRender],
46
+ * and applies the URL policy to whatever the template produced.
47
+ */
48
+ function createElementWithPolicy(
49
+ policy: UrlPolicy,
50
+ type: any,
51
+ props: any,
52
+ ...children: any[]
53
+ ) {
54
+ /*
55
+ Section 12's policy, applied here rather than in the engine: every element
56
+ a template produces passes through this pragma, so an `href`, `src` or
57
+ `srcSet` carrying `javascript:` is dropped before the element exists.
58
+ Doing it per engine would mean doing it three times, and forgetting it
59
+ once.
60
+ */
61
+ props = sanitizeUrlProps(type, props, policy) as any;
62
+ // Recursively process children
63
+ function processChild(child: any, fallbackKey?: string | number): any {
64
+ if (Array.isArray(child)) {
65
+ return child.map((c, i) => processChild(c, i));
66
+ }
67
+
68
+ if (React.isValidElement(child)) {
69
+ // Assign key if missing
70
+ if (child.key == null && fallbackKey != null) {
71
+ return React.cloneElement(child, { key: fallbackKey });
72
+ }
73
+ return child;
74
+ }
75
+
76
+ // If child has [ElementRender], process it
77
+ if (
78
+ child &&
79
+ typeof child === 'object' &&
80
+ typeof child[ElementRender] === 'function'
81
+ ) {
82
+ const result = processReactType(
83
+ child,
84
+ child.props || {},
85
+ ...(child.children || []),
86
+ );
87
+ if (!React.isValidElement(result)) {
88
+ throw new Error(
89
+ `[ElementRender] must return a valid React element, got ${typeof result}`,
90
+ );
91
+ }
92
+ if (result.key == null && fallbackKey != null) {
93
+ return React.cloneElement(result, { key: fallbackKey });
94
+ }
95
+ return result;
96
+ }
97
+
98
+ return child;
99
+ }
100
+
101
+ // Recursively process the type
102
+ function processReactType(
103
+ reactType: any,
104
+ typeProps: any = {},
105
+ ...typeChildren: any[]
106
+ ): any {
107
+ // If the object has ElementRender, call it
108
+ if (
109
+ reactType &&
110
+ typeof reactType === 'object' &&
111
+ typeof reactType[ElementRender] === 'function'
112
+ ) {
113
+ const elementResult = reactType[ElementRender]({
114
+ ...typeProps,
115
+ children: typeChildren,
116
+ });
117
+ if (!React.isValidElement(elementResult)) {
118
+ throw new Error(`[ElementRender] must return a valid React element`);
119
+ }
120
+ return elementResult;
121
+ }
122
+
123
+ // Standard React element
124
+ return React.createElement(reactType, typeProps, ...typeChildren);
125
+ }
126
+
127
+ // Process children
128
+ const processedChildren = children.map(processChild);
129
+
130
+ // Process main type
131
+ return processReactType(type, props, ...processedChildren);
132
+ }
133
+
134
+ interface TemplateDiagnosticProps {
135
+ error: unknown;
136
+ template: string;
137
+ availableVariables?: string[];
138
+ compilation?: boolean;
139
+ }
140
+
141
+ function TemplateDiagnostic({
142
+ error,
143
+ template,
144
+ availableVariables = [],
145
+ compilation = false,
146
+ }: TemplateDiagnosticProps) {
147
+ const t = useI18n();
148
+ return (
149
+ <div
150
+ role='alert'
151
+ style={{
152
+ color: 'red',
153
+ border: '1px solid currentColor',
154
+ padding: 15,
155
+ margin: 10,
156
+ overflowWrap: 'anywhere',
157
+ }}
158
+ >
159
+ <h3>
160
+ {t(compilation ? 'template.compileTitle' : 'template.runtimeTitle')}
161
+ </h3>
162
+ <p>
163
+ <strong>{t('template.errorLabel')}</strong>{' '}
164
+ {error instanceof Error ? error.message : String(error)}
165
+ </p>
166
+ {error instanceof Error && error.stack && (
167
+ <details>
168
+ <summary>{t('template.errorDetails')}</summary>
169
+ <pre>{error.stack}</pre>
170
+ </details>
171
+ )}
172
+ <details>
173
+ <summary>{t('template.source')}</summary>
174
+ <pre>{template}</pre>
175
+ </details>
176
+ <p>
177
+ {t('template.availableVariables', {
178
+ names: availableVariables.join(', ') || t('template.noVariables'),
179
+ })}
180
+ </p>
181
+ </div>
182
+ );
183
+ }
184
+
185
+ class TemplateErrorBoundary extends React.Component<
186
+ React.PropsWithChildren<{ template: string; availableVariables: string[] }>,
187
+ { error?: Error }
188
+ > {
189
+ state: { error?: Error } = {};
190
+ static getDerivedStateFromError(error: Error) {
191
+ return { error };
192
+ }
193
+ render() {
194
+ return this.state.error ? (
195
+ <TemplateDiagnostic {...this.props} error={this.state.error} />
196
+ ) : (
197
+ this.props.children
198
+ );
199
+ }
200
+ }
201
+
202
+ interface CompiledTemplateProps {
203
+ runtimePragma: ReturnType<typeof makeCreateElement>;
204
+ bindings: Record<string, unknown>;
205
+ }
206
+
207
+ // Define the type for the props received by the dynamic component
208
+ export default function DynamicJSXRenderer({
209
+ jsxTemplate,
210
+ props,
211
+ urlPolicy,
212
+ }: any) {
213
+ // Policy changes update the compiled template's runtime bindings without
214
+ // replacing its component type or discarding a child's draft.
215
+ const policy = urlPolicy ?? defaultUrlPolicy;
216
+ const pragma = useMemo(() => makeCreateElement(() => policy), [policy]);
217
+ // Compile once when jsxTemplate changes
218
+ const Component = useMemo<React.ComponentType<CompiledTemplateProps>>(() => {
219
+ try {
220
+ // Compile JSX with the custom element pragma.
221
+ const { code } = Sucrase.transform(jsxTemplate, {
222
+ transforms: ['jsx'],
223
+ jsxPragma: 'createElement',
224
+ });
225
+
226
+ // Create a new function that takes React and props as arguments
227
+ // It must return the component "Template"
228
+ // We expect the template string to define a function named Template
229
+ // e.g. function Template(props) { return <div>Hello {props.name}</div>; }
230
+ const fn = new Function(
231
+ 'React',
232
+ 'createElement',
233
+ 'useTrackedSnapshot',
234
+ `"use strict"; ${code}; return Template;`,
235
+ );
236
+ // The compiled component remains stable across locale and data changes.
237
+ const SafeTemplate = React.memo(function SafeTemplate({
238
+ runtimePragma,
239
+ bindings,
240
+ }: CompiledTemplateProps) {
241
+ const RenderTemplate = useMemo(
242
+ () => fn(React, runtimePragma, useTrackedSnapshot),
243
+ [runtimePragma],
244
+ );
245
+ return RenderTemplate(bindings);
246
+ });
247
+ SafeTemplate.displayName = 'SafeTemplate';
248
+ return SafeTemplate;
249
+ } catch (err) {
250
+ console.error('JSX compile error:', err);
251
+ // Fallback component if compile fails
252
+ const CompileError = React.memo<CompiledTemplateProps>(({ bindings }) => (
253
+ <TemplateDiagnostic
254
+ error={err}
255
+ template={jsxTemplate}
256
+ availableVariables={Object.keys(bindings)}
257
+ compilation
258
+ />
259
+ ));
260
+ CompileError.displayName = 'TemplateCompileError';
261
+ return CompileError;
262
+ }
263
+ }, [jsxTemplate]);
264
+
265
+ return (
266
+ <TemplateErrorBoundary
267
+ template={jsxTemplate}
268
+ availableVariables={Object.keys(props)}
269
+ >
270
+ {/* eslint-disable-next-line react/static-components -- Runtime templates must be compiled; the component type is memoized until its source changes. */}
271
+ <Component runtimePragma={pragma} bindings={props} />
272
+ </TemplateErrorBoundary>
273
+ );
274
+ }