@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,258 @@
1
+ import {
2
+ useDynamicElements,
3
+ useDynamicExternal,
4
+ } from '@chobantonov/jsonforms-react-renderer-common/dynamic/DynamicUiProvider';
5
+ import {
6
+ Actions,
7
+ JsonSchema,
8
+ LayoutProps,
9
+ RankedTester,
10
+ Translator,
11
+ and,
12
+ rankWith,
13
+ uiTypeIs,
14
+ } from '@jsonforms/core';
15
+ import {
16
+ JsonFormsDispatch,
17
+ useJsonForms,
18
+ withJsonFormsLayoutProps,
19
+ } from '@jsonforms/react';
20
+ import React, { useCallback, useEffect, useMemo } from 'react';
21
+ import type { ExtendedUISchemaElement } from '../core/uiSchema';
22
+ import { useNamedChildren } from '../util/childNames';
23
+ import { buildFormContext, type FormContext } from '../util/formContext';
24
+ import { useHandleAction } from '../util/actionContext';
25
+ import { UrlPolicy, resolveUrlPolicy } from '../util/urlPolicy';
26
+ import type { TemplateLayoutElement } from '../util/templateLang';
27
+
28
+ /**
29
+ * A `TemplateLayout` whose template is a **function**, not a string.
30
+ *
31
+ * The same element, authored in TypeScript instead of serialized. Nothing is
32
+ * compiled from a string, so there is no parser and - importantly - **no
33
+ * `allowScriptEvaluation`**: the permission exists because string evaluation
34
+ * needs CSP `unsafe-eval`, and a function the build already compiled needs
35
+ * nothing of the sort. A TSX template is strictly the safer of the two forms.
36
+ *
37
+ * It is not portable, and that is the deal: a function cannot be serialized,
38
+ * so a form carrying one cannot be sent over the wire. The portable
39
+ * `TemplateLayoutElement.template` stays a `string`; only the React-specific
40
+ * `ReactTemplateLayoutElement` widens it.
41
+ */
42
+
43
+ /** What a `Slot` receives. `children` is the fallback, per section 13. */
44
+ export interface SlotProps {
45
+ name: string;
46
+ children?: React.ReactNode;
47
+ }
48
+
49
+ /**
50
+ * Everything a TSX template is handed.
51
+ *
52
+ * Deliberately close to what the string engines expose, so the three forms
53
+ * stay one idea - plus the pieces a function can actually use: the element
54
+ * definitions for inspection, a guarded write, and the layout's own schema
55
+ * and path.
56
+ */
57
+ export interface TemplateRenderProps<T = unknown> {
58
+ /** The whole form's data, as the string engines' `data` binding. */
59
+ data: T;
60
+ /** Where this layout sits, for addressing a write. */
61
+ path: string;
62
+ /** Core schema errors, unfiltered. */
63
+ errors: unknown[];
64
+ /** The schema at this layout's path. */
65
+ schema: JsonSchema;
66
+ /** This element, for its own `options`. */
67
+ uischema: TemplateLayoutElement;
68
+ /**
69
+ * The child **elements**, by name - the definitions, not rendered output.
70
+ * Read `.type`, `.scope`, `.options`, or reach a layout's own `.elements`.
71
+ */
72
+ elements: Record<string, ExtendedUISchemaElement>;
73
+ /** Places a named child, with optional fallback content. */
74
+ Slot: (props: SlotProps) => React.ReactElement | null;
75
+ /** False when the form or this subtree is disabled or read-only. */
76
+ enabled: boolean;
77
+ /**
78
+ * True when the form is read-only.
79
+ *
80
+ * Distinct from `enabled` on purpose. A read-only form sets `enabled` to
81
+ * false as well, but the two mean different things to a widget: *disabled*
82
+ * is "not available right now", *read-only* is "display this, do not edit
83
+ * it" - and the second often wants text rather than a greyed-out input.
84
+ */
85
+ readonly: boolean;
86
+ /**
87
+ * Writes through the normal change dispatch.
88
+ *
89
+ * Section 13 requires that a template's two-way binding "must not bypass
90
+ * readonly/restrict or normal form change dispatch", so this is a no-op
91
+ * while `enabled` is false. It does **not** know about `restrict`, which is
92
+ * per-renderer and has no shared guard a layout can consult - a template can
93
+ * still write past an array's `maxItems`. Recorded rather than pretended
94
+ * away.
95
+ */
96
+ handleChange: (path: string, value: unknown) => void;
97
+ translate: Translator;
98
+ /**
99
+ * Section 12's URL policy, resolved from the form's config.
100
+ *
101
+ * **This profile has to apply it itself**, and that is not an oversight.
102
+ * The string engines route every element through a `createElement` pragma
103
+ * this package owns, so an `href` carrying `javascript:` is dropped before
104
+ * the element exists. A TSX template is compiled by the *build*, with
105
+ * React's own pragma, and there is nothing to intercept.
106
+ *
107
+ * The distinction is real: a string template is **data** and may arrive with
108
+ * the form; a TSX template is **code** in this repository, which can already
109
+ * do anything. What is still data either way is the *value* - so a template
110
+ * writing `href={data.url}` should pass it through
111
+ * `isAllowedUrl(url, urlPolicy)` first, which is why the policy is handed
112
+ * over here rather than left to be imported and resolved again.
113
+ */
114
+ urlPolicy: UrlPolicy;
115
+ /** Section 3's `FormContext`, in full. */
116
+ context: FormContext;
117
+ }
118
+
119
+ export type TemplateRender<T = unknown> = (
120
+ props: TemplateRenderProps<T>,
121
+ ) => React.ReactNode;
122
+
123
+ /** The React-only element: `template` may be a function here. */
124
+ export type ReactTemplateLayoutElement<T = unknown> = Omit<
125
+ TemplateLayoutElement,
126
+ 'template'
127
+ > & { template: string | TemplateRender<T> };
128
+
129
+ /**
130
+ * Selected by the **shape of `template`**, not by `lang`.
131
+ *
132
+ * A function is not written in a template language, so routing it through
133
+ * `lang` would be the wrong axis - and it must win over the string engines,
134
+ * which a shared `uiTypeIs('TemplateLayout')` alone would not guarantee.
135
+ */
136
+ export const tsxTemplateLayoutTester: RankedTester = rankWith(
137
+ 5,
138
+ and(
139
+ uiTypeIs('TemplateLayout'),
140
+ (uischema) =>
141
+ typeof (uischema as { template?: unknown } | undefined)?.template ===
142
+ 'function',
143
+ ),
144
+ );
145
+
146
+ type SlotContextValue = Pick<
147
+ LayoutProps,
148
+ 'schema' | 'path' | 'enabled' | 'renderers' | 'cells'
149
+ > & {
150
+ byName: Record<string, ExtendedUISchemaElement>;
151
+ };
152
+ const SlotContext = React.createContext<SlotContextValue | undefined>(
153
+ undefined,
154
+ );
155
+ const Slot = ({ name, children: fallback }: SlotProps) => {
156
+ const current = React.useContext(SlotContext);
157
+ const element = current?.byName[name];
158
+ if (!current || !element) {
159
+ // oxlint-disable-next-line no-console
160
+ console.warn(
161
+ `template.unknownSlot: no child is named ${JSON.stringify(name)}; known names are ${JSON.stringify(Object.keys(current?.byName ?? {}))}.`,
162
+ );
163
+ return <>{fallback ?? null}</>;
164
+ }
165
+ return (
166
+ <JsonFormsDispatch
167
+ uischema={element}
168
+ schema={current.schema}
169
+ path={current.path}
170
+ enabled={current.enabled}
171
+ renderers={current.renderers}
172
+ cells={current.cells}
173
+ />
174
+ );
175
+ };
176
+
177
+ export const TsxTemplateLayoutRendererComponent = (
178
+ props: LayoutProps & { uischema: ReactTemplateLayoutElement },
179
+ ) => {
180
+ const { uischema, schema, path, enabled, visible, renderers, cells } = props;
181
+ const ctx = useJsonForms();
182
+ const external = useDynamicExternal();
183
+ const fireActionEvent = useHandleAction();
184
+
185
+ const { children, names, diagnostics } = useNamedChildren(uischema.elements);
186
+
187
+ useEffect(() => {
188
+ for (const diagnostic of diagnostics) {
189
+ // oxlint-disable-next-line no-console
190
+ console.warn(diagnostic);
191
+ }
192
+ }, [diagnostics]);
193
+
194
+ const effectiveChildren = useDynamicElements(children, path ?? '');
195
+ const byName = useMemo(() => {
196
+ const map: Record<string, ExtendedUISchemaElement> = {};
197
+ effectiveChildren.forEach((element, index) => {
198
+ const name = names[index];
199
+ if (name !== undefined) {
200
+ map[name] = element;
201
+ }
202
+ });
203
+ return map;
204
+ }, [effectiveChildren, names]);
205
+
206
+ const handleChange = useCallback(
207
+ (target: string, value: unknown) => {
208
+ if (enabled === false) {
209
+ return;
210
+ }
211
+ ctx.dispatch?.(Actions.update(target, () => value));
212
+ },
213
+ [ctx, enabled],
214
+ );
215
+
216
+ if (visible === false) {
217
+ return null;
218
+ }
219
+
220
+ const template = uischema.template;
221
+ if (typeof template !== 'function') {
222
+ // The tester guarantees otherwise; this keeps the type honest.
223
+ return null;
224
+ }
225
+
226
+ return (
227
+ <SlotContext.Provider
228
+ value={{ byName, schema, path, enabled, renderers, cells }}
229
+ >
230
+ {template({
231
+ data: ctx.core?.data,
232
+ path: path ?? '',
233
+ errors: ctx.core?.errors ?? [],
234
+ schema,
235
+ uischema: uischema as TemplateLayoutElement,
236
+ elements: byName,
237
+ Slot,
238
+ enabled: enabled !== false,
239
+ readonly: (ctx as { readonly?: boolean }).readonly === true,
240
+ handleChange,
241
+ translate: (ctx.i18n?.translate ??
242
+ ((key: string) => key)) as Translator,
243
+ urlPolicy: resolveUrlPolicy((ctx as { config?: unknown }).config),
244
+ context: buildFormContext({
245
+ ctx: ctx as any,
246
+ schema,
247
+ uischema,
248
+ fireActionEvent,
249
+ external,
250
+ }),
251
+ })}
252
+ </SlotContext.Provider>
253
+ );
254
+ };
255
+
256
+ export const TsxTemplateLayoutRenderer = withJsonFormsLayoutProps(
257
+ TsxTemplateLayoutRendererComponent as any,
258
+ );
@@ -0,0 +1,14 @@
1
+ export * from './TemplateLayoutRenderer';
2
+ export * from './TsxTemplateLayoutRenderer';
3
+ export * from './ButtonRenderer';
4
+ export * from './GenericMixedRenderer';
5
+ export * from './TemplateRenderer';
6
+ export * from './SlotRenderer';
7
+ export * from './SpacerRenderer';
8
+ export * from './ImageViewRenderer';
9
+ export * from './SeparatorRenderer';
10
+ export * from './HorizontalLayoutRenderer';
11
+
12
+ export * from './SplitLayoutRenderer';
13
+ export * from './LinkRenderer';
14
+ export * from './MarkupLabelRenderer';
@@ -0,0 +1,100 @@
1
+ import { useI18n } from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
2
+ import {
3
+ RankedTester,
4
+ UISchemaElement,
5
+ rankWith,
6
+ uiTypeIs,
7
+ } from '@jsonforms/core';
8
+ import React from 'react';
9
+ import {
10
+ TemplateLang,
11
+ resolveTemplateEngine,
12
+ resolveTemplateLang,
13
+ } from '../util/templateLang';
14
+
15
+ /**
16
+ * Selecting a template engine, and refusing to run one.
17
+ *
18
+ * A `TemplateLayout` is dispatched to a renderer per **language**, so each
19
+ * engine is an ordinary registry entry with its own tester rather than a
20
+ * branch inside one component. The tester is where the choice belongs: a
21
+ * `TesterContext` carries the form-wide `config`, which is what
22
+ * `defaultTemplateLang` lives in.
23
+ */
24
+
25
+ /**
26
+ * A tester for one engine.
27
+ *
28
+ * Deliberately does **not** consult `allowScriptEvaluation`. A tester that
29
+ * returned `-1` for a disabled host would leave the element unmatched and the
30
+ * form silent; the specification asks for the opposite - "report
31
+ * unsupported/evaluation-disabled behavior instead". So the renderer is still
32
+ * selected, and it explains itself.
33
+ */
34
+ export const templateLangTester = (lang: TemplateLang): RankedTester =>
35
+ rankWith(2, (uischema, _schema, context) => {
36
+ if (!uiTypeIs('TemplateLayout')(uischema, _schema, context)) {
37
+ return false;
38
+ }
39
+ return resolveTemplateLang(uischema, context?.config).lang === lang;
40
+ });
41
+
42
+ /**
43
+ * The fallback for a `TemplateLayout` no engine claimed.
44
+ *
45
+ * "Unknown or unsupported languages must be diagnosed rather than interpreted
46
+ * as another engine." Ranked below the engines, so it only wins when the
47
+ * resolution produced no supported language.
48
+ */
49
+ export const unsupportedTemplateLangTester: RankedTester = rankWith(
50
+ 1,
51
+ uiTypeIs('TemplateLayout'),
52
+ );
53
+
54
+ export interface TemplateDiagnosticProps {
55
+ uischema: UISchemaElement;
56
+ config?: unknown;
57
+ visible?: boolean;
58
+ }
59
+
60
+ const DIAGNOSTIC_STYLE: React.CSSProperties = {
61
+ border: '1px solid currentColor',
62
+ borderRadius: 4,
63
+ padding: '0.75rem 1rem',
64
+ opacity: 0.85,
65
+ fontSize: '0.9rem',
66
+ };
67
+
68
+ /**
69
+ * Says why a template is not being rendered, in place of rendering it.
70
+ *
71
+ * Both reasons are reported the same way and neither is an error state the
72
+ * form can recover from by itself: one is an authoring mistake, the other a
73
+ * deliberate host policy.
74
+ */
75
+ export const TemplateDiagnostic = ({
76
+ uischema,
77
+ config,
78
+ visible,
79
+ }: TemplateDiagnosticProps) => {
80
+ const text = useI18n();
81
+ const resolved = resolveTemplateEngine(uischema, config);
82
+ if (visible === false || !resolved.diagnostic) {
83
+ return null;
84
+ }
85
+ const message =
86
+ resolved.diagnostic === 'unsupported'
87
+ ? text('template.unsupported', {
88
+ language: JSON.stringify(resolved.requested),
89
+ })
90
+ : text('template.disabled');
91
+ return (
92
+ <div
93
+ role='alert'
94
+ data-template-diagnostic={resolved.diagnostic}
95
+ style={DIAGNOSTIC_STYLE}
96
+ >
97
+ {message}
98
+ </div>
99
+ );
100
+ };
@@ -0,0 +1,29 @@
1
+ import React from 'react';
2
+ import { UISchemaElement } from '@jsonforms/core';
3
+
4
+ export type ActionEvent = {
5
+ action: string;
6
+ label: string;
7
+ /**
8
+ * `params` from the element, passed through untouched.
9
+ *
10
+ * Section 14 makes this a top-level field on `Button`, and it is how one
11
+ * command serves several buttons - `setLocale` with `{ "locale": "bg" }`
12
+ * rather than a `setLocaleBg` action per language.
13
+ */
14
+ params?: Record<string, unknown>;
15
+ /** The UI schema element that triggered the action. */
16
+ element: UISchemaElement;
17
+ /**
18
+ * The JSON Forms context, reachable from a `script` as `this.context`.
19
+ */
20
+ context?: unknown;
21
+ };
22
+
23
+ export type HandleAction = (event: ActionEvent) => void | Promise<void>;
24
+
25
+ export const HandleActionContext = React.createContext<
26
+ HandleAction | undefined
27
+ >(undefined);
28
+
29
+ export const useHandleAction = () => React.useContext(HandleActionContext);