@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,329 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DynamicUiProvider,
|
|
3
|
+
withDynamicRenderers,
|
|
4
|
+
withDynamicCells,
|
|
5
|
+
} from '@chobantonov/jsonforms-react-renderer-common/dynamic/DynamicUiProvider';
|
|
6
|
+
import React, {
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useState,
|
|
9
|
+
useEffect,
|
|
10
|
+
useMemo,
|
|
11
|
+
useRef,
|
|
12
|
+
} from 'react';
|
|
13
|
+
import { Actions, Generate } from '@jsonforms/core';
|
|
14
|
+
import {
|
|
15
|
+
JsonFormsDispatch,
|
|
16
|
+
JsonFormsInitStateProps,
|
|
17
|
+
JsonFormsStateProvider,
|
|
18
|
+
useJsonForms,
|
|
19
|
+
} from '@jsonforms/react';
|
|
20
|
+
import type { JsonFormsReactProps } from '@jsonforms/react';
|
|
21
|
+
import {
|
|
22
|
+
AdditionalErrorStore,
|
|
23
|
+
AdditionalErrorStoreProvider,
|
|
24
|
+
createAdditionalErrorStore,
|
|
25
|
+
useAdditionalErrorStore,
|
|
26
|
+
} from '../util/additionalErrors';
|
|
27
|
+
import {
|
|
28
|
+
HandleAction,
|
|
29
|
+
HandleActionContext,
|
|
30
|
+
useHandleAction,
|
|
31
|
+
} from '../util/actionContext';
|
|
32
|
+
|
|
33
|
+
// Keep authored UI options at the published spellings before core resolves them.
|
|
34
|
+
export const canonicalUiOptions = <T,>(value: T): T => {
|
|
35
|
+
if (Array.isArray(value)) return value.map(canonicalUiOptions) as T;
|
|
36
|
+
if (
|
|
37
|
+
value === null ||
|
|
38
|
+
typeof value !== 'object' ||
|
|
39
|
+
Object.getPrototypeOf(value) !== Object.prototype
|
|
40
|
+
)
|
|
41
|
+
return value;
|
|
42
|
+
const entries = Object.entries(value);
|
|
43
|
+
let changed = false;
|
|
44
|
+
const result: Record<string, unknown> = {};
|
|
45
|
+
for (const [key, child] of entries) {
|
|
46
|
+
if (
|
|
47
|
+
key === 'options' &&
|
|
48
|
+
typeof (value as { type?: unknown }).type === 'string' &&
|
|
49
|
+
child &&
|
|
50
|
+
typeof child === 'object' &&
|
|
51
|
+
!Array.isArray(child) &&
|
|
52
|
+
Object.prototype.hasOwnProperty.call(child, 'readOnly')
|
|
53
|
+
) {
|
|
54
|
+
const { readOnly: _ignored, ...rest } = child as Record<string, unknown>;
|
|
55
|
+
result[key] = canonicalUiOptions(rest);
|
|
56
|
+
changed = true;
|
|
57
|
+
} else {
|
|
58
|
+
const normalized = canonicalUiOptions(child);
|
|
59
|
+
result[key] = normalized;
|
|
60
|
+
changed ||= normalized !== child;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return (changed ? result : value) as T;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const canonicalConfig = <T extends Record<string, unknown> | undefined>(
|
|
67
|
+
value: T,
|
|
68
|
+
): T =>
|
|
69
|
+
value && Object.prototype.hasOwnProperty.call(value, 'readOnly')
|
|
70
|
+
? (Object.fromEntries(
|
|
71
|
+
Object.entries(value).filter(([key]) => key !== 'readOnly'),
|
|
72
|
+
) as T)
|
|
73
|
+
: value;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* `<JsonForms>` with this renderer set's integration already wired.
|
|
77
|
+
*
|
|
78
|
+
* The React counterpart of the Vue 2 `ResolvedJsonForms`, and it exists for
|
|
79
|
+
* the same reason: the extended renderers need things a bare `<JsonForms>`
|
|
80
|
+
* does not provide, and an application should not have to know what they are.
|
|
81
|
+
*
|
|
82
|
+
* | | Without this |
|
|
83
|
+
* | --- | --- |
|
|
84
|
+
* | The additional-error store | Two props that are only useful together, plus a provider |
|
|
85
|
+
* | The action handler | A `HandleActionContext.Provider` around the form, or every `Button` is a no-op |
|
|
86
|
+
*
|
|
87
|
+
* ```tsx
|
|
88
|
+
* <ExtendedJsonForms schema={schema} uischema={uischema} data={data}
|
|
89
|
+
* renderers={renderers} cells={cells} onChange={onChange} />
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* A form using the Monaco control with `propagateErrors` and a plain
|
|
93
|
+
* `<JsonForms>` publishes into nothing. It now says so - `additionalErrors.noStore`
|
|
94
|
+
* - rather than going quiet, but this is the fix rather than the warning.
|
|
95
|
+
*
|
|
96
|
+
* **Bring your own store** when the application needs to publish too, which is
|
|
97
|
+
* the ordinary case for server-side validation:
|
|
98
|
+
*
|
|
99
|
+
* ```tsx
|
|
100
|
+
* const store = useRef(createAdditionalErrorStore()).current;
|
|
101
|
+
*
|
|
102
|
+
* <ExtendedJsonForms store={store} … />
|
|
103
|
+
*
|
|
104
|
+
* // after a rejected submit
|
|
105
|
+
* store.publish('server', response.validationErrors);
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* ## What it deliberately does not do
|
|
109
|
+
*
|
|
110
|
+
* **Resolve `$ref`.** The Vue 2 wrapper does, and it is tempting to copy, but
|
|
111
|
+
* the two cases are not the same:
|
|
112
|
+
*
|
|
113
|
+
* - A **local** `$ref` - `#/$defs/x`, `#/definitions/x` - already works.
|
|
114
|
+
* JSON Forms resolves it while rendering and Ajv while validating; verified
|
|
115
|
+
* against both spellings.
|
|
116
|
+
* - A **remote** one, or a `schemaUrl`, does not. Resolving it is asynchronous,
|
|
117
|
+
* so a wrapper that did it would have to hold the form unmounted while it
|
|
118
|
+
* fetched, and would own a loading and a failure state.
|
|
119
|
+
*
|
|
120
|
+
* That is a different component with a different lifecycle, and putting it
|
|
121
|
+
* here would make every form pay a mount delay for a capability most do not
|
|
122
|
+
* use. If remote schemas are needed, they belong in a `ResolvedJsonForms` of
|
|
123
|
+
* their own, wrapping this.
|
|
124
|
+
*/
|
|
125
|
+
export interface ExtendedJsonFormsProps
|
|
126
|
+
extends JsonFormsInitStateProps, JsonFormsReactProps {
|
|
127
|
+
/**
|
|
128
|
+
* A store the application also publishes into.
|
|
129
|
+
*
|
|
130
|
+
* Absent, an enclosing `AdditionalErrorStoreProvider` is used if there is
|
|
131
|
+
* one - it can only have been put there deliberately - and otherwise a store
|
|
132
|
+
* is created for this form. Several forms under one provider therefore share
|
|
133
|
+
* a store, which is the host's decision to make and means they see each
|
|
134
|
+
* other's published errors.
|
|
135
|
+
*/
|
|
136
|
+
store?: AdditionalErrorStore;
|
|
137
|
+
/** Host-owned immutable values, independent of overlay activation. */
|
|
138
|
+
external?: Readonly<Record<string, unknown>>;
|
|
139
|
+
/**
|
|
140
|
+
* What a `Button` hands its command to.
|
|
141
|
+
*
|
|
142
|
+
* Absent, an enclosing `HandleActionContext` is left in place rather than
|
|
143
|
+
* shadowed with `undefined` - so a container that provides one for several
|
|
144
|
+
* forms keeps working, and only a host that actually passes a handler
|
|
145
|
+
* replaces it.
|
|
146
|
+
*/
|
|
147
|
+
onAction?: HandleAction;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Re-reduces the form when the store changes, so the middleware can inject.
|
|
152
|
+
*
|
|
153
|
+
* Middleware is **passive**: publishing an error dispatches nothing, so
|
|
154
|
+
* without this the form would not see a published error until the user
|
|
155
|
+
* happened to type.
|
|
156
|
+
*
|
|
157
|
+
* The obvious alternative - putting the errors in the `additionalErrors` prop
|
|
158
|
+
* - looks simpler and is broken. `JsonFormsStateProvider` lists that prop in
|
|
159
|
+
* an effect's dependencies and re-dispatches `updateCore(data, …)` with the
|
|
160
|
+
* **prop** data of that render; a renderer publishes from its own effect,
|
|
161
|
+
* which runs *before* the parent's `onChange` has told the host about the
|
|
162
|
+
* edit that provoked it. So the re-dispatch carries the previous data and
|
|
163
|
+
* undoes the edit. It showed up as a language select that would not move:
|
|
164
|
+
* choosing a language made the editor publish, and publishing put the old
|
|
165
|
+
* language back.
|
|
166
|
+
*
|
|
167
|
+
* Dispatching from **inside** the provider avoids the whole question - the
|
|
168
|
+
* data comes from core, which is current by construction, and the prop never
|
|
169
|
+
* changes.
|
|
170
|
+
*/
|
|
171
|
+
const AdditionalErrorsBridge = ({ store }: { store: AdditionalErrorStore }) => {
|
|
172
|
+
const ctx = useJsonForms();
|
|
173
|
+
const dispatch = ctx.dispatch;
|
|
174
|
+
const coreRef = useRef(ctx.core);
|
|
175
|
+
useLayoutEffect(() => {
|
|
176
|
+
coreRef.current = ctx.core;
|
|
177
|
+
}, [ctx.core]);
|
|
178
|
+
|
|
179
|
+
useEffect(() => {
|
|
180
|
+
if (!dispatch) {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
return store.subscribe(() => {
|
|
184
|
+
const core = coreRef.current;
|
|
185
|
+
if (!core) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
/*
|
|
189
|
+
No `additionalErrors` option, so core keeps what it has and the
|
|
190
|
+
middleware writes the store's current set over it. Data and schema
|
|
191
|
+
come from core rather than from a prop, so this cannot move the form.
|
|
192
|
+
*/
|
|
193
|
+
dispatch(
|
|
194
|
+
Actions.updateCore(core.data, core.schema, core.uischema, {
|
|
195
|
+
ajv: core.ajv,
|
|
196
|
+
validationMode: core.validationMode,
|
|
197
|
+
} as never),
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
}, [store, dispatch]);
|
|
201
|
+
|
|
202
|
+
return null;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
export const ExtendedJsonForms = ({
|
|
206
|
+
external,
|
|
207
|
+
store,
|
|
208
|
+
onAction,
|
|
209
|
+
ajv,
|
|
210
|
+
data,
|
|
211
|
+
schema,
|
|
212
|
+
uischema,
|
|
213
|
+
renderers,
|
|
214
|
+
cells,
|
|
215
|
+
config,
|
|
216
|
+
uischemas,
|
|
217
|
+
readonly,
|
|
218
|
+
validationMode,
|
|
219
|
+
i18n,
|
|
220
|
+
additionalErrors,
|
|
221
|
+
middleware,
|
|
222
|
+
onChange,
|
|
223
|
+
}: ExtendedJsonFormsProps) => {
|
|
224
|
+
/*
|
|
225
|
+
Per mounted form, not per render: a store is the identity errors are
|
|
226
|
+
published against, and a new one each render would lose them.
|
|
227
|
+
*/
|
|
228
|
+
const [fallback] = useState(createAdditionalErrorStore);
|
|
229
|
+
const inherited = useAdditionalErrorStore();
|
|
230
|
+
const outerAction = useHandleAction();
|
|
231
|
+
const effective = store ?? (inherited.inert ? fallback : inherited);
|
|
232
|
+
|
|
233
|
+
/*
|
|
234
|
+
Composed from the same pieces `<JsonForms>` uses, rather than rendering it,
|
|
235
|
+
because the bridge above has to sit **inside** the provider to reach its
|
|
236
|
+
dispatch. Everything else here is what `JsonForms` itself does.
|
|
237
|
+
*/
|
|
238
|
+
const schemaToUse = useMemo(
|
|
239
|
+
() => (schema !== undefined ? schema : Generate.jsonSchema(data)),
|
|
240
|
+
[schema, data],
|
|
241
|
+
);
|
|
242
|
+
const uischemaToUse = useMemo(
|
|
243
|
+
() =>
|
|
244
|
+
typeof uischema === 'object'
|
|
245
|
+
? canonicalUiOptions(uischema)
|
|
246
|
+
: Generate.uiSchema(schemaToUse, undefined, undefined, schemaToUse),
|
|
247
|
+
[uischema, schemaToUse],
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
const combinedMiddleware = useMemo(
|
|
251
|
+
() =>
|
|
252
|
+
middleware
|
|
253
|
+
? (state: never, action: never, defaultReducer: never) =>
|
|
254
|
+
effective.middleware(state, action, ((innerState, innerAction) =>
|
|
255
|
+
middleware(
|
|
256
|
+
innerState,
|
|
257
|
+
innerAction,
|
|
258
|
+
defaultReducer as never,
|
|
259
|
+
)) as never)
|
|
260
|
+
: effective.middleware,
|
|
261
|
+
[effective, middleware],
|
|
262
|
+
) as typeof effective.middleware;
|
|
263
|
+
|
|
264
|
+
const configToUse = useMemo(() => canonicalConfig(config), [config]);
|
|
265
|
+
|
|
266
|
+
const rendererRegistry = useMemo(
|
|
267
|
+
() => withDynamicRenderers(renderers),
|
|
268
|
+
[renderers],
|
|
269
|
+
);
|
|
270
|
+
const cellRegistry = useMemo(() => withDynamicCells(cells), [cells]);
|
|
271
|
+
|
|
272
|
+
const form = (
|
|
273
|
+
<JsonFormsStateProvider
|
|
274
|
+
initState={{
|
|
275
|
+
core: {
|
|
276
|
+
ajv,
|
|
277
|
+
data,
|
|
278
|
+
schema: schemaToUse,
|
|
279
|
+
uischema: uischemaToUse,
|
|
280
|
+
validationMode,
|
|
281
|
+
/*
|
|
282
|
+
Only the host's own. What the store holds arrives through the
|
|
283
|
+
middleware, so this prop never changes when something publishes -
|
|
284
|
+
which is the entire point.
|
|
285
|
+
*/
|
|
286
|
+
additionalErrors,
|
|
287
|
+
} as never,
|
|
288
|
+
config: configToUse,
|
|
289
|
+
uischemas,
|
|
290
|
+
renderers: rendererRegistry,
|
|
291
|
+
cells: cellRegistry,
|
|
292
|
+
readonly,
|
|
293
|
+
i18n,
|
|
294
|
+
}}
|
|
295
|
+
onChange={onChange}
|
|
296
|
+
middleware={combinedMiddleware}
|
|
297
|
+
>
|
|
298
|
+
<AdditionalErrorsBridge store={effective} />
|
|
299
|
+
<JsonFormsDispatch />
|
|
300
|
+
</JsonFormsStateProvider>
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
const wrapped = (
|
|
304
|
+
<AdditionalErrorStoreProvider store={effective}>
|
|
305
|
+
<DynamicUiProvider
|
|
306
|
+
config={config}
|
|
307
|
+
external={external}
|
|
308
|
+
locale={i18n?.locale}
|
|
309
|
+
translate={i18n?.translate}
|
|
310
|
+
>
|
|
311
|
+
{form}
|
|
312
|
+
</DynamicUiProvider>
|
|
313
|
+
</AdditionalErrorStoreProvider>
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
/*
|
|
317
|
+
Only when this form was given one. Wrapping unconditionally would set the
|
|
318
|
+
context to `undefined` for everything inside, which silently turns every
|
|
319
|
+
`Button` into a no-op in a host whose provider sits further up.
|
|
320
|
+
*/
|
|
321
|
+
const handler = onAction ?? outerAction;
|
|
322
|
+
return handler === outerAction ? (
|
|
323
|
+
wrapped
|
|
324
|
+
) : (
|
|
325
|
+
<HandleActionContext.Provider value={handler}>
|
|
326
|
+
{wrapped}
|
|
327
|
+
</HandleActionContext.Provider>
|
|
328
|
+
);
|
|
329
|
+
};
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useAdditionalPropertyItems,
|
|
3
|
+
type AdditionalPropertiesProps,
|
|
4
|
+
} from '@chobantonov/jsonforms-react-renderer-common/objects/useAdditionalPropertyEntries';
|
|
5
|
+
import {
|
|
6
|
+
renamePropertyKey,
|
|
7
|
+
toObjectSchema,
|
|
8
|
+
toAdditionalPropertyItem,
|
|
9
|
+
} from '@chobantonov/jsonforms-react-renderer-common/objects/additionalProperties';
|
|
10
|
+
import {
|
|
11
|
+
createDefaultValue,
|
|
12
|
+
JsonSchema,
|
|
13
|
+
JsonSchema7,
|
|
14
|
+
resolveSchema,
|
|
15
|
+
} from '@jsonforms/core';
|
|
16
|
+
import { JsonFormsDispatch } from '@jsonforms/react';
|
|
17
|
+
import { useExtendedTranslator } from '../util/useExtendedTranslator';
|
|
18
|
+
|
|
19
|
+
/** The translator `useExtendedTranslator` returns. */
|
|
20
|
+
type ExtendedTranslator = ReturnType<typeof useExtendedTranslator>;
|
|
21
|
+
import React, { useState } from 'react';
|
|
22
|
+
|
|
23
|
+
export type GenericAdditionalPropertiesProps = Omit<
|
|
24
|
+
AdditionalPropertiesProps,
|
|
25
|
+
'embedded'
|
|
26
|
+
>;
|
|
27
|
+
|
|
28
|
+
const getPropertyNamePattern = (
|
|
29
|
+
schema: JsonSchema,
|
|
30
|
+
rootSchema: JsonSchema,
|
|
31
|
+
): string | undefined => {
|
|
32
|
+
const objectSchema = toObjectSchema(schema);
|
|
33
|
+
let propertyNames = objectSchema.propertyNames as JsonSchema7 | undefined;
|
|
34
|
+
if (
|
|
35
|
+
typeof propertyNames === 'object' &&
|
|
36
|
+
typeof propertyNames.$ref === 'string'
|
|
37
|
+
) {
|
|
38
|
+
propertyNames =
|
|
39
|
+
(resolveSchema(rootSchema, propertyNames.$ref, rootSchema) as
|
|
40
|
+
| JsonSchema7
|
|
41
|
+
| undefined) ?? propertyNames;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (typeof propertyNames === 'object' && propertyNames.pattern) {
|
|
45
|
+
return propertyNames.pattern;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (
|
|
49
|
+
objectSchema.additionalProperties === false &&
|
|
50
|
+
objectSchema.patternProperties
|
|
51
|
+
) {
|
|
52
|
+
const patterns = Object.keys(objectSchema.patternProperties);
|
|
53
|
+
return patterns.length > 0 ? patterns.join('|') : undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return undefined;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/*
|
|
60
|
+
Takes the translator rather than calling the hook: it is a plain function,
|
|
61
|
+
used twice per render (the add box and the row being renamed), and a hook
|
|
62
|
+
cannot be called from either position.
|
|
63
|
+
*/
|
|
64
|
+
const validatePropertyName = (
|
|
65
|
+
t: ExtendedTranslator,
|
|
66
|
+
propertyName: string,
|
|
67
|
+
data: any,
|
|
68
|
+
schema: JsonSchema,
|
|
69
|
+
rootSchema: JsonSchema,
|
|
70
|
+
currentPropertyName?: string,
|
|
71
|
+
): string | undefined => {
|
|
72
|
+
if (!propertyName) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (
|
|
77
|
+
typeof data === 'object' &&
|
|
78
|
+
data !== null &&
|
|
79
|
+
Object.prototype.hasOwnProperty.call(data, propertyName) &&
|
|
80
|
+
propertyName !== currentPropertyName
|
|
81
|
+
) {
|
|
82
|
+
return t('additionalProperties.nameTaken', { name: propertyName });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (
|
|
86
|
+
propertyName.includes('[') ||
|
|
87
|
+
propertyName.includes(']') ||
|
|
88
|
+
propertyName.includes('.')
|
|
89
|
+
) {
|
|
90
|
+
return t('additionalProperties.nameInvalid', { name: propertyName });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const pattern = getPropertyNamePattern(schema, rootSchema);
|
|
94
|
+
if (pattern && !new RegExp(pattern).test(propertyName)) {
|
|
95
|
+
return t('additionalProperties.namePattern', { pattern });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return undefined;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export const GenericAdditionalProperties = ({
|
|
102
|
+
cells,
|
|
103
|
+
config,
|
|
104
|
+
data,
|
|
105
|
+
enabled,
|
|
106
|
+
handleChange,
|
|
107
|
+
label,
|
|
108
|
+
path,
|
|
109
|
+
readonly,
|
|
110
|
+
renderers,
|
|
111
|
+
rootSchema,
|
|
112
|
+
schema,
|
|
113
|
+
uischema,
|
|
114
|
+
}: GenericAdditionalPropertiesProps) => {
|
|
115
|
+
const t = useExtendedTranslator();
|
|
116
|
+
const [newPropertyName, setNewPropertyName] = useState('');
|
|
117
|
+
const [renamingPropertyName, setRenamingPropertyName] = useState<
|
|
118
|
+
string | null
|
|
119
|
+
>(null);
|
|
120
|
+
const [renameValue, setRenameValue] = useState('');
|
|
121
|
+
const {
|
|
122
|
+
maxPropertiesReached,
|
|
123
|
+
minPropertiesReached,
|
|
124
|
+
appliedOptions,
|
|
125
|
+
objectData,
|
|
126
|
+
allowIfMissing,
|
|
127
|
+
additionalPropertyItems,
|
|
128
|
+
shouldShow,
|
|
129
|
+
} = useAdditionalPropertyItems({
|
|
130
|
+
data,
|
|
131
|
+
schema,
|
|
132
|
+
rootSchema,
|
|
133
|
+
path,
|
|
134
|
+
uischema,
|
|
135
|
+
config,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
if (!shouldShow) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const propertyName = newPropertyName.trim();
|
|
143
|
+
const propertyNameError = validatePropertyName(
|
|
144
|
+
t,
|
|
145
|
+
propertyName,
|
|
146
|
+
data,
|
|
147
|
+
schema,
|
|
148
|
+
rootSchema,
|
|
149
|
+
);
|
|
150
|
+
const addPropertyDisabled =
|
|
151
|
+
!enabled ||
|
|
152
|
+
readonly ||
|
|
153
|
+
(appliedOptions.restrict && maxPropertiesReached) ||
|
|
154
|
+
Boolean(propertyNameError) ||
|
|
155
|
+
!propertyName;
|
|
156
|
+
const removePropertyDisabled =
|
|
157
|
+
!enabled || readonly || (appliedOptions.restrict && minPropertiesReached);
|
|
158
|
+
|
|
159
|
+
const addProperty = () => {
|
|
160
|
+
if (addPropertyDisabled) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const additionalProperty = toAdditionalPropertyItem(
|
|
165
|
+
propertyName,
|
|
166
|
+
path,
|
|
167
|
+
schema,
|
|
168
|
+
rootSchema,
|
|
169
|
+
allowIfMissing,
|
|
170
|
+
);
|
|
171
|
+
const updatedData = objectData ? { ...objectData } : {};
|
|
172
|
+
|
|
173
|
+
updatedData[propertyName] = createDefaultValue(
|
|
174
|
+
additionalProperty.schema,
|
|
175
|
+
rootSchema,
|
|
176
|
+
);
|
|
177
|
+
handleChange(path, updatedData);
|
|
178
|
+
setNewPropertyName('');
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const removeProperty = (propertyToRemove: string) => {
|
|
182
|
+
if (removePropertyDisabled || !objectData) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const updatedData = { ...objectData };
|
|
187
|
+
delete updatedData[propertyToRemove];
|
|
188
|
+
handleChange(path, updatedData);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const renameProperty = (propertyToRename: string) => {
|
|
192
|
+
const trimmed = renameValue.trim();
|
|
193
|
+
const renameError = validatePropertyName(
|
|
194
|
+
t,
|
|
195
|
+
trimmed,
|
|
196
|
+
data,
|
|
197
|
+
schema,
|
|
198
|
+
rootSchema,
|
|
199
|
+
propertyToRename,
|
|
200
|
+
);
|
|
201
|
+
if (
|
|
202
|
+
renameError ||
|
|
203
|
+
!trimmed ||
|
|
204
|
+
trimmed === propertyToRename ||
|
|
205
|
+
!objectData
|
|
206
|
+
) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const updatedData = renamePropertyKey(
|
|
211
|
+
objectData,
|
|
212
|
+
propertyToRename,
|
|
213
|
+
trimmed,
|
|
214
|
+
);
|
|
215
|
+
handleChange(path, updatedData);
|
|
216
|
+
setRenamingPropertyName(null);
|
|
217
|
+
setRenameValue('');
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<div className='jsonforms-additional-properties'>
|
|
222
|
+
<div className='jsonforms-additional-properties-add'>
|
|
223
|
+
<input
|
|
224
|
+
aria-label={
|
|
225
|
+
label
|
|
226
|
+
? t('additionalProperties.addTo', { label })
|
|
227
|
+
: t('additionalProperties.addLabel')
|
|
228
|
+
}
|
|
229
|
+
disabled={!enabled || readonly}
|
|
230
|
+
placeholder={t('additionalProperties.namePlaceholder')}
|
|
231
|
+
type='text'
|
|
232
|
+
value={newPropertyName}
|
|
233
|
+
onChange={(event) => setNewPropertyName(event.currentTarget.value)}
|
|
234
|
+
onKeyDown={(event) => {
|
|
235
|
+
if (event.key === 'Enter') {
|
|
236
|
+
addProperty();
|
|
237
|
+
}
|
|
238
|
+
}}
|
|
239
|
+
/>
|
|
240
|
+
<button
|
|
241
|
+
disabled={addPropertyDisabled}
|
|
242
|
+
type='button'
|
|
243
|
+
onClick={addProperty}
|
|
244
|
+
>
|
|
245
|
+
{t('additionalProperties.add')}
|
|
246
|
+
</button>
|
|
247
|
+
</div>
|
|
248
|
+
{propertyNameError ? (
|
|
249
|
+
<div className='jsonforms-additional-properties-error'>
|
|
250
|
+
{propertyNameError}
|
|
251
|
+
</div>
|
|
252
|
+
) : null}
|
|
253
|
+
<div className='jsonforms-additional-properties-list'>
|
|
254
|
+
{additionalPropertyItems.map((item) => {
|
|
255
|
+
const isRenaming = renamingPropertyName === item.propertyName;
|
|
256
|
+
const renameError = validatePropertyName(
|
|
257
|
+
t,
|
|
258
|
+
renameValue.trim(),
|
|
259
|
+
data,
|
|
260
|
+
schema,
|
|
261
|
+
rootSchema,
|
|
262
|
+
item.propertyName,
|
|
263
|
+
);
|
|
264
|
+
const renameDisabled =
|
|
265
|
+
!enabled ||
|
|
266
|
+
readonly ||
|
|
267
|
+
Boolean(renameError) ||
|
|
268
|
+
!renameValue.trim() ||
|
|
269
|
+
renameValue.trim() === item.propertyName;
|
|
270
|
+
|
|
271
|
+
return (
|
|
272
|
+
<div
|
|
273
|
+
className='jsonforms-additional-property'
|
|
274
|
+
key={item.propertyName}
|
|
275
|
+
>
|
|
276
|
+
<div className='jsonforms-additional-property-control'>
|
|
277
|
+
<JsonFormsDispatch
|
|
278
|
+
schema={item.schema}
|
|
279
|
+
uischema={item.uischema}
|
|
280
|
+
path={item.path}
|
|
281
|
+
enabled={enabled}
|
|
282
|
+
renderers={renderers}
|
|
283
|
+
cells={cells}
|
|
284
|
+
readonly={readonly}
|
|
285
|
+
/>
|
|
286
|
+
</div>
|
|
287
|
+
{enabled ? (
|
|
288
|
+
<div className='jsonforms-additional-property-actions'>
|
|
289
|
+
{isRenaming ? (
|
|
290
|
+
<>
|
|
291
|
+
<input
|
|
292
|
+
aria-label={t('additionalProperties.renameNamed', {
|
|
293
|
+
name: item.propertyName,
|
|
294
|
+
})}
|
|
295
|
+
autoFocus
|
|
296
|
+
disabled={readonly}
|
|
297
|
+
type='text'
|
|
298
|
+
value={renameValue}
|
|
299
|
+
onChange={(event) =>
|
|
300
|
+
setRenameValue(event.currentTarget.value)
|
|
301
|
+
}
|
|
302
|
+
onKeyDown={(event) => {
|
|
303
|
+
if (event.key === 'Enter') {
|
|
304
|
+
renameProperty(item.propertyName);
|
|
305
|
+
} else if (event.key === 'Escape') {
|
|
306
|
+
setRenamingPropertyName(null);
|
|
307
|
+
setRenameValue('');
|
|
308
|
+
}
|
|
309
|
+
}}
|
|
310
|
+
/>
|
|
311
|
+
<button
|
|
312
|
+
disabled={renameDisabled}
|
|
313
|
+
type='button'
|
|
314
|
+
onClick={() => renameProperty(item.propertyName)}
|
|
315
|
+
>
|
|
316
|
+
{t('additionalProperties.save')}
|
|
317
|
+
</button>
|
|
318
|
+
<button
|
|
319
|
+
type='button'
|
|
320
|
+
onClick={() => {
|
|
321
|
+
setRenamingPropertyName(null);
|
|
322
|
+
setRenameValue('');
|
|
323
|
+
}}
|
|
324
|
+
>
|
|
325
|
+
{t('additionalProperties.cancel')}
|
|
326
|
+
</button>
|
|
327
|
+
{renameError ? (
|
|
328
|
+
<div className='jsonforms-additional-properties-error'>
|
|
329
|
+
{renameError}
|
|
330
|
+
</div>
|
|
331
|
+
) : null}
|
|
332
|
+
</>
|
|
333
|
+
) : (
|
|
334
|
+
<>
|
|
335
|
+
<button
|
|
336
|
+
disabled={readonly}
|
|
337
|
+
type='button'
|
|
338
|
+
onClick={() => {
|
|
339
|
+
setRenamingPropertyName(item.propertyName);
|
|
340
|
+
setRenameValue(item.propertyName);
|
|
341
|
+
}}
|
|
342
|
+
>
|
|
343
|
+
{t('additionalProperties.rename')}
|
|
344
|
+
</button>
|
|
345
|
+
<button
|
|
346
|
+
disabled={removePropertyDisabled}
|
|
347
|
+
type='button'
|
|
348
|
+
onClick={() => removeProperty(item.propertyName)}
|
|
349
|
+
>
|
|
350
|
+
{t('additionalProperties.delete')}
|
|
351
|
+
</button>
|
|
352
|
+
</>
|
|
353
|
+
)}
|
|
354
|
+
</div>
|
|
355
|
+
) : null}
|
|
356
|
+
</div>
|
|
357
|
+
);
|
|
358
|
+
})}
|
|
359
|
+
</div>
|
|
360
|
+
</div>
|
|
361
|
+
);
|
|
362
|
+
};
|