## Description Prior to this change, the `defaultValue` for fields have only been used in the application layer of Payload. With this change, you get the added benefit of having the database columns get the default also. This is especially helpful when adding new columns to postgres with existing data to avoid needing to write complex migrations. In MongoDB this change applies the default to the Mongoose model which is useful when calling payload.db.create() directly. This only works for statically defined values. 🙏 A big thanks to @r1tsuu for the feature and implementation idea as I lifted some code from PR https://github.com/payloadcms/payload/pull/6983 - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] New feature (non-breaking change which adds functionality) - [x] This change requires a documentation update ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes - [x] I have made corresponding changes to the documentation
152 lines
3.2 KiB
TypeScript
152 lines
3.2 KiB
TypeScript
import type { Field } from 'payload'
|
|
|
|
import { pagesSlug, postsSlug } from '../shared.js'
|
|
import deepMerge from '../utilities/deepMerge.js'
|
|
|
|
export const appearanceOptions = {
|
|
primary: {
|
|
label: 'Primary Button',
|
|
value: 'primary',
|
|
},
|
|
secondary: {
|
|
label: 'Secondary Button',
|
|
value: 'secondary',
|
|
},
|
|
default: {
|
|
label: 'Default',
|
|
value: 'default',
|
|
},
|
|
}
|
|
|
|
export type LinkAppearances = 'default' | 'primary' | 'secondary'
|
|
|
|
type LinkType = (options?: {
|
|
appearances?: LinkAppearances[] | false
|
|
disableLabel?: boolean
|
|
overrides?: Record<string, unknown>
|
|
}) => Field
|
|
|
|
const link: LinkType = ({ appearances, disableLabel = false, overrides = {} } = {}) => {
|
|
const linkResult: Field = {
|
|
name: 'link',
|
|
type: 'group',
|
|
admin: {
|
|
hideGutter: true,
|
|
},
|
|
fields: [
|
|
{
|
|
type: 'row',
|
|
fields: [
|
|
{
|
|
name: 'type',
|
|
type: 'radio',
|
|
options: [
|
|
{
|
|
label: 'Internal link',
|
|
value: 'reference',
|
|
},
|
|
{
|
|
label: 'Custom URL',
|
|
value: 'custom',
|
|
},
|
|
],
|
|
defaultValue: 'reference',
|
|
admin: {
|
|
layout: 'horizontal',
|
|
width: '50%',
|
|
},
|
|
},
|
|
{
|
|
name: 'newTab',
|
|
label: 'Open in new tab',
|
|
type: 'checkbox',
|
|
admin: {
|
|
width: '50%',
|
|
style: {
|
|
alignSelf: 'flex-end',
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
const linkTypes: Field[] = [
|
|
{
|
|
name: 'reference',
|
|
label: 'Document to link to',
|
|
type: 'relationship',
|
|
relationTo: [postsSlug, pagesSlug],
|
|
required: true,
|
|
maxDepth: 1,
|
|
admin: {
|
|
condition: (_, siblingData) => siblingData?.type === 'reference',
|
|
},
|
|
},
|
|
{
|
|
name: 'url',
|
|
label: 'Custom URL',
|
|
type: 'text',
|
|
required: true,
|
|
admin: {
|
|
condition: (_, siblingData) => siblingData?.type === 'custom',
|
|
},
|
|
},
|
|
]
|
|
|
|
if (!disableLabel) {
|
|
linkTypes.map((linkType) => ({
|
|
...linkType,
|
|
admin: {
|
|
...linkType.admin,
|
|
width: '50%',
|
|
},
|
|
}))
|
|
|
|
linkResult.fields.push({
|
|
type: 'row',
|
|
fields: [
|
|
...linkTypes,
|
|
{
|
|
name: 'label',
|
|
label: 'Label',
|
|
type: 'text',
|
|
required: true,
|
|
admin: {
|
|
width: '50%',
|
|
},
|
|
},
|
|
],
|
|
})
|
|
} else {
|
|
linkResult.fields = [...linkResult.fields, ...linkTypes]
|
|
}
|
|
|
|
if (appearances !== false) {
|
|
let appearanceOptionsToUse = [
|
|
appearanceOptions.default,
|
|
appearanceOptions.primary,
|
|
appearanceOptions.secondary,
|
|
]
|
|
|
|
if (appearances) {
|
|
appearanceOptionsToUse = appearances.map((appearance) => appearanceOptions[appearance])
|
|
}
|
|
|
|
linkResult.fields.push({
|
|
name: 'appearance',
|
|
type: 'select',
|
|
defaultValue: appearanceOptionsToUse[0].value,
|
|
options: appearanceOptionsToUse,
|
|
admin: {
|
|
description: 'Choose how the link should be rendered.',
|
|
},
|
|
})
|
|
}
|
|
|
|
return deepMerge(linkResult, overrides)
|
|
}
|
|
|
|
export default link
|