Skip to content

Commit 785609b

Browse files
authored
Merge pull request #120 from payloadcms/fix/richtext-relationship-modal
fix: modal issues with richtext relationship
2 parents 3c9d71d + 8ea4407 commit 785609b

File tree

3 files changed

+58
-45
lines changed

3 files changed

+58
-45
lines changed

src/admin/components/elements/Button/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const Button: React.FC<Props> = (props) => {
7878
className: classes,
7979
onClick: handleClick,
8080
rel: newTab ? 'noopener noreferrer' : undefined,
81-
target: newTab ? '_blank' : undefined
81+
target: newTab ? '_blank' : undefined,
8282
};
8383

8484
switch (el) {
+15-15
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { MouseEvent } from 'react';
22

33
export type Props = {
4-
className?: string,
5-
type?: 'submit' | 'button',
6-
el?: 'link' | 'anchor' | undefined,
7-
to?: string,
8-
url?: string,
9-
children?: React.ReactNode,
10-
onClick?: (event: MouseEvent) => void,
11-
disabled?: boolean,
12-
icon?: React.ReactNode | ['chevron' | 'x' | 'plus'],
13-
iconStyle?: 'with-border' | 'without-border' | 'none',
14-
buttonStyle?: 'primary' | 'secondary' | 'transparent' | 'error' | 'none' | 'icon-label',
15-
round?: boolean,
16-
size?: 'small' | 'medium',
17-
iconPosition?: 'left' | 'right',
18-
newTab?: boolean
4+
className?: string,
5+
type?: 'submit' | 'button',
6+
el?: 'link' | 'anchor' | undefined,
7+
to?: string,
8+
url?: string,
9+
children?: React.ReactNode,
10+
onClick?: (event: MouseEvent) => void,
11+
disabled?: boolean,
12+
icon?: React.ReactNode | ['chevron' | 'x' | 'plus'],
13+
iconStyle?: 'with-border' | 'without-border' | 'none',
14+
buttonStyle?: 'primary' | 'secondary' | 'transparent' | 'error' | 'none' | 'icon-label',
15+
round?: boolean,
16+
size?: 'small' | 'medium',
17+
iconPosition?: 'left' | 'right',
18+
newTab?: boolean
1919
}

src/admin/components/forms/field-types/RichText/elements/relationship/Button/index.tsx

+42-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Fragment, useCallback, useState } from 'react';
1+
import React, { Fragment, useCallback, useEffect, useState } from 'react';
22
import { Modal, useModal } from '@faceless-ui/modal';
33
import { Transforms } from 'slate';
44
import { ReactEditor, useSlate } from 'slate-react';
@@ -49,12 +49,14 @@ const insertRelationship = (editor, { value, relationTo, depth }) => {
4949
ReactEditor.focus(editor);
5050
};
5151

52-
const RelationshipButton = ({ path }) => {
52+
const RelationshipButton: React.FC<{path: string}> = ({ path }) => {
5353
const { open, closeAll } = useModal();
5454
const editor = useSlate();
5555
const { serverURL, routes: { api }, collections } = useConfig();
56+
const [renderModal, setRenderModal] = useState(false);
5657
const [loading, setLoading] = useState(false);
5758
const [hasEnabledCollections] = useState(() => collections.find(({ admin: { enableRichTextRelationship } }) => enableRichTextRelationship));
59+
const modalSlug = `${path}-add-relationship`;
5860

5961
const handleAddRelationship = useCallback(async (_, { relationTo, value, depth }) => {
6062
setLoading(true);
@@ -64,9 +66,15 @@ const RelationshipButton = ({ path }) => {
6466

6567
insertRelationship(editor, { value: json, depth, relationTo });
6668
closeAll();
69+
setRenderModal(false);
70+
setLoading(false);
6771
}, [editor, closeAll, api, serverURL]);
6872

69-
const modalSlug = `${path}-add-relationship`;
73+
useEffect(() => {
74+
if (renderModal) {
75+
open(modalSlug);
76+
}
77+
}, [renderModal, open, modalSlug]);
7078

7179
if (!hasEnabledCollections) return null;
7280

@@ -75,36 +83,41 @@ const RelationshipButton = ({ path }) => {
7583
<ElementButton
7684
className={baseClass}
7785
format="relationship"
78-
onClick={() => open(modalSlug)}
86+
onClick={() => setRenderModal(true)}
7987
>
8088
<RelationshipIcon />
8189
</ElementButton>
82-
<Modal
83-
slug={modalSlug}
84-
className={`${baseClass}__modal`}
85-
>
86-
<MinimalTemplate>
87-
<header className={`${baseClass}__header`}>
88-
<h3>Add Relationship</h3>
89-
<Button
90-
buttonStyle="none"
91-
onClick={closeAll}
90+
{renderModal && (
91+
<Modal
92+
slug={modalSlug}
93+
className={`${baseClass}__modal`}
94+
>
95+
<MinimalTemplate>
96+
<header className={`${baseClass}__header`}>
97+
<h3>Add Relationship</h3>
98+
<Button
99+
buttonStyle="none"
100+
onClick={() => {
101+
closeAll();
102+
setRenderModal(false);
103+
}}
104+
>
105+
<X />
106+
</Button>
107+
</header>
108+
<Form
109+
onSubmit={handleAddRelationship}
110+
initialData={initialFormData}
111+
disabled={loading}
92112
>
93-
<X />
94-
</Button>
95-
</header>
96-
<Form
97-
onSubmit={handleAddRelationship}
98-
initialData={initialFormData}
99-
disabled={loading}
100-
>
101-
<Fields />
102-
<Submit>
103-
Add relationship
104-
</Submit>
105-
</Form>
106-
</MinimalTemplate>
107-
</Modal>
113+
<Fields />
114+
<Submit>
115+
Add relationship
116+
</Submit>
117+
</Form>
118+
</MinimalTemplate>
119+
</Modal>
120+
)}
108121
</Fragment>
109122
);
110123
};

0 commit comments

Comments
 (0)