@contentful/field-editor-reference 5.22.2 → 5.22.3
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/cjs/resources/MultipleResourceReferenceEditor.spec.js +1 -1
- package/dist/cjs/resources/SingleResourceReferenceEditor.spec.js +1 -1
- package/dist/cjs/resources/testHelpers/resourceEditorHelpers.js +8 -20
- package/dist/cjs/resources/useResourceLinkActions.js +12 -33
- package/dist/esm/resources/MultipleResourceReferenceEditor.spec.js +1 -1
- package/dist/esm/resources/SingleResourceReferenceEditor.spec.js +1 -1
- package/dist/esm/resources/testHelpers/resourceEditorHelpers.js +8 -20
- package/dist/esm/resources/useResourceLinkActions.js +13 -34
- package/dist/types/resources/useResourceLinkActions.d.ts +2 -4
- package/package.json +2 -2
|
@@ -101,7 +101,7 @@ describe('Multiple resource editor', ()=>{
|
|
|
101
101
|
const button = await _react1.screen.findByText('Add existing content');
|
|
102
102
|
expect(button).toBeDefined();
|
|
103
103
|
_react1.fireEvent.click(button);
|
|
104
|
-
const dialogFn = sdk.dialogs.
|
|
104
|
+
const dialogFn = sdk.dialogs.selectMultipleResourceEntities;
|
|
105
105
|
expect(dialogFn).toHaveBeenCalledTimes(1);
|
|
106
106
|
const options = dialogFn.mock.calls[0][0];
|
|
107
107
|
expect(options).toEqual({
|
|
@@ -97,7 +97,7 @@ describe('Single resource editor', ()=>{
|
|
|
97
97
|
const button = await _react1.screen.findByText('Add existing content');
|
|
98
98
|
expect(button).toBeDefined();
|
|
99
99
|
_react1.fireEvent.click(button);
|
|
100
|
-
const dialogFn = sdk.dialogs.
|
|
100
|
+
const dialogFn = sdk.dialogs.selectSingleResourceEntity;
|
|
101
101
|
expect(dialogFn).toHaveBeenCalledTimes(1);
|
|
102
102
|
const options = dialogFn.mock.calls[0][0];
|
|
103
103
|
expect(options).toEqual({
|
|
@@ -29,31 +29,19 @@ function mockSdkForField(fieldDefinition, fieldValue) {
|
|
|
29
29
|
locale: 'en'
|
|
30
30
|
},
|
|
31
31
|
dialogs: {
|
|
32
|
-
|
|
32
|
+
selectSingleResourceEntity: jest.fn().mockResolvedValue({
|
|
33
33
|
sys: {
|
|
34
|
-
type: '
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
sys: {
|
|
38
|
-
type: 'Link',
|
|
39
|
-
linkType: 'Space',
|
|
40
|
-
id: 'x-space'
|
|
41
|
-
}
|
|
42
|
-
}
|
|
34
|
+
type: 'ResourceLink',
|
|
35
|
+
linkType: 'Contentful:Entry',
|
|
36
|
+
urn: 'crn:contentful:::content:spaces/space-id/entries/example-entity-urn'
|
|
43
37
|
}
|
|
44
38
|
}),
|
|
45
|
-
|
|
39
|
+
selectMultipleResourceEntities: jest.fn().mockResolvedValue([
|
|
46
40
|
{
|
|
47
41
|
sys: {
|
|
48
|
-
type: '
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
sys: {
|
|
52
|
-
type: 'Link',
|
|
53
|
-
linkType: 'Space',
|
|
54
|
-
id: 'x-space'
|
|
55
|
-
}
|
|
56
|
-
}
|
|
42
|
+
type: 'ResourceLink',
|
|
43
|
+
linkType: 'Contentful:Entry',
|
|
44
|
+
urn: 'crn:contentful:::content:spaces/space-id/entries/example-entity-urn'
|
|
57
45
|
}
|
|
58
46
|
}
|
|
59
47
|
])
|
|
@@ -9,61 +9,40 @@ Object.defineProperty(exports, "useResourceLinkActions", {
|
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
11
|
const _react = require("react");
|
|
12
|
-
const
|
|
13
|
-
sys: {
|
|
14
|
-
type: 'ResourceLink',
|
|
15
|
-
linkType: 'Contentful:Entry',
|
|
16
|
-
urn: entry.sys.urn ?? `crn:${apiUrl}:::content:spaces/${entry.sys.space.sys.id}/entries/${entry.sys.id}`
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
const getUpdatedValue = (field, entries, apiUrl)=>{
|
|
12
|
+
const getUpdatedValue = (field, linkItems)=>{
|
|
20
13
|
const multiple = field.type === 'Array';
|
|
21
14
|
if (multiple) {
|
|
22
|
-
const linkItems = entries.map((entry)=>toLinkItem(entry, apiUrl));
|
|
23
15
|
const prevValue = field.getValue() || [];
|
|
24
16
|
return [
|
|
25
17
|
...prevValue,
|
|
26
18
|
...linkItems
|
|
27
19
|
];
|
|
28
20
|
} else {
|
|
29
|
-
return
|
|
21
|
+
return linkItems[0];
|
|
30
22
|
}
|
|
31
23
|
};
|
|
32
|
-
function useResourceLinkActions({ dialogs, field
|
|
33
|
-
const handleAfterLink = (0, _react.useCallback)((entries)=>{
|
|
34
|
-
if (!onAfterLink) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
entries.forEach(onAfterLink);
|
|
38
|
-
}, [
|
|
39
|
-
onAfterLink
|
|
40
|
-
]);
|
|
24
|
+
function useResourceLinkActions({ dialogs, field }) {
|
|
41
25
|
const onLinkedExisting = (0, _react.useMemo)(()=>{
|
|
42
|
-
return (
|
|
43
|
-
const updatedValue = getUpdatedValue(field,
|
|
44
|
-
|
|
45
|
-
|
|
26
|
+
return (links)=>{
|
|
27
|
+
const updatedValue = getUpdatedValue(field, links);
|
|
28
|
+
if (updatedValue) {
|
|
29
|
+
field.setValue(updatedValue);
|
|
30
|
+
}
|
|
46
31
|
};
|
|
47
32
|
}, [
|
|
48
|
-
field
|
|
49
|
-
handleAfterLink,
|
|
50
|
-
apiUrl
|
|
33
|
+
field
|
|
51
34
|
]);
|
|
52
35
|
const multiple = field.type === 'Array';
|
|
53
36
|
const onLinkExisting = (0, _react.useMemo)(()=>{
|
|
54
|
-
const promptSelection = multiple ? async ()=>await dialogs.
|
|
37
|
+
const promptSelection = multiple ? async ()=>await dialogs.selectMultipleResourceEntities({
|
|
55
38
|
allowedResources: field.allowedResources
|
|
56
39
|
}) : async ()=>[
|
|
57
|
-
await dialogs.
|
|
40
|
+
await dialogs.selectSingleResourceEntity({
|
|
58
41
|
allowedResources: field.allowedResources
|
|
59
42
|
})
|
|
60
43
|
];
|
|
61
44
|
return async ()=>{
|
|
62
|
-
|
|
63
|
-
if (!res) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
onLinkedExisting(res);
|
|
45
|
+
onLinkedExisting(await promptSelection());
|
|
67
46
|
};
|
|
68
47
|
}, [
|
|
69
48
|
dialogs,
|
|
@@ -56,7 +56,7 @@ describe('Multiple resource editor', ()=>{
|
|
|
56
56
|
const button = await screen.findByText('Add existing content');
|
|
57
57
|
expect(button).toBeDefined();
|
|
58
58
|
fireEvent.click(button);
|
|
59
|
-
const dialogFn = sdk.dialogs.
|
|
59
|
+
const dialogFn = sdk.dialogs.selectMultipleResourceEntities;
|
|
60
60
|
expect(dialogFn).toHaveBeenCalledTimes(1);
|
|
61
61
|
const options = dialogFn.mock.calls[0][0];
|
|
62
62
|
expect(options).toEqual({
|
|
@@ -52,7 +52,7 @@ describe('Single resource editor', ()=>{
|
|
|
52
52
|
const button = await screen.findByText('Add existing content');
|
|
53
53
|
expect(button).toBeDefined();
|
|
54
54
|
fireEvent.click(button);
|
|
55
|
-
const dialogFn = sdk.dialogs.
|
|
55
|
+
const dialogFn = sdk.dialogs.selectSingleResourceEntity;
|
|
56
56
|
expect(dialogFn).toHaveBeenCalledTimes(1);
|
|
57
57
|
const options = dialogFn.mock.calls[0][0];
|
|
58
58
|
expect(options).toEqual({
|
|
@@ -11,31 +11,19 @@ export function mockSdkForField(fieldDefinition, fieldValue) {
|
|
|
11
11
|
locale: 'en'
|
|
12
12
|
},
|
|
13
13
|
dialogs: {
|
|
14
|
-
|
|
14
|
+
selectSingleResourceEntity: jest.fn().mockResolvedValue({
|
|
15
15
|
sys: {
|
|
16
|
-
type: '
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
sys: {
|
|
20
|
-
type: 'Link',
|
|
21
|
-
linkType: 'Space',
|
|
22
|
-
id: 'x-space'
|
|
23
|
-
}
|
|
24
|
-
}
|
|
16
|
+
type: 'ResourceLink',
|
|
17
|
+
linkType: 'Contentful:Entry',
|
|
18
|
+
urn: 'crn:contentful:::content:spaces/space-id/entries/example-entity-urn'
|
|
25
19
|
}
|
|
26
20
|
}),
|
|
27
|
-
|
|
21
|
+
selectMultipleResourceEntities: jest.fn().mockResolvedValue([
|
|
28
22
|
{
|
|
29
23
|
sys: {
|
|
30
|
-
type: '
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
sys: {
|
|
34
|
-
type: 'Link',
|
|
35
|
-
linkType: 'Space',
|
|
36
|
-
id: 'x-space'
|
|
37
|
-
}
|
|
38
|
-
}
|
|
24
|
+
type: 'ResourceLink',
|
|
25
|
+
linkType: 'Contentful:Entry',
|
|
26
|
+
urn: 'crn:contentful:::content:spaces/space-id/entries/example-entity-urn'
|
|
39
27
|
}
|
|
40
28
|
}
|
|
41
29
|
])
|
|
@@ -1,59 +1,38 @@
|
|
|
1
|
-
import {
|
|
2
|
-
const
|
|
3
|
-
sys: {
|
|
4
|
-
type: 'ResourceLink',
|
|
5
|
-
linkType: 'Contentful:Entry',
|
|
6
|
-
urn: entry.sys.urn ?? `crn:${apiUrl}:::content:spaces/${entry.sys.space.sys.id}/entries/${entry.sys.id}`
|
|
7
|
-
}
|
|
8
|
-
});
|
|
9
|
-
const getUpdatedValue = (field, entries, apiUrl)=>{
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
const getUpdatedValue = (field, linkItems)=>{
|
|
10
3
|
const multiple = field.type === 'Array';
|
|
11
4
|
if (multiple) {
|
|
12
|
-
const linkItems = entries.map((entry)=>toLinkItem(entry, apiUrl));
|
|
13
5
|
const prevValue = field.getValue() || [];
|
|
14
6
|
return [
|
|
15
7
|
...prevValue,
|
|
16
8
|
...linkItems
|
|
17
9
|
];
|
|
18
10
|
} else {
|
|
19
|
-
return
|
|
11
|
+
return linkItems[0];
|
|
20
12
|
}
|
|
21
13
|
};
|
|
22
|
-
export function useResourceLinkActions({ dialogs, field
|
|
23
|
-
const handleAfterLink = useCallback((entries)=>{
|
|
24
|
-
if (!onAfterLink) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
entries.forEach(onAfterLink);
|
|
28
|
-
}, [
|
|
29
|
-
onAfterLink
|
|
30
|
-
]);
|
|
14
|
+
export function useResourceLinkActions({ dialogs, field }) {
|
|
31
15
|
const onLinkedExisting = useMemo(()=>{
|
|
32
|
-
return (
|
|
33
|
-
const updatedValue = getUpdatedValue(field,
|
|
34
|
-
|
|
35
|
-
|
|
16
|
+
return (links)=>{
|
|
17
|
+
const updatedValue = getUpdatedValue(field, links);
|
|
18
|
+
if (updatedValue) {
|
|
19
|
+
field.setValue(updatedValue);
|
|
20
|
+
}
|
|
36
21
|
};
|
|
37
22
|
}, [
|
|
38
|
-
field
|
|
39
|
-
handleAfterLink,
|
|
40
|
-
apiUrl
|
|
23
|
+
field
|
|
41
24
|
]);
|
|
42
25
|
const multiple = field.type === 'Array';
|
|
43
26
|
const onLinkExisting = useMemo(()=>{
|
|
44
|
-
const promptSelection = multiple ? async ()=>await dialogs.
|
|
27
|
+
const promptSelection = multiple ? async ()=>await dialogs.selectMultipleResourceEntities({
|
|
45
28
|
allowedResources: field.allowedResources
|
|
46
29
|
}) : async ()=>[
|
|
47
|
-
await dialogs.
|
|
30
|
+
await dialogs.selectSingleResourceEntity({
|
|
48
31
|
allowedResources: field.allowedResources
|
|
49
32
|
})
|
|
50
33
|
];
|
|
51
34
|
return async ()=>{
|
|
52
|
-
|
|
53
|
-
if (!res) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
onLinkedExisting(res);
|
|
35
|
+
onLinkedExisting(await promptSelection());
|
|
57
36
|
};
|
|
58
37
|
}, [
|
|
59
38
|
dialogs,
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { FieldAppSDK } from '@contentful/app-sdk';
|
|
2
|
-
import { EntryProps } from 'contentful-management';
|
|
1
|
+
import type { FieldAppSDK } from '@contentful/app-sdk';
|
|
3
2
|
import { LinkActionsProps } from '../components';
|
|
4
|
-
export declare function useResourceLinkActions({ dialogs, field,
|
|
3
|
+
export declare function useResourceLinkActions({ dialogs, field, }: Pick<FieldAppSDK, 'field' | 'dialogs'> & {
|
|
5
4
|
apiUrl: string;
|
|
6
|
-
onAfterLink?: (e: EntryProps) => void;
|
|
7
5
|
}): LinkActionsProps;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/field-editor-reference",
|
|
3
|
-
"version": "5.22.
|
|
3
|
+
"version": "5.22.3",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -67,5 +67,5 @@
|
|
|
67
67
|
"publishConfig": {
|
|
68
68
|
"registry": "https://npm.pkg.github.com/"
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "87df8d557b8011b5bd1fcf1560762469683fa32e"
|
|
71
71
|
}
|