Creating Custom Context Menu Items
Summary
- Bespoke Menu Items can be added to the Context Menu by developers
Developers can provide bespoke menu items in the Context Menu.
This is done using the customContextMenu callback in Context Menu Options.
Each item is a UserContextMenuItem with properties such as label, icon, hidden, disabled, onClick, and subMenuItems.
Deep Dive
Anatomy of a Custom Context Menu Item
Developer Guide
Providing a Standard Custom Context Menu Item
A few steps are required to define a standard Context Menu item (no frameworkComponent):
menuType: 'User',
label: (menuContext: ContextMenuContext) => {
return menuContext.adaptableApi.dashboardApi.isDashboardCollapsed()
? 'Expand Dashboard'
: 'Collapse Dashboard';
},The menuType is always 'User'.
The label property provides the menu item text. It can be a string or a function that receives ContextMenuContext and returns a string.
onClick: (menuContext: ContextMenuContext) => {
menuContext.adaptableApi.dashboardApi.isDashboardCollapsed()
? menuContext.adaptableApi.dashboardApi.expandDashboard()
: menuContext.adaptableApi.dashboardApi.collapseDashboard();
},The onClick function runs when the user selects the item. It receives ContextMenuContext and returns void.
hidden: menuContext.isRowGroupColumn,
disabled: menuContext.adaptableColumn?.dataType == 'date',Use hidden and disabled to control visibility and whether the item can be selected.
icon: {
name: 'theme',
},Supply an AdaptableIcon on the icon property.
Find Out More
See Guide to Adaptable Icons for full details on icons
subMenuItems: [
{
menuType: 'User',
label: 'Hide Dashboard',
onClick: (menuContext: ContextMenuContext) => {
menuContext.adaptableApi.dashboardApi.hideDashboard();
},
},
{
menuType: 'User',
label: 'Float Dashboard',
onClick: (menuContext: ContextMenuContext) => {
menuContext.adaptableApi.dashboardApi.floatDashboard();
},
},
],Each menu item can include subMenuItems for nested menus. Each sub-item is another UserContextMenuItem.
When the Dashboard is visible, use a Dashboard parent with Hide and Float sub items. When it is hidden (dashboardApi.isDashboardHidden()), the demo uses a single Show Dashboard item that calls showDashboard().
contextMenuOptions: {
customContextMenu: (context: CustomContextMenuContext) => {
return [
changeThemeMenuItem,
dashboardMenuItem,
'-',
...context.defaultAgGridMenuStructure,
'-',
...context.defaultAdaptableMenuStructure,
];
},
},The customContextMenu callback receives AG Grid and AdapTable menu structures on context. Build items such as changeThemeMenuItem and dashboardMenuItem, then place them where you want in the Context Menu (often before the default blocks).
- Show Dark Theme / Show Light Theme — single action at the top of the Context Menu; hidden on row-group columns; disabled for
datecolumns - Dashboard — Show Dashboard when the Dashboard is hidden; otherwise a sub menu with Hide and Float
- Default AG Grid and AdapTable menu items remain below