import { RichText, useBlockProps } from '@wordpress/block-editor'; import { registerBlockType, type BlockEditProps, type BlockSaveProps, } from '@wordpress/blocks'; import { Button } from '@wordpress/components'; const NAME = 'recomaze/faq'; interface Question { question: string; answer: string; } type Attributes = { questions: Question[]; }; const Edit = ({ attributes, setAttributes, }: BlockEditProps): JSX.Element => { const questions = attributes.questions ?? []; const change = (index: number, patch: Partial): void => setAttributes({ questions: questions.map((item, at) => at === index ? { ...item, ...patch } : item ), }); return (
{questions.map((item, index) => (
change(index, { question })} /> change(index, { answer })} />
))}
); }; const Save = ({ attributes }: BlockSaveProps): JSX.Element => (
{(attributes.questions ?? []) .filter(item => item.question.trim() !== '') .map((item, index) => (
))}
); registerBlockType( { name: NAME, title: 'FAQ', category: 'text', attributes: { questions: { type: 'array', default: [] } }, }, { edit: Edit, save: Save } );