@terreno/admin-frontend 0.13.3 → 0.14.1
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/dist/AdminFieldRenderer.d.ts +5 -53
- package/dist/AdminFieldRenderer.js +6 -168
- package/dist/AdminFieldRenderer.js.map +1 -1
- package/dist/AdminFieldRendererCore.d.ts +26 -0
- package/dist/AdminFieldRendererCore.js +138 -0
- package/dist/AdminFieldRendererCore.js.map +1 -0
- package/dist/AdminModelForm.d.ts +6 -7
- package/dist/AdminModelForm.js.map +1 -1
- package/dist/AdminModelList.d.ts +2 -3
- package/dist/AdminModelList.js.map +1 -1
- package/dist/AdminModelTable.d.ts +2 -2
- package/dist/AdminModelTable.js +2 -1
- package/dist/AdminModelTable.js.map +1 -1
- package/dist/AdminNestedArrayField.d.ts +6 -7
- package/dist/AdminNestedArrayField.js +26 -2
- package/dist/AdminNestedArrayField.js.map +1 -1
- package/dist/AdminObjectPicker.d.ts +2 -2
- package/dist/AdminObjectPicker.js +13 -7
- package/dist/AdminObjectPicker.js.map +1 -1
- package/dist/AdminPrimitiveArrayField.d.ts +2 -3
- package/dist/AdminPrimitiveArrayField.js.map +1 -1
- package/dist/AdminScriptList.d.ts +2 -2
- package/dist/AdminScriptList.js.map +1 -1
- package/dist/AdminScriptRunModal.d.ts +2 -2
- package/dist/AdminScriptRunModal.js.map +1 -1
- package/dist/AdminVersionConfig.d.ts +2 -2
- package/dist/AdminVersionConfig.js +16 -10
- package/dist/AdminVersionConfig.js.map +1 -1
- package/dist/ConfigurationScreen.d.ts +2 -2
- package/dist/ConfigurationScreen.js +18 -9
- package/dist/ConfigurationScreen.js.map +1 -1
- package/dist/ConsentFormEditor.d.ts +27 -2
- package/dist/ConsentFormEditor.js +48 -32
- package/dist/ConsentFormEditor.js.map +1 -1
- package/dist/ConsentFormList.d.ts +2 -1
- package/dist/ConsentFormList.js.map +1 -1
- package/dist/ConsentResponseViewer.d.ts +2 -1
- package/dist/ConsentResponseViewer.js.map +1 -1
- package/dist/DocumentStorageBrowser.js.map +1 -1
- package/dist/types.d.ts +29 -4
- package/dist/types.js.map +1 -1
- package/dist/useAdminApi.d.ts +2 -2
- package/dist/useAdminApi.js +8 -5
- package/dist/useAdminApi.js.map +1 -1
- package/dist/useAdminConfig.d.ts +2 -3
- package/dist/useAdminConfig.js +2 -0
- package/dist/useAdminConfig.js.map +1 -1
- package/dist/useAdminScripts.d.ts +2 -3
- package/dist/useAdminScripts.js +6 -3
- package/dist/useAdminScripts.js.map +1 -1
- package/dist/useConfigurationApi.d.ts +11 -8
- package/dist/useConfigurationApi.js +7 -4
- package/dist/useConfigurationApi.js.map +1 -1
- package/dist/useDocumentStorageApi.d.ts +2 -2
- package/dist/useDocumentStorageApi.js +9 -6
- package/dist/useDocumentStorageApi.js.map +1 -1
- package/package.json +2 -2
- package/src/AdminFieldRenderer.test.tsx +48 -22
- package/src/AdminFieldRenderer.tsx +18 -371
- package/src/AdminFieldRendererCore.tsx +356 -0
- package/src/AdminModelForm.test.tsx +146 -40
- package/src/AdminModelForm.tsx +22 -17
- package/src/AdminModelList.test.tsx +14 -5
- package/src/AdminModelList.tsx +5 -5
- package/src/AdminModelTable.test.tsx +58 -31
- package/src/AdminModelTable.tsx +11 -10
- package/src/AdminNestedArrayField.tsx +91 -26
- package/src/AdminObjectPicker.tsx +33 -16
- package/src/AdminPrimitiveArrayField.test.tsx +14 -10
- package/src/AdminPrimitiveArrayField.tsx +2 -3
- package/src/AdminScriptList.test.tsx +7 -4
- package/src/AdminScriptList.tsx +2 -3
- package/src/AdminScriptRunModal.test.tsx +5 -2
- package/src/AdminScriptRunModal.tsx +2 -3
- package/src/AdminVersionConfig.test.tsx +47 -21
- package/src/AdminVersionConfig.tsx +29 -5
- package/src/CheckboxListEditor.test.tsx +9 -6
- package/src/ConfigurationScreen.test.tsx +16 -11
- package/src/ConfigurationScreen.tsx +43 -29
- package/src/ConsentFormEditor.test.tsx +95 -75
- package/src/ConsentFormEditor.tsx +76 -49
- package/src/ConsentFormList.test.tsx +28 -10
- package/src/ConsentFormList.tsx +14 -2
- package/src/ConsentResponseViewer.test.tsx +15 -12
- package/src/ConsentResponseViewer.tsx +3 -2
- package/src/DocumentStorageBrowser.test.tsx +69 -42
- package/src/DocumentStorageBrowser.tsx +4 -4
- package/src/LocaleContentEditor.test.tsx +28 -18
- package/src/isolated/AdminFieldRenderer.isolated.tsx +5 -2
- package/src/isolated/AdminNestedArrayField.isolated.tsx +49 -36
- package/src/isolated/AdminObjectPicker.isolated.tsx +31 -26
- package/src/isolated/AdminRefField.isolated.tsx +4 -1
- package/src/isolated/AdminScriptRunModal.isolated.tsx +24 -17
- package/src/isolated/DocumentStorageBrowser.isolated.tsx +80 -63
- package/src/isolated/hooks.isolated.tsx +40 -33
- package/src/types.ts +34 -4
- package/src/useAdminApi.ts +21 -13
- package/src/useAdminConfig.ts +5 -4
- package/src/useAdminScripts.ts +9 -7
- package/src/useConfigurationApi.ts +26 -12
- package/src/useDocumentStorageApi.ts +12 -9
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BooleanField,
|
|
3
|
+
DateTimeField,
|
|
4
|
+
MarkdownEditorField,
|
|
5
|
+
SelectField,
|
|
6
|
+
TextField,
|
|
7
|
+
} from "@terreno/ui";
|
|
8
|
+
import startCase from "lodash/startCase";
|
|
9
|
+
import React from "react";
|
|
10
|
+
import {AdminPrimitiveArrayField} from "./AdminPrimitiveArrayField";
|
|
11
|
+
import {AdminRefField} from "./AdminRefField";
|
|
12
|
+
import {CheckboxListEditor} from "./CheckboxListEditor";
|
|
13
|
+
import {LocaleContentEditor} from "./LocaleContentEditor";
|
|
14
|
+
import type {AdminFieldConfig, AdminFieldValue, AdminScreenProps, RefRendererMap} from "./types";
|
|
15
|
+
|
|
16
|
+
// Attempts to parse a string as JSON, returning the parsed value or the raw string
|
|
17
|
+
const parseJsonValue = (text: string): AdminFieldValue => {
|
|
18
|
+
const trimmed = text.trim();
|
|
19
|
+
if (trimmed === "") {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(trimmed);
|
|
24
|
+
} catch {
|
|
25
|
+
return trimmed;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// Serializes any value to a display string for the JSON editor
|
|
30
|
+
const serializeJsonValue = (val: AdminFieldValue): string => {
|
|
31
|
+
if (val == null) {
|
|
32
|
+
return "";
|
|
33
|
+
}
|
|
34
|
+
if (typeof val === "string") {
|
|
35
|
+
return val;
|
|
36
|
+
}
|
|
37
|
+
return JSON.stringify(val, null, 2);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export interface AdminFieldRendererCoreProps extends AdminScreenProps {
|
|
41
|
+
fieldKey: string;
|
|
42
|
+
fieldConfig: AdminFieldConfig;
|
|
43
|
+
value: AdminFieldValue;
|
|
44
|
+
onChange: (value: AdminFieldValue) => void;
|
|
45
|
+
errorText?: string;
|
|
46
|
+
modelConfigs?: Array<{name: string; routePath: string}>;
|
|
47
|
+
/** Parent document form state, used to derive dynamic options for sub-fields */
|
|
48
|
+
parentFormState?: Record<string, AdminFieldValue>;
|
|
49
|
+
/**
|
|
50
|
+
* Optional map of custom ref-field renderers keyed by referenced model name.
|
|
51
|
+
* Forwarded to {@link AdminPrimitiveArrayField} so nested fields participate in the override.
|
|
52
|
+
*/
|
|
53
|
+
refRenderers?: RefRendererMap;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Renders admin form fields for all types except arrays of sub-documents.
|
|
58
|
+
* Nested sub-document arrays are handled by {@link AdminNestedArrayField} via
|
|
59
|
+
* {@link AdminFieldRenderer} to avoid a circular import.
|
|
60
|
+
*/
|
|
61
|
+
export const AdminFieldRendererCore: React.FC<AdminFieldRendererCoreProps> = ({
|
|
62
|
+
fieldKey,
|
|
63
|
+
fieldConfig,
|
|
64
|
+
value,
|
|
65
|
+
onChange,
|
|
66
|
+
errorText,
|
|
67
|
+
baseUrl,
|
|
68
|
+
api,
|
|
69
|
+
modelConfigs,
|
|
70
|
+
parentFormState,
|
|
71
|
+
refRenderers,
|
|
72
|
+
}) => {
|
|
73
|
+
const label = startCase(fieldKey);
|
|
74
|
+
const helperText = fieldConfig.description;
|
|
75
|
+
|
|
76
|
+
// Dynamic enum: look for a sibling array field whose items have a `key` property.
|
|
77
|
+
if (!fieldConfig.enum && parentFormState && fieldConfig.type === "string") {
|
|
78
|
+
const pluralKey = `${fieldKey}s`;
|
|
79
|
+
const siblingArray = parentFormState[pluralKey];
|
|
80
|
+
if (Array.isArray(siblingArray) && siblingArray.length > 0 && siblingArray[0]?.key != null) {
|
|
81
|
+
const dynamicOptions = (siblingArray as Array<{key?: string}>)
|
|
82
|
+
.map((item) => item.key)
|
|
83
|
+
.filter(Boolean)
|
|
84
|
+
.map((k) => ({label: k as string, value: k as string}));
|
|
85
|
+
if (dynamicOptions.length > 0) {
|
|
86
|
+
return (
|
|
87
|
+
<SelectField
|
|
88
|
+
errorText={errorText}
|
|
89
|
+
helperText={helperText}
|
|
90
|
+
onChange={onChange}
|
|
91
|
+
options={dynamicOptions}
|
|
92
|
+
title={label}
|
|
93
|
+
value={typeof value === "string" ? value : ""}
|
|
94
|
+
/>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ObjectId with ref -> reference field (skip arrays — those go to AdminPrimitiveArrayField)
|
|
101
|
+
if (fieldConfig.ref && fieldConfig.type !== "array") {
|
|
102
|
+
const CustomRenderer = refRenderers?.[fieldConfig.ref];
|
|
103
|
+
const refModel = modelConfigs?.find((m) => m.name === fieldConfig.ref);
|
|
104
|
+
if (CustomRenderer) {
|
|
105
|
+
return (
|
|
106
|
+
<CustomRenderer
|
|
107
|
+
api={api}
|
|
108
|
+
baseUrl={baseUrl}
|
|
109
|
+
errorText={errorText}
|
|
110
|
+
helperText={helperText}
|
|
111
|
+
onChange={onChange}
|
|
112
|
+
refModelName={fieldConfig.ref}
|
|
113
|
+
routePath={refModel?.routePath ?? ""}
|
|
114
|
+
title={label}
|
|
115
|
+
value={typeof value === "string" ? value : ""}
|
|
116
|
+
/>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (refModel) {
|
|
120
|
+
return (
|
|
121
|
+
<AdminRefField
|
|
122
|
+
api={api}
|
|
123
|
+
baseUrl={baseUrl}
|
|
124
|
+
errorText={errorText}
|
|
125
|
+
helperText={helperText}
|
|
126
|
+
onChange={onChange}
|
|
127
|
+
refModelName={fieldConfig.ref}
|
|
128
|
+
routePath={refModel.routePath}
|
|
129
|
+
title={label}
|
|
130
|
+
value={typeof value === "string" ? value : ""}
|
|
131
|
+
/>
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (fieldConfig.type === "boolean") {
|
|
137
|
+
return (
|
|
138
|
+
<BooleanField
|
|
139
|
+
errorText={errorText}
|
|
140
|
+
helperText={helperText}
|
|
141
|
+
onChange={onChange}
|
|
142
|
+
title={label}
|
|
143
|
+
value={typeof value === "boolean" ? value : false}
|
|
144
|
+
/>
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (fieldConfig.enum && fieldConfig.enum.length > 0) {
|
|
149
|
+
const includesNullOption = fieldConfig.enum.some((enumValue) => enumValue == null);
|
|
150
|
+
const enumOptions = fieldConfig.enum
|
|
151
|
+
.filter((enumValue): enumValue is string => typeof enumValue === "string")
|
|
152
|
+
.map((v: string) => ({label: startCase(v), value: v}));
|
|
153
|
+
const options = includesNullOption ? [{label: "None", value: ""}, ...enumOptions] : enumOptions;
|
|
154
|
+
return (
|
|
155
|
+
<SelectField
|
|
156
|
+
errorText={errorText}
|
|
157
|
+
helperText={helperText}
|
|
158
|
+
onChange={(nextValue: string) => onChange(nextValue === "" ? undefined : nextValue)}
|
|
159
|
+
options={options}
|
|
160
|
+
title={label}
|
|
161
|
+
value={typeof value === "string" ? value : ""}
|
|
162
|
+
/>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (
|
|
167
|
+
fieldConfig.type === "date" ||
|
|
168
|
+
fieldConfig.type === "datetime" ||
|
|
169
|
+
(fieldConfig.type === "string" && fieldKey.toLowerCase().includes("date"))
|
|
170
|
+
) {
|
|
171
|
+
return (
|
|
172
|
+
<DateTimeField
|
|
173
|
+
errorText={errorText}
|
|
174
|
+
helperText={helperText}
|
|
175
|
+
onChange={onChange}
|
|
176
|
+
testID={`admin-field-${fieldKey}`}
|
|
177
|
+
title={label}
|
|
178
|
+
type="datetime"
|
|
179
|
+
value={typeof value === "string" ? value : ""}
|
|
180
|
+
/>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (fieldConfig.type === "number") {
|
|
185
|
+
return (
|
|
186
|
+
<TextField
|
|
187
|
+
errorText={errorText}
|
|
188
|
+
helperText={helperText}
|
|
189
|
+
onChange={(text: string) => {
|
|
190
|
+
const num = Number(text);
|
|
191
|
+
onChange(Number.isNaN(num) ? text : num);
|
|
192
|
+
}}
|
|
193
|
+
testID={`admin-field-${fieldKey}`}
|
|
194
|
+
title={label}
|
|
195
|
+
value={value != null ? String(value) : ""}
|
|
196
|
+
/>
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (fieldConfig.widget === "locale-content") {
|
|
201
|
+
const localeValue =
|
|
202
|
+
value && typeof value === "object" && !Array.isArray(value)
|
|
203
|
+
? (value as Record<string, string>)
|
|
204
|
+
: {};
|
|
205
|
+
return (
|
|
206
|
+
<LocaleContentEditor
|
|
207
|
+
errorText={errorText}
|
|
208
|
+
helperText={helperText}
|
|
209
|
+
onChange={onChange}
|
|
210
|
+
title={label}
|
|
211
|
+
value={localeValue}
|
|
212
|
+
/>
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (fieldConfig.widget === "locale-default") {
|
|
217
|
+
const contentMap = parentFormState?.content;
|
|
218
|
+
const localeKeys =
|
|
219
|
+
contentMap && typeof contentMap === "object" && !Array.isArray(contentMap)
|
|
220
|
+
? Object.keys(contentMap)
|
|
221
|
+
: [];
|
|
222
|
+
const hasLocales = localeKeys.length > 0;
|
|
223
|
+
const options = localeKeys.map((k) => ({label: k.toUpperCase(), value: k}));
|
|
224
|
+
return (
|
|
225
|
+
<SelectField
|
|
226
|
+
disabled={!hasLocales}
|
|
227
|
+
errorText={errorText}
|
|
228
|
+
helperText={
|
|
229
|
+
hasLocales
|
|
230
|
+
? helperText
|
|
231
|
+
: "Add at least one locale with content before setting a default locale."
|
|
232
|
+
}
|
|
233
|
+
onChange={onChange}
|
|
234
|
+
options={options}
|
|
235
|
+
title={label}
|
|
236
|
+
value={typeof value === "string" ? value : ""}
|
|
237
|
+
/>
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (fieldConfig.type === "object" || fieldConfig.type === "mixed") {
|
|
242
|
+
const displayValue = serializeJsonValue(value);
|
|
243
|
+
return (
|
|
244
|
+
<TextField
|
|
245
|
+
errorText={errorText}
|
|
246
|
+
helperText={helperText ?? "JSON value (string, number, boolean, object, or array)"}
|
|
247
|
+
onChange={(text: string) => {
|
|
248
|
+
onChange(parseJsonValue(text));
|
|
249
|
+
}}
|
|
250
|
+
testID={`admin-field-${fieldKey}`}
|
|
251
|
+
title={label}
|
|
252
|
+
value={displayValue}
|
|
253
|
+
/>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (fieldConfig.type === "array" && fieldConfig.itemType && !fieldConfig.items) {
|
|
258
|
+
return (
|
|
259
|
+
<AdminPrimitiveArrayField
|
|
260
|
+
api={api}
|
|
261
|
+
baseUrl={baseUrl}
|
|
262
|
+
errorText={errorText}
|
|
263
|
+
helperText={helperText}
|
|
264
|
+
itemEnum={fieldConfig.itemEnum}
|
|
265
|
+
itemRef={fieldConfig.itemRef}
|
|
266
|
+
itemType={fieldConfig.itemType}
|
|
267
|
+
modelConfigs={modelConfigs}
|
|
268
|
+
onChange={onChange}
|
|
269
|
+
refRenderers={refRenderers}
|
|
270
|
+
title={label}
|
|
271
|
+
value={Array.isArray(value) ? value : []}
|
|
272
|
+
/>
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (fieldConfig.type === "array") {
|
|
277
|
+
const jsonValue =
|
|
278
|
+
value != null ? (typeof value === "string" ? value : JSON.stringify(value, null, 2)) : "[]";
|
|
279
|
+
return (
|
|
280
|
+
<TextField
|
|
281
|
+
errorText={errorText}
|
|
282
|
+
grow
|
|
283
|
+
helperText={helperText ?? "Enter valid JSON array"}
|
|
284
|
+
multiline
|
|
285
|
+
onChange={(text: string) => {
|
|
286
|
+
try {
|
|
287
|
+
onChange(JSON.parse(text));
|
|
288
|
+
} catch {
|
|
289
|
+
onChange(text);
|
|
290
|
+
}
|
|
291
|
+
}}
|
|
292
|
+
rows={4}
|
|
293
|
+
testID={`admin-field-${fieldKey}`}
|
|
294
|
+
title={label}
|
|
295
|
+
value={jsonValue}
|
|
296
|
+
/>
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (fieldConfig.widget === "markdown") {
|
|
301
|
+
return (
|
|
302
|
+
<MarkdownEditorField
|
|
303
|
+
errorText={errorText}
|
|
304
|
+
helperText={helperText}
|
|
305
|
+
onChange={onChange}
|
|
306
|
+
testID={`admin-field-${fieldKey}`}
|
|
307
|
+
title={label}
|
|
308
|
+
value={typeof value === "string" ? value : ""}
|
|
309
|
+
/>
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (fieldConfig.widget === "checkbox-list") {
|
|
314
|
+
return (
|
|
315
|
+
<CheckboxListEditor
|
|
316
|
+
errorText={errorText}
|
|
317
|
+
helperText={helperText}
|
|
318
|
+
onChange={onChange}
|
|
319
|
+
title={label}
|
|
320
|
+
value={
|
|
321
|
+
Array.isArray(value)
|
|
322
|
+
? (value as React.ComponentProps<typeof CheckboxListEditor>["value"])
|
|
323
|
+
: []
|
|
324
|
+
}
|
|
325
|
+
/>
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (fieldConfig.widget === "textarea") {
|
|
330
|
+
return (
|
|
331
|
+
<TextField
|
|
332
|
+
errorText={errorText}
|
|
333
|
+
grow
|
|
334
|
+
helperText={helperText}
|
|
335
|
+
multiline
|
|
336
|
+
onChange={onChange}
|
|
337
|
+
rows={6}
|
|
338
|
+
testID={`admin-field-${fieldKey}`}
|
|
339
|
+
title={label}
|
|
340
|
+
value={typeof value === "string" ? value : ""}
|
|
341
|
+
/>
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Default: string -> TextField
|
|
346
|
+
return (
|
|
347
|
+
<TextField
|
|
348
|
+
errorText={errorText}
|
|
349
|
+
helperText={helperText}
|
|
350
|
+
onChange={onChange}
|
|
351
|
+
testID={`admin-field-${fieldKey}`}
|
|
352
|
+
title={label}
|
|
353
|
+
value={typeof value === "string" ? value : ""}
|
|
354
|
+
/>
|
|
355
|
+
);
|
|
356
|
+
};
|