I am looking for a way to get the same result as the Standard Gutenberg block "Table" for my new custom Gutenberg block.
I want that inside the fields are seen as inside a form and that there is a button that if clicked will save the fields and show the preview even if you reload the page.
Currently this is my code and I'm stuck because I can't find much documentation about it as to what is the best way to go about it:
const { registerBlockType } = wp.blocks;
const { RichText, BlockControls, AlignmentToolbar, InspectorControls } =
wp.blockEditor;
const {
Placeholder,
Toolbar,
Modal,
Button,
PanelBody,
PanelRow,
SelectControl,
CheckboxControl,
TextControl,
} = wp.components;
const { useState } = wp.element;
const { withSelect } = wp.data;
// Registering A Block
registerBlockType("hsbg/hsbg-table", {
title: "HSBG Tabella",
category: "heisenberg",
icon: "superhero",
description: "First Hello World Block",
keyword: ["test", "searchme"],
attributes: {
columns: {
type: "number",
default: 2,
},
selectedCategoryTag: {
type: "string",
default: "",
},
maxRowsToShow: {
type: "number",
default: 5,
},
},
supports: {
align: ["wide", "full"],
},
// Block Edit Function
edit: withSelect((select) => {
return {
// Send a GET query to the REST API.
categories: select("core").getEntityRecords("taxonomy", "category", {
per_page: -1, // Otteniamo tutte le categorie
hide_empty: true, // Nascondiamo le categorie vuote
}),
tags: select("core").getEntityRecords("taxonomy", "post_tag", {
per_page: -1, // Otteniamo tutti i tag
hide_empty: true, // Nascondiamo i tag vuoti
}),
};
})(({ categories, tags, className, attributes, setAttributes }) => {
const [isPreviewShown, setIsPreviewShown] = useState(false);
const [formValues, setFormValues] = useState({
selectedCategoryTag: attributes.selectedCategoryTag,
columns: attributes.columns,
maxRowsToShow: attributes.maxRowsToShow,
});
// Wait for categories and tags to be returned.
if (!categories || !tags) {
return "Loading...";
}
// If no categories and tags are returned.
if (categories && categories.length === 0 && tags && tags.length === 0) {
return "No taxonomy.";
}
const categoryOptions = categories
? categories.map((category) => ({
label: category.name,
value: category.id,
}))
: [];
const tagOptions = tags
? tags.map((tag) => ({
label: tag.name,
value: tag.id,
}))
: [];
const categoryTagOptions = [
{ label: "--- Select category ---", value: "" },
...categoryOptions,
{ label: "--- Select tag ---", value: "" },
...tagOptions,
];
const handleInputChange = (key, value) => {
setFormValues({ ...formValues, [key]: value });
};
const handlePreviewClick = () => {
setIsPreviewShown(true);
setAttributes({
selectedCategoryTag: formValues.selectedCategoryTag,
columns: formValues.columns,
maxRowsToShow: formValues.maxRowsToShow,
});
};
const handleCancelClick = () => {
setIsPreviewShown(false);
};
return (
<div className={`${className} container`}>
{!isPreviewShown ? (
<Placeholder
icon="editor-table"
label="HSBG Tabella"
instructions="Settings data for the block."
>
<SelectControl
label="View"
value={attributes.selectedCategoryTag}
options={categoryTagOptions}
onChange={(value) =>
setAttributes({ selectedCategoryTag: value })
}
/>
<TextControl
label="Columns number"
type="number"
value={attributes.columns}
onChange={(value) => setAttributes({ columns: parseInt(value) })}
/>
<TextControl
label="Rows number"
type="number"
value={attributes.maxRowsToShow}
onChange={(value) =>
setAttributes({ maxRowsToShow: parseInt(value) })
}
/>
<Button variant="primary">Create table</Button>
</Placeholder>
) : (
<p>Result!</p>
)}
<InspectorControls>
<PanelBody title="Settings" initialOpen={true}>
<PanelRow>
<SelectControl
label="View"
value={attributes.selectedCategoryTag}
options={categoryTagOptions}
onChange={(value) =>
setAttributes({ selectedCategoryTag: value })
}
/>
</PanelRow>
<PanelRow>
<TextControl
label="Columns number"
type="number"
value={attributes.columns}
onChange={(value) =>
setAttributes({ columns: parseInt(value) })
}
/>
</PanelRow>
<PanelRow>
<TextControl
label="Rows number"
type="number"
value={attributes.maxRowsToShow}
onChange={(value) =>
setAttributes({ maxRowsToShow: parseInt(value) })
}
/>
</PanelRow>
</PanelBody>
</InspectorControls>
</div>
);
}),
//Block Save Function
save: (props) => {
return (
<div>
<p>Good!</p>
</div>
);
},
});
Thank you to those who take the time to answer me, really appreciated!