@firecms/collection_editor 3.0.0-alpha.5 → 3.0.0-alpha.50

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 (140) hide show
  1. package/dist/ConfigControllerProvider.d.ts +36 -0
  2. package/dist/index.d.ts +11 -0
  3. package/dist/index.es.js +6998 -0
  4. package/dist/index.es.js.map +1 -0
  5. package/dist/index.umd.js +4 -0
  6. package/dist/index.umd.js.map +1 -0
  7. package/dist/types/collection_editor_controller.d.ts +35 -0
  8. package/dist/types/collection_inference.d.ts +2 -0
  9. package/dist/types/config_controller.d.ts +41 -0
  10. package/dist/types/config_permissions.d.ts +19 -0
  11. package/dist/types/persisted_collection.d.ts +6 -0
  12. package/dist/ui/CollectionViewHeaderAction.d.ts +10 -0
  13. package/dist/ui/EditorCollectionAction.d.ts +2 -0
  14. package/dist/ui/HomePageEditorCollectionAction.d.ts +2 -0
  15. package/dist/ui/MissingReferenceWidget.d.ts +3 -0
  16. package/dist/ui/NewCollectionCard.d.ts +2 -0
  17. package/dist/ui/PropertyAddColumnComponent.d.ts +6 -0
  18. package/dist/ui/RootCollectionSuggestions.d.ts +1 -0
  19. package/dist/ui/collection_editor/CollectionDetailsForm.d.ts +9 -0
  20. package/dist/ui/collection_editor/CollectionEditorDialog.d.ts +38 -0
  21. package/dist/ui/collection_editor/CollectionEditorWelcomeView.d.ts +15 -0
  22. package/dist/ui/collection_editor/CollectionPropertiesEditorForm.d.ts +20 -0
  23. package/dist/ui/collection_editor/CollectionYupValidation.d.ts +14 -0
  24. package/dist/ui/collection_editor/EntityCustomViewsSelectDialog.d.ts +4 -0
  25. package/dist/ui/collection_editor/EnumForm.d.ts +13 -0
  26. package/dist/ui/collection_editor/GetCodeDialog.d.ts +5 -0
  27. package/dist/ui/collection_editor/PropertyEditView.d.ts +40 -0
  28. package/dist/ui/collection_editor/PropertyFieldPreview.d.ts +15 -0
  29. package/dist/ui/collection_editor/PropertySelectItem.d.ts +8 -0
  30. package/dist/ui/collection_editor/PropertyTree.d.ts +32 -0
  31. package/dist/ui/collection_editor/SelectIcons.d.ts +6 -0
  32. package/dist/ui/collection_editor/SubcollectionsEditTab.d.ts +12 -0
  33. package/dist/ui/collection_editor/UnsavedChangesDialog.d.ts +9 -0
  34. package/dist/ui/collection_editor/import/CollectionEditorImportDataPreview.d.ts +7 -0
  35. package/dist/ui/collection_editor/import/CollectionEditorImportMapping.d.ts +7 -0
  36. package/dist/ui/collection_editor/import/clean_import_data.d.ts +7 -0
  37. package/dist/ui/collection_editor/properties/BlockPropertyField.d.ts +8 -0
  38. package/dist/ui/collection_editor/properties/BooleanPropertyField.d.ts +3 -0
  39. package/dist/ui/collection_editor/properties/CommonPropertyFields.d.ts +10 -0
  40. package/dist/ui/collection_editor/properties/DateTimePropertyField.d.ts +3 -0
  41. package/dist/ui/collection_editor/properties/EnumPropertyField.d.ts +8 -0
  42. package/dist/ui/collection_editor/properties/FieldHelperView.d.ts +4 -0
  43. package/dist/ui/collection_editor/properties/KeyValuePropertyField.d.ts +3 -0
  44. package/dist/ui/collection_editor/properties/MapPropertyField.d.ts +8 -0
  45. package/dist/ui/collection_editor/properties/NumberPropertyField.d.ts +3 -0
  46. package/dist/ui/collection_editor/properties/ReferencePropertyField.d.ts +13 -0
  47. package/dist/ui/collection_editor/properties/RepeatPropertyField.d.ts +10 -0
  48. package/dist/ui/collection_editor/properties/StoragePropertyField.d.ts +5 -0
  49. package/dist/ui/collection_editor/properties/StringPropertyField.d.ts +5 -0
  50. package/dist/ui/collection_editor/properties/UrlPropertyField.d.ts +4 -0
  51. package/dist/ui/collection_editor/properties/advanced/AdvancedPropertyValidation.d.ts +3 -0
  52. package/dist/ui/collection_editor/properties/validation/ArrayPropertyValidation.d.ts +5 -0
  53. package/dist/ui/collection_editor/properties/validation/GeneralPropertyValidation.d.ts +4 -0
  54. package/dist/ui/collection_editor/properties/validation/NumberPropertyValidation.d.ts +3 -0
  55. package/dist/ui/collection_editor/properties/validation/StringPropertyValidation.d.ts +11 -0
  56. package/dist/ui/collection_editor/properties/validation/ValidationPanel.d.ts +2 -0
  57. package/dist/ui/collection_editor/templates/blog_template.d.ts +2 -0
  58. package/dist/ui/collection_editor/templates/products_template.d.ts +2 -0
  59. package/dist/ui/collection_editor/templates/users_template.d.ts +2 -0
  60. package/dist/ui/collection_editor/util.d.ts +4 -0
  61. package/dist/ui/collection_editor/utils/strings.d.ts +1 -0
  62. package/dist/ui/collection_editor/utils/supported_fields.d.ts +3 -0
  63. package/dist/ui/collection_editor/utils/update_property_for_widget.d.ts +2 -0
  64. package/dist/ui/collection_editor/utils/useTraceUpdate.d.ts +1 -0
  65. package/dist/useCollectionEditorController.d.ts +6 -0
  66. package/dist/useCollectionEditorPlugin.d.ts +45 -0
  67. package/dist/useCollectionsConfigController.d.ts +6 -0
  68. package/dist/utils/arrays.d.ts +1 -0
  69. package/dist/utils/entities.d.ts +3 -0
  70. package/dist/utils/icons.d.ts +1 -0
  71. package/dist/utils/synonyms.d.ts +1951 -0
  72. package/package.json +26 -23
  73. package/src/ConfigControllerProvider.tsx +321 -0
  74. package/src/index.ts +35 -0
  75. package/src/types/collection_editor_controller.tsx +42 -0
  76. package/src/types/collection_inference.ts +3 -0
  77. package/src/types/config_controller.tsx +50 -0
  78. package/src/types/config_permissions.ts +20 -0
  79. package/src/types/persisted_collection.ts +9 -0
  80. package/src/ui/CollectionViewHeaderAction.tsx +42 -0
  81. package/src/ui/EditorCollectionAction.tsx +95 -0
  82. package/src/ui/HomePageEditorCollectionAction.tsx +88 -0
  83. package/src/ui/MissingReferenceWidget.tsx +34 -0
  84. package/src/ui/NewCollectionCard.tsx +46 -0
  85. package/src/ui/PropertyAddColumnComponent.tsx +41 -0
  86. package/src/ui/RootCollectionSuggestions.tsx +62 -0
  87. package/src/ui/collection_editor/CollectionDetailsForm.tsx +353 -0
  88. package/src/ui/collection_editor/CollectionEditorDialog.tsx +744 -0
  89. package/src/ui/collection_editor/CollectionEditorWelcomeView.tsx +212 -0
  90. package/src/ui/collection_editor/CollectionPropertiesEditorForm.tsx +480 -0
  91. package/src/ui/collection_editor/CollectionYupValidation.tsx +7 -0
  92. package/src/ui/collection_editor/EntityCustomViewsSelectDialog.tsx +36 -0
  93. package/src/ui/collection_editor/EnumForm.tsx +356 -0
  94. package/src/ui/collection_editor/GetCodeDialog.tsx +118 -0
  95. package/src/ui/collection_editor/PropertyEditView.tsx +564 -0
  96. package/src/ui/collection_editor/PropertyFieldPreview.tsx +201 -0
  97. package/src/ui/collection_editor/PropertySelectItem.tsx +31 -0
  98. package/src/ui/collection_editor/PropertyTree.tsx +238 -0
  99. package/src/ui/collection_editor/SelectIcons.tsx +72 -0
  100. package/src/ui/collection_editor/SubcollectionsEditTab.tsx +252 -0
  101. package/src/ui/collection_editor/UnsavedChangesDialog.tsx +47 -0
  102. package/src/ui/collection_editor/import/CollectionEditorImportDataPreview.tsx +37 -0
  103. package/src/ui/collection_editor/import/CollectionEditorImportMapping.tsx +275 -0
  104. package/src/ui/collection_editor/import/clean_import_data.ts +53 -0
  105. package/src/ui/collection_editor/properties/BlockPropertyField.tsx +134 -0
  106. package/src/ui/collection_editor/properties/BooleanPropertyField.tsx +36 -0
  107. package/src/ui/collection_editor/properties/CommonPropertyFields.tsx +111 -0
  108. package/src/ui/collection_editor/properties/DateTimePropertyField.tsx +86 -0
  109. package/src/ui/collection_editor/properties/EnumPropertyField.tsx +116 -0
  110. package/src/ui/collection_editor/properties/FieldHelperView.tsx +13 -0
  111. package/src/ui/collection_editor/properties/KeyValuePropertyField.tsx +20 -0
  112. package/src/ui/collection_editor/properties/MapPropertyField.tsx +157 -0
  113. package/src/ui/collection_editor/properties/NumberPropertyField.tsx +38 -0
  114. package/src/ui/collection_editor/properties/ReferencePropertyField.tsx +184 -0
  115. package/src/ui/collection_editor/properties/RepeatPropertyField.tsx +107 -0
  116. package/src/ui/collection_editor/properties/StoragePropertyField.tsx +194 -0
  117. package/src/ui/collection_editor/properties/StringPropertyField.tsx +79 -0
  118. package/src/ui/collection_editor/properties/UrlPropertyField.tsx +89 -0
  119. package/src/ui/collection_editor/properties/advanced/AdvancedPropertyValidation.tsx +36 -0
  120. package/src/ui/collection_editor/properties/validation/ArrayPropertyValidation.tsx +50 -0
  121. package/src/ui/collection_editor/properties/validation/GeneralPropertyValidation.tsx +49 -0
  122. package/src/ui/collection_editor/properties/validation/NumberPropertyValidation.tsx +99 -0
  123. package/src/ui/collection_editor/properties/validation/StringPropertyValidation.tsx +131 -0
  124. package/src/ui/collection_editor/properties/validation/ValidationPanel.tsx +28 -0
  125. package/src/ui/collection_editor/templates/blog_template.ts +115 -0
  126. package/src/ui/collection_editor/templates/products_template.ts +88 -0
  127. package/src/ui/collection_editor/templates/users_template.ts +34 -0
  128. package/src/ui/collection_editor/util.ts +21 -0
  129. package/src/ui/collection_editor/utils/strings.ts +8 -0
  130. package/src/ui/collection_editor/utils/supported_fields.tsx +28 -0
  131. package/src/ui/collection_editor/utils/update_property_for_widget.ts +271 -0
  132. package/src/ui/collection_editor/utils/useTraceUpdate.tsx +23 -0
  133. package/src/useCollectionEditorController.tsx +9 -0
  134. package/src/useCollectionEditorPlugin.tsx +128 -0
  135. package/src/useCollectionsConfigController.tsx +9 -0
  136. package/src/utils/arrays.ts +3 -0
  137. package/src/utils/entities.ts +38 -0
  138. package/src/utils/icons.ts +17 -0
  139. package/src/utils/synonyms.ts +1952 -0
  140. package/src/vite-env.d.ts +1 -0
@@ -0,0 +1,564 @@
1
+ import React, { useDeferredValue, useEffect, useRef, useState } from "react";
2
+ import equal from "react-fast-compare"
3
+
4
+ import { Formik, FormikErrors, FormikProps, getIn } from "formik";
5
+ import {
6
+ Button,
7
+ cn,
8
+ DEFAULT_FIELD_CONFIGS,
9
+ DeleteConfirmationDialog,
10
+ DeleteIcon,
11
+ Dialog,
12
+ DialogActions,
13
+ DialogContent,
14
+ FieldConfigBadge,
15
+ FieldConfigId,
16
+ getFieldConfig,
17
+ getFieldId,
18
+ IconButton,
19
+ InfoLabel,
20
+ isPropertyBuilder,
21
+ mergeDeep,
22
+ Property,
23
+ PropertyConfig,
24
+ Select,
25
+ toSnakeCase,
26
+ Typography
27
+ } from "@firecms/core";
28
+ import { EnumPropertyField } from "./properties/EnumPropertyField";
29
+ import { StoragePropertyField } from "./properties/StoragePropertyField";
30
+ import { MapPropertyField } from "./properties/MapPropertyField";
31
+ import { RepeatPropertyField } from "./properties/RepeatPropertyField";
32
+ import { CommonPropertyFields } from "./properties/CommonPropertyFields";
33
+ import { StringPropertyField } from "./properties/StringPropertyField";
34
+ import { BooleanPropertyField } from "./properties/BooleanPropertyField";
35
+ import { BlockPropertyField } from "./properties/BlockPropertyField";
36
+ import { NumberPropertyField } from "./properties/NumberPropertyField";
37
+ import { ReferencePropertyField } from "./properties/ReferencePropertyField";
38
+ import { DateTimePropertyField } from "./properties/DateTimePropertyField";
39
+ import { AdvancedPropertyValidation } from "./properties/advanced/AdvancedPropertyValidation";
40
+ import { editableProperty } from "../../utils/entities";
41
+ import { KeyValuePropertyField } from "./properties/KeyValuePropertyField";
42
+ import { updatePropertyFromWidget } from "./utils/update_property_for_widget";
43
+ import { PropertySelectItem } from "./PropertySelectItem";
44
+ import { UrlPropertyField } from "./properties/UrlPropertyField";
45
+
46
+ export type PropertyWithId = Property & {
47
+ id?: string
48
+ };
49
+
50
+ export type OnPropertyChangedParams = {
51
+ id?: string,
52
+ property: Property,
53
+ namespace?: string,
54
+ previousId?: string
55
+ };
56
+
57
+ export type PropertyFormProps = {
58
+ includeIdAndName?: boolean;
59
+ existingProperty: boolean;
60
+ autoUpdateId?: boolean;
61
+ autoOpenTypeSelect: boolean;
62
+ inArray: boolean;
63
+ propertyKey?: string;
64
+ propertyNamespace?: string;
65
+ property?: Property;
66
+ onPropertyChanged?: (params: OnPropertyChangedParams) => void;
67
+ onPropertyChangedImmediate?: boolean;
68
+ onDelete?: (id?: string, namespace?: string) => void;
69
+ onError?: (id: string, namespace?: string, error?: FormikErrors<any>) => void;
70
+ initialErrors?: FormikErrors<any>;
71
+ forceShowErrors?: boolean;
72
+ existingPropertyKeys?: string[];
73
+ allowDataInference: boolean;
74
+ getData?: () => Promise<object[]>;
75
+ getHelpers?: (formikProps: FormikProps<PropertyWithId>) => void;
76
+ propertyConfigs: Record<string, PropertyConfig>;
77
+ collectionEditable: boolean;
78
+ };
79
+
80
+ export const PropertyForm = React.memo(
81
+ function PropertyForm({
82
+ includeIdAndName = true,
83
+ autoOpenTypeSelect,
84
+ existingProperty,
85
+ autoUpdateId,
86
+ inArray,
87
+ propertyKey,
88
+ propertyNamespace,
89
+ property,
90
+ onPropertyChanged,
91
+ onPropertyChangedImmediate = true,
92
+ onDelete,
93
+ onError,
94
+ initialErrors,
95
+ forceShowErrors,
96
+ existingPropertyKeys,
97
+ allowDataInference,
98
+ getHelpers,
99
+ getData,
100
+ propertyConfigs,
101
+ collectionEditable
102
+ }: PropertyFormProps) {
103
+
104
+ const initialValue: PropertyWithId = {
105
+ id: "",
106
+ name: ""
107
+ } as PropertyWithId;
108
+
109
+ const disabled = (Boolean(property && !editableProperty(property)) && !collectionEditable);
110
+
111
+ const lastSubmittedProperty = useRef<OnPropertyChangedParams | undefined>(property ? {
112
+ id: propertyKey,
113
+ previousId: propertyKey,
114
+ property
115
+ } : undefined);
116
+
117
+ const doOnPropertyChanged = ({
118
+ id,
119
+ property
120
+ }: OnPropertyChangedParams) => {
121
+ const params = {
122
+ id,
123
+ previousId: lastSubmittedProperty.current?.id,
124
+ property,
125
+ namespace: propertyNamespace
126
+ };
127
+ lastSubmittedProperty.current = params;
128
+ onPropertyChanged?.(params);
129
+ };
130
+
131
+ return <Formik
132
+ key={`property_view_${propertyKey}`}
133
+ initialErrors={initialErrors}
134
+ initialValues={property
135
+ ? { id: propertyKey, ...property } as PropertyWithId
136
+ : initialValue}
137
+ onSubmit={(newPropertyWithId: PropertyWithId, helpers) => {
138
+ console.log("Submitting property", newPropertyWithId)
139
+ const {
140
+ id,
141
+ ...property
142
+ } = newPropertyWithId;
143
+ doOnPropertyChanged({
144
+ id,
145
+ property: { ...property, editable: property.editable ?? true }
146
+ });
147
+ if (!existingProperty)
148
+ helpers.resetForm({ values: initialValue });
149
+ }}
150
+ // validate={(values) => {
151
+ // console.log("validate property", values)
152
+ // const errors: any = {};
153
+ // if (!values?.dataType || !getFieldConfig(values)) {
154
+ // errors.selectedWidget = "Required";
155
+ // }
156
+ // if (existingPropertyKeys && values?.id && existingPropertyKeys.includes(values?.id)) {
157
+ // errors.id = "";
158
+ // }
159
+ // console.log("errors", errors)
160
+ // return errors;
161
+ // }}
162
+ >
163
+ {(props) => {
164
+
165
+ // eslint-disable-next-line react-hooks/rules-of-hooks
166
+ useEffect(() => {
167
+ getHelpers?.(props);
168
+ }, [props]);
169
+
170
+ return <PropertyEditView
171
+ onPropertyChanged={onPropertyChangedImmediate
172
+ ? doOnPropertyChanged
173
+ : undefined}
174
+ onDelete={onDelete}
175
+ includeIdAndTitle={includeIdAndName}
176
+ propertyNamespace={propertyNamespace}
177
+ onError={onError}
178
+ showErrors={forceShowErrors || props.submitCount > 0}
179
+ existing={existingProperty}
180
+ autoUpdateId={autoUpdateId}
181
+ inArray={inArray}
182
+ autoOpenTypeSelect={autoOpenTypeSelect}
183
+ existingPropertyKeys={existingPropertyKeys}
184
+ disabled={disabled}
185
+ getData={getData}
186
+ allowDataInference={allowDataInference}
187
+ propertyConfigs={propertyConfigs}
188
+ collectionEditable={collectionEditable}
189
+ {...props}/>;
190
+
191
+ }}
192
+
193
+ </Formik>
194
+ }, (a, b) =>
195
+ a.getData === b.getData &&
196
+ a.includeIdAndName === b.includeIdAndName &&
197
+ a.autoOpenTypeSelect === b.autoOpenTypeSelect &&
198
+ a.autoUpdateId === b.autoUpdateId &&
199
+ a.existingProperty === b.existingProperty
200
+ );
201
+
202
+ export function PropertyFormDialog({
203
+ open,
204
+ onCancel,
205
+ onOkClicked,
206
+ onPropertyChanged,
207
+ getData,
208
+ collectionEditable,
209
+ ...formProps
210
+ }: PropertyFormProps & {
211
+ open?: boolean;
212
+ onOkClicked?: () => void;
213
+ onCancel?: () => void;
214
+ }) {
215
+ const helpersRef = useRef<FormikProps<PropertyWithId>>();
216
+ const getHelpers = (helpers: FormikProps<PropertyWithId>) => {
217
+ helpersRef.current = helpers;
218
+ };
219
+
220
+ return <Dialog
221
+ open={open ?? false}
222
+ maxWidth={"xl"}
223
+ fullWidth={true}
224
+ >
225
+
226
+ <DialogContent>
227
+ <PropertyForm {...formProps}
228
+ onPropertyChanged={(params) => {
229
+ onPropertyChanged?.(params);
230
+ onOkClicked?.();
231
+ }}
232
+ collectionEditable={collectionEditable}
233
+ onPropertyChangedImmediate={false}
234
+ getHelpers={getHelpers}
235
+ getData={getData}
236
+ />
237
+ </DialogContent>
238
+
239
+ <DialogActions>
240
+
241
+ {onCancel && <Button
242
+ variant={"text"}
243
+ onClick={() => {
244
+ onCancel();
245
+ helpersRef.current?.resetForm();
246
+ }}>
247
+ Cancel
248
+ </Button>}
249
+
250
+ <Button variant="outlined"
251
+ color="primary"
252
+ onClick={() => helpersRef.current?.submitForm()}>
253
+ Ok
254
+ </Button>
255
+ </DialogActions>
256
+ </Dialog>;
257
+
258
+ }
259
+
260
+ function PropertyEditView({
261
+ values,
262
+ errors,
263
+ touched,
264
+ setValues,
265
+ setFieldValue,
266
+ existing,
267
+ autoUpdateId = false,
268
+ autoOpenTypeSelect,
269
+ includeIdAndTitle,
270
+ onPropertyChanged,
271
+ onDelete,
272
+ propertyNamespace,
273
+ onError,
274
+ showErrors,
275
+ disabled,
276
+ inArray,
277
+ existingPropertyKeys,
278
+ getData,
279
+ allowDataInference,
280
+ propertyConfigs,
281
+ collectionEditable
282
+ }: {
283
+ includeIdAndTitle?: boolean;
284
+ existing: boolean;
285
+ autoUpdateId?: boolean;
286
+ autoOpenTypeSelect: boolean;
287
+ propertyNamespace?: string;
288
+ onPropertyChanged?: (params: OnPropertyChangedParams) => void;
289
+ onDelete?: (id?: string, namespace?: string) => void;
290
+ onError?: (id: string, namespace?: string, error?: FormikErrors<any>) => void;
291
+ showErrors: boolean;
292
+ inArray: boolean;
293
+ disabled: boolean;
294
+ existingPropertyKeys?: string[];
295
+ getData?: () => Promise<object[]>;
296
+ allowDataInference: boolean;
297
+ propertyConfigs: Record<string, PropertyConfig>;
298
+ collectionEditable: boolean;
299
+ } & FormikProps<PropertyWithId>) {
300
+
301
+ const [selectOpen, setSelectOpen] = useState(autoOpenTypeSelect);
302
+ const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
303
+ const [selectedFieldConfigId, setSelectedFieldConfigId] = useState<string | undefined>(values?.dataType ? getFieldId(values) : undefined);
304
+
305
+ const allSupportedFields = Object.entries(DEFAULT_FIELD_CONFIGS).concat(Object.entries(propertyConfigs));
306
+
307
+ const displayedWidgets = inArray
308
+ ? allSupportedFields.filter(([_, propertyConfig]) => !isPropertyBuilder(propertyConfig.property) && propertyConfig.property?.dataType !== "array")
309
+ : allSupportedFields;
310
+
311
+ const deferredValues = useDeferredValue(values);
312
+ const nameFieldRef = useRef<HTMLInputElement>(null);
313
+
314
+ const lastSubmittedProperty = useRef<object>(values);
315
+
316
+ const id = "id";
317
+
318
+ const selectedWidgetError = showErrors && getIn(errors, "selectedWidget");
319
+
320
+ useEffect(() => {
321
+ const idTouched = getIn(touched, id);
322
+ if (!idTouched && autoUpdateId && values?.name) {
323
+ setFieldValue(id, toSnakeCase(values.name))
324
+ }
325
+ }, [autoUpdateId, touched, values?.name]);
326
+
327
+ useEffect(() => {
328
+ if (onPropertyChanged) {
329
+ if ((!includeIdAndTitle || deferredValues.id)) {
330
+ const {
331
+ id,
332
+ ...property
333
+ } = deferredValues;
334
+ if (!equal(deferredValues, lastSubmittedProperty.current)) {
335
+ onPropertyChanged({
336
+ id,
337
+ property,
338
+ namespace: propertyNamespace
339
+ });
340
+ lastSubmittedProperty.current = deferredValues;
341
+ }
342
+ }
343
+ }
344
+ }, [deferredValues, includeIdAndTitle, onPropertyChanged, propertyNamespace]);
345
+
346
+ useEffect(() => {
347
+ if (values?.id && onError && Object.keys(errors).length > 0) {
348
+ onError(values?.id, propertyNamespace, errors);
349
+ }
350
+ }, [errors, onError, propertyNamespace, values?.id]);
351
+
352
+ const onWidgetSelectChanged = (newSelectedWidgetId: FieldConfigId) => {
353
+ setSelectedFieldConfigId(newSelectedWidgetId);
354
+ setValues(updatePropertyFromWidget(values, newSelectedWidgetId, propertyConfigs));
355
+ // Ugly hack to autofocus the name field
356
+ setTimeout(() => {
357
+ nameFieldRef.current?.focus();
358
+ }, 0);
359
+ };
360
+
361
+ let childComponent;
362
+ if (selectedFieldConfigId === "text_field" ||
363
+ selectedFieldConfigId === "multiline" ||
364
+ selectedFieldConfigId === "markdown" ||
365
+ selectedFieldConfigId === "email") {
366
+ childComponent =
367
+ <StringPropertyField widgetId={selectedFieldConfigId}
368
+ disabled={disabled}
369
+ showErrors={showErrors}/>;
370
+ } else if (selectedFieldConfigId === "url") {
371
+ childComponent =
372
+ <UrlPropertyField disabled={disabled}
373
+ showErrors={showErrors}/>;
374
+ } else if (selectedFieldConfigId === "select" ||
375
+ selectedFieldConfigId === "number_select") {
376
+ childComponent = <EnumPropertyField
377
+ multiselect={false}
378
+ allowDataInference={allowDataInference}
379
+ updateIds={!existing}
380
+ disabled={disabled}
381
+ getData={getData}
382
+ showErrors={showErrors}/>;
383
+ } else if (selectedFieldConfigId === "multi_select" ||
384
+ selectedFieldConfigId === "multi_number_select") {
385
+ childComponent = <EnumPropertyField
386
+ multiselect={true}
387
+ updateIds={!existing}
388
+ disabled={disabled}
389
+ allowDataInference={allowDataInference}
390
+ getData={getData}
391
+ showErrors={showErrors}/>;
392
+ } else if (selectedFieldConfigId === "file_upload") {
393
+ childComponent =
394
+ <StoragePropertyField existing={existing}
395
+ multiple={false}
396
+ disabled={disabled}/>;
397
+ } else if (selectedFieldConfigId === "multi_file_upload") {
398
+ childComponent =
399
+ <StoragePropertyField existing={existing}
400
+ multiple={true}
401
+ disabled={disabled}/>;
402
+ } else if (selectedFieldConfigId === "switch") {
403
+ childComponent = <BooleanPropertyField disabled={disabled}/>;
404
+ } else if (selectedFieldConfigId === "number_input") {
405
+ childComponent = <NumberPropertyField disabled={disabled}/>;
406
+ } else if (selectedFieldConfigId === "group") {
407
+ childComponent =
408
+ <MapPropertyField disabled={disabled} getData={getData} allowDataInference={allowDataInference}
409
+ collectionEditable={collectionEditable}
410
+ propertyConfigs={propertyConfigs}/>;
411
+ } else if (selectedFieldConfigId === "block") {
412
+ childComponent =
413
+ <BlockPropertyField disabled={disabled} getData={getData} allowDataInference={allowDataInference}
414
+ collectionEditable={collectionEditable}
415
+ propertyConfigs={propertyConfigs}/>;
416
+ } else if (selectedFieldConfigId === "reference") {
417
+ childComponent =
418
+ <ReferencePropertyField showErrors={showErrors}
419
+ existing={existing}
420
+ multiple={false}
421
+ disabled={disabled}/>;
422
+ } else if (selectedFieldConfigId === "date_time") {
423
+ childComponent = <DateTimePropertyField disabled={disabled}/>;
424
+ } else if (selectedFieldConfigId === "multi_references") {
425
+ childComponent =
426
+ <ReferencePropertyField showErrors={showErrors}
427
+ existing={existing}
428
+ multiple={true}
429
+ disabled={disabled}/>;
430
+ } else if (selectedFieldConfigId === "repeat") {
431
+ childComponent =
432
+ <RepeatPropertyField showErrors={showErrors}
433
+ existing={existing}
434
+ getData={getData}
435
+ allowDataInference={allowDataInference}
436
+ disabled={disabled}
437
+ collectionEditable={collectionEditable}
438
+ propertyConfigs={propertyConfigs}/>;
439
+ } else if (selectedFieldConfigId === "key_value") {
440
+ childComponent =
441
+ <KeyValuePropertyField disabled={disabled}/>;
442
+ } else {
443
+ childComponent = null;
444
+ }
445
+
446
+ return (
447
+ <>
448
+ {disabled && <InfoLabel mode={"warn"}>
449
+ <Typography>This property can&apos;t be edited</Typography>
450
+ <Typography variant={"caption"}>
451
+ You may not have permission to
452
+ edit it or it is defined in code with no <code>editable</code> flag
453
+ </Typography>
454
+ </InfoLabel>}
455
+
456
+ <div className="flex mt-2 justify-between">
457
+ <div className={"w-full flex flex-col gap-2"}>
458
+ <Select
459
+ // className={"w-full"}
460
+ error={Boolean(selectedWidgetError)}
461
+ value={selectedFieldConfigId ?? ""}
462
+ placeholder={"Select a property widget"}
463
+ open={selectOpen}
464
+ onOpenChange={setSelectOpen}
465
+ position={"item-aligned"}
466
+ disabled={disabled}
467
+ renderValue={(value) => {
468
+ if (!value) {
469
+ return <em>Select a property
470
+ widget</em>;
471
+ }
472
+ const key = value as FieldConfigId;
473
+ const propertyConfig = DEFAULT_FIELD_CONFIGS[key] ?? propertyConfigs[key];
474
+ const baseProperty = propertyConfig.property;
475
+ const baseFieldConfig = baseProperty && !isPropertyBuilder(baseProperty) ? getFieldConfig(baseProperty, propertyConfigs) : undefined;
476
+ const optionDisabled = isPropertyBuilder(baseProperty) || (existing && baseProperty.dataType !== values?.dataType);
477
+ const computedFieldConfig = baseFieldConfig ? mergeDeep(baseFieldConfig, propertyConfig) : propertyConfig;
478
+ return <div
479
+ onClick={(e) => {
480
+ if (optionDisabled) {
481
+ e.stopPropagation();
482
+ e.preventDefault();
483
+ }
484
+ }}
485
+ className={cn(
486
+ "flex items-center",
487
+ optionDisabled ? "w-full pointer-events-none opacity-50" : "")}>
488
+ <div className={"mr-8"}>
489
+ <FieldConfigBadge propertyConfig={computedFieldConfig}/>
490
+ </div>
491
+ <div className={"flex flex-col items-start text-base text-left"}>
492
+ <div>{computedFieldConfig.name}</div>
493
+ <Typography variant={"caption"}
494
+ color={"disabled"}>
495
+ {optionDisabled ? "You can only switch to widgets that use the same data type" : computedFieldConfig.description}
496
+ </Typography>
497
+ </div>
498
+ </div>
499
+ }}
500
+ onValueChange={(value) => {
501
+ onWidgetSelectChanged(value as FieldConfigId);
502
+ }}>
503
+ {displayedWidgets.map(([key, propertyConfig]) => {
504
+ const baseProperty = propertyConfig.property;
505
+ const optionDisabled = existing && !isPropertyBuilder(baseProperty) && baseProperty.dataType !== values?.dataType;
506
+ return <PropertySelectItem
507
+ key={key}
508
+ value={key}
509
+ optionDisabled={optionDisabled}
510
+ propertyConfig={propertyConfig}
511
+ existing={existing}/>;
512
+ })}
513
+ </Select>
514
+
515
+ {selectedWidgetError &&
516
+ <Typography variant="caption"
517
+ className={"ml-3.5"}
518
+ color={"error"}>Required</Typography>}
519
+
520
+ {/*<Typography variant="caption" className={"ml-3.5"}>Define your own custom properties and*/}
521
+ {/* components</Typography>*/}
522
+
523
+ </div>
524
+
525
+ {onDelete && values?.id &&
526
+ <IconButton
527
+ variant={"ghost"}
528
+ className="m-4"
529
+ disabled={disabled}
530
+ onClick={() => setDeleteDialogOpen(true)}>
531
+ <DeleteIcon/>
532
+ </IconButton>}
533
+ </div>
534
+
535
+ <div className={"grid grid-cols-12 gap-y-12 mt-8 mb-8"}>
536
+ {includeIdAndTitle &&
537
+ <CommonPropertyFields showErrors={showErrors}
538
+ disabledId={existing}
539
+ isNewProperty={!existing}
540
+ existingPropertyKeys={existingPropertyKeys}
541
+ disabled={disabled}
542
+ ref={nameFieldRef}/>}
543
+
544
+ {childComponent}
545
+
546
+ <div className={"col-span-12"}>
547
+ <AdvancedPropertyValidation disabled={disabled}/>
548
+ </div>
549
+ </div>
550
+
551
+ {onDelete &&
552
+ <DeleteConfirmationDialog open={deleteDialogOpen}
553
+ onAccept={() => onDelete(values?.id, propertyNamespace)}
554
+ onCancel={() => setDeleteDialogOpen(false)}
555
+ title={<div>Delete this property?</div>}
556
+ body={
557
+ <div> This will <b>not delete any
558
+ data</b>, only modify the
559
+ collection.</div>
560
+ }/>}
561
+
562
+ </>
563
+ );
564
+ }