You can extend the form builder with custom components using the Registry from react-form-builder2.
1. Define the component
For components that need to interact with form state, use React.forwardRef to pass the ref and props (like name, defaultValue, disabled).
2. Register the component
Use Registry.register(name, component) to add it to the library.
3. Add to Toolbar
Define a new item in your toolbarItems array with type: "custom". If the component uses a ref, set forwardRef: true.
4. Use in Builder
Pass the updated toolbarItems to ReactFormBuilder.
import { ReactFormBuilder, ElementStore, Registry } from "react-form-builder2";
// 1. Define
const MyInput = React.forwardRef((props, ref) => {
const { name, defaultValue, disabled } = props;
return (
<input
ref={ref}
name={name}
defaultValue={defaultValue}
disabled={disabled}
/>
);
});
// 2. Register
Registry.register("MyInput", MyInput);
// 3. Define Toolbar Item
const items = [
{
key: "MyInput",
element: "CustomElement",
component: MyInput,
type: "custom",
forwardRef: true,
field_name: "my_input_",
name: "My Input",
icon: "fa fa-cog",
props: { test: "test_input" },
label: "Label Input",
},
];
// 4. Use
<ReactFormBuilder toolbarItems={items} />