Skip to content

Commit dd96f9a

Browse files
authored
fix(richtext-slate): fix issue with richText field cell not being a link when used as a useAsTitle (#8272)
1 parent 023c650 commit dd96f9a

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed
Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
11
'use client'
22
import type { DefaultCellComponentProps } from 'payload'
33

4-
import { useTableCell } from '@payloadcms/ui'
4+
import { useConfig, useTableCell } from '@payloadcms/ui'
5+
import { formatAdminURL } from '@payloadcms/ui/shared'
6+
import LinkImport from 'next/link.js'
57
import React from 'react'
68

9+
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
10+
711
export const RichTextCell: React.FC<DefaultCellComponentProps<any[]>> = () => {
8-
const { cellData } = useTableCell()
12+
const { cellData, cellProps, columnIndex, customCellContext, rowData } = useTableCell()
913
const flattenedText = cellData?.map((i) => i?.children?.map((c) => c.text)).join(' ')
1014

15+
const {
16+
config: {
17+
routes: { admin: adminRoute },
18+
},
19+
} = useConfig()
20+
21+
const { link } = cellProps || {}
22+
let WrapElement: React.ComponentType<any> | string = 'span'
23+
24+
const wrapElementProps: {
25+
className?: string
26+
href?: string
27+
onClick?: () => void
28+
type?: 'button'
29+
} = {}
30+
31+
const isLink = link !== undefined ? link : columnIndex === 0
32+
33+
if (isLink) {
34+
WrapElement = Link
35+
wrapElementProps.href = customCellContext?.collectionSlug
36+
? formatAdminURL({
37+
adminRoute,
38+
path: `/collections/${customCellContext?.collectionSlug}/${rowData.id}`,
39+
})
40+
: ''
41+
}
42+
43+
if (isLink) {
44+
return <WrapElement {...wrapElementProps}>{flattenedText}</WrapElement>
45+
}
1146
// Limiting the number of characters shown is done in a CSS rule
1247
return <span>{flattenedText}</span>
1348
}

0 commit comments

Comments
 (0)