@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.
- package/LICENSE +22 -0
- package/README.md +17 -2
- package/lib/AgGridControlRenderer.impl-BO-Z1x0Y.js +2 -0
- package/lib/AgGridControlRenderer.impl-BO-Z1x0Y.js.map +1 -0
- package/lib/AgGridControlRenderer.impl-BZS8_ocu.mjs +429 -0
- package/lib/AgGridControlRenderer.impl-BZS8_ocu.mjs.map +1 -0
- package/lib/DynamicJSXRenderer-Cyw40Kz0.js +2 -0
- package/lib/DynamicJSXRenderer-Cyw40Kz0.js.map +1 -0
- package/lib/DynamicJSXRenderer-DyB3ZpZV.mjs +109 -0
- package/lib/DynamicJSXRenderer-DyB3ZpZV.mjs.map +1 -0
- package/lib/MarkupLabelRenderer.impl-CWfdcUuH.mjs +12 -0
- package/lib/MarkupLabelRenderer.impl-CWfdcUuH.mjs.map +1 -0
- package/lib/MarkupLabelRenderer.impl-DnuzIPQg.js +2 -0
- package/lib/MarkupLabelRenderer.impl-DnuzIPQg.js.map +1 -0
- package/lib/MonacoControlRenderer.impl-BwgLGNMK.js +3 -0
- package/lib/MonacoControlRenderer.impl-BwgLGNMK.js.map +1 -0
- package/lib/MonacoControlRenderer.impl-CyjMHbOd.mjs +186 -0
- package/lib/MonacoControlRenderer.impl-CyjMHbOd.mjs.map +1 -0
- package/lib/RactiveTemplate-7L44A38e.js +2 -0
- package/lib/RactiveTemplate-7L44A38e.js.map +1 -0
- package/lib/RactiveTemplate-Cat7CxUp.mjs +74 -0
- package/lib/RactiveTemplate-Cat7CxUp.mjs.map +1 -0
- package/lib/ajv-localizers.cjs.js +2 -0
- package/lib/ajv-localizers.cjs.js.map +1 -0
- package/lib/ajv-localizers.esm.js +7 -0
- package/lib/ajv-localizers.esm.js.map +1 -0
- package/lib/ajvI18n-2UTyJv5g.js +2 -0
- package/lib/ajvI18n-2UTyJv5g.js.map +1 -0
- package/lib/ajvI18n-DO8FFvx1.mjs +227 -0
- package/lib/ajvI18n-DO8FFvx1.mjs.map +1 -0
- package/lib/celTemplate-Cl0alaCz.js +3 -0
- package/lib/celTemplate-Cl0alaCz.js.map +1 -0
- package/lib/celTemplate-D_AGhaIL.mjs +137 -0
- package/lib/celTemplate-D_AGhaIL.mjs.map +1 -0
- package/lib/components/DynamicJSXRenderer.d.ts +7 -0
- package/lib/components/ExtendedJsonForms.d.ts +80 -0
- package/lib/components/GenericAdditionalProperties.d.ts +4 -0
- package/lib/components/InterpolatedText.d.ts +34 -0
- package/lib/components/RactiveTemplate.d.ts +37 -0
- package/lib/components/editorRendererTypes.d.ts +139 -0
- package/lib/components/elementRender.d.ts +15 -0
- package/lib/configNamespaces-BFclHtB8.mjs +1 -0
- package/lib/configNamespaces-sboXDudL.js +1 -0
- package/lib/core/ajv.d.ts +82 -0
- package/lib/core/ajvI18n/bg.d.ts +2 -0
- package/lib/core/ajvI18n/index.d.ts +118 -0
- package/lib/core/ajvI18n/localizers.d.ts +20 -0
- package/lib/core/dynamicDefaults.d.ts +34 -0
- package/lib/core/keywords.d.ts +24 -0
- package/lib/core/transform.d.ts +29 -0
- package/lib/core/uiSchema.d.ts +48 -0
- package/lib/elementRender-Dbp2e-Cc.js +2 -0
- package/lib/elementRender-Dbp2e-Cc.js.map +1 -0
- package/lib/elementRender-y8MK9CCE.mjs +5 -0
- package/lib/elementRender-y8MK9CCE.mjs.map +1 -0
- package/lib/index.d.ts +41 -0
- package/lib/jsonforms-react-extended.cjs.js +16 -0
- package/lib/jsonforms-react-extended.cjs.js.map +1 -0
- package/lib/jsonforms-react-extended.esm.js +2335 -0
- package/lib/jsonforms-react-extended.esm.js.map +1 -0
- package/lib/locale/bg.d.ts +14 -0
- package/lib/markdown-C_y73Q0w.mjs +123 -0
- package/lib/markdown-C_y73Q0w.mjs.map +1 -0
- package/lib/markdown-CuqtpGVd.js +2 -0
- package/lib/markdown-CuqtpGVd.js.map +1 -0
- package/lib/objectWithoutProperties-6zedhz76.js +2 -0
- package/lib/objectWithoutProperties-6zedhz76.js.map +1 -0
- package/lib/objectWithoutProperties-yjQMyZ0y.mjs +58 -0
- package/lib/objectWithoutProperties-yjQMyZ0y.mjs.map +1 -0
- package/lib/renderers/AgGridControlRenderer.d.ts +10 -0
- package/lib/renderers/AgGridControlRenderer.impl.d.ts +4 -0
- package/lib/renderers/ButtonRenderer.d.ts +83 -0
- package/lib/renderers/GenericMixedRenderer.d.ts +9 -0
- package/lib/renderers/HorizontalLayoutRenderer.d.ts +5 -0
- package/lib/renderers/ImageViewRenderer.d.ts +48 -0
- package/lib/renderers/LinkRenderer.d.ts +23 -0
- package/lib/renderers/MarkupLabelRenderer.d.ts +35 -0
- package/lib/renderers/MarkupLabelRenderer.impl.d.ts +17 -0
- package/lib/renderers/MonacoControlRenderer.d.ts +8 -0
- package/lib/renderers/MonacoControlRenderer.impl.d.ts +4 -0
- package/lib/renderers/RactiveTemplateLayoutRenderer.d.ts +6 -0
- package/lib/renderers/SeparatorRenderer.d.ts +17 -0
- package/lib/renderers/SlotRenderer.d.ts +5 -0
- package/lib/renderers/SpacerRenderer.d.ts +5 -0
- package/lib/renderers/SplitLayoutRenderer.d.ts +18 -0
- package/lib/renderers/TemplateLayoutRenderer.d.ts +13 -0
- package/lib/renderers/TemplateRenderer.d.ts +17 -0
- package/lib/renderers/TsxTemplateLayoutRenderer.d.ts +111 -0
- package/lib/renderers/index.d.ts +13 -0
- package/lib/renderers/templateEngines.d.ts +43 -0
- package/lib/util/actionContext.d.ts +23 -0
- package/lib/util/additionalErrors.d.ts +77 -0
- package/lib/util/celEnvironment.d.ts +23 -0
- package/lib/util/celTemplate.d.ts +47 -0
- package/lib/util/childNames.d.ts +46 -0
- package/lib/util/configNamespaces.d.ts +1 -0
- package/lib/util/cron.d.ts +122 -0
- package/lib/util/editorControls.d.ts +8 -0
- package/lib/util/editorDiagnostics.d.ts +56 -0
- package/lib/util/extendedControls.d.ts +4 -0
- package/lib/util/extendedLocale.d.ts +32 -0
- package/lib/util/fileFilterValue.d.ts +3 -0
- package/lib/util/formContext.d.ts +54 -0
- package/lib/util/gridColumns.d.ts +6 -0
- package/lib/util/horizontalLayout.d.ts +10 -0
- package/lib/util/i18nDefaults.d.ts +45 -0
- package/lib/util/interpolate.d.ts +105 -0
- package/lib/util/labelFilterValue.d.ts +2 -0
- package/lib/util/lazyControl.d.ts +36 -0
- package/lib/util/lazyTemplate.d.ts +18 -0
- package/lib/util/markdown.d.ts +55 -0
- package/lib/util/markdownEscape.d.ts +27 -0
- package/lib/util/markup.d.ts +74 -0
- package/lib/util/maskControls.d.ts +2 -0
- package/lib/util/templateLang.d.ts +87 -0
- package/lib/util/templateUrls.d.ts +40 -0
- package/lib/util/urlPolicy.d.ts +42 -0
- package/lib/util/useCronPicker.d.ts +24 -0
- package/lib/util/useDurationControl.d.ts +99 -0
- package/lib/util/useEditorAppearance.d.ts +21 -0
- package/lib/util/useExtendedTranslator.d.ts +10 -0
- package/lib/util/useMonacoShadowStyles.d.ts +2 -0
- package/package.json +110 -3
- package/src/components/DynamicJSXRenderer.tsx +274 -0
- package/src/components/ExtendedJsonForms.tsx +329 -0
- package/src/components/GenericAdditionalProperties.tsx +362 -0
- package/src/components/InterpolatedText.tsx +114 -0
- package/src/components/RactiveTemplate.tsx +161 -0
- package/src/components/editorRendererTypes.ts +135 -0
- package/src/components/elementRender.ts +15 -0
- package/src/core/ajv.ts +177 -0
- package/src/core/ajvI18n/bg.ts +203 -0
- package/src/core/ajvI18n/index.ts +267 -0
- package/src/core/ajvI18n/localizers.ts +25 -0
- package/src/core/dynamicDefaults.ts +170 -0
- package/src/core/keywords.ts +71 -0
- package/src/core/transform.ts +137 -0
- package/src/core/uiSchema.ts +53 -0
- package/src/index.tsx +196 -0
- package/src/locale/bg.ts +51 -0
- package/src/renderers/AgGridControlRenderer.impl.tsx +788 -0
- package/src/renderers/AgGridControlRenderer.tsx +24 -0
- package/src/renderers/ButtonRenderer.tsx +328 -0
- package/src/renderers/GenericMixedRenderer.tsx +224 -0
- package/src/renderers/HorizontalLayoutRenderer.tsx +93 -0
- package/src/renderers/ImageViewRenderer.tsx +328 -0
- package/src/renderers/LinkRenderer.tsx +92 -0
- package/src/renderers/MarkupLabelRenderer.impl.tsx +26 -0
- package/src/renderers/MarkupLabelRenderer.tsx +327 -0
- package/src/renderers/MonacoControlRenderer.impl.tsx +351 -0
- package/src/renderers/MonacoControlRenderer.tsx +24 -0
- package/src/renderers/RactiveTemplateLayoutRenderer.tsx +172 -0
- package/src/renderers/SeparatorRenderer.tsx +55 -0
- package/src/renderers/SlotRenderer.tsx +34 -0
- package/src/renderers/SpacerRenderer.tsx +69 -0
- package/src/renderers/SplitLayoutRenderer.tsx +220 -0
- package/src/renderers/TemplateLayoutRenderer.tsx +394 -0
- package/src/renderers/TemplateRenderer.tsx +71 -0
- package/src/renderers/TsxTemplateLayoutRenderer.tsx +258 -0
- package/src/renderers/index.ts +14 -0
- package/src/renderers/templateEngines.tsx +100 -0
- package/src/util/actionContext.ts +29 -0
- package/src/util/additionalErrors.tsx +391 -0
- package/src/util/celEnvironment.ts +300 -0
- package/src/util/celTemplate.ts +267 -0
- package/src/util/childNames.ts +135 -0
- package/src/util/configNamespaces.ts +6 -0
- package/src/util/cron.ts +433 -0
- package/src/util/editorControls.ts +58 -0
- package/src/util/editorDiagnostics.ts +103 -0
- package/src/util/extendedControls.ts +34 -0
- package/src/util/extendedLocale.ts +67 -0
- package/src/util/fileFilterValue.ts +28 -0
- package/src/util/formContext.ts +74 -0
- package/src/util/gridColumns.ts +80 -0
- package/src/util/horizontalLayout.ts +59 -0
- package/src/util/i18nDefaults.ts +80 -0
- package/src/util/interpolate.ts +218 -0
- package/src/util/labelFilterValue.ts +49 -0
- package/src/util/lazyControl.tsx +66 -0
- package/src/util/lazyTemplate.tsx +110 -0
- package/src/util/markdown.tsx +345 -0
- package/src/util/markdownEscape.ts +29 -0
- package/src/util/markup.ts +144 -0
- package/src/util/maskControls.ts +6 -0
- package/src/util/templateLang.ts +145 -0
- package/src/util/templateUrls.ts +274 -0
- package/src/util/urlPolicy.ts +115 -0
- package/src/util/useCronPicker.ts +102 -0
- package/src/util/useDurationControl.ts +272 -0
- package/src/util/useEditorAppearance.ts +86 -0
- package/src/util/useExtendedTranslator.ts +25 -0
- package/src/util/useMonacoShadowStyles.ts +47 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ControlProps } from '@jsonforms/core';
|
|
3
|
+
import { withJsonFormsControlProps } from '@jsonforms/react';
|
|
4
|
+
import { createLazyControl } from '../util/lazyControl';
|
|
5
|
+
import { EditorRendererComponents } from '../components/editorRendererTypes';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ag-grid-community + ag-grid-react are ~1MB; importing them here would put
|
|
9
|
+
* them in the main bundle for every form, so the implementation is fetched
|
|
10
|
+
* only once a grid control is actually rendered.
|
|
11
|
+
*/
|
|
12
|
+
export const createAgGridControl = (components: EditorRendererComponents) =>
|
|
13
|
+
createLazyControl(
|
|
14
|
+
async () =>
|
|
15
|
+
(await import('./AgGridControlRenderer.impl')).createAgGridControl(
|
|
16
|
+
components,
|
|
17
|
+
) as React.ComponentType<ControlProps>,
|
|
18
|
+
components,
|
|
19
|
+
{ loading: 'grid.loading', error: 'grid.loadError' },
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
export const createAgGridControlRenderer = (
|
|
23
|
+
components: EditorRendererComponents,
|
|
24
|
+
) => withJsonFormsControlProps(createAgGridControl(components));
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { useI18n } from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
|
|
2
|
+
import React, {
|
|
3
|
+
useMemo,
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
} from 'react';
|
|
9
|
+
import {
|
|
10
|
+
BaseUISchemaElement,
|
|
11
|
+
LayoutProps,
|
|
12
|
+
RankedTester,
|
|
13
|
+
UISchemaElement,
|
|
14
|
+
deriveLabelForUISchemaElement,
|
|
15
|
+
rankWith,
|
|
16
|
+
uiTypeIs,
|
|
17
|
+
} from '@jsonforms/core';
|
|
18
|
+
import {
|
|
19
|
+
TranslateProps,
|
|
20
|
+
useJsonForms,
|
|
21
|
+
withJsonFormsLayoutProps,
|
|
22
|
+
withTranslateProps,
|
|
23
|
+
} from '@jsonforms/react';
|
|
24
|
+
import { useHandleAction, type ActionEvent } from '../util/actionContext';
|
|
25
|
+
import { allowsScriptEvaluation } from '../util/templateLang';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* `Button` - invoking a command, as opposed to `Link`, which navigates.
|
|
29
|
+
*
|
|
30
|
+
* Section 14 puts `label`, `icon`, `color`, `params`, `action` and `script`
|
|
31
|
+
* at the **top level** of the element. `options` is still read underneath
|
|
32
|
+
* them, because forms in this repository were authored that way.
|
|
33
|
+
*
|
|
34
|
+
* `action` and `script` are mutually exclusive. The action path hands an
|
|
35
|
+
* `ActionEvent` to the host and awaits it; the script path runs a string
|
|
36
|
+
* directly and is gated on the script-evaluation permission.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
export type ButtonSemanticColor =
|
|
40
|
+
| 'primary'
|
|
41
|
+
| 'secondary'
|
|
42
|
+
| 'alternative'
|
|
43
|
+
| 'success'
|
|
44
|
+
| 'warning'
|
|
45
|
+
| 'error';
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A `script` written in TypeScript rather than serialized.
|
|
49
|
+
*
|
|
50
|
+
* The event arrives as the **first argument**. `this` is bound to it as well,
|
|
51
|
+
* so a body pasted across from the string form keeps working - but an
|
|
52
|
+
* argument is the idiom, because `.call()` cannot bind an arrow function's
|
|
53
|
+
* `this` and an arrow is what anyone writes by reflex:
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* script: (event) => console.log(event.context) // works
|
|
57
|
+
* script: function () { console.log(this.context); } // also works
|
|
58
|
+
* script: () => console.log(this.context) // `this` is undefined
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* Section 14 places function-valued scripts outside the portable model, so
|
|
62
|
+
* this is a build-time authoring convenience and never appears on the wire.
|
|
63
|
+
*/
|
|
64
|
+
export type ButtonScript =
|
|
65
|
+
| string
|
|
66
|
+
| ((this: ActionEvent, event: ActionEvent) => void | Promise<void>);
|
|
67
|
+
|
|
68
|
+
/*
|
|
69
|
+
Built on `BaseUISchemaElement`, **not** `UISchemaElement`.
|
|
70
|
+
|
|
71
|
+
In this core version `UISchemaElement` is a *union* of nine types, so
|
|
72
|
+
intersecting it distributes across all of them - the XOR below would become
|
|
73
|
+
twenty-seven members, TypeScript would report a mismatch against whichever
|
|
74
|
+
it tried last (`HorizontalLayout`, typically), and an author would be told
|
|
75
|
+
their Button was missing `elements`. `BaseUISchemaElement` is the single
|
|
76
|
+
interface those nine share, and a Button is assignable to `UISchemaElement`
|
|
77
|
+
through it either way.
|
|
78
|
+
|
|
79
|
+
The literal `type: 'Button'` is the discriminant that lets TypeScript pick
|
|
80
|
+
the right member and say something useful when it cannot.
|
|
81
|
+
*/
|
|
82
|
+
export type ButtonUiSchemaBase = BaseUISchemaElement & {
|
|
83
|
+
type: 'Button';
|
|
84
|
+
label?: string;
|
|
85
|
+
text?: string;
|
|
86
|
+
name?: string;
|
|
87
|
+
icon?: string;
|
|
88
|
+
color?: ButtonSemanticColor;
|
|
89
|
+
params?: Record<string, unknown>;
|
|
90
|
+
options?: {
|
|
91
|
+
disabled?: boolean;
|
|
92
|
+
fullWidth?: boolean;
|
|
93
|
+
[key: string]: unknown;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* `action` and `script` are **mutually exclusive**, and the type says so.
|
|
99
|
+
*
|
|
100
|
+
* Section 14 states the exclusivity without saying which wins, because an
|
|
101
|
+
* element carrying both is an authoring mistake. The `never` members make it
|
|
102
|
+
* one TypeScript refuses to compile; the runtime still has to cope, because
|
|
103
|
+
* JSON-authored forms never pass through this type.
|
|
104
|
+
*/
|
|
105
|
+
export type ButtonUiSchema =
|
|
106
|
+
| (ButtonUiSchemaBase & { action: string; script?: never })
|
|
107
|
+
| (ButtonUiSchemaBase & { script: ButtonScript; action?: never })
|
|
108
|
+
| (ButtonUiSchemaBase & { action?: never; script?: never });
|
|
109
|
+
|
|
110
|
+
export type ButtonRendererComponentProps = LayoutProps &
|
|
111
|
+
TranslateProps & {
|
|
112
|
+
uischema: ButtonUiSchema;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export type ButtonRendererOptions = {
|
|
116
|
+
/** Return the component before JSON Forms prop injection for native adapter exports. */
|
|
117
|
+
withProps?: boolean;
|
|
118
|
+
ButtonComponent?: React.ComponentType<any>;
|
|
119
|
+
buttonProps?: Record<string, any>;
|
|
120
|
+
getButtonProps?: (
|
|
121
|
+
props: ButtonRendererComponentProps & {
|
|
122
|
+
pending: boolean;
|
|
123
|
+
color?: ButtonSemanticColor;
|
|
124
|
+
icon?: string;
|
|
125
|
+
},
|
|
126
|
+
) => Record<string, any>;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Builds and runs a `script`.
|
|
131
|
+
*
|
|
132
|
+
* Section 14: "`script` is a string containing an async function body, with
|
|
133
|
+
* top-level await supported. Invoke it with the ActionEvent as `this`,
|
|
134
|
+
* equivalent to `await new AsyncFunction(script).call(source)`."
|
|
135
|
+
*/
|
|
136
|
+
const runScript = async (
|
|
137
|
+
script: ButtonScript,
|
|
138
|
+
source: ActionEvent,
|
|
139
|
+
): Promise<void> => {
|
|
140
|
+
if (typeof script === 'function') {
|
|
141
|
+
/*
|
|
142
|
+
Bound **and** passed. `.call()` cannot bind an arrow function's `this` -
|
|
143
|
+
it is lexical - so a `() => this.context` compiles, runs and quietly
|
|
144
|
+
reads the wrong thing. Handing the event as an argument as well means
|
|
145
|
+
neither idiom can be got wrong, at the cost of one word.
|
|
146
|
+
*/
|
|
147
|
+
await script.call(source, source);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const AsyncFunction = Object.getPrototypeOf(async function () {
|
|
151
|
+
/* the async function constructor is not a global */
|
|
152
|
+
}).constructor;
|
|
153
|
+
await new AsyncFunction(script).call(source);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export const buttonRendererTester: RankedTester = rankWith(
|
|
157
|
+
2,
|
|
158
|
+
uiTypeIs('Button'),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const DIAGNOSTIC_STYLE: React.CSSProperties = {
|
|
162
|
+
border: '1px solid currentColor',
|
|
163
|
+
borderRadius: 4,
|
|
164
|
+
padding: '0.5rem 0.75rem',
|
|
165
|
+
opacity: 0.85,
|
|
166
|
+
fontSize: '0.85rem',
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export const createButtonRenderer = ({
|
|
170
|
+
withProps = true,
|
|
171
|
+
ButtonComponent = 'button' as any,
|
|
172
|
+
buttonProps = {},
|
|
173
|
+
getButtonProps,
|
|
174
|
+
}: ButtonRendererOptions = {}) => {
|
|
175
|
+
const ButtonRenderer = (props: ButtonRendererComponentProps) => {
|
|
176
|
+
const { enabled, uischema, visible, config, t } = props;
|
|
177
|
+
const text = useI18n();
|
|
178
|
+
const handleAction = useHandleAction();
|
|
179
|
+
const ctx = useJsonForms();
|
|
180
|
+
|
|
181
|
+
/*
|
|
182
|
+
"Pending/loading covers the complete fireActionEvent promise. Duplicate
|
|
183
|
+
activation SHOULD be prevented while pending." The ref is what actually
|
|
184
|
+
prevents the second activation: a second click can arrive before React
|
|
185
|
+
has re-rendered with the new state.
|
|
186
|
+
*/
|
|
187
|
+
const [pending, setPending] = useState(false);
|
|
188
|
+
const running = useRef(false);
|
|
189
|
+
|
|
190
|
+
const element = uischema;
|
|
191
|
+
const label =
|
|
192
|
+
deriveLabelForUISchemaElement(element as any, t) ??
|
|
193
|
+
element.label ??
|
|
194
|
+
element.text ??
|
|
195
|
+
element.name ??
|
|
196
|
+
text('button.action');
|
|
197
|
+
/*
|
|
198
|
+
Deliberately **not** falling back to the label. The label is translated,
|
|
199
|
+
so an action derived from it would change with the form's language - a
|
|
200
|
+
host answering `setLocale` in English would stop recognising the command
|
|
201
|
+
once the form was in Bulgarian. `name` is stable and is kept.
|
|
202
|
+
*/
|
|
203
|
+
const script = element.script;
|
|
204
|
+
const action =
|
|
205
|
+
element.action ?? (script === undefined ? element.name : undefined);
|
|
206
|
+
// "Missing optional Button params are normalized to `{}` when
|
|
207
|
+
// constructing ActionEvent", so a host can read `event.params.x` without
|
|
208
|
+
// guarding every access.
|
|
209
|
+
const params = useMemo(() => element.params ?? {}, [element.params]);
|
|
210
|
+
|
|
211
|
+
const activate = useCallback(async () => {
|
|
212
|
+
if (running.current) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
running.current = true;
|
|
216
|
+
setPending(true);
|
|
217
|
+
const event = {
|
|
218
|
+
action: action ?? '',
|
|
219
|
+
label,
|
|
220
|
+
params,
|
|
221
|
+
element: element as UISchemaElement,
|
|
222
|
+
context: ctx,
|
|
223
|
+
};
|
|
224
|
+
try {
|
|
225
|
+
/*
|
|
226
|
+
**Action first.** Section 14 states the exclusivity without saying
|
|
227
|
+
which wins, so the question is which failure is least harmful when a
|
|
228
|
+
form carries both - and the answer is the one the host can see.
|
|
229
|
+
|
|
230
|
+
An action goes to the application's handler, where it can be logged,
|
|
231
|
+
refused or authorised. A script runs arbitrary code with no such
|
|
232
|
+
oversight, and the specification calls it "a last-resort,
|
|
233
|
+
non-portable runtime escape hatch". Preferring the portable,
|
|
234
|
+
auditable path is the same choice this project makes everywhere
|
|
235
|
+
else. It also happens to be the only one available when a JSON form
|
|
236
|
+
carries a string script and the host has not granted
|
|
237
|
+
`allowScriptEvaluation`.
|
|
238
|
+
*/
|
|
239
|
+
if (script !== undefined && action === undefined) {
|
|
240
|
+
await runScript(script, event);
|
|
241
|
+
} else {
|
|
242
|
+
// Also the path for a button declaring neither, which fires an
|
|
243
|
+
// event with an empty action rather than being a dead control.
|
|
244
|
+
await handleAction?.(event);
|
|
245
|
+
}
|
|
246
|
+
} finally {
|
|
247
|
+
// "Rejection clears pending and propagates through existing
|
|
248
|
+
// application/platform error handling" - so the reset is in `finally`
|
|
249
|
+
// and the rejection is not swallowed.
|
|
250
|
+
running.current = false;
|
|
251
|
+
setPending(false);
|
|
252
|
+
}
|
|
253
|
+
}, [action, label, params, script, element, handleAction, ctx]);
|
|
254
|
+
|
|
255
|
+
/*
|
|
256
|
+
Both present is an authoring mistake that the XOR type makes impossible
|
|
257
|
+
in TypeScript and cannot prevent in JSON. A warning rather than a
|
|
258
|
+
rendered message: replacing the button would break a form that works
|
|
259
|
+
today the moment somebody adds a stray `action`, and the person filling
|
|
260
|
+
it in can do nothing about either.
|
|
261
|
+
*/
|
|
262
|
+
useEffect(() => {
|
|
263
|
+
if (element.action !== undefined && element.script !== undefined) {
|
|
264
|
+
// oxlint-disable-next-line no-console
|
|
265
|
+
console.warn(
|
|
266
|
+
`action.conflict: ${JSON.stringify(
|
|
267
|
+
label,
|
|
268
|
+
)} declares both an action and a script, which are mutually exclusive. The action is used.`,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
}, [element.action, element.script, label]);
|
|
272
|
+
|
|
273
|
+
if (!visible) {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/*
|
|
278
|
+
"String evaluation requires allowScriptEvaluation=true… the renderer
|
|
279
|
+
MUST NOT weaken CSP and MUST report unsupported/evaluation-disabled
|
|
280
|
+
behavior instead." A script button that silently did nothing would look
|
|
281
|
+
like a broken command rather than a policy decision.
|
|
282
|
+
*/
|
|
283
|
+
/*
|
|
284
|
+
The gate is on **string** evaluation, which is what needs CSP
|
|
285
|
+
`unsafe-eval`. A function the build already compiled needs no such
|
|
286
|
+
permission, so requiring one would be theatre.
|
|
287
|
+
*/
|
|
288
|
+
if (typeof script === 'string' && !allowsScriptEvaluation(config)) {
|
|
289
|
+
return (
|
|
290
|
+
<div
|
|
291
|
+
role='alert'
|
|
292
|
+
data-button-diagnostic='script.evaluationDisabled'
|
|
293
|
+
style={DIAGNOSTIC_STYLE}
|
|
294
|
+
>
|
|
295
|
+
{text('button.scriptDisabled', { label })}
|
|
296
|
+
</div>
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const disabled = !enabled || element.options?.disabled === true || pending;
|
|
301
|
+
const mappedProps = getButtonProps?.({
|
|
302
|
+
...props,
|
|
303
|
+
pending,
|
|
304
|
+
color: element.color,
|
|
305
|
+
icon: element.icon,
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
return (
|
|
309
|
+
<ButtonComponent
|
|
310
|
+
type='button'
|
|
311
|
+
{...buttonProps}
|
|
312
|
+
{...mappedProps}
|
|
313
|
+
disabled={disabled}
|
|
314
|
+
aria-busy={pending || undefined}
|
|
315
|
+
data-button-pending={pending || undefined}
|
|
316
|
+
onClick={activate}
|
|
317
|
+
>
|
|
318
|
+
{label}
|
|
319
|
+
</ButtonComponent>
|
|
320
|
+
);
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
return withProps
|
|
324
|
+
? withJsonFormsLayoutProps(withTranslateProps(ButtonRenderer as any) as any)
|
|
325
|
+
: ButtonRenderer;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
export const ButtonRenderer = createButtonRenderer();
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useTranslator,
|
|
3
|
+
useI18n,
|
|
4
|
+
} from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
|
|
5
|
+
|
|
6
|
+
import { findMixedDetailUiSchema } from '@chobantonov/jsonforms-react-renderer-common/core/detailUiSchema';
|
|
7
|
+
import {
|
|
8
|
+
ControlProps,
|
|
9
|
+
createDefaultValue,
|
|
10
|
+
findUISchema,
|
|
11
|
+
isControl,
|
|
12
|
+
JsonSchema,
|
|
13
|
+
JsonSchema7,
|
|
14
|
+
rankWith,
|
|
15
|
+
RankedTester,
|
|
16
|
+
resolveSchema,
|
|
17
|
+
Scopable,
|
|
18
|
+
TesterContext,
|
|
19
|
+
UISchemaElement,
|
|
20
|
+
} from '@jsonforms/core';
|
|
21
|
+
import {
|
|
22
|
+
JsonFormsDispatch,
|
|
23
|
+
useJsonForms,
|
|
24
|
+
withJsonFormsControlProps,
|
|
25
|
+
} from '@jsonforms/react';
|
|
26
|
+
import React, { useMemo } from 'react';
|
|
27
|
+
|
|
28
|
+
import {
|
|
29
|
+
ANY_TYPES,
|
|
30
|
+
getJsonDataType,
|
|
31
|
+
getSchemaTypes,
|
|
32
|
+
mixedControlFor,
|
|
33
|
+
resolveMixedArrayItems,
|
|
34
|
+
removeInapplicableKeywords,
|
|
35
|
+
type JsonDataType,
|
|
36
|
+
} from '@chobantonov/jsonforms-react-renderer-common/mixed/mixedSchema';
|
|
37
|
+
|
|
38
|
+
export const schemaForType = (
|
|
39
|
+
schema: JsonSchema,
|
|
40
|
+
type: JsonDataType,
|
|
41
|
+
rootSchema: JsonSchema,
|
|
42
|
+
): JsonSchema => {
|
|
43
|
+
const nextSchema: JsonSchema7 = {
|
|
44
|
+
...(typeof schema === 'object' ? (schema as JsonSchema7) : {}),
|
|
45
|
+
type,
|
|
46
|
+
};
|
|
47
|
+
delete nextSchema.anyOf;
|
|
48
|
+
delete nextSchema.oneOf;
|
|
49
|
+
delete nextSchema.allOf;
|
|
50
|
+
removeInapplicableKeywords(nextSchema, type);
|
|
51
|
+
|
|
52
|
+
if (type === 'object') {
|
|
53
|
+
nextSchema.additionalProperties = nextSchema.additionalProperties ?? true;
|
|
54
|
+
} else if (type === 'array') {
|
|
55
|
+
const items = resolveMixedArrayItems(nextSchema.items, rootSchema);
|
|
56
|
+
nextSchema.items =
|
|
57
|
+
items && typeof items === 'object'
|
|
58
|
+
? (items as JsonSchema7 | JsonSchema7[])
|
|
59
|
+
: { type: ANY_TYPES as JsonSchema7['type'] };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return nextSchema;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const GenericMixedRendererComponent = ({
|
|
66
|
+
cells,
|
|
67
|
+
data,
|
|
68
|
+
enabled,
|
|
69
|
+
handleChange,
|
|
70
|
+
label,
|
|
71
|
+
path,
|
|
72
|
+
renderers,
|
|
73
|
+
readonly,
|
|
74
|
+
rootSchema,
|
|
75
|
+
schema,
|
|
76
|
+
uischema,
|
|
77
|
+
visible,
|
|
78
|
+
}: ControlProps) => {
|
|
79
|
+
const text = useI18n();
|
|
80
|
+
const translateType = useTranslator();
|
|
81
|
+
const jsonforms = useJsonForms();
|
|
82
|
+
const uischemas = useMemo(
|
|
83
|
+
() => jsonforms.uischemas ?? [],
|
|
84
|
+
[jsonforms.uischemas],
|
|
85
|
+
);
|
|
86
|
+
const types = useMemo(() => getSchemaTypes(schema), [schema]);
|
|
87
|
+
const dataType = getJsonDataType(data);
|
|
88
|
+
const selectedType =
|
|
89
|
+
dataType && types.includes(dataType)
|
|
90
|
+
? dataType
|
|
91
|
+
: dataType === 'integer' && types.includes('number')
|
|
92
|
+
? 'number'
|
|
93
|
+
: null;
|
|
94
|
+
const selectedSchema = useMemo(
|
|
95
|
+
() =>
|
|
96
|
+
selectedType
|
|
97
|
+
? schemaForType(schema, selectedType, rootSchema)
|
|
98
|
+
: undefined,
|
|
99
|
+
[rootSchema, schema, selectedType],
|
|
100
|
+
);
|
|
101
|
+
const detailUiSchema = useMemo(
|
|
102
|
+
() =>
|
|
103
|
+
selectedSchema
|
|
104
|
+
? findMixedDetailUiSchema(
|
|
105
|
+
selectedSchema,
|
|
106
|
+
uischema,
|
|
107
|
+
path,
|
|
108
|
+
rootSchema,
|
|
109
|
+
uischemas ?? [],
|
|
110
|
+
findUISchema,
|
|
111
|
+
)
|
|
112
|
+
: undefined,
|
|
113
|
+
[path, rootSchema, selectedSchema, uischema, uischemas],
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
if (!visible) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const renderedControl =
|
|
121
|
+
selectedType !== 'null' && selectedSchema && detailUiSchema ? (
|
|
122
|
+
<JsonFormsDispatch
|
|
123
|
+
schema={selectedSchema}
|
|
124
|
+
uischema={detailUiSchema}
|
|
125
|
+
path={path}
|
|
126
|
+
enabled={enabled}
|
|
127
|
+
renderers={renderers}
|
|
128
|
+
cells={cells}
|
|
129
|
+
readonly={readonly}
|
|
130
|
+
/>
|
|
131
|
+
) : null;
|
|
132
|
+
const isStructuredType =
|
|
133
|
+
selectedType === 'object' || selectedType === 'array';
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<div className='jsonforms-mixed-renderer'>
|
|
137
|
+
<label className='jsonforms-mixed-renderer-type'>
|
|
138
|
+
{label ? <span>{label}</span> : null}
|
|
139
|
+
<select
|
|
140
|
+
disabled={!enabled}
|
|
141
|
+
value={selectedType ?? ''}
|
|
142
|
+
onChange={(event) => {
|
|
143
|
+
const nextType = event.currentTarget.value as JsonDataType;
|
|
144
|
+
const nextSchema = schemaForType(schema, nextType, rootSchema);
|
|
145
|
+
handleChange(path, createDefaultValue(nextSchema, rootSchema));
|
|
146
|
+
}}
|
|
147
|
+
>
|
|
148
|
+
<option disabled value=''>
|
|
149
|
+
{text('mixed.typePlaceholder')}
|
|
150
|
+
</option>
|
|
151
|
+
{types.map((type) => (
|
|
152
|
+
<option
|
|
153
|
+
key={translateType(`mixed.type.${type}`, type)}
|
|
154
|
+
value={type}
|
|
155
|
+
>
|
|
156
|
+
{translateType(`mixed.type.${type}`, type)}
|
|
157
|
+
</option>
|
|
158
|
+
))}
|
|
159
|
+
</select>
|
|
160
|
+
</label>
|
|
161
|
+
{isStructuredType ? (
|
|
162
|
+
<details
|
|
163
|
+
className='jsonforms-mixed-renderer-detail'
|
|
164
|
+
key={selectedType}
|
|
165
|
+
open
|
|
166
|
+
>
|
|
167
|
+
<summary>{selectedType}</summary>
|
|
168
|
+
{renderedControl}
|
|
169
|
+
</details>
|
|
170
|
+
) : (
|
|
171
|
+
renderedControl
|
|
172
|
+
)}
|
|
173
|
+
</div>
|
|
174
|
+
);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export const isMixedSchema = (
|
|
178
|
+
uischema: UISchemaElement & Scopable,
|
|
179
|
+
schema: JsonSchema,
|
|
180
|
+
context: TesterContext,
|
|
181
|
+
) => {
|
|
182
|
+
if (schema && typeof schema === 'boolean') {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (!schema || typeof schema !== 'object') {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (Array.isArray(schema.type)) {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (schema.type === undefined && isControl(uischema)) {
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (schema.type === 'object') {
|
|
199
|
+
const schemaPath = uischema.scope;
|
|
200
|
+
if (schemaPath) {
|
|
201
|
+
const currentDataSchema = resolveSchema(
|
|
202
|
+
schema,
|
|
203
|
+
schemaPath,
|
|
204
|
+
context?.rootSchema,
|
|
205
|
+
);
|
|
206
|
+
return Array.isArray(
|
|
207
|
+
(currentDataSchema as JsonSchema7 | undefined)?.type,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return false;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
export const isMixedControl = mixedControlFor(isMixedSchema);
|
|
216
|
+
|
|
217
|
+
export const genericMixedControlTester: RankedTester = rankWith(
|
|
218
|
+
20,
|
|
219
|
+
isMixedControl,
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
export const GenericMixedRenderer = withJsonFormsControlProps(
|
|
223
|
+
GenericMixedRendererComponent,
|
|
224
|
+
);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getAjv,
|
|
3
|
+
getConfig,
|
|
4
|
+
isVisible,
|
|
5
|
+
Layout,
|
|
6
|
+
LayoutProps,
|
|
7
|
+
RankedTester,
|
|
8
|
+
rankWith,
|
|
9
|
+
uiTypeIs,
|
|
10
|
+
} from '@jsonforms/core';
|
|
11
|
+
import {
|
|
12
|
+
JsonFormsDispatch,
|
|
13
|
+
useJsonForms,
|
|
14
|
+
withJsonFormsLayoutProps,
|
|
15
|
+
} from '@jsonforms/react';
|
|
16
|
+
import React from 'react';
|
|
17
|
+
import { horizontalLayoutWidths } from '../util/horizontalLayout';
|
|
18
|
+
|
|
19
|
+
// Above the base layouts, below specialized split layouts (rank 5).
|
|
20
|
+
export const horizontalColumnsLayoutTester: RankedTester = rankWith(
|
|
21
|
+
3,
|
|
22
|
+
uiTypeIs('HorizontalLayout'),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
export const HorizontalColumnsLayoutRendererComponent = (
|
|
26
|
+
props: LayoutProps,
|
|
27
|
+
) => {
|
|
28
|
+
const context = useJsonForms();
|
|
29
|
+
const state = { jsonforms: context };
|
|
30
|
+
const elements = ((props.uischema as Layout).elements ?? [])
|
|
31
|
+
.map((element, index) => ({ element, index }))
|
|
32
|
+
.filter(({ element }) =>
|
|
33
|
+
isVisible(
|
|
34
|
+
element,
|
|
35
|
+
context.core?.data,
|
|
36
|
+
props.path,
|
|
37
|
+
getAjv(state),
|
|
38
|
+
getConfig(state),
|
|
39
|
+
),
|
|
40
|
+
);
|
|
41
|
+
const gap = '1rem';
|
|
42
|
+
const widths = horizontalLayoutWidths(
|
|
43
|
+
elements.map(({ element }) => element.options?.columns),
|
|
44
|
+
gap,
|
|
45
|
+
);
|
|
46
|
+
if (!props.visible) return null;
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<div
|
|
50
|
+
style={{
|
|
51
|
+
display: 'flex',
|
|
52
|
+
flexDirection: 'column',
|
|
53
|
+
gap,
|
|
54
|
+
width: '100%',
|
|
55
|
+
minWidth: 0,
|
|
56
|
+
}}
|
|
57
|
+
>
|
|
58
|
+
{Array.from(new Set(widths.map(({ row }) => row))).map((row) => (
|
|
59
|
+
<div
|
|
60
|
+
key={row}
|
|
61
|
+
data-columns-row={row}
|
|
62
|
+
style={{ display: 'flex', gap, minWidth: 0 }}
|
|
63
|
+
>
|
|
64
|
+
{elements.map(({ element, index }, position) => {
|
|
65
|
+
const item = widths[position];
|
|
66
|
+
if (item.row !== row) return null;
|
|
67
|
+
return (
|
|
68
|
+
<div
|
|
69
|
+
key={`${props.path}-${index}`}
|
|
70
|
+
style={item.style}
|
|
71
|
+
data-columns={item.columns}
|
|
72
|
+
data-columns-diagnostic={item.diagnostic}
|
|
73
|
+
>
|
|
74
|
+
<JsonFormsDispatch
|
|
75
|
+
schema={props.schema}
|
|
76
|
+
uischema={element}
|
|
77
|
+
path={props.path}
|
|
78
|
+
enabled={props.enabled}
|
|
79
|
+
renderers={props.renderers}
|
|
80
|
+
cells={props.cells}
|
|
81
|
+
/>
|
|
82
|
+
</div>
|
|
83
|
+
);
|
|
84
|
+
})}
|
|
85
|
+
</div>
|
|
86
|
+
))}
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export const HorizontalColumnsLayoutRenderer = withJsonFormsLayoutProps(
|
|
92
|
+
HorizontalColumnsLayoutRendererComponent,
|
|
93
|
+
);
|