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