Hey, I want to add new row to
type: blocksfield(slug: 'content').
const [fields, dispatch] = useAllFormFields();
dispatch({
type: "ADD_ROW",
path: "content",
rowIndex: 0,
blockType: "h2",
});but I have no idea how it should work. I also have h2 block:
export const H2Block: Block = {
slug: "h2",
fields: [
{
name: "text",
type: "text",
required: true,
},
],
};You would basically need to do what this function is doing here:
https://github.com/payloadcms/payload/blob/master/src/admin/components/forms/field-types/Blocks/index.tsx#L100Which means you will need to copy the buildStateFromSchema folder as well - I think we could export the buildStateFromSchema so you could import it though 🤔
import React from "react";
import { useAllFormFields } from "payload/components/forms";
import { H2Block } from "../../blocks/H2Block";
import { useTranslation } from "react-i18next";
import buildStateFromSchema from "payload/dist/admin/components/forms/Form/buildStateFromSchema";
import { useOperation } from "payload/dist/admin/components/utilities/OperationProvider";
import { useAuth } from "payload/dist/admin/components/utilities/Auth";
import { useForm } from "payload/dist/admin/components/forms/Form/context";
import { useLocale } from "payload/dist/admin/components/utilities/Locale";
import { useDocumentInfo } from "payload/dist/admin/components/utilities/DocumentInfo";
import reducer from "payload/dist/admin/components/forms/field-types/rowReducer";
export const CustomField: React.FC<{ path: string }> = ({ path }) => {
const [value, setValue] = React.useState("");
const formContext = useForm();
const operation = useOperation();
const { user } = useAuth();
const { id } = useDocumentInfo();
const locale = useLocale();
const { t } = useTranslation("fields");
const [rows, dispatchRows] = React.useReducer(reducer, undefined);
async function handleChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
setValue(e.target.value);
const subFieldState = await buildStateFromSchema({
fieldSchema: H2Block.fields,
operation,
user,
locale,
id,
t,
});
formContext.dispatchFields({
type: "ADD_ROW",
path: "content",
rowIndex: 0,
blockType: H2Block.slug,
subFieldState,
});
dispatchRows({ type: "ADD", rowIndex: 0, blockType: H2Block.slug });
}
return <textarea onChange={handleChange} value={value} />;
};it doesn't work :/
ah rip
I did not see that the blocks component is dispatching its rows to an internal state
what if you use
formContext.setModified(true)after dispatching fields? (For clarity, you can remove
const [rows, dispatchRows]state)
nope, still nothing
but it show this message on reload so setModified did work
for context, I try to use this on Post collection with fields:
{
name: "content",
type: "blocks",
blocks: [H2Block, ParagraphBlock],
}Unfortunately I do not think it is possible at the moment, but does sound like an improvement to allow for this to happen. The reason it is not possible is because the blocks field handles its own row state internally, you can see here how it loads rows in this effect:
https://github.com/payloadcms/payload/blob/master/src/admin/components/forms/field-types/Blocks/index.tsx#L193But I would guess, that you with what you have right now, it is adding the data to the document how you would expect (just not visually). What happens if you type in your textarea and then save the doc and reload? Does your data show up in the correct place after page reload?
Cast to embedded failed for value "1" (type number) at path "content" because of "ObjectParameterError"on save
Any chance for fix, this or next week?
hey, please let me know on this one. I'll fork it and try to fix myself if your team have no free slots to work on this. Thanks!
I don't think I can guarantee a fix for this at the moment, because the right solution would be to determine if there is a way to allow for blocks to be re-organized from outside of their internal state. Which is not really an easy feat, we would need to lift the state out of the blocks, but then ensure that it works in all scenarios, i.e. sibling block fields, nested block fields, etc. And allowing it to reorder after save seems like a niche use-case imo. But this will likely be revisited when we tackle abstracting form fields so they can be used in custom components.
Gotcha, thanks for reply 🍻
Star
Discord
online
Get dedicated engineering support directly from the Payload team.