Skip to content

Instantly share code, notes, and snippets.

@ozbeksu
Last active February 27, 2023 00:42
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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