@firecms/core 3.0.0-alpha.57 → 3.0.0-alpha.59

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 (34) hide show
  1. package/dist/components/EntityCollectionTable/internal/default_entity_actions.d.ts +1 -0
  2. package/dist/{form/components → components}/LabelWithIcon.d.ts +1 -2
  3. package/dist/components/ReferenceWidget.d.ts +27 -0
  4. package/dist/components/index.d.ts +2 -0
  5. package/dist/form/components/index.d.ts +0 -1
  6. package/dist/index.es.js +882 -789
  7. package/dist/index.es.js.map +1 -1
  8. package/dist/index.umd.js +5 -5
  9. package/dist/index.umd.js.map +1 -1
  10. package/dist/types/firecms_context.d.ts +9 -9
  11. package/package.json +3 -3
  12. package/src/components/EntityCollectionTable/internal/default_entity_actions.tsx +17 -2
  13. package/src/{form/components → components}/LabelWithIcon.tsx +1 -2
  14. package/src/components/ReferenceWidget.tsx +143 -0
  15. package/src/components/index.tsx +2 -0
  16. package/src/form/components/index.tsx +0 -1
  17. package/src/form/field_bindings/ArrayCustomShapedFieldBinding.tsx +2 -1
  18. package/src/form/field_bindings/ArrayOfReferencesFieldBinding.tsx +2 -1
  19. package/src/form/field_bindings/BlockFieldBinding.tsx +2 -1
  20. package/src/form/field_bindings/DateTimeFieldBinding.tsx +2 -1
  21. package/src/form/field_bindings/KeyValueFieldBinding.tsx +3 -1
  22. package/src/form/field_bindings/MapFieldBinding.tsx +2 -1
  23. package/src/form/field_bindings/MarkdownFieldBinding.tsx +2 -1
  24. package/src/form/field_bindings/MultiSelectBinding.tsx +2 -1
  25. package/src/form/field_bindings/ReadOnlyFieldBinding.tsx +2 -1
  26. package/src/form/field_bindings/ReferenceFieldBinding.tsx +2 -1
  27. package/src/form/field_bindings/RepeatFieldBinding.tsx +2 -1
  28. package/src/form/field_bindings/SelectFieldBinding.tsx +2 -1
  29. package/src/form/field_bindings/StorageUploadFieldBinding.tsx +2 -1
  30. package/src/form/field_bindings/SwitchFieldBinding.tsx +1 -1
  31. package/src/form/field_bindings/TextFieldBinding.tsx +2 -1
  32. package/src/types/firecms_context.tsx +10 -11
  33. package/dist/form/components/ReferenceWidget.d.ts +0 -24
  34. package/src/form/components/ReferenceWidget.tsx +0 -255
@@ -1,255 +0,0 @@
1
- import React, { useCallback, useMemo } from "react";
2
-
3
- import { Entity, EntityCollection, EntityReference, FilterValues, ResolvedProperty } from "../../types";
4
- import { getReferenceFrom, getReferencePreviewKeys } from "../../util";
5
- import { PropertyPreview, SkeletonPropertyComponent } from "../../preview";
6
- import { LabelWithIcon } from "../components";
7
- import {
8
- useEntityFetch,
9
- useFireCMSContext,
10
- useNavigationController,
11
- useReferenceDialog,
12
- useSideEntityController
13
- } from "../../hooks";
14
- import {
15
- Button,
16
- ClearIcon,
17
- cn,
18
- ErrorIcon,
19
- IconButton,
20
- KeyboardTabIcon,
21
- LinkIcon,
22
- Tooltip,
23
- Typography
24
- } from "@firecms/ui";
25
- import { ErrorBoundary, ErrorView } from "../../components";
26
-
27
- /**
28
- * This field allows selecting reference/s.
29
- * @param name
30
- * @param path
31
- * @param disabled
32
- * @param value
33
- * @param setValue
34
- * @param previewProperties
35
- * @param forceFilter
36
- * @constructor
37
- */
38
- export function ReferenceWidget<M extends Record<string, any>>({
39
- name,
40
- path,
41
- disabled,
42
- value,
43
- setValue,
44
- previewProperties,
45
- forceFilter
46
- }: {
47
- name?: string,
48
- value?: EntityReference,
49
- setValue: (value: EntityReference | null) => void,
50
- path: string,
51
- disabled?: boolean,
52
- previewProperties?: string[];
53
- /**
54
- * Allow selection of entities that pass the given filter only.
55
- */
56
- forceFilter?: FilterValues<string>;
57
- }) {
58
-
59
- const fireCMSContext = useFireCMSContext();
60
- const navigationController = useNavigationController();
61
- const sideEntityController = useSideEntityController();
62
-
63
- const collection: EntityCollection | undefined = useMemo(() => {
64
- return navigationController.getCollection(path);
65
- }, [path, navigationController]);
66
-
67
- if (!collection) {
68
- throw Error(`Couldn't find the corresponding collection for the path: ${path}`);
69
- }
70
-
71
- const validValue = value && value instanceof EntityReference;
72
-
73
- const {
74
- entity,
75
- dataLoading,
76
- dataLoadingError
77
- } = useEntityFetch({
78
- path,
79
- entityId: validValue ? value.id : undefined,
80
- collection,
81
- useCache: true
82
- });
83
-
84
- const onSingleEntitySelected = useCallback((entity: Entity<M>) => {
85
- if (disabled)
86
- return;
87
- setValue(entity ? getReferenceFrom(entity) : null);
88
- }, [disabled, setValue]);
89
-
90
- const referenceDialogController = useReferenceDialog({
91
- multiselect: false,
92
- path,
93
- collection,
94
- onSingleEntitySelected,
95
- forceFilter
96
- }
97
- );
98
-
99
- const handleClickOpen = useCallback(() => {
100
- referenceDialogController.open();
101
- }, [referenceDialogController]);
102
-
103
- const clearValue = useCallback((e: React.MouseEvent) => {
104
- e.stopPropagation();
105
- setValue(null);
106
- }, [setValue]);
107
-
108
- const seeEntityDetails = useCallback((e: React.MouseEvent) => {
109
- e.stopPropagation();
110
- if (entity) {
111
- fireCMSContext.onAnalyticsEvent?.("entity_click_from_reference", {
112
- path: entity.path,
113
- entityId: entity.id
114
- });
115
- sideEntityController.open({
116
- entityId: entity.id,
117
- path,
118
- updateUrl: true
119
- });
120
- }
121
- }, [entity, path, sideEntityController]);
122
-
123
- const buildEntityView = (collection?: EntityCollection<any>) => {
124
-
125
- const missingEntity = entity && !entity.values;
126
-
127
- let body: React.ReactNode;
128
- if (!collection) {
129
- body = (
130
- <ErrorView
131
- error={"The specified collection does not exist. Check console"}/>
132
- );
133
- } else if (missingEntity) {
134
- body = (
135
- <Tooltip title={value && value.path}>
136
- <div className="flex items-center p-4 flex-grow">
137
- <ErrorIcon size={"small"} color={"error"}/>
138
- <div className="ml-4">Missing
139
- reference {entity && entity.id}</div>
140
- </div>
141
- </Tooltip>
142
- );
143
- } else {
144
- if (validValue) {
145
-
146
- const listProperties = getReferencePreviewKeys(collection, fireCMSContext.propertyConfigs, previewProperties, 3);
147
-
148
- body = (
149
- <div className="flex flex-col flex-grow ml-4 mr-4">
150
-
151
- {listProperties && listProperties.map((key) => {
152
- const property = collection.properties[key as string];
153
- if (!property) return null;
154
- return (
155
- <div
156
- key={`reference_previews_${key as string}`}
157
- className="mt-1 mb-1">
158
- <ErrorBoundary>{
159
- entity
160
- ? <PropertyPreview
161
- propertyKey={key as string}
162
- value={(entity.values)[key]}
163
- property={property as ResolvedProperty}
164
- entity={entity}
165
- size={"tiny"}/>
166
- : <SkeletonPropertyComponent
167
- property={property as ResolvedProperty}
168
- size={"tiny"}/>}
169
- </ErrorBoundary>
170
- </div>
171
- );
172
- })}
173
- </div>
174
- );
175
- } else {
176
- body = <div className="p-4 flex justify-center"
177
- onClick={disabled ? undefined : handleClickOpen}>
178
- <Typography variant={"label"}
179
- className="flex-grow text-center">No value
180
- set</Typography>
181
- {!disabled && <Button variant="outlined"
182
- color="primary">
183
- Set
184
- </Button>}
185
- </div>;
186
- }
187
- }
188
-
189
- return (
190
- <div
191
- onClick={disabled ? undefined : handleClickOpen}
192
- className="flex">
193
-
194
- <div className="flex flex-col flex-grow">
195
-
196
- <div className="flex flex-col flex-grow">
197
- <LabelWithIcon icon={<LinkIcon color={"inherit"}
198
- />}
199
- className="text-text-secondary dark:text-text-secondary-dark ml-3.5"
200
- title={name}
201
- />
202
-
203
- {entity &&
204
- <div className="self-center m-4">
205
- <Tooltip title={value && value.path}>
206
- <Typography variant={"caption"}
207
- className={"font-mono"}>
208
- {entity.id}
209
- </Typography>
210
- </Tooltip>
211
- </div>}
212
-
213
- {!missingEntity && entity && value &&
214
- <Tooltip title={`See details for ${entity.id}`}>
215
- <span>
216
- <IconButton
217
- onClick={seeEntityDetails}
218
- size="large">
219
- <KeyboardTabIcon/>
220
- </IconButton>
221
- </span>
222
- </Tooltip>}
223
-
224
- {value && <div>
225
- <Tooltip title=" Clear">
226
- <span>
227
- <IconButton
228
- disabled={disabled}
229
- onClick={disabled ? undefined : clearValue}
230
- size="large">
231
- <ClearIcon/>
232
- </IconButton>
233
- </span>
234
- </Tooltip>
235
- </div>}
236
-
237
- </div>
238
-
239
- {body}
240
-
241
- </div>
242
- </div>
243
- );
244
- };
245
-
246
- return <Typography variant={"label"}
247
- className={cn("relative w-full transition-colors duration-200 ease-in border rounded font-medium",
248
- disabled ? "bg-opacity-50" : "hover:bg-opacity-75",
249
- "text-opacity-50 dark:text-white dark:text-opacity-50")}
250
- >
251
-
252
- {collection && buildEntityView(collection)}
253
-
254
- </Typography>;
255
- }