-
Notifications
You must be signed in to change notification settings - Fork 114
/
MultipleReferenceEditor.tsx
137 lines (124 loc) · 4.19 KB
/
MultipleReferenceEditor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as React from 'react';
import arrayMove from 'array-move';
import { ReferenceValue, EntityType, ContentType } from '../types';
import { ReferenceEditor, ReferenceEditorProps } from './ReferenceEditor';
import { LinkEntityActions } from '../components';
import { SortEndHandler, SortStartHandler } from 'react-sortable-hoc';
import { useLinkActionsProps } from '../components/LinkActions/LinkEntityActions';
import { useCallback } from 'react';
import { CustomEntityCardProps } from './customCardTypes';
import { useEditorPermissions } from './useEditorPermissions';
type ChildProps = {
entityType: EntityType;
items: ReferenceValue[];
isDisabled: boolean;
setValue: (value: ReferenceValue[]) => void;
allContentTypes: ContentType[];
onSortStart: SortStartHandler;
onSortEnd: SortEndHandler;
onMove: (oldIndex: number, newIndex: number) => void;
};
type EditorProps = ReferenceEditorProps &
Omit<ChildProps, 'onSortStart' | 'onSortEnd' | 'onMove'> & {
children: (props: ReferenceEditorProps & ChildProps) => React.ReactElement;
};
function onLinkOrCreate(
setValue: ChildProps['setValue'],
entityType: ChildProps['entityType'],
items: ChildProps['items'],
ids: string[],
index?: number
): void {
const links: ReferenceValue[] = ids.map((id) => ({
sys: { type: 'Link', linkType: entityType, id },
}));
const newItems = Array.from(items);
newItems.splice(index !== undefined ? index : items.length, 0, ...links);
setValue(newItems);
}
function Editor(props: EditorProps) {
const { items, setValue, entityType } = props;
const editorPermissions = useEditorPermissions(props);
const onSortStart: SortStartHandler = useCallback((_, event) => event.preventDefault(), []);
const onSortEnd: SortEndHandler = useCallback(
({ oldIndex, newIndex }) => {
const newItems = arrayMove(items, oldIndex, newIndex);
setValue(newItems);
},
[items, setValue]
);
const onMove = useCallback(
(oldIndex, newIndex) => {
const newItems = arrayMove(items, oldIndex, newIndex);
setValue(newItems);
},
[items, setValue]
);
const onCreate = useCallback(
(id: string, index?: number) => onLinkOrCreate(setValue, entityType, items, [id], index),
[setValue, items, entityType]
);
const onLink = useCallback(
(ids: string[], index?: number) => onLinkOrCreate(setValue, entityType, items, ids, index),
[setValue, items, entityType]
);
const linkActionsProps = useLinkActionsProps({
...props,
canLinkMultiple: true,
editorPermissions,
onCreate,
onLink,
});
const customCardRenderer = useCallback(
(cardProps: CustomEntityCardProps, _, renderDefaultCard) =>
props.renderCustomCard
? props.renderCustomCard(cardProps, linkActionsProps, renderDefaultCard)
: false,
[linkActionsProps]
);
return (
<>
{props.children({
...props,
onSortStart: onSortStart,
onSortEnd: onSortEnd,
onMove,
renderCustomCard: props.renderCustomCard && customCardRenderer,
})}
<LinkEntityActions renderCustomActions={props.renderCustomActions} {...linkActionsProps} />
</>
);
}
export function MultipleReferenceEditor(
props: ReferenceEditorProps & {
entityType: EntityType;
children: (props: ReferenceEditorProps & ChildProps) => React.ReactElement;
}
) {
const allContentTypes = props.sdk.space.getCachedContentTypes();
return (
<ReferenceEditor<ReferenceValue[]> {...props}>
{({ value, disabled, setValue, externalReset }) => {
const items = (value || [])
// If null values have found their way into the persisted
// value for the multiref field, replace them with an object
// that has the shape of a Link to make the missing entry/asset
// card render
.map((link) => link || { sys: { id: 'null-value' } });
return (
<Editor
{...props}
items={items}
isDisabled={disabled}
setValue={setValue}
key={`${externalReset}-list`}
allContentTypes={allContentTypes}
/>
);
}}
</ReferenceEditor>
);
}
MultipleReferenceEditor.defaultProps = {
hasCardEditActions: true,
};