Skip to content

Commit

Permalink
Add renderer for relationship nodes in document renderer (#4821)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown authored Feb 12, 2021
1 parent 91a4611 commit 3968aa5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cuddly-melons-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/document-renderer': minor
---

Added renderer for relationship nodes
24 changes: 23 additions & 1 deletion packages-next/document-renderer/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type MarkRenderers = { [Key in Mark]: OnlyChildrenComponent };
interface Renderers {
inline: {
link: Component<{ children: ReactNode; href: string }> | 'a';
relationship: Component<{
data: { id: string; label: string; data: Record<string, any> } | null;
}>;
} & MarkRenderers;
block: {
block: OnlyChildrenComponent;
Expand Down Expand Up @@ -59,6 +62,9 @@ const defaultRenderers: Renderers = {
subscript: 'sub',
superscript: 'sup',
underline: 'u',
relationship: ({ data }) => {
return <span>{data?.label}</span>;
},
},
block: {
block: 'div',
Expand Down Expand Up @@ -110,7 +116,7 @@ function DocumentNode({
if (typeof _node.text === 'string') {
let child = <Fragment>{_node.text}</Fragment>;
(Object.keys(renderers.inline) as (keyof typeof renderers.inline)[]).forEach(markName => {
if (markName !== 'link' && _node[markName]) {
if (markName !== 'link' && markName !== 'relationship' && _node[markName]) {
const Mark = renderers.inline[markName];
child = <Mark>{child}</Mark>;
}
Expand Down Expand Up @@ -175,6 +181,22 @@ function DocumentNode({
/>
);
}
case 'relationship': {
const data = node.data as any;
return (
<renderers.inline.relationship
data={
data
? {
id: data.id,
label: data.label || data.id,
data: data.data || {},
}
: null
}
/>
);
}
}
return <Fragment>{children}</Fragment>;
}
Expand Down

0 comments on commit 3968aa5

Please sign in to comment.