@firecms/collection_editor 3.0.0-alpha.8 → 3.0.0-alpha.80
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/ConfigControllerProvider.d.ts +37 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.es.js +5257 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +4 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/types/collection_editor_controller.d.ts +35 -0
- package/dist/types/collection_inference.d.ts +2 -0
- package/dist/types/config_controller.d.ts +51 -0
- package/dist/types/config_permissions.d.ts +19 -0
- package/dist/types/persisted_collection.d.ts +6 -0
- package/dist/ui/CollectionViewHeaderAction.d.ts +10 -0
- package/dist/ui/EditorCollectionAction.d.ts +2 -0
- package/dist/ui/HomePageEditorCollectionAction.d.ts +2 -0
- package/dist/ui/MissingReferenceWidget.d.ts +3 -0
- package/dist/ui/NewCollectionButton.d.ts +1 -0
- package/dist/ui/NewCollectionCard.d.ts +2 -0
- package/dist/ui/PropertyAddColumnComponent.d.ts +6 -0
- package/dist/ui/RootCollectionSuggestions.d.ts +1 -0
- package/dist/ui/collection_editor/CollectionDetailsForm.d.ts +10 -0
- package/dist/ui/collection_editor/CollectionEditorDialog.d.ts +38 -0
- package/dist/ui/collection_editor/CollectionEditorWelcomeView.d.ts +15 -0
- package/dist/ui/collection_editor/CollectionPropertiesEditorForm.d.ts +20 -0
- package/dist/ui/collection_editor/CollectionYupValidation.d.ts +14 -0
- package/dist/ui/collection_editor/EntityCustomViewsSelectDialog.d.ts +4 -0
- package/dist/ui/collection_editor/EnumForm.d.ts +13 -0
- package/dist/ui/collection_editor/GetCodeDialog.d.ts +5 -0
- package/dist/ui/collection_editor/PropertyEditView.d.ts +40 -0
- package/dist/ui/collection_editor/PropertyFieldPreview.d.ts +15 -0
- package/dist/ui/collection_editor/PropertySelectItem.d.ts +8 -0
- package/dist/ui/collection_editor/PropertyTree.d.ts +32 -0
- package/dist/ui/collection_editor/SubcollectionsEditTab.d.ts +12 -0
- package/dist/ui/collection_editor/UnsavedChangesDialog.d.ts +9 -0
- package/dist/ui/collection_editor/import/CollectionEditorImportDataPreview.d.ts +7 -0
- package/dist/ui/collection_editor/import/CollectionEditorImportMapping.d.ts +7 -0
- package/dist/ui/collection_editor/import/clean_import_data.d.ts +7 -0
- package/dist/ui/collection_editor/properties/BlockPropertyField.d.ts +8 -0
- package/dist/ui/collection_editor/properties/BooleanPropertyField.d.ts +3 -0
- package/dist/ui/collection_editor/properties/CommonPropertyFields.d.ts +11 -0
- package/dist/ui/collection_editor/properties/DateTimePropertyField.d.ts +3 -0
- package/dist/ui/collection_editor/properties/EnumPropertyField.d.ts +8 -0
- package/dist/ui/collection_editor/properties/FieldHelperView.d.ts +4 -0
- package/dist/ui/collection_editor/properties/KeyValuePropertyField.d.ts +3 -0
- package/dist/ui/collection_editor/properties/MapPropertyField.d.ts +8 -0
- package/dist/ui/collection_editor/properties/NumberPropertyField.d.ts +3 -0
- package/dist/ui/collection_editor/properties/ReferencePropertyField.d.ts +13 -0
- package/dist/ui/collection_editor/properties/RepeatPropertyField.d.ts +10 -0
- package/dist/ui/collection_editor/properties/StoragePropertyField.d.ts +5 -0
- package/dist/ui/collection_editor/properties/StringPropertyField.d.ts +5 -0
- package/dist/ui/collection_editor/properties/UrlPropertyField.d.ts +4 -0
- package/dist/ui/collection_editor/properties/advanced/AdvancedPropertyValidation.d.ts +3 -0
- package/dist/ui/collection_editor/properties/validation/ArrayPropertyValidation.d.ts +5 -0
- package/dist/ui/collection_editor/properties/validation/GeneralPropertyValidation.d.ts +4 -0
- package/dist/ui/collection_editor/properties/validation/NumberPropertyValidation.d.ts +3 -0
- package/dist/ui/collection_editor/properties/validation/StringPropertyValidation.d.ts +11 -0
- package/dist/ui/collection_editor/properties/validation/ValidationPanel.d.ts +2 -0
- package/dist/ui/collection_editor/templates/blog_template.d.ts +2 -0
- package/dist/ui/collection_editor/templates/pages_template.d.ts +2 -0
- package/dist/ui/collection_editor/templates/products_template.d.ts +2 -0
- package/dist/ui/collection_editor/templates/users_template.d.ts +2 -0
- package/dist/ui/collection_editor/util.d.ts +4 -0
- package/dist/ui/collection_editor/utils/strings.d.ts +1 -0
- package/dist/ui/collection_editor/utils/supported_fields.d.ts +3 -0
- package/dist/ui/collection_editor/utils/update_property_for_widget.d.ts +2 -0
- package/dist/ui/collection_editor/utils/useTraceUpdate.d.ts +1 -0
- package/dist/useCollectionEditorController.d.ts +6 -0
- package/dist/useCollectionEditorPlugin.d.ts +46 -0
- package/dist/useCollectionsConfigController.d.ts +6 -0
- package/dist/utils/arrays.d.ts +1 -0
- package/dist/utils/entities.d.ts +3 -0
- package/package.json +24 -27
- package/src/ConfigControllerProvider.tsx +330 -0
- package/src/index.ts +35 -0
- package/src/types/collection_editor_controller.tsx +42 -0
- package/src/types/collection_inference.ts +3 -0
- package/src/types/config_controller.tsx +60 -0
- package/src/types/config_permissions.ts +20 -0
- package/src/types/persisted_collection.ts +9 -0
- package/src/ui/CollectionViewHeaderAction.tsx +43 -0
- package/src/ui/EditorCollectionAction.tsx +109 -0
- package/src/ui/HomePageEditorCollectionAction.tsx +84 -0
- package/src/ui/MissingReferenceWidget.tsx +35 -0
- package/src/ui/NewCollectionButton.tsx +16 -0
- package/src/ui/NewCollectionCard.tsx +47 -0
- package/src/ui/PropertyAddColumnComponent.tsx +42 -0
- package/src/ui/RootCollectionSuggestions.tsx +55 -0
- package/src/ui/collection_editor/CollectionDetailsForm.tsx +366 -0
- package/src/ui/collection_editor/CollectionEditorDialog.tsx +754 -0
- package/src/ui/collection_editor/CollectionEditorWelcomeView.tsx +206 -0
- package/src/ui/collection_editor/CollectionPropertiesEditorForm.tsx +481 -0
- package/src/ui/collection_editor/CollectionYupValidation.tsx +7 -0
- package/src/ui/collection_editor/EntityCustomViewsSelectDialog.tsx +37 -0
- package/src/ui/collection_editor/EnumForm.tsx +358 -0
- package/src/ui/collection_editor/GetCodeDialog.tsx +120 -0
- package/src/ui/collection_editor/PropertyEditView.tsx +558 -0
- package/src/ui/collection_editor/PropertyFieldPreview.tsx +204 -0
- package/src/ui/collection_editor/PropertySelectItem.tsx +32 -0
- package/src/ui/collection_editor/PropertyTree.tsx +233 -0
- package/src/ui/collection_editor/SubcollectionsEditTab.tsx +253 -0
- package/src/ui/collection_editor/UnsavedChangesDialog.tsx +47 -0
- package/src/ui/collection_editor/import/CollectionEditorImportDataPreview.tsx +37 -0
- package/src/ui/collection_editor/import/CollectionEditorImportMapping.tsx +272 -0
- package/src/ui/collection_editor/import/clean_import_data.ts +53 -0
- package/src/ui/collection_editor/properties/BlockPropertyField.tsx +135 -0
- package/src/ui/collection_editor/properties/BooleanPropertyField.tsx +36 -0
- package/src/ui/collection_editor/properties/CommonPropertyFields.tsx +137 -0
- package/src/ui/collection_editor/properties/DateTimePropertyField.tsx +87 -0
- package/src/ui/collection_editor/properties/EnumPropertyField.tsx +117 -0
- package/src/ui/collection_editor/properties/FieldHelperView.tsx +13 -0
- package/src/ui/collection_editor/properties/KeyValuePropertyField.tsx +20 -0
- package/src/ui/collection_editor/properties/MapPropertyField.tsx +149 -0
- package/src/ui/collection_editor/properties/NumberPropertyField.tsx +38 -0
- package/src/ui/collection_editor/properties/ReferencePropertyField.tsx +182 -0
- package/src/ui/collection_editor/properties/RepeatPropertyField.tsx +108 -0
- package/src/ui/collection_editor/properties/StoragePropertyField.tsx +194 -0
- package/src/ui/collection_editor/properties/StringPropertyField.tsx +79 -0
- package/src/ui/collection_editor/properties/UrlPropertyField.tsx +89 -0
- package/src/ui/collection_editor/properties/advanced/AdvancedPropertyValidation.tsx +36 -0
- package/src/ui/collection_editor/properties/validation/ArrayPropertyValidation.tsx +50 -0
- package/src/ui/collection_editor/properties/validation/GeneralPropertyValidation.tsx +50 -0
- package/src/ui/collection_editor/properties/validation/NumberPropertyValidation.tsx +100 -0
- package/src/ui/collection_editor/properties/validation/StringPropertyValidation.tsx +132 -0
- package/src/ui/collection_editor/properties/validation/ValidationPanel.tsx +28 -0
- package/src/ui/collection_editor/templates/blog_template.ts +115 -0
- package/src/ui/collection_editor/templates/pages_template.ts +188 -0
- package/src/ui/collection_editor/templates/products_template.ts +88 -0
- package/src/ui/collection_editor/templates/users_template.ts +42 -0
- package/src/ui/collection_editor/util.ts +21 -0
- package/src/ui/collection_editor/utils/strings.ts +8 -0
- package/src/ui/collection_editor/utils/supported_fields.tsx +29 -0
- package/src/ui/collection_editor/utils/update_property_for_widget.ts +271 -0
- package/src/ui/collection_editor/utils/useTraceUpdate.tsx +23 -0
- package/src/useCollectionEditorController.tsx +9 -0
- package/src/useCollectionEditorPlugin.tsx +137 -0
- package/src/useCollectionsConfigController.tsx +9 -0
- package/src/utils/arrays.ts +3 -0
- package/src/utils/entities.ts +38 -0
- package/src/vite-env.d.ts +1 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import React, { useCallback, useState } from "react";
|
|
2
|
+
import { AddIcon, Button, Paper, Typography } from "@firecms/ui";
|
|
3
|
+
import { getIn, useFormikContext } from "formik";
|
|
4
|
+
import { PropertyFormDialog } from "../PropertyEditView";
|
|
5
|
+
import { getFullId, idToPropertiesPath, namespaceToPropertiesOrderPath } from "../util";
|
|
6
|
+
import { PropertyTree } from "../PropertyTree";
|
|
7
|
+
import { ArrayProperty, Property, PropertyConfig } from "@firecms/core";
|
|
8
|
+
|
|
9
|
+
export function BlockPropertyField({ disabled, getData, allowDataInference, propertyConfigs, collectionEditable }: {
|
|
10
|
+
disabled: boolean;
|
|
11
|
+
getData?: () => Promise<object[]>;
|
|
12
|
+
allowDataInference: boolean;
|
|
13
|
+
propertyConfigs: Record<string, PropertyConfig>,
|
|
14
|
+
collectionEditable: boolean;
|
|
15
|
+
}) {
|
|
16
|
+
|
|
17
|
+
const {
|
|
18
|
+
values,
|
|
19
|
+
setFieldValue
|
|
20
|
+
} = useFormikContext<ArrayProperty>();
|
|
21
|
+
|
|
22
|
+
const [propertyDialogOpen, setPropertyDialogOpen] = useState<boolean>(false);
|
|
23
|
+
const [selectedPropertyKey, setSelectedPropertyKey] = useState<string | undefined>();
|
|
24
|
+
const [selectedPropertyNamespace, setSelectedPropertyNamespace] = useState<string | undefined>();
|
|
25
|
+
|
|
26
|
+
const onPropertyCreated = useCallback(({
|
|
27
|
+
id,
|
|
28
|
+
property
|
|
29
|
+
}: { id?: string, property: Property }) => {
|
|
30
|
+
if (!id)
|
|
31
|
+
throw Error();
|
|
32
|
+
setFieldValue("oneOf.properties", {
|
|
33
|
+
...(values.oneOf?.properties ?? {}),
|
|
34
|
+
[id]: property
|
|
35
|
+
}, false);
|
|
36
|
+
setFieldValue("oneOf.propertiesOrder", [...(values.oneOf?.propertiesOrder ?? Object.keys(values.oneOf?.properties ?? {})), id], false);
|
|
37
|
+
setPropertyDialogOpen(false);
|
|
38
|
+
}, [values.oneOf?.properties, values.oneOf?.propertiesOrder]);
|
|
39
|
+
|
|
40
|
+
const selectedPropertyFullId = selectedPropertyKey ? getFullId(selectedPropertyKey, selectedPropertyNamespace) : undefined;
|
|
41
|
+
const selectedProperty = selectedPropertyFullId ? getIn(values.oneOf?.properties, selectedPropertyFullId.replaceAll(".", ".properties.")) : undefined;
|
|
42
|
+
|
|
43
|
+
const deleteProperty = useCallback((propertyKey?: string, namespace?: string) => {
|
|
44
|
+
const fullId = propertyKey ? getFullId(propertyKey, namespace) : undefined;
|
|
45
|
+
if (!fullId)
|
|
46
|
+
throw Error("collection editor miss config");
|
|
47
|
+
|
|
48
|
+
setFieldValue(`oneOf.${idToPropertiesPath(fullId)}`, undefined, false);
|
|
49
|
+
const propertiesOrderPath = `oneOf.${namespaceToPropertiesOrderPath(namespace)}`;
|
|
50
|
+
const currentPropertiesOrder: string[] = getIn(values, propertiesOrderPath);
|
|
51
|
+
setFieldValue(propertiesOrderPath, currentPropertiesOrder.filter((p) => p !== propertyKey), false);
|
|
52
|
+
|
|
53
|
+
setPropertyDialogOpen(false);
|
|
54
|
+
setSelectedPropertyKey(undefined);
|
|
55
|
+
setSelectedPropertyNamespace(undefined);
|
|
56
|
+
}, [setFieldValue, values]);
|
|
57
|
+
|
|
58
|
+
const addChildButton = <Button
|
|
59
|
+
autoFocus
|
|
60
|
+
color="primary"
|
|
61
|
+
|
|
62
|
+
onClick={() => setPropertyDialogOpen(true)}
|
|
63
|
+
startIcon={<AddIcon/>}
|
|
64
|
+
>
|
|
65
|
+
Add property to {values.name ?? "this block"}
|
|
66
|
+
</Button>;
|
|
67
|
+
|
|
68
|
+
const onPropertyMove = useCallback((propertiesOrder: string[], namespace?: string) => {
|
|
69
|
+
setFieldValue(`oneOf.${namespaceToPropertiesOrderPath(namespace)}`, propertiesOrder, false);
|
|
70
|
+
}, []);
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<>
|
|
74
|
+
<div className={"col-span-12"}>
|
|
75
|
+
<div className={"flex justify-between items-end mt-8 mb-4"}>
|
|
76
|
+
<Typography variant={"subtitle2"}>Properties in this
|
|
77
|
+
block</Typography>
|
|
78
|
+
{addChildButton}
|
|
79
|
+
</div>
|
|
80
|
+
<Paper className="p-2 pl-8">
|
|
81
|
+
|
|
82
|
+
<PropertyTree
|
|
83
|
+
properties={values.oneOf?.properties ?? {}}
|
|
84
|
+
propertiesOrder={values.oneOf?.propertiesOrder}
|
|
85
|
+
errors={{}}
|
|
86
|
+
collectionEditable={collectionEditable}
|
|
87
|
+
onPropertyClick={disabled
|
|
88
|
+
? undefined
|
|
89
|
+
: (propertyKey, namespace) => {
|
|
90
|
+
setSelectedPropertyKey(propertyKey);
|
|
91
|
+
setSelectedPropertyNamespace(namespace);
|
|
92
|
+
setPropertyDialogOpen(true);
|
|
93
|
+
}}
|
|
94
|
+
onPropertyMove={disabled
|
|
95
|
+
? undefined
|
|
96
|
+
: onPropertyMove}/>
|
|
97
|
+
|
|
98
|
+
{!disabled && !values.oneOf?.propertiesOrder?.length &&
|
|
99
|
+
<div className="h-full flex items-center justify-center p-4">
|
|
100
|
+
Add the first property to this block
|
|
101
|
+
</div>}
|
|
102
|
+
|
|
103
|
+
</Paper>
|
|
104
|
+
</div>
|
|
105
|
+
|
|
106
|
+
{!disabled && <PropertyFormDialog
|
|
107
|
+
inArray={false}
|
|
108
|
+
forceShowErrors={false}
|
|
109
|
+
open={propertyDialogOpen}
|
|
110
|
+
getData={getData}
|
|
111
|
+
allowDataInference={allowDataInference}
|
|
112
|
+
onCancel={() => {
|
|
113
|
+
setPropertyDialogOpen(false);
|
|
114
|
+
setSelectedPropertyKey(undefined);
|
|
115
|
+
setSelectedPropertyNamespace(undefined);
|
|
116
|
+
}}
|
|
117
|
+
onOkClicked={() => {
|
|
118
|
+
setPropertyDialogOpen(false);
|
|
119
|
+
setSelectedPropertyKey(undefined);
|
|
120
|
+
setSelectedPropertyNamespace(undefined);
|
|
121
|
+
}}
|
|
122
|
+
collectionEditable={collectionEditable}
|
|
123
|
+
onDelete={deleteProperty}
|
|
124
|
+
propertyKey={selectedPropertyKey}
|
|
125
|
+
propertyNamespace={selectedPropertyNamespace}
|
|
126
|
+
property={selectedProperty}
|
|
127
|
+
existingProperty={Boolean(selectedPropertyKey)}
|
|
128
|
+
autoUpdateId={!selectedPropertyKey}
|
|
129
|
+
autoOpenTypeSelect={!selectedPropertyKey}
|
|
130
|
+
onPropertyChanged={onPropertyCreated}
|
|
131
|
+
existingPropertyKeys={selectedPropertyKey ? undefined : values.oneOf?.propertiesOrder}
|
|
132
|
+
propertyConfigs={propertyConfigs}/>}
|
|
133
|
+
|
|
134
|
+
</>);
|
|
135
|
+
}
|
|
@@ -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,137 @@
|
|
|
1
|
+
import { Field, getIn, useFormikContext } from "formik";
|
|
2
|
+
import { DebouncedTextField } from "@firecms/ui";
|
|
3
|
+
import { PropertyWithId } from "../PropertyEditView";
|
|
4
|
+
import React from "react";
|
|
5
|
+
import { FieldHelperView } from "./FieldHelperView";
|
|
6
|
+
import { toSnakeCase, unslugify } from "@firecms/core";
|
|
7
|
+
|
|
8
|
+
type CommonPropertyFieldsProps = {
|
|
9
|
+
showErrors: boolean,
|
|
10
|
+
disabledId: boolean,
|
|
11
|
+
existingPropertyKeys?: string[];
|
|
12
|
+
disabled: boolean;
|
|
13
|
+
isNewProperty: boolean;
|
|
14
|
+
autoUpdateId: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const CommonPropertyFields = React.forwardRef<HTMLDivElement, CommonPropertyFieldsProps>(
|
|
18
|
+
function CommonPropertyFields({
|
|
19
|
+
showErrors,
|
|
20
|
+
disabledId,
|
|
21
|
+
existingPropertyKeys,
|
|
22
|
+
disabled,
|
|
23
|
+
autoUpdateId,
|
|
24
|
+
isNewProperty
|
|
25
|
+
}, ref) {
|
|
26
|
+
|
|
27
|
+
const {
|
|
28
|
+
errors,
|
|
29
|
+
values,
|
|
30
|
+
setFieldValue,
|
|
31
|
+
setFieldTouched,
|
|
32
|
+
touched
|
|
33
|
+
} = useFormikContext<PropertyWithId>();
|
|
34
|
+
|
|
35
|
+
const name = "name";
|
|
36
|
+
const nameError = showErrors && getIn(errors, name);
|
|
37
|
+
|
|
38
|
+
const id = "id";
|
|
39
|
+
const idError = showErrors && getIn(errors, id);
|
|
40
|
+
|
|
41
|
+
const description = "description";
|
|
42
|
+
const descriptionError = showErrors && getIn(errors, description);
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<div className={"flex flex-col gap-2 col-span-12"}>
|
|
46
|
+
|
|
47
|
+
<div>
|
|
48
|
+
<Field
|
|
49
|
+
inputRef={ref}
|
|
50
|
+
as={DebouncedTextField}
|
|
51
|
+
value={values[name]}
|
|
52
|
+
onChange={(e: any) => {
|
|
53
|
+
const newNameValue = e.target.value;
|
|
54
|
+
setFieldValue(name, newNameValue);
|
|
55
|
+
setFieldTouched(name, true, true);
|
|
56
|
+
const idTouched = getIn(touched, id);
|
|
57
|
+
if (!idTouched && autoUpdateId) {
|
|
58
|
+
setFieldValue(id, newNameValue ? toSnakeCase(newNameValue) : "")
|
|
59
|
+
}
|
|
60
|
+
}}
|
|
61
|
+
style={{ fontSize: 20 }}
|
|
62
|
+
validate={validateName}
|
|
63
|
+
placeholder={"Field name"}
|
|
64
|
+
required
|
|
65
|
+
disabled={disabled}
|
|
66
|
+
error={Boolean(nameError)}/>
|
|
67
|
+
|
|
68
|
+
<FieldHelperView error={Boolean(nameError)}>
|
|
69
|
+
{nameError}
|
|
70
|
+
</FieldHelperView>
|
|
71
|
+
</div>
|
|
72
|
+
|
|
73
|
+
<div>
|
|
74
|
+
<Field
|
|
75
|
+
as={DebouncedTextField}
|
|
76
|
+
label={"ID"}
|
|
77
|
+
value={values[id]}
|
|
78
|
+
onChange={(e: any) => {
|
|
79
|
+
const newIdValue = e.target.value;
|
|
80
|
+
setFieldValue(id, newIdValue);
|
|
81
|
+
setFieldTouched(id, true, true);
|
|
82
|
+
const nameTouched = getIn(touched, name);
|
|
83
|
+
if (!nameTouched && autoUpdateId) {
|
|
84
|
+
setFieldValue(name, newIdValue ? unslugify(newIdValue) : "")
|
|
85
|
+
}
|
|
86
|
+
}}
|
|
87
|
+
validate={() => validateId(values[id], existingPropertyKeys)}
|
|
88
|
+
disabled={disabledId || disabled}
|
|
89
|
+
required
|
|
90
|
+
size="small"
|
|
91
|
+
error={Boolean(idError)}/>
|
|
92
|
+
<FieldHelperView error={Boolean(idError)}>
|
|
93
|
+
{idError}
|
|
94
|
+
</FieldHelperView>
|
|
95
|
+
</div>
|
|
96
|
+
|
|
97
|
+
<div>
|
|
98
|
+
<Field name={description}
|
|
99
|
+
as={DebouncedTextField}
|
|
100
|
+
label={"Description"}
|
|
101
|
+
disabled={disabled}
|
|
102
|
+
error={Boolean(descriptionError)}/>
|
|
103
|
+
<FieldHelperView error={Boolean(descriptionError)}>
|
|
104
|
+
{descriptionError}
|
|
105
|
+
</FieldHelperView>
|
|
106
|
+
</div>
|
|
107
|
+
|
|
108
|
+
</div>
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
const idRegEx = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
115
|
+
|
|
116
|
+
function validateId(value?: string, existingPropertyKeys?: string[]) {
|
|
117
|
+
|
|
118
|
+
let error;
|
|
119
|
+
if (!value) {
|
|
120
|
+
error = "You must specify an id for the field";
|
|
121
|
+
}
|
|
122
|
+
if (value && !value.match(idRegEx)) {
|
|
123
|
+
error = "The id can only contain letters, numbers and underscores (_), and not start with a number";
|
|
124
|
+
}
|
|
125
|
+
if (value && existingPropertyKeys && existingPropertyKeys.includes(value)) {
|
|
126
|
+
error = "There is another field with this ID already";
|
|
127
|
+
}
|
|
128
|
+
return error;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function validateName(value: string) {
|
|
132
|
+
let error;
|
|
133
|
+
if (!value) {
|
|
134
|
+
error = "You must specify a title for the field";
|
|
135
|
+
}
|
|
136
|
+
return error;
|
|
137
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { getIn, useFormikContext } from "formik";
|
|
3
|
+
import { NumberProperty, StringProperty } from "@firecms/core";
|
|
4
|
+
import { Select, SelectItem } from "@firecms/ui";
|
|
5
|
+
import { GeneralPropertyValidation } from "./validation/GeneralPropertyValidation";
|
|
6
|
+
import { ValidationPanel } from "./validation/ValidationPanel";
|
|
7
|
+
import { FieldHelperView } from "./FieldHelperView";
|
|
8
|
+
|
|
9
|
+
export function DateTimePropertyField({ disabled }: {
|
|
10
|
+
disabled: boolean;
|
|
11
|
+
}) {
|
|
12
|
+
|
|
13
|
+
const {
|
|
14
|
+
values,
|
|
15
|
+
errors,
|
|
16
|
+
touched,
|
|
17
|
+
setFieldValue
|
|
18
|
+
} = useFormikContext<StringProperty | NumberProperty>();
|
|
19
|
+
|
|
20
|
+
const modePath = "mode";
|
|
21
|
+
const modeValue: string | undefined = getIn(values, modePath);
|
|
22
|
+
const modeError: string | undefined = getIn(touched, modePath) && getIn(errors, modePath);
|
|
23
|
+
|
|
24
|
+
const autoValuePath = "autoValue";
|
|
25
|
+
const autoValueValue: string | undefined = getIn(values, autoValuePath);
|
|
26
|
+
const autoValueError: string | undefined = getIn(touched, autoValuePath) && getIn(errors, autoValuePath);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<>
|
|
30
|
+
<div className={"flex flex-col col-span-12"}>
|
|
31
|
+
<Select name={modePath}
|
|
32
|
+
value={modeValue ?? "date"}
|
|
33
|
+
error={Boolean(modeError)}
|
|
34
|
+
onValueChange={(v) => setFieldValue(modePath, v)}
|
|
35
|
+
label={"Mode"}
|
|
36
|
+
renderValue={(v) => {
|
|
37
|
+
switch (v) {
|
|
38
|
+
case "date_time":
|
|
39
|
+
return "Date/Time";
|
|
40
|
+
case "date":
|
|
41
|
+
return "Date";
|
|
42
|
+
default:
|
|
43
|
+
return "";
|
|
44
|
+
}
|
|
45
|
+
}}
|
|
46
|
+
disabled={disabled}>
|
|
47
|
+
<SelectItem value={"date_time"}> Date/Time </SelectItem>
|
|
48
|
+
<SelectItem value={"date"}> Date </SelectItem>
|
|
49
|
+
</Select>
|
|
50
|
+
<FieldHelperView error={Boolean(modeError)}>
|
|
51
|
+
{modeError}
|
|
52
|
+
</FieldHelperView>
|
|
53
|
+
|
|
54
|
+
<Select name={autoValuePath}
|
|
55
|
+
disabled={disabled}
|
|
56
|
+
value={autoValueValue ?? ""}
|
|
57
|
+
onValueChange={(v) => setFieldValue(autoValuePath, v)}
|
|
58
|
+
renderValue={(v) => {
|
|
59
|
+
switch (v) {
|
|
60
|
+
case "on_create":
|
|
61
|
+
return "On create";
|
|
62
|
+
case "on_update":
|
|
63
|
+
return "On any update";
|
|
64
|
+
default:
|
|
65
|
+
return "None";
|
|
66
|
+
}
|
|
67
|
+
}}
|
|
68
|
+
error={Boolean(autoValueError)}
|
|
69
|
+
label={"Automatic value"}>
|
|
70
|
+
<SelectItem value={""}> None </SelectItem>
|
|
71
|
+
<SelectItem value={"on_create"}> On create </SelectItem>
|
|
72
|
+
<SelectItem value={"on_update"}> On any update </SelectItem>
|
|
73
|
+
</Select>
|
|
74
|
+
<FieldHelperView error={Boolean(autoValueError)}>
|
|
75
|
+
{autoValueError ?? "Update this field automatically when creating or updating the entity"}
|
|
76
|
+
</FieldHelperView>
|
|
77
|
+
|
|
78
|
+
</div>
|
|
79
|
+
|
|
80
|
+
<div className={"col-span-12"}>
|
|
81
|
+
<ValidationPanel>
|
|
82
|
+
<GeneralPropertyValidation disabled={disabled}/>
|
|
83
|
+
</ValidationPanel>
|
|
84
|
+
</div>
|
|
85
|
+
</>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import React, { useMemo } from "react";
|
|
2
|
+
import { getIn, useFormikContext } from "formik";
|
|
3
|
+
import { EnumValueConfig, resolveEnumValues, useSnackbarController } from "@firecms/core";
|
|
4
|
+
import { Select, SelectItem } from "@firecms/ui";
|
|
5
|
+
import { EnumForm } from "../EnumForm";
|
|
6
|
+
import { StringPropertyValidation } from "./validation/StringPropertyValidation";
|
|
7
|
+
import { ArrayPropertyValidation } from "./validation/ArrayPropertyValidation";
|
|
8
|
+
import { ValidationPanel } from "./validation/ValidationPanel";
|
|
9
|
+
import { PropertyWithId } from "../PropertyEditView";
|
|
10
|
+
|
|
11
|
+
export function EnumPropertyField({
|
|
12
|
+
multiselect,
|
|
13
|
+
updateIds,
|
|
14
|
+
disabled,
|
|
15
|
+
showErrors,
|
|
16
|
+
allowDataInference,
|
|
17
|
+
getData
|
|
18
|
+
}: {
|
|
19
|
+
multiselect: boolean;
|
|
20
|
+
updateIds: boolean;
|
|
21
|
+
disabled: boolean;
|
|
22
|
+
showErrors: boolean;
|
|
23
|
+
allowDataInference?: boolean;
|
|
24
|
+
getData?: () => Promise<object[]>;
|
|
25
|
+
}) {
|
|
26
|
+
|
|
27
|
+
const {
|
|
28
|
+
values,
|
|
29
|
+
handleChange,
|
|
30
|
+
errors,
|
|
31
|
+
touched,
|
|
32
|
+
setFieldError,
|
|
33
|
+
setFieldValue
|
|
34
|
+
} = useFormikContext<PropertyWithId>();
|
|
35
|
+
|
|
36
|
+
const snackbarContext = useSnackbarController();
|
|
37
|
+
|
|
38
|
+
const enumValuesPath = multiselect ? "of.enumValues" : "enumValues";
|
|
39
|
+
|
|
40
|
+
const defaultValue = getIn(values, "defaultValue");
|
|
41
|
+
|
|
42
|
+
const valuesEnumValues = getIn(values, enumValuesPath);
|
|
43
|
+
const enumValues: EnumValueConfig[] = useMemo(() => {
|
|
44
|
+
if (!valuesEnumValues || typeof valuesEnumValues === "boolean")
|
|
45
|
+
return [] as EnumValueConfig[];
|
|
46
|
+
return resolveEnumValues(valuesEnumValues) ?? [] as EnumValueConfig[];
|
|
47
|
+
}, [valuesEnumValues]);
|
|
48
|
+
|
|
49
|
+
const onValuesChanged = (value: EnumValueConfig[]) => {
|
|
50
|
+
if (!values)
|
|
51
|
+
return;
|
|
52
|
+
setFieldValue(enumValuesPath, value);
|
|
53
|
+
if (!multiselect) {
|
|
54
|
+
const enumIds = value.filter(v => Boolean(v?.id)).map((v: any) => v.id);
|
|
55
|
+
if (defaultValue && !enumIds.includes(defaultValue)) {
|
|
56
|
+
setFieldValue("defaultValue", undefined);
|
|
57
|
+
snackbarContext.open({
|
|
58
|
+
type: "warning",
|
|
59
|
+
message: "Default value was cleared"
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<>
|
|
67
|
+
<div className={"col-span-12"}>
|
|
68
|
+
<EnumForm enumValues={enumValues}
|
|
69
|
+
updateIds={updateIds}
|
|
70
|
+
disabled={disabled}
|
|
71
|
+
allowDataInference={allowDataInference}
|
|
72
|
+
onError={(hasError) => {
|
|
73
|
+
setFieldError(enumValuesPath, hasError ? "" : undefined);
|
|
74
|
+
}}
|
|
75
|
+
getData={getData
|
|
76
|
+
? () => getData()
|
|
77
|
+
.then(res => res.map(d => values.id && getIn(d, values.id)).filter(Boolean))
|
|
78
|
+
: undefined}
|
|
79
|
+
onValuesChanged={onValuesChanged}/>
|
|
80
|
+
</div>
|
|
81
|
+
|
|
82
|
+
<div className={"col-span-12"}>
|
|
83
|
+
|
|
84
|
+
<ValidationPanel>
|
|
85
|
+
{!multiselect &&
|
|
86
|
+
<StringPropertyValidation disabled={disabled}
|
|
87
|
+
showErrors={showErrors}/>}
|
|
88
|
+
{multiselect &&
|
|
89
|
+
<ArrayPropertyValidation disabled={disabled}/>}
|
|
90
|
+
</ValidationPanel>
|
|
91
|
+
|
|
92
|
+
</div>
|
|
93
|
+
|
|
94
|
+
{!multiselect && <div className={"col-span-12"}>
|
|
95
|
+
|
|
96
|
+
<Select
|
|
97
|
+
disabled={disabled}
|
|
98
|
+
position={"item-aligned"}
|
|
99
|
+
onValueChange={(value: string) => {
|
|
100
|
+
setFieldValue("defaultValue", value);
|
|
101
|
+
}}
|
|
102
|
+
label={"Default value"}
|
|
103
|
+
value={defaultValue ?? ""}>
|
|
104
|
+
{enumValues
|
|
105
|
+
.filter((enumValue) => Boolean(enumValue?.id))
|
|
106
|
+
.map((enumValue) => (
|
|
107
|
+
<SelectItem key={enumValue.id}
|
|
108
|
+
value={enumValue.id?.toString()}>
|
|
109
|
+
{enumValue.label}
|
|
110
|
+
</SelectItem>
|
|
111
|
+
))}
|
|
112
|
+
</Select>
|
|
113
|
+
|
|
114
|
+
</div>}
|
|
115
|
+
</>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Typography } from "@firecms/ui";
|
|
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
|
+
}
|