fix(richtext-slate): fix issue with richText field cell not being a link when used as a useAsTitle (#8272)

This commit is contained in:
Paul
2024-09-17 18:00:00 -06:00
committed by GitHub
parent 023c650e03
commit dd96f9a058

View File

@@ -1,13 +1,48 @@
'use client' 'use client'
import type { DefaultCellComponentProps } from 'payload' import type { DefaultCellComponentProps } from 'payload'
import { useTableCell } from '@payloadcms/ui' import { useConfig, useTableCell } from '@payloadcms/ui'
import { formatAdminURL } from '@payloadcms/ui/shared'
import LinkImport from 'next/link.js'
import React from 'react' import React from 'react'
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
export const RichTextCell: React.FC<DefaultCellComponentProps<any[]>> = () => { export const RichTextCell: React.FC<DefaultCellComponentProps<any[]>> = () => {
const { cellData } = useTableCell() const { cellData, cellProps, columnIndex, customCellContext, rowData } = useTableCell()
const flattenedText = cellData?.map((i) => i?.children?.map((c) => c.text)).join(' ') const flattenedText = cellData?.map((i) => i?.children?.map((c) => c.text)).join(' ')
const {
config: {
routes: { admin: adminRoute },
},
} = useConfig()
const { link } = cellProps || {}
let WrapElement: React.ComponentType<any> | string = 'span'
const wrapElementProps: {
className?: string
href?: string
onClick?: () => void
type?: 'button'
} = {}
const isLink = link !== undefined ? link : columnIndex === 0
if (isLink) {
WrapElement = Link
wrapElementProps.href = customCellContext?.collectionSlug
? formatAdminURL({
adminRoute,
path: `/collections/${customCellContext?.collectionSlug}/${rowData.id}`,
})
: ''
}
if (isLink) {
return <WrapElement {...wrapElementProps}>{flattenedText}</WrapElement>
}
// Limiting the number of characters shown is done in a CSS rule // Limiting the number of characters shown is done in a CSS rule
return <span>{flattenedText}</span> return <span>{flattenedText}</span>
} }