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

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 +6995 -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 +565 -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 +29 -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,134 @@
1
+ import React, { useCallback, useState } from "react";
2
+ import { AddIcon, ArrayProperty, Button, Paper, Property, PropertyConfig, Typography } from "@firecms/core";
3
+ import { getIn, useFormikContext } from "formik";
4
+ import { PropertyFormDialog } from "../PropertyEditView";
5
+ import { getFullId, idToPropertiesPath, namespaceToPropertiesOrderPath } from "../util";
6
+ import { PropertyTree } from "../PropertyTree";
7
+
8
+ export function BlockPropertyField({ disabled, getData, allowDataInference, propertyConfigs, collectionEditable }: {
9
+ disabled: boolean;
10
+ getData?: () => Promise<object[]>;
11
+ allowDataInference: boolean;
12
+ propertyConfigs: Record<string, PropertyConfig>,
13
+ collectionEditable: boolean;
14
+ }) {
15
+
16
+ const {
17
+ values,
18
+ setFieldValue
19
+ } = useFormikContext<ArrayProperty>();
20
+
21
+ const [propertyDialogOpen, setPropertyDialogOpen] = useState<boolean>(false);
22
+ const [selectedPropertyKey, setSelectedPropertyKey] = useState<string | undefined>();
23
+ const [selectedPropertyNamespace, setSelectedPropertyNamespace] = useState<string | undefined>();
24
+
25
+ const onPropertyCreated = useCallback(({
26
+ id,
27
+ property
28
+ }: { id?: string, property: Property }) => {
29
+ if (!id)
30
+ throw Error();
31
+ setFieldValue("oneOf.properties", {
32
+ ...(values.oneOf?.properties ?? {}),
33
+ [id]: property
34
+ }, false);
35
+ setFieldValue("oneOf.propertiesOrder", [...(values.oneOf?.propertiesOrder ?? Object.keys(values.oneOf?.properties ?? {})), id], false);
36
+ setPropertyDialogOpen(false);
37
+ }, [values.oneOf?.properties, values.oneOf?.propertiesOrder]);
38
+
39
+ const selectedPropertyFullId = selectedPropertyKey ? getFullId(selectedPropertyKey, selectedPropertyNamespace) : undefined;
40
+ const selectedProperty = selectedPropertyFullId ? getIn(values.oneOf?.properties, selectedPropertyFullId.replaceAll(".", ".properties.")) : undefined;
41
+
42
+ const deleteProperty = useCallback((propertyKey?: string, namespace?: string) => {
43
+ const fullId = propertyKey ? getFullId(propertyKey, namespace) : undefined;
44
+ if (!fullId)
45
+ throw Error("collection editor miss config");
46
+
47
+ setFieldValue(`oneOf.${idToPropertiesPath(fullId)}`, undefined, false);
48
+ const propertiesOrderPath = `oneOf.${namespaceToPropertiesOrderPath(namespace)}`;
49
+ const currentPropertiesOrder: string[] = getIn(values, propertiesOrderPath);
50
+ setFieldValue(propertiesOrderPath, currentPropertiesOrder.filter((p) => p !== propertyKey), false);
51
+
52
+ setPropertyDialogOpen(false);
53
+ setSelectedPropertyKey(undefined);
54
+ setSelectedPropertyNamespace(undefined);
55
+ }, [setFieldValue, values]);
56
+
57
+ const addChildButton = <Button
58
+ autoFocus
59
+ color="primary"
60
+
61
+ onClick={() => setPropertyDialogOpen(true)}
62
+ startIcon={<AddIcon/>}
63
+ >
64
+ Add property to {values.name ?? "this block"}
65
+ </Button>;
66
+
67
+ const onPropertyMove = useCallback((propertiesOrder: string[], namespace?: string) => {
68
+ setFieldValue(`oneOf.${namespaceToPropertiesOrderPath(namespace)}`, propertiesOrder, false);
69
+ }, []);
70
+
71
+ return (
72
+ <>
73
+ <div className={"col-span-12"}>
74
+ <div className={"flex justify-between items-end mt-8 mb-4"}>
75
+ <Typography variant={"subtitle2"}>Properties in this
76
+ block</Typography>
77
+ {addChildButton}
78
+ </div>
79
+ <Paper className="p-2 pl-8">
80
+
81
+ <PropertyTree
82
+ properties={values.oneOf?.properties ?? {}}
83
+ propertiesOrder={values.oneOf?.propertiesOrder}
84
+ errors={{}}
85
+ collectionEditable={collectionEditable}
86
+ onPropertyClick={disabled
87
+ ? undefined
88
+ : (propertyKey, namespace) => {
89
+ setSelectedPropertyKey(propertyKey);
90
+ setSelectedPropertyNamespace(namespace);
91
+ setPropertyDialogOpen(true);
92
+ }}
93
+ onPropertyMove={disabled
94
+ ? undefined
95
+ : onPropertyMove}/>
96
+
97
+ {!disabled && !values.oneOf?.propertiesOrder?.length &&
98
+ <div className="h-full flex items-center justify-center p-4">
99
+ Add the first property to this block
100
+ </div>}
101
+
102
+ </Paper>
103
+ </div>
104
+
105
+ {!disabled && <PropertyFormDialog
106
+ inArray={false}
107
+ forceShowErrors={false}
108
+ open={propertyDialogOpen}
109
+ getData={getData}
110
+ allowDataInference={allowDataInference}
111
+ onCancel={() => {
112
+ setPropertyDialogOpen(false);
113
+ setSelectedPropertyKey(undefined);
114
+ setSelectedPropertyNamespace(undefined);
115
+ }}
116
+ onOkClicked={() => {
117
+ setPropertyDialogOpen(false);
118
+ setSelectedPropertyKey(undefined);
119
+ setSelectedPropertyNamespace(undefined);
120
+ }}
121
+ collectionEditable={collectionEditable}
122
+ onDelete={deleteProperty}
123
+ propertyKey={selectedPropertyKey}
124
+ propertyNamespace={selectedPropertyNamespace}
125
+ property={selectedProperty}
126
+ existingProperty={Boolean(selectedPropertyKey)}
127
+ autoUpdateId={!selectedPropertyKey}
128
+ autoOpenTypeSelect={!selectedPropertyKey}
129
+ onPropertyChanged={onPropertyCreated}
130
+ existingPropertyKeys={selectedPropertyKey ? undefined : values.oneOf?.propertiesOrder}
131
+ propertyConfigs={propertyConfigs}/>}
132
+
133
+ </>);
134
+ }
@@ -0,0 +1,36 @@
1
+ import React from "react";
2
+ import { FastField, getIn, useFormikContext } from "formik";
3
+
4
+ import { GeneralPropertyValidation } from "./validation/GeneralPropertyValidation";
5
+ import { ValidationPanel } from "./validation/ValidationPanel";
6
+ import { SwitchControl } from "@firecms/core";
7
+
8
+ export function BooleanPropertyField({ disabled }: {
9
+ disabled: boolean;
10
+ }) {
11
+ const { values } = useFormikContext();
12
+ const defaultValue = getIn(values, "defaultValue");
13
+
14
+ return (
15
+ <>
16
+ <div className={"col-span-12"}>
17
+
18
+ <ValidationPanel>
19
+ <GeneralPropertyValidation disabled={disabled}/>
20
+ </ValidationPanel>
21
+
22
+ </div>
23
+
24
+ <div className={"col-span-12"}>
25
+
26
+ <FastField type="checkbox"
27
+ name={"defaultValue"}
28
+ label={defaultValue === null || defaultValue === undefined ? "Default value not set" : ("Default value is " + defaultValue.toString())}
29
+ disabled={disabled}
30
+ allowIndeterminate={true}
31
+ component={SwitchControl}/>
32
+
33
+ </div>
34
+ </>
35
+ );
36
+ }
@@ -0,0 +1,111 @@
1
+ import { Field, getIn, useFormikContext } from "formik";
2
+ import { DebouncedTextField, TextField } from "@firecms/core";
3
+ import { PropertyWithId } from "../PropertyEditView";
4
+ import React from "react";
5
+ import { FieldHelperView } from "./FieldHelperView";
6
+
7
+ type CommonPropertyFieldsProps = {
8
+ showErrors: boolean,
9
+ disabledId: boolean,
10
+ existingPropertyKeys?: string[];
11
+ disabled: boolean;
12
+ isNewProperty: boolean;
13
+ };
14
+
15
+ export const CommonPropertyFields = React.forwardRef<HTMLDivElement, CommonPropertyFieldsProps>(
16
+ function CommonPropertyFields({
17
+ showErrors,
18
+ disabledId,
19
+ existingPropertyKeys,
20
+ disabled,
21
+ isNewProperty
22
+ }, ref) {
23
+
24
+ const {
25
+ errors
26
+ } = useFormikContext<PropertyWithId>();
27
+
28
+ const name = "name";
29
+ const nameError = showErrors && getIn(errors, name);
30
+
31
+ const id = "id";
32
+ const idError = showErrors && getIn(errors, id);
33
+
34
+ const description = "description";
35
+ const descriptionError = showErrors && getIn(errors, description);
36
+
37
+ return (
38
+ <div className={"flex flex-col gap-2 col-span-12"}>
39
+
40
+ <div>
41
+ <Field
42
+ inputRef={ref}
43
+ name={name}
44
+ as={DebouncedTextField}
45
+ style={{ fontSize: 20 }}
46
+ validate={validateName}
47
+ placeholder={"Field name"}
48
+ required
49
+ disabled={disabled}
50
+ error={Boolean(nameError)}/>
51
+
52
+ <FieldHelperView error={Boolean(nameError)}>
53
+ {nameError}
54
+ </FieldHelperView>
55
+ </div>
56
+
57
+ <div>
58
+ <Field name={id}
59
+ as={TextField}
60
+ label={"ID"}
61
+ validate={(value?: string) => validateId(value, existingPropertyKeys)}
62
+ disabled={disabledId || disabled}
63
+ required
64
+ size="small"
65
+ error={Boolean(idError)}/>
66
+ <FieldHelperView error={Boolean(idError)}>
67
+ {idError}
68
+ </FieldHelperView>
69
+ </div>
70
+
71
+ <div>
72
+ <Field name={description}
73
+ as={DebouncedTextField}
74
+ label={"Description"}
75
+ disabled={disabled}
76
+ error={Boolean(descriptionError)}/>
77
+ <FieldHelperView error={Boolean(descriptionError)}>
78
+ {descriptionError}
79
+ </FieldHelperView>
80
+ </div>
81
+
82
+ </div>
83
+ );
84
+
85
+ }
86
+ );
87
+
88
+ const idRegEx = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
89
+
90
+ function validateId(value?: string, existingPropertyKeys?: string[]) {
91
+
92
+ let error;
93
+ if (!value) {
94
+ error = "You must specify an id for the field";
95
+ }
96
+ if (value && !value.match(idRegEx)) {
97
+ error = "The id can only contain letters, numbers and underscores (_), and not start with a number";
98
+ }
99
+ if (value && existingPropertyKeys && existingPropertyKeys.includes(value)) {
100
+ error = "There is another field with this ID already";
101
+ }
102
+ return error;
103
+ }
104
+
105
+ function validateName(value: string) {
106
+ let error;
107
+ if (!value) {
108
+ error = "You must specify a title for the field";
109
+ }
110
+ return error;
111
+ }
@@ -0,0 +1,86 @@
1
+ import React from "react";
2
+ import { getIn, useFormikContext } from "formik";
3
+ import { NumberProperty, Select, SelectItem, StringProperty } from "@firecms/core";
4
+ import { GeneralPropertyValidation } from "./validation/GeneralPropertyValidation";
5
+ import { ValidationPanel } from "./validation/ValidationPanel";
6
+ import { FieldHelperView } from "./FieldHelperView";
7
+
8
+ export function DateTimePropertyField({ disabled }: {
9
+ disabled: boolean;
10
+ }) {
11
+
12
+ const {
13
+ values,
14
+ errors,
15
+ touched,
16
+ setFieldValue
17
+ } = useFormikContext<StringProperty | NumberProperty>();
18
+
19
+ const modePath = "mode";
20
+ const modeValue: string | undefined = getIn(values, modePath);
21
+ const modeError: string | undefined = getIn(touched, modePath) && getIn(errors, modePath);
22
+
23
+ const autoValuePath = "autoValue";
24
+ const autoValueValue: string | undefined = getIn(values, autoValuePath);
25
+ const autoValueError: string | undefined = getIn(touched, autoValuePath) && getIn(errors, autoValuePath);
26
+
27
+ return (
28
+ <>
29
+ <div className={"flex flex-col col-span-12"}>
30
+ <Select name={modePath}
31
+ value={modeValue ?? "date"}
32
+ error={Boolean(modeError)}
33
+ onValueChange={(v) => setFieldValue(modePath, v)}
34
+ label={"Mode"}
35
+ renderValue={(v) => {
36
+ switch (v) {
37
+ case "date_time":
38
+ return "Date/Time";
39
+ case "date":
40
+ return "Date";
41
+ default:
42
+ return "";
43
+ }
44
+ }}
45
+ disabled={disabled}>
46
+ <SelectItem value={"date_time"}> Date/Time </SelectItem>
47
+ <SelectItem value={"date"}> Date </SelectItem>
48
+ </Select>
49
+ <FieldHelperView error={Boolean(modeError)}>
50
+ {modeError}
51
+ </FieldHelperView>
52
+
53
+ <Select name={autoValuePath}
54
+ disabled={disabled}
55
+ value={autoValueValue ?? ""}
56
+ onValueChange={(v) => setFieldValue(autoValuePath, v)}
57
+ renderValue={(v) => {
58
+ switch (v) {
59
+ case "on_create":
60
+ return "On create";
61
+ case "on_update":
62
+ return "On any update";
63
+ default:
64
+ return "None";
65
+ }
66
+ }}
67
+ error={Boolean(autoValueError)}
68
+ label={"Automatic value"}>
69
+ <SelectItem value={""}> None </SelectItem>
70
+ <SelectItem value={"on_create"}> On create </SelectItem>
71
+ <SelectItem value={"on_update"}> On any update </SelectItem>
72
+ </Select>
73
+ <FieldHelperView error={Boolean(autoValueError)}>
74
+ {autoValueError ?? "Update this field automatically when creating or updating the entity"}
75
+ </FieldHelperView>
76
+
77
+ </div>
78
+
79
+ <div className={"col-span-12"}>
80
+ <ValidationPanel>
81
+ <GeneralPropertyValidation disabled={disabled}/>
82
+ </ValidationPanel>
83
+ </div>
84
+ </>
85
+ );
86
+ }
@@ -0,0 +1,116 @@
1
+ import React, { useMemo } from "react";
2
+ import { getIn, useFormikContext } from "formik";
3
+ import { EnumValueConfig, resolveEnumValues, Select, SelectItem, useSnackbarController } from "@firecms/core";
4
+ import { EnumForm } from "../EnumForm";
5
+ import { StringPropertyValidation } from "./validation/StringPropertyValidation";
6
+ import { ArrayPropertyValidation } from "./validation/ArrayPropertyValidation";
7
+ import { ValidationPanel } from "./validation/ValidationPanel";
8
+ import { PropertyWithId } from "../PropertyEditView";
9
+
10
+ export function EnumPropertyField({
11
+ multiselect,
12
+ updateIds,
13
+ disabled,
14
+ showErrors,
15
+ allowDataInference,
16
+ getData
17
+ }: {
18
+ multiselect: boolean;
19
+ updateIds: boolean;
20
+ disabled: boolean;
21
+ showErrors: boolean;
22
+ allowDataInference?: boolean;
23
+ getData?: () => Promise<object[]>;
24
+ }) {
25
+
26
+ const {
27
+ values,
28
+ handleChange,
29
+ errors,
30
+ touched,
31
+ setFieldError,
32
+ setFieldValue
33
+ } = useFormikContext<PropertyWithId>();
34
+
35
+ const snackbarContext = useSnackbarController();
36
+
37
+ const enumValuesPath = multiselect ? "of.enumValues" : "enumValues";
38
+
39
+ const defaultValue = getIn(values, "defaultValue");
40
+
41
+ const valuesEnumValues = getIn(values, enumValuesPath);
42
+ const enumValues: EnumValueConfig[] = useMemo(() => {
43
+ if (!valuesEnumValues || typeof valuesEnumValues === "boolean")
44
+ return [] as EnumValueConfig[];
45
+ return resolveEnumValues(valuesEnumValues) ?? [] as EnumValueConfig[];
46
+ }, [valuesEnumValues]);
47
+
48
+ const onValuesChanged = (value: EnumValueConfig[]) => {
49
+ if (!values)
50
+ return;
51
+ setFieldValue(enumValuesPath, value);
52
+ if (!multiselect) {
53
+ const enumIds = value.filter(v => Boolean(v?.id)).map((v: any) => v.id);
54
+ if (defaultValue && !enumIds.includes(defaultValue)) {
55
+ setFieldValue("defaultValue", undefined);
56
+ snackbarContext.open({
57
+ type: "warning",
58
+ message: "Default value was cleared"
59
+ })
60
+ }
61
+ }
62
+ };
63
+
64
+ return (
65
+ <>
66
+ <div className={"col-span-12"}>
67
+ <EnumForm enumValues={enumValues}
68
+ updateIds={updateIds}
69
+ disabled={disabled}
70
+ allowDataInference={allowDataInference}
71
+ onError={(hasError) => {
72
+ setFieldError(enumValuesPath, hasError ? "" : undefined);
73
+ }}
74
+ getData={getData
75
+ ? () => getData()
76
+ .then(res => res.map(d => values.id && getIn(d, values.id)).filter(Boolean))
77
+ : undefined}
78
+ onValuesChanged={onValuesChanged}/>
79
+ </div>
80
+
81
+ <div className={"col-span-12"}>
82
+
83
+ <ValidationPanel>
84
+ {!multiselect &&
85
+ <StringPropertyValidation disabled={disabled}
86
+ showErrors={showErrors}/>}
87
+ {multiselect &&
88
+ <ArrayPropertyValidation disabled={disabled}/>}
89
+ </ValidationPanel>
90
+
91
+ </div>
92
+
93
+ {!multiselect && <div className={"col-span-12"}>
94
+
95
+ <Select
96
+ disabled={disabled}
97
+ position={"item-aligned"}
98
+ onValueChange={(value: string) => {
99
+ setFieldValue("defaultValue", value);
100
+ }}
101
+ label={"Default value"}
102
+ value={defaultValue ?? ""}>
103
+ {enumValues
104
+ .filter((enumValue) => Boolean(enumValue?.id))
105
+ .map((enumValue) => (
106
+ <SelectItem key={enumValue.id}
107
+ value={enumValue.id?.toString()}>
108
+ {enumValue.label}
109
+ </SelectItem>
110
+ ))}
111
+ </Select>
112
+
113
+ </div>}
114
+ </>
115
+ );
116
+ }
@@ -0,0 +1,13 @@
1
+ import { Typography } from "@firecms/core";
2
+
3
+ export function FieldHelperView({
4
+ error,
5
+ children
6
+ }: { error?: boolean, children?: React.ReactNode }) {
7
+ if (!children) return null;
8
+ return (
9
+ <Typography variant={"caption"} color={error ? "error" : "secondary"} className={"ml-3.5 mt-0.5"}>
10
+ {children}
11
+ </Typography>
12
+ );
13
+ }
@@ -0,0 +1,20 @@
1
+ import React from "react";
2
+ import { ValidationPanel } from "./validation/ValidationPanel";
3
+ import { GeneralPropertyValidation } from "./validation/GeneralPropertyValidation";
4
+
5
+ export function KeyValuePropertyField({ disabled }: {
6
+ disabled: boolean;
7
+ }) {
8
+
9
+ return (
10
+ <>
11
+ <div className={"col-span-12"}>
12
+
13
+ <ValidationPanel>
14
+ <GeneralPropertyValidation disabled={disabled}/>
15
+ </ValidationPanel>
16
+
17
+ </div>
18
+ </>
19
+ );
20
+ }
@@ -0,0 +1,157 @@
1
+ import React, { useCallback, useState } from "react";
2
+ import {
3
+ AddIcon,
4
+ BooleanSwitchWithLabel,
5
+ Button,
6
+ MapProperty,
7
+ Paper,
8
+ Property,
9
+ PropertyConfig,
10
+ Typography
11
+ } from "@firecms/core";
12
+ import { PropertyFormDialog } from "../PropertyEditView";
13
+ import { getIn, useFormikContext } from "formik";
14
+ import { PropertyTree } from "../PropertyTree";
15
+ import { getFullId, idToPropertiesPath, namespaceToPropertiesOrderPath, namespaceToPropertiesPath } from "../util";
16
+ import { FieldHelperView } from "./FieldHelperView";
17
+
18
+ export function MapPropertyField({ disabled, getData, allowDataInference, propertyConfigs, collectionEditable }: {
19
+ disabled: boolean;
20
+ getData?: () => Promise<object[]>;
21
+ allowDataInference: boolean;
22
+ propertyConfigs: Record<string, PropertyConfig>,
23
+ collectionEditable: boolean;
24
+ }) {
25
+
26
+ const {
27
+ values,
28
+ setFieldValue
29
+ } = useFormikContext<MapProperty>();
30
+
31
+ const [propertyDialogOpen, setPropertyDialogOpen] = useState<boolean>(false);
32
+ const [selectedPropertyKey, setSelectedPropertyKey] = useState<string | undefined>();
33
+ const [selectedPropertyNamespace, setSelectedPropertyNamespace] = useState<string | undefined>();
34
+
35
+ const propertiesOrder = values.propertiesOrder ?? Object.keys(values.properties ?? {});
36
+ const onPropertyCreated = useCallback(({
37
+ id,
38
+ property
39
+ }: { id?: string, property: Property }) => {
40
+ if (!id)
41
+ throw Error();
42
+ setFieldValue("properties", {
43
+ ...(values.properties ?? {}),
44
+ [id]: property
45
+ }, false);
46
+ setFieldValue("propertiesOrder", [...propertiesOrder, id], false);
47
+ setPropertyDialogOpen(false);
48
+ }, [values.properties, propertiesOrder]);
49
+
50
+ const deleteProperty = useCallback((propertyKey?: string, namespace?: string) => {
51
+ const fullId = propertyKey ? getFullId(propertyKey, namespace) : undefined;
52
+ if (!fullId)
53
+ throw Error("collection editor miss config");
54
+
55
+ const propertiesPath = idToPropertiesPath(fullId);
56
+ const propertiesOrderPath = namespaceToPropertiesOrderPath(namespace);
57
+
58
+ const currentPropertiesOrder: string[] = getIn(values, propertiesOrderPath) ?? Object.keys(getIn(values, namespaceToPropertiesPath(namespace)));
59
+
60
+ setFieldValue(propertiesPath, undefined, false);
61
+ setFieldValue(propertiesOrderPath, currentPropertiesOrder.filter((p) => p !== propertyKey), false);
62
+
63
+ setPropertyDialogOpen(false);
64
+ setSelectedPropertyKey(undefined);
65
+ setSelectedPropertyNamespace(undefined);
66
+ }, [setFieldValue, values]);
67
+
68
+ const selectedPropertyFullId = selectedPropertyKey ? getFullId(selectedPropertyKey, selectedPropertyNamespace) : undefined;
69
+ const selectedProperty = selectedPropertyFullId ? getIn(values.properties, selectedPropertyFullId.replaceAll(".", ".properties.")) : undefined;
70
+
71
+ const addChildButton = <Button
72
+ color="primary"
73
+ variant={"outlined"}
74
+ onClick={() => setPropertyDialogOpen(true)}
75
+ startIcon={<AddIcon/>}
76
+ >
77
+ Add property to {values.name ?? "this group"}
78
+ </Button>;
79
+
80
+ const empty = !propertiesOrder || propertiesOrder.length < 1;
81
+
82
+ const onPropertyMove = useCallback((propertiesOrder: string[], namespace?: string) => {
83
+ setFieldValue(namespaceToPropertiesOrderPath(namespace), propertiesOrder, false);
84
+ }, []);
85
+
86
+ return (
87
+ <>
88
+ <div className={"col-span-12"}>
89
+ <div className="flex justify-between items-end my-4">
90
+ <Typography variant={"subtitle2"}>Properties in this group</Typography>
91
+ {addChildButton}
92
+ </div>
93
+ <Paper className="p-2 pl-8">
94
+ <PropertyTree
95
+ properties={values.properties ?? {}}
96
+ propertiesOrder={propertiesOrder}
97
+ errors={{}}
98
+ collectionEditable={collectionEditable}
99
+ onPropertyClick={(propertyKey, namespace) => {
100
+ setSelectedPropertyKey(propertyKey);
101
+ setSelectedPropertyNamespace(namespace);
102
+ setPropertyDialogOpen(true);
103
+ }}
104
+ onPropertyMove={onPropertyMove}/>
105
+
106
+ {empty &&
107
+ <Typography variant={"label"}
108
+ className="h-full flex items-center justify-center p-4">
109
+ Add the first property to this group
110
+ </Typography>}
111
+ </Paper>
112
+ </div>
113
+
114
+ <div className={"col-span-12"}>
115
+ <BooleanSwitchWithLabel
116
+ position={"start"}
117
+ size={"small"}
118
+ label="Spread children as columns"
119
+ onValueChange={(v) => setFieldValue("spreadChildren", v)}
120
+ value={values.spreadChildren ?? false}
121
+ />
122
+ <FieldHelperView>
123
+ Set this flag to true if you want to display the children of this group as individual columns.
124
+ </FieldHelperView>
125
+ </div>
126
+
127
+ <PropertyFormDialog
128
+ inArray={false}
129
+ forceShowErrors={false}
130
+ open={propertyDialogOpen}
131
+ allowDataInference={allowDataInference}
132
+ collectionEditable={collectionEditable}
133
+ onCancel={() => {
134
+ setPropertyDialogOpen(false);
135
+ setSelectedPropertyKey(undefined);
136
+ setSelectedPropertyNamespace(undefined);
137
+ }}
138
+ onOkClicked={() => {
139
+ setPropertyDialogOpen(false);
140
+ setSelectedPropertyKey(undefined);
141
+ setSelectedPropertyNamespace(undefined);
142
+ }}
143
+ getData={getData}
144
+ onDelete={deleteProperty}
145
+ propertyKey={selectedPropertyKey}
146
+ propertyNamespace={selectedPropertyNamespace}
147
+ property={selectedProperty}
148
+ existingProperty={Boolean(selectedPropertyKey)}
149
+ autoUpdateId={!selectedPropertyKey}
150
+ autoOpenTypeSelect={!selectedPropertyKey}
151
+ onPropertyChanged={onPropertyCreated}
152
+ existingPropertyKeys={selectedPropertyKey ? undefined : propertiesOrder}
153
+ propertyConfigs={propertyConfigs}
154
+ />
155
+
156
+ </>);
157
+ }