@chobantonov/jsonforms-react-antd-webcomponent 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 (102) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +86 -2
  3. package/dist/JsonFormsAntdElement.d.ts +26 -0
  4. package/dist/index.d.ts +3 -0
  5. package/dist/jsonforms-react-antd.js +1 -0
  6. package/dist/register.d.ts +1 -0
  7. package/dist/register.js +6 -0
  8. package/dist/register.js.map +1 -0
  9. package/dist/renderers.d.ts +2 -0
  10. package/dist/src-BFTWxxIf.mjs +202 -0
  11. package/dist/src-BFTWxxIf.mjs.map +1 -0
  12. package/dist/standalone/AgGridControlRenderer.impl-BZS8_ocu-B7smOgCn.mjs +39186 -0
  13. package/dist/standalone/AgGridControlRenderer.impl-BZS8_ocu-B7smOgCn.mjs.map +1 -0
  14. package/dist/standalone/DynamicJSXRenderer-DyB3ZpZV-DwdyKIB1.mjs +14894 -0
  15. package/dist/standalone/DynamicJSXRenderer-DyB3ZpZV-DwdyKIB1.mjs.map +1 -0
  16. package/dist/standalone/MarkupLabelRenderer.impl-CWfdcUuH-D5lfFu1i.mjs +12 -0
  17. package/dist/standalone/MarkupLabelRenderer.impl-CWfdcUuH-D5lfFu1i.mjs.map +1 -0
  18. package/dist/standalone/MonacoControlRenderer.impl-CyjMHbOd-CQ8xjLar.mjs +774 -0
  19. package/dist/standalone/MonacoControlRenderer.impl-CyjMHbOd-CQ8xjLar.mjs.map +1 -0
  20. package/dist/standalone/RactiveTemplate-Cat7CxUp-BXSHwrZ1.mjs +8028 -0
  21. package/dist/standalone/RactiveTemplate-Cat7CxUp-BXSHwrZ1.mjs.map +1 -0
  22. package/dist/standalone/bg-peTJz2yn.mjs +54 -0
  23. package/dist/standalone/bg-peTJz2yn.mjs.map +1 -0
  24. package/dist/standalone/bg_BG-B44LSCyD.mjs +222 -0
  25. package/dist/standalone/bg_BG-B44LSCyD.mjs.map +1 -0
  26. package/dist/standalone/celTemplate-D_AGhaIL-RG6XN-Eu.mjs +137 -0
  27. package/dist/standalone/celTemplate-D_AGhaIL-RG6XN-Eu.mjs.map +1 -0
  28. package/dist/standalone/common-BEnIxhFt.mjs +18 -0
  29. package/dist/standalone/common-BEnIxhFt.mjs.map +1 -0
  30. package/dist/standalone/dayjs.min-adoc_mEQ.mjs +300 -0
  31. package/dist/standalone/dayjs.min-adoc_mEQ.mjs.map +1 -0
  32. package/dist/standalone/dynamicCel.esm-DGWfIAPF.mjs +43 -0
  33. package/dist/standalone/dynamicCel.esm-DGWfIAPF.mjs.map +1 -0
  34. package/dist/standalone/en-D7K60EG2.mjs +25 -0
  35. package/dist/standalone/en-D7K60EG2.mjs.map +1 -0
  36. package/dist/standalone/en_US-Bk9ytucu.mjs +223 -0
  37. package/dist/standalone/en_US-Bk9ytucu.mjs.map +1 -0
  38. package/dist/standalone/es-BToyXotK.mjs +51 -0
  39. package/dist/standalone/es-BToyXotK.mjs.map +1 -0
  40. package/dist/standalone/es_ES-n86UqGMA.mjs +245 -0
  41. package/dist/standalone/es_ES-n86UqGMA.mjs.map +1 -0
  42. package/dist/standalone/fr-DlN8a0TJ.mjs +52 -0
  43. package/dist/standalone/fr-DlN8a0TJ.mjs.map +1 -0
  44. package/dist/standalone/fr_FR-BNIh97oT.mjs +223 -0
  45. package/dist/standalone/fr_FR-BNIh97oT.mjs.map +1 -0
  46. package/dist/standalone/index.js +56734 -0
  47. package/dist/standalone/index.js.map +1 -0
  48. package/dist/standalone/it-0HHNpxpp.mjs +51 -0
  49. package/dist/standalone/it-0HHNpxpp.mjs.map +1 -0
  50. package/dist/standalone/it_IT-COjke9P6.mjs +245 -0
  51. package/dist/standalone/it_IT-COjke9P6.mjs.map +1 -0
  52. package/dist/standalone/ja-CqnjT8BB.mjs +57 -0
  53. package/dist/standalone/ja-CqnjT8BB.mjs.map +1 -0
  54. package/dist/standalone/ja_JP-DA4GA8OS.mjs +248 -0
  55. package/dist/standalone/ja_JP-DA4GA8OS.mjs.map +1 -0
  56. package/dist/standalone/jsonforms-react-extended.esm-CQmkt5NG.mjs +13673 -0
  57. package/dist/standalone/jsonforms-react-extended.esm-CQmkt5NG.mjs.map +1 -0
  58. package/dist/standalone/lib-0rajvaUg.mjs +3170 -0
  59. package/dist/standalone/lib-0rajvaUg.mjs.map +1 -0
  60. package/dist/standalone/markdown-C_y73Q0w-CfLH-8Sz.mjs +3061 -0
  61. package/dist/standalone/markdown-C_y73Q0w-CfLH-8Sz.mjs.map +1 -0
  62. package/dist/standalone/nl-CFqJarW7.mjs +52 -0
  63. package/dist/standalone/nl-CFqJarW7.mjs.map +1 -0
  64. package/dist/standalone/nl_NL-4FmulTf3.mjs +222 -0
  65. package/dist/standalone/nl_NL-4FmulTf3.mjs.map +1 -0
  66. package/dist/standalone/objectSpread2-C50HRvBU.mjs +71 -0
  67. package/dist/standalone/pl-Du1zDNA_.mjs +71 -0
  68. package/dist/standalone/pl-Du1zDNA_.mjs.map +1 -0
  69. package/dist/standalone/pl_PL-CBfuF99B.mjs +248 -0
  70. package/dist/standalone/pl_PL-CBfuF99B.mjs.map +1 -0
  71. package/dist/standalone/plainRecord.esm-BR1q_7e9.mjs +30 -0
  72. package/dist/standalone/plainRecord.esm-BR1q_7e9.mjs.map +1 -0
  73. package/dist/standalone/pt-Dg1pEKF5.mjs +52 -0
  74. package/dist/standalone/pt-Dg1pEKF5.mjs.map +1 -0
  75. package/dist/standalone/pt_PT--r6dtf89.mjs +268 -0
  76. package/dist/standalone/pt_PT--r6dtf89.mjs.map +1 -0
  77. package/dist/standalone/react-dom-Slk9aa5V.mjs +5644 -0
  78. package/dist/standalone/react-dom-Slk9aa5V.mjs.map +1 -0
  79. package/dist/standalone/rolldown-runtime-DUcC5eAW.mjs +26 -0
  80. package/dist/standalone/rowDetail.esm-Ce56gyeo.mjs +770 -0
  81. package/dist/standalone/rowDetail.esm-Ce56gyeo.mjs.map +1 -0
  82. package/dist/standalone/ru-CeNU1BY9.mjs +74 -0
  83. package/dist/standalone/ru-CeNU1BY9.mjs.map +1 -0
  84. package/dist/standalone/ru_RU-CIRMzMfd.mjs +245 -0
  85. package/dist/standalone/ru_RU-CIRMzMfd.mjs.map +1 -0
  86. package/dist/standalone/tr-Demj1YNf.mjs +51 -0
  87. package/dist/standalone/tr-Demj1YNf.mjs.map +1 -0
  88. package/dist/standalone/tr_TR-DOU85NN8.mjs +245 -0
  89. package/dist/standalone/tr_TR-DOU85NN8.mjs.map +1 -0
  90. package/dist/standalone/uk-Dq-ndPcu.mjs +67 -0
  91. package/dist/standalone/uk-Dq-ndPcu.mjs.map +1 -0
  92. package/dist/standalone/uk_UA-_65wU-M8.mjs +245 -0
  93. package/dist/standalone/uk_UA-_65wU-M8.mjs.map +1 -0
  94. package/dist/standalone/zh-cn-DteM8IK0.mjs +60 -0
  95. package/dist/standalone/zh-cn-DteM8IK0.mjs.map +1 -0
  96. package/dist/standalone/zh_CN-BaFiZ2OD.mjs +226 -0
  97. package/dist/standalone/zh_CN-BaFiZ2OD.mjs.map +1 -0
  98. package/package.json +62 -3
  99. package/src/JsonFormsAntdElement.tsx +327 -0
  100. package/src/index.tsx +4 -0
  101. package/src/register.ts +5 -0
  102. package/src/renderers.ts +20 -0
@@ -0,0 +1,327 @@
1
+ import { defaultTranslator } from '@jsonforms/core';
2
+ import {
3
+ FormElement,
4
+ formAttributes,
5
+ type FormElementState,
6
+ } from '@chobantonov/jsonforms-react-renderer-common/webcomponent/element';
7
+ import {
8
+ ExtendedJsonForms,
9
+ createAdditionalErrorStore,
10
+ createFormsAjv,
11
+ createAjvErrorTranslator,
12
+ type AjvLocalizers,
13
+ } from '@chobantonov/jsonforms-react-antd-extended-renderers';
14
+ import { antdWebcomponentCells, antdWebcomponentRenderers } from './renderers';
15
+ import { ConfigProvider, Form, InputProps, theme as antdTheme } from 'antd';
16
+ import { useAntdLocale } from '@chobantonov/jsonforms-react-antd-renderers';
17
+ import React from 'react';
18
+ import { createRoot, Root } from 'react-dom/client';
19
+ import { StyleProvider } from '@ant-design/cssinjs';
20
+ import {
21
+ createTranslator,
22
+ parseBoolean,
23
+ parseJson,
24
+ parseMode,
25
+ type JsonInput,
26
+ } from '@chobantonov/jsonforms-react-renderer-common/webcomponent/input';
27
+
28
+ export { createTranslator, parseBoolean, parseJson, parseMode };
29
+ export type { JsonInput };
30
+
31
+ export const JSON_FORMS_ANTD_TAG = 'jsonforms-react-antd';
32
+
33
+ /**
34
+ * `ConfigProvider`, told which language it is in.
35
+ *
36
+ * antd owns a second set of strings the form never authors - month names,
37
+ * "Today", "OK", a select's empty text - and they stay English unless its
38
+ * locale is set. The locale is fetched as its own chunk on first use, so a
39
+ * build carries every supported language without shipping them all.
40
+ *
41
+ * `locale` being undefined is antd's own default, which is English. That is
42
+ * what a tag this build does not carry falls back to, and a form in English
43
+ * chrome is a better outcome than one that will not render.
44
+ */
45
+ const LocalizedConfigProvider = ({
46
+ localeTag,
47
+ children,
48
+ ...rest
49
+ }: Omit<React.ComponentProps<typeof ConfigProvider>, 'locale'> & {
50
+ localeTag?: string;
51
+ }) => (
52
+ <ConfigProvider {...rest} locale={useAntdLocale(localeTag)}>
53
+ {children}
54
+ </ConfigProvider>
55
+ );
56
+
57
+ /**
58
+ * Ajv's **own** message localizers, off by default.
59
+ *
60
+ * `ajv-i18n` ships around twenty languages, and importing them is a fixed cost
61
+ * a distributed web component should not pay for a form that never changes
62
+ * locale. Schema-authored `errorMessage` text is translated regardless,
63
+ * through the element's own `translations` - that needs no locale data.
64
+ *
65
+ * A host that wants "must be >= 5" in its language opts in at startup, before
66
+ * any element is upgraded:
67
+ *
68
+ * ```ts
69
+ * import { setAjvLocalizers } from '@chobantonov/jsonforms-react-antd-webcomponent';
70
+ * import { ajvLocalizers } from '@chobantonov/jsonforms-react-extended-renderers/ajv-localizers';
71
+ *
72
+ * setAjvLocalizers(ajvLocalizers);
73
+ * ```
74
+ *
75
+ * or carries only what it ships, which is the point of the option:
76
+ *
77
+ * ```ts
78
+ * setAjvLocalizers({ bg: localizeBg });
79
+ * ```
80
+ *
81
+ * Read through a getter by the validator, so setting it after an element has
82
+ * been created still takes effect on the next validation.
83
+ */
84
+ let ajvLocalizers: AjvLocalizers | undefined;
85
+
86
+ export const setAjvLocalizers = (localizers: AjvLocalizers | undefined) => {
87
+ ajvLocalizers = localizers;
88
+ };
89
+
90
+ export class JsonFormsAntdElement extends FormElement {
91
+ static get observedAttributes() {
92
+ return formAttributes;
93
+ }
94
+
95
+ private root?: Root;
96
+ /*
97
+ Per element, not per render: Ajv caches compiled schemas, and a fresh
98
+ instance each paint would recompile the whole schema on every keystroke.
99
+ */
100
+ private ajv = createFormsAjv({
101
+ /*
102
+ A getter, read at validation time: one element outlives many locales,
103
+ and the validator is created once per element (below), so capturing the
104
+ state here would freeze whichever locale happened to be set first.
105
+
106
+ This localizes **schema-authored** `errorMessage` text, which costs
107
+ nothing to carry. Ajv's *own* messages ("must be >= 5") stay English
108
+ unless the host also supplies `localizers` - that import pulls every
109
+ language `ajv-i18n` ships, which a distributed bundle should opt into
110
+ rather than inherit. See `setAjvLocalizers` below.
111
+ */
112
+ i18n: () => ({
113
+ locale: this.state.locale,
114
+ translate: this.state.translations
115
+ ? createTranslator(this.state.translations, this.state.locale)
116
+ : defaultTranslator,
117
+ }),
118
+ /*
119
+ Deliberately absent from here in the common case: Ajv's own messages are
120
+ localized at render time instead (see `translateError` below), because
121
+ core does not revalidate on a locale change and a message produced
122
+ during validation would stay in the previous language.
123
+ */
124
+ });
125
+ /*
126
+ Per element, like the validator: a store is the identity errors are
127
+ published against, and it outlives every render of this element.
128
+ */
129
+ readonly errorStore = createAdditionalErrorStore();
130
+ private colorScheme?: MediaQueryList;
131
+ private renderQueued = false;
132
+ private connectionVersion = 0;
133
+ protected state: FormElementState = {
134
+ validationMode: 'ValidateAndShow',
135
+ locale: 'en',
136
+ mode: 'system',
137
+ readonly: false,
138
+ customStyle: '',
139
+ };
140
+
141
+ constructor() {
142
+ super();
143
+ this.attachShadow({ mode: 'open' });
144
+ }
145
+
146
+ connectedCallback() {
147
+ if (!this.shadowRoot) return;
148
+ this.connectionVersion += 1;
149
+ this.root ??= createRoot(this.shadowRoot);
150
+ this.colorScheme = window.matchMedia('(prefers-color-scheme: dark)');
151
+ this.colorScheme.addEventListener('change', this.handleColorSchemeChange);
152
+ this.scheduleRender();
153
+ }
154
+
155
+ disconnectedCallback() {
156
+ const connectionVersion = ++this.connectionVersion;
157
+ const root = this.root;
158
+ this.colorScheme?.removeEventListener(
159
+ 'change',
160
+ this.handleColorSchemeChange,
161
+ );
162
+ this.colorScheme = undefined;
163
+
164
+ queueMicrotask(() => {
165
+ if (
166
+ this.isConnected ||
167
+ connectionVersion !== this.connectionVersion ||
168
+ root !== this.root
169
+ ) {
170
+ return;
171
+ }
172
+ root?.unmount();
173
+ if (root === this.root) this.root = undefined;
174
+ });
175
+ }
176
+
177
+ private handleColorSchemeChange = () => this.scheduleRender();
178
+
179
+ protected setValue(name: string, value: JsonInput) {
180
+ const key = name.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
181
+ this.state = {
182
+ ...this.state,
183
+ [key]: value,
184
+ };
185
+ this.scheduleRender();
186
+ }
187
+
188
+ private scheduleRender() {
189
+ if (this.renderQueued) return;
190
+ this.renderQueued = true;
191
+ queueMicrotask(() => {
192
+ this.renderQueued = false;
193
+ if (this.isConnected) this.render();
194
+ });
195
+ }
196
+
197
+ private dispatch(name: string, detail: unknown) {
198
+ this.dispatchEvent(
199
+ new CustomEvent(name, {
200
+ detail,
201
+ bubbles: true,
202
+ composed: true,
203
+ }),
204
+ );
205
+ }
206
+
207
+ private render() {
208
+ if (!this.root) return;
209
+
210
+ const input = this.readFormInput();
211
+ if (!input) return;
212
+ const { schema, data } = input;
213
+
214
+ const readonly = parseBoolean(this.state.readonly) ?? false;
215
+ const selectedMode = parseMode(this.state.mode);
216
+ const dark =
217
+ parseBoolean(this.state.dark) ??
218
+ (selectedMode === 'dark' ||
219
+ (selectedMode === 'system' && Boolean(this.colorScheme?.matches)));
220
+ const rtl = parseBoolean(this.state.rtl) ?? false;
221
+ const config = parseJson(this.state.config) as
222
+ | Record<string, unknown>
223
+ | undefined;
224
+ const translate = this.state.translations
225
+ ? createTranslator(this.state.translations, this.state.locale)
226
+ : undefined;
227
+ /*
228
+ Only when the host has opted into locale data; without it core's default
229
+ translator applies, which leaves Ajv's English wording alone.
230
+ */
231
+ const translateError = ajvLocalizers
232
+ ? createAjvErrorTranslator(ajvLocalizers, () => this.state.locale)
233
+ : undefined;
234
+ const rendererSettings =
235
+ (parseJson(this.state.rendererSettings) as Record<string, unknown>) ?? {};
236
+
237
+ this.root.render(
238
+ <StyleProvider container={this.shadowRoot ?? undefined}>
239
+ <LocalizedConfigProvider
240
+ localeTag={this.state.locale}
241
+ direction={rtl ? 'rtl' : 'ltr'}
242
+ getPopupContainer={() =>
243
+ (this.shadowRoot?.querySelector(
244
+ '.jsonforms-react-antd',
245
+ ) as HTMLElement | null) ?? this
246
+ }
247
+ theme={{
248
+ algorithm: dark
249
+ ? antdTheme.darkAlgorithm
250
+ : antdTheme.defaultAlgorithm,
251
+ }}
252
+ >
253
+ <div
254
+ className={
255
+ dark ? 'jsonforms-react-antd dark' : 'jsonforms-react-antd'
256
+ }
257
+ dir={rtl ? 'rtl' : 'ltr'}
258
+ >
259
+ <style>{`
260
+ :host { display: block; color-scheme: ${dark ? 'dark' : 'light'}; }
261
+ .jsonforms-react-antd { box-sizing: border-box; min-width: 0; color-scheme: light; background: #fff; color: rgba(0, 0, 0, 0.88); }
262
+ .jsonforms-react-antd.dark { color-scheme: dark; background: #141414; color: rgba(255, 255, 255, 0.88); }
263
+ ${this.state.customStyle ?? ''}
264
+ `}</style>
265
+ <slot name='styles' />
266
+ <slot name='form-header' />
267
+ <Form
268
+ layout='vertical'
269
+ variant={
270
+ (rendererSettings.inputVariant ??
271
+ 'outlined') as InputProps['variant']
272
+ }
273
+ >
274
+ {/*
275
+ `ExtendedJsonForms` rather than `JsonForms`: it installs the
276
+ additional-error store - the middleware and the prop, which
277
+ only work together - and provides the action handler. Without
278
+ it the Monaco control's `propagateErrors` would publish into
279
+ nothing, and an element's own `additionalErrors` attribute is
280
+ merged rather than replaced.
281
+ */}
282
+ <ExtendedJsonForms
283
+ external={
284
+ parseJson(this.state.external) as
285
+ | Readonly<Record<string, unknown>>
286
+ | undefined
287
+ }
288
+ store={this.errorStore}
289
+ onAction={(event) => this.dispatch('handle-action', event)}
290
+ data={data}
291
+ schema={schema}
292
+ uischema={parseJson(this.state.uischema)}
293
+ uischemas={parseJson(this.state.uischemas) as any}
294
+ config={{
295
+ ...config,
296
+ readonly,
297
+ }}
298
+ readonly={readonly}
299
+ validationMode={this.state.validationMode}
300
+ i18n={{
301
+ locale: this.state.locale,
302
+ translate,
303
+ ...(translateError ? { translateError } : {}),
304
+ }}
305
+ additionalErrors={parseJson(this.state.additionalErrors) as any}
306
+ ajv={this.ajv}
307
+ renderers={antdWebcomponentRenderers}
308
+ cells={antdWebcomponentCells}
309
+ onChange={(event) => this.dispatch('change', event)}
310
+ />
311
+ </Form>
312
+ <slot name='form-footer' />
313
+ </div>
314
+ </LocalizedConfigProvider>
315
+ </StyleProvider>,
316
+ );
317
+ }
318
+ }
319
+
320
+ export const registerJsonFormsAntd = (
321
+ tagName = JSON_FORMS_ANTD_TAG,
322
+ ): CustomElementConstructor => {
323
+ const existing = customElements.get(tagName);
324
+ if (existing) return existing;
325
+ customElements.define(tagName, JsonFormsAntdElement);
326
+ return JsonFormsAntdElement;
327
+ };
package/src/index.tsx ADDED
@@ -0,0 +1,4 @@
1
+ export * from './JsonFormsAntdElement';
2
+ export * from './renderers';
3
+
4
+ export { TRUST_DYNAMIC_FUNCTIONS } from '@chobantonov/jsonforms-react-antd-extended-renderers';
@@ -0,0 +1,5 @@
1
+ import { registerJsonFormsAntd } from './index';
2
+
3
+ registerJsonFormsAntd();
4
+
5
+ export * from './index';
@@ -0,0 +1,20 @@
1
+ import {
2
+ antdCells,
3
+ antdRenderers,
4
+ } from '@chobantonov/jsonforms-react-antd-renderers';
5
+ import {
6
+ antdExtendedCells,
7
+ antdExtendedRenderers,
8
+ } from '@chobantonov/jsonforms-react-antd-extended-renderers';
9
+
10
+ export const antdWebcomponentRenderers = [
11
+ ...antdRenderers,
12
+ ...antdExtendedRenderers,
13
+ ];
14
+ /*
15
+ The extended cells are concatenated, not omitted: a column whose schema says
16
+ `format: "color"` or `format: "duration"` would otherwise fall back to a
17
+ plain text cell, because both controls exist only in the renderer registry
18
+ and array columns dispatch through the cells registry.
19
+ */
20
+ export const antdWebcomponentCells = [...antdCells, ...antdExtendedCells];