admin管理员组文章数量:1277338
I have two gutenberg blocks to make up an accordion. The parent accordion uses InnerBlocks
with allowedBlocks
set to just the panel block. So the two blocks are:
ea/accordion
ea/accordion-panel
Each panel has two elements, a heading for the panel (using RichText
) and InnerBlocks
with the content. When I save the accordion, only the InnerBlocks content is being saved. How can I save the heading as well?
More detail:
The ea/accordion-panel
has the following index.js
registerBlockType("ea/accordion-panel", {
parent: ["ea/accordion"],
attributes: {
heading: {
type: "string",
source: "html",
selector: "h2",
},
},
edit: Edit,
save,
});
And the following edit function:
export default function Edit({ attributes, setAttributes }) {
return (
<div {...useBlockProps({ className: "ea-accordion-panel" })}>
<div className="ea-accordion-panel--title">
<RichText
tagName="h2"
value={attributes.heading}
onChange={(heading) => setAttributes({ heading })}
placeholder={__("Heading...")}
/>
</div>
<InnerBlocks />
</div>
);
}
But it is only the InnerBlocks
content that is shown in the editor after saving. (not the heading
, even though this is set and stored in attributes)
The ea/accordion
itself doesn't store any attributes, so maybe this is where the error is?
accordion/index.js
import { registerBlockType } from "@wordpress/blocks";
import "./style.scss";
import Edit from "./edit";
import save from "./save";
registerBlockType("ea/accordion", {
edit: Edit,
save,
});
accordion edit function:
export default function Edit() {
const blockProps = useBlockProps({ className: "ea-accordion" });
return (
<div {...blockProps}>
<InnerBlocks allowedBlocks={["ea-accordion-panel"]} />
</div>
);
}
Before saving, here we have
accordion
InnerBlocks
accordion-panel
heading => A new heading
InnerBlocks
Paragraph => Some content
accordion-panel
heading => Another heading
InnerBlocks
Paragraph => Some more content
Then after save...
accordion
InnerBlocks
accordion-panel
heading => empty
InnerBlocks
Paragraph => Some content
accordion-panel
heading => empty
InnerBlocks
Paragraph => Some more content
Update
I found the issue, the save function looked like this:
<div {...blockProps}>
<!-- No heading tag -->
<div className="ea-accordion-panel--content">
<InnerBlocks.Content />
</div>
</div>
I have two gutenberg blocks to make up an accordion. The parent accordion uses InnerBlocks
with allowedBlocks
set to just the panel block. So the two blocks are:
ea/accordion
ea/accordion-panel
Each panel has two elements, a heading for the panel (using RichText
) and InnerBlocks
with the content. When I save the accordion, only the InnerBlocks content is being saved. How can I save the heading as well?
More detail:
The ea/accordion-panel
has the following index.js
registerBlockType("ea/accordion-panel", {
parent: ["ea/accordion"],
attributes: {
heading: {
type: "string",
source: "html",
selector: "h2",
},
},
edit: Edit,
save,
});
And the following edit function:
export default function Edit({ attributes, setAttributes }) {
return (
<div {...useBlockProps({ className: "ea-accordion-panel" })}>
<div className="ea-accordion-panel--title">
<RichText
tagName="h2"
value={attributes.heading}
onChange={(heading) => setAttributes({ heading })}
placeholder={__("Heading...")}
/>
</div>
<InnerBlocks />
</div>
);
}
But it is only the InnerBlocks
content that is shown in the editor after saving. (not the heading
, even though this is set and stored in attributes)
The ea/accordion
itself doesn't store any attributes, so maybe this is where the error is?
accordion/index.js
import { registerBlockType } from "@wordpress/blocks";
import "./style.scss";
import Edit from "./edit";
import save from "./save";
registerBlockType("ea/accordion", {
edit: Edit,
save,
});
accordion edit function:
export default function Edit() {
const blockProps = useBlockProps({ className: "ea-accordion" });
return (
<div {...blockProps}>
<InnerBlocks allowedBlocks={["ea-accordion-panel"]} />
</div>
);
}
Before saving, here we have
accordion
InnerBlocks
accordion-panel
heading => A new heading
InnerBlocks
Paragraph => Some content
accordion-panel
heading => Another heading
InnerBlocks
Paragraph => Some more content
Then after save...
accordion
InnerBlocks
accordion-panel
heading => empty
InnerBlocks
Paragraph => Some content
accordion-panel
heading => empty
InnerBlocks
Paragraph => Some more content
Update
I found the issue, the save function looked like this:
<div {...blockProps}>
<!-- No heading tag -->
<div className="ea-accordion-panel--content">
<InnerBlocks.Content />
</div>
</div>
Share
Improve this question
edited Sep 30, 2021 at 14:08
Djave
asked Sep 30, 2021 at 9:23
DjaveDjave
2584 silver badges25 bronze badges
3
|
1 Answer
Reset to default 0I had checked everywhere except the save
function (see updated question). I updated it as follows:
<div {...blockProps}>
<div className="ea-accordion-panel--heading">
<RichText.Content tagName="h2" value={attributes.heading} />
</div>
<div className="ea-accordion-panel--content">
<InnerBlocks.Content />
</div>
</div>
本文标签: Store data from nested block of gutenberg
版权声明:本文标题:Store data from nested block of gutenberg 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741292486a2370628.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
RichText.Content
so wasn't saving it :facepalm. Thank you, your comment helped me fix it. – Djave Commented Sep 30, 2021 at 13:10