How to implement Recursive Block? #3345
Answered
by
GeorgeHulpoi
GeorgeHulpoi
asked this question in
Q&A
-
Hello! I want to implement a recursive block. export const HTMLElementBlock: Block = {
slug: 'html-element',
labels: {
singular: 'HTML Element Block',
plural: 'HTML Element Blocks',
},
interfaceName: 'HTMLElementBlock',
fields: [
{
name: 'layout',
type: 'blocks',
blocks: [
HTMLElementBlock,
// rest of blocks
],
},
],
}; The only problem is that I can't use the |
Beta Was this translation helpful? Give feedback.
Answered by
GeorgeHulpoi
Sep 17, 2023
Replies: 1 comment
-
I managed to resolve the issue with this code: import { Block, Field } from 'payload/types';
type HTMLElementBlockType = (options?: {
depth?: number;
}) => Block;
const block: HTMLElementBlockType = ({ depth = 0 } = {}) => {
const layoutField: Field = {
name: 'layout',
type: 'blocks',
blocks: [],
};
let o = {
slug: 'html-element',
labels: {
singular: 'HTML Element Block',
plural: 'HTML Element Blocks',
},
interfaceName: 'HTMLElementBlock',
fields: [layoutField],
};
if (depth < 5) {
layoutField.blocks.push(block({ depth: depth + 1 }));
}
return o;
};
export default block; |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
GeorgeHulpoi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I managed to resolve the issue with this code: