registerToolbarItem
Description
Registers a React component under a given type name so it can be referenced from Toolbar items. After registration, the toolbar will instantiate the provided component when an item uses the corresponding comp/type identifier.
Usage
registerToolbarItem(type: string, handler: React.ComponentType<any>): void;
Parameters
The function accepts the following parameters:
-
type- the unique name that identifies the custom toolbar component. -
handler- the React component that implements the custom toolbar item
Examples
Register custom component as toolbar's element
import { Toolbar, registerToolbarItem } from "@svar-ui/react-toolbar";
import { Text } from "@svar-ui/react-core";
// register custom component
registerToolbarItem("text", Text);
<Toolbar
items={[
{ id: "label", text: "Toolbar with text input" },
{ id: "search", comp: "text" },
]}
/>
Create custom button control for toolbar
In MyControl.jsx
export default function MyControl({ menu, text, onClick }) {
return (
<button className={menu ? "menu" : ""} onClick={onClick}>
{text}
</button>
);
}
.my-control.css
.menu {
font-weight: bold;
}
- custom control can fire
onClickevent on activation menuproperty will be set to true when the component is inside of a popup menu- any other property set in the
itemobject will be available as a prop
In App.jsx:
import { Toolbar, registerToolbarItem } from "@svar-ui/react-toolbar";
import MyControl from "./MyControl.jsx";
// register custom component
registerToolbarItem("cb", MyControl);
<Toolbar
items={[
{ id: "label", text: "Toolbar with custom button" },
{ id: "search", comp: "cb" },
]}
/>
Create custom input control for toolbar
In MyControl.jsx:
export default function MyControl({ value, onChange, type = "text" }) {
return (
<input
type={type}
value={value}
onChange={(e) => onChange(e.target.value)}
/>
);
}
valueproperty will contain initial value- changes to value must be propagated via
onChangeand will be reflected in the toolbar's state - any other property set in the
itemobject will be available as a prop
In App.jsx:
import { Toolbar, registerToolbarItem } from "@svar-ui/react-toolbar";
import MyControl from "./MyControl.jsx";
// register custom component
registerToolbarItem("ci", MyControl);
<Toolbar
items={[
{ id: "label", text: "Toolbar with custom input" },
{ id: "search", comp: "ci", type: "search" },
]}
/>