Skip to main content

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 onClick event on activation
  • menu property will be set to true when the component is inside of a popup menu
  • any other property set in the item object 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)}
/>
);
}
  • value property will contain initial value
  • changes to value must be propagated via onChange and will be reflected in the toolbar's state
  • any other property set in the item object 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" },
]}
/>