Skip to content

Instantly share code, notes, and snippets.

@HendrikRoth
Forked from ozbeksu/LinkBlock.ts
Created January 6, 2023 00:05
Show Gist options
  • Save HendrikRoth/3af99b236dcfebe20b2292316803d7bb to your computer and use it in GitHub Desktop.
Save HendrikRoth/3af99b236dcfebe20b2292316803d7bb to your computer and use it in GitHub Desktop.
PayloadCMS Revursive Block
import {Block} from 'payload/types';
const createRecursiveLinksBlock = (current = 0, maxDepth = 3): Block => {
if (current < maxDepth - 1) {
current++;
return {
slug: `Level ${current}`,
fields: [
{
name: 'name',
type: 'text',
},
{
name: 'children',
type: 'blocks',
blocks: [createRecursiveLinksBlock(current)],
},
],
};
}
return {
slug: `Level ${current + 1}`,
fields: [
{
name: 'name',
type: 'text',
},
],
};
};
const LinkBlock: Block = createRecursiveLinksBlock();
export default LinkBlock;
import {CollectionConfig} from 'payload/types';
import {LinkBlock} from '../blocks';
const Navigations: CollectionConfig = {
slug: 'navigations',
admin: {
useAsTitle: 'name',
},
access: {
read: () => true,
},
fields: [
{
name: 'name',
type: 'text',
},
{
name: 'links',
type: 'blocks',
blocks: [LinkBlock],
},
],
};
export default Navigations;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment