@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
116 lines (115 loc) • 3.35 kB
JavaScript
import { deepMerge } from '@websolutespa/payload-utils';
import { options } from '../../options';
export const appearanceOptions = {
text: {
label: 'Text',
value: 'text'
},
primaryButton: {
label: 'Primary Button',
value: 'primaryButton'
},
secondaryButton: {
label: 'Secondary Button',
value: 'secondaryButton'
}
};
export const withLink = ({ appearances, disableLabel = false, ...props } = {})=>{
const generatedLink = {
name: 'link',
type: 'group',
fields: [
{
name: 'type',
type: 'radio',
options: [
{
label: 'Internal link',
value: 'reference'
},
{
label: 'Custom URL',
value: 'custom'
}
],
defaultValue: 'reference',
admin: {
layout: 'horizontal'
}
}
]
};
const linkOptions = {
type: 'row',
fields: [
{
name: 'reference',
label: 'Document to link to',
type: 'relationship',
relationTo: options.pages,
required: true,
maxDepth: 2,
admin: {
condition: (_, siblingData)=>siblingData?.type === 'reference'
}
},
{
name: 'url',
label: 'Custom URL',
type: 'text',
required: true,
admin: {
condition: (_, siblingData)=>siblingData?.type === 'custom'
}
}
]
};
if (!disableLabel) {
linkOptions.fields.unshift({
name: 'label',
label: 'Label',
type: 'text',
required: true,
admin: {
width: '50%'
}
});
linkOptions.fields[1].admin.width = '50%';
linkOptions.fields[2].admin.width = '50%';
generatedLink.fields[0].admin.width = '50%';
generatedLink.fields.push(linkOptions);
} else {
generatedLink.fields.push(linkOptions);
}
generatedLink.fields.push({
name: 'newTab',
label: 'Open In New Tab',
type: 'checkbox'
});
// Use all appearances by default
if (typeof appearances === 'undefined') {
generatedLink.fields.unshift({
name: 'appearance',
label: 'Appearance',
type: 'select',
defaultValue: 'text',
options: [
appearanceOptions.text,
appearanceOptions.primaryButton,
appearanceOptions.secondaryButton
]
});
}
// Manually select appearances
if (appearances) {
generatedLink.fields.unshift({
name: 'appearance',
label: 'Appearance',
type: 'select',
defaultValue: appearances[0],
options: appearances.map((appearance)=>appearanceOptions[appearance])
});
}
return deepMerge(generatedLink, props);
};
//# sourceMappingURL=withLink.js.map