156 lines
4.7 KiB
TypeScript
156 lines
4.7 KiB
TypeScript
import type { ArrayField, Field, GroupField } from 'payload'
|
|
|
|
import { rowLabel } from '../admin-ui/rowLabel.js'
|
|
|
|
export type LinkAppearance = 'link' | 'primary' | 'secondary'
|
|
|
|
export type BuildLinkFieldOptions = {
|
|
/** Visual styles the editor may pick. `false` → no appearance select. Default all three. */
|
|
appearances?: false | LinkAppearance[]
|
|
/** Hide the label — for links that wrap something (a whole card, an image). */
|
|
disableLabel?: boolean
|
|
/** Field name. Default 'link'. */
|
|
name?: string
|
|
/** Collection the "Page" option points at. Default 'pages'. */
|
|
relationTo?: string
|
|
required?: boolean
|
|
/** Upload collection for "File" links (PDF, documents). Default 'media'. */
|
|
uploadTo?: string
|
|
}
|
|
|
|
const URL_PATTERN = /^(?:https?:\/\/|mailto:|tel:|#)/
|
|
|
|
type SiblingWithType = { type?: string } | undefined
|
|
|
|
/**
|
|
* The ONE link shape for a project: a page, an external URL (also mailto:,
|
|
* tel:, #anchor) or an uploaded file — plus label, new-tab flag and visual
|
|
* style. Every CTA, card link and download in every block uses this field, so
|
|
* there is no zoo of ctaLabel / buttonUrl / pdfUrl fields and one renderer
|
|
* (resolveLinkHref + the project's CMSLink) handles them all.
|
|
*
|
|
* Generates a named TypeScript type `CmsLink` in payload-types.ts.
|
|
*
|
|
* fields: [buildLinkField()] // single link
|
|
* fields: [buildLinkField({ disableLabel: true, appearances: false })] // clickable card
|
|
*/
|
|
export function buildLinkField({
|
|
name = 'link',
|
|
appearances = ['primary', 'secondary', 'link'],
|
|
disableLabel = false,
|
|
relationTo = 'pages',
|
|
required = false,
|
|
uploadTo = 'media',
|
|
}: BuildLinkFieldOptions = {}): GroupField {
|
|
const fields: Field[] = [
|
|
{
|
|
type: 'row',
|
|
fields: [
|
|
{
|
|
name: 'type',
|
|
type: 'radio',
|
|
admin: { layout: 'horizontal', width: '50%' },
|
|
defaultValue: 'internal',
|
|
options: [
|
|
{ label: 'Page', value: 'internal' },
|
|
{ label: 'URL', value: 'external' },
|
|
{ label: 'File', value: 'file' },
|
|
],
|
|
},
|
|
{
|
|
name: 'newTab',
|
|
type: 'checkbox',
|
|
admin: { style: { alignSelf: 'flex-end' }, width: '50%' },
|
|
label: 'Open in new tab',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'page',
|
|
type: 'relationship',
|
|
admin: { condition: (_: unknown, s: SiblingWithType) => s?.type === 'internal' },
|
|
relationTo,
|
|
required,
|
|
},
|
|
{
|
|
name: 'url',
|
|
type: 'text',
|
|
admin: {
|
|
condition: (_: unknown, s: SiblingWithType) => s?.type === 'external',
|
|
description: 'Full URL (https://…), mailto:, tel: or #section-anchor.',
|
|
},
|
|
required,
|
|
validate: (v: null | string | undefined) =>
|
|
!v || URL_PATTERN.test(v) || 'Use https://…, mailto:, tel: or #anchor',
|
|
},
|
|
{
|
|
name: 'file',
|
|
type: 'upload',
|
|
admin: {
|
|
condition: (_: unknown, s: SiblingWithType) => s?.type === 'file',
|
|
description: 'Upload a PDF / document / image. Never paste a file URL.',
|
|
},
|
|
relationTo: uploadTo,
|
|
required,
|
|
},
|
|
]
|
|
|
|
if (!disableLabel) {
|
|
fields.push({ name: 'label', type: 'text', localized: true, required })
|
|
}
|
|
|
|
if (appearances && appearances.length) {
|
|
fields.push({
|
|
name: 'appearance',
|
|
type: 'select',
|
|
defaultValue: appearances[0],
|
|
options: appearances.map((a) => ({ label: a, value: a })),
|
|
})
|
|
}
|
|
|
|
return {
|
|
name,
|
|
type: 'group',
|
|
admin: { hideGutter: true },
|
|
fields,
|
|
interfaceName: 'CmsLink',
|
|
}
|
|
}
|
|
|
|
export type BuildLinksFieldOptions = {
|
|
/**
|
|
* What one row is called in the panel — the add button reads "Add <singular>".
|
|
* Default { singular: 'Button', plural: 'Buttons' }; e.g. { singular: 'Link', plural: 'Links' } for a link list.
|
|
*/
|
|
labels?: { plural: string; singular: string }
|
|
/** Max number of buttons. Default 2. */
|
|
maxRows?: number
|
|
/** Field name. Default 'links'. */
|
|
name?: string
|
|
} & Omit<BuildLinkFieldOptions, 'disableLabel' | 'name'>
|
|
|
|
/**
|
|
* 0..N buttons — use for every "CTA(s)" of a section. Each row holds one
|
|
* `link` (buildLinkField). Render: `links.map(({ id, link }) => <CMSLink … />)`.
|
|
*/
|
|
export function buildLinksField({
|
|
name = 'links',
|
|
labels = { plural: 'Buttons', singular: 'Button' },
|
|
maxRows = 2,
|
|
...linkOptions
|
|
}: BuildLinksFieldOptions = {}): ArrayField {
|
|
return {
|
|
name,
|
|
type: 'array',
|
|
admin: {
|
|
initCollapsed: true,
|
|
// Rows read "Contact us" / "Download catalogue", not "Button 01". Needs the importMap entry.
|
|
components: rowLabel(['link.label', 'link.url'], labels.singular),
|
|
},
|
|
fields: [buildLinkField(linkOptions)],
|
|
label: labels.plural,
|
|
labels,
|
|
maxRows,
|
|
}
|
|
}
|