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';
},
1
Provide a Label and Menu Type

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();
},
2
Supply an onClick function

The onClick function runs when the user selects the item. It receives ContextMenuContext and returns void.

hidden: menuContext.isRowGroupColumn,
disabled: menuContext.adaptableColumn?.dataType == 'date',
3
Specify whether the Menu Item should be Hidden or Disabled

Use hidden and disabled to control visibility and whether the item can be selected.

icon: {
  name: 'theme',
},
4
Provide an Icon

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();
    },
  },
],
5
Add Sub Menu Items (if required)

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,
    ];
  },
},
6
Return the items from customContextMenu

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).

Context Menu: Custom Menu Items
Fork
  • Show Dark Theme / Show Light Theme — single action at the top of the Context Menu; hidden on row-group columns; disabled for date columns
  • DashboardShow Dashboard when the Dashboard is hidden; otherwise a sub menu with Hide and Float
  • Default AG Grid and AdapTable menu items remain below