Adaptable

#Context Menu

The context menu is the menu the grid shows when the user right-clicks a cell. Adaptable ships a built-in menu, and you can replace or extend it with your own menus — one default menu for all views, or a different menu per view.

Context menus are plain state: you define them under contextMenu.availableMenus and reference them by id from your views.

#Defining context menus

Each key in contextMenu.availableMenus is the id of a menu; the value is its definition — a label and a getMenuItems function that returns the items to show.

Defining context menus in state
contextMenu: {
  availableMenus: {
    'simple-menu': {
      label: 'Simple menu',
      getMenuItems: ({ items }) => [...items, { key: 'hello', label: 'Hello' }],
    },
    'export-menu': {
      label: 'Export menu',
      getMenuItems: () => [
        {
          key: 'export',
          label: 'Export',
          // a submenu — nesting can go multiple levels deep
          menu: {
            items: [
              { key: 'excel', label: 'Export to Excel' },
              { key: 'csv', label: 'Export to CSV' },
            ],
          },
        },
      ],
    },
  },
},

getMenuItems runs every time the user right-clicks, so the items can depend on the cell that was clicked. It receives a single object with:

  • adaptableApi — the Adaptable API.
  • cellValue — the value of the clicked cell.
  • column — the Adaptable column that was clicked. undefined when the user clicked outside a cell.
  • currentView — the current view, computed (it carries more than the view definition in state).
  • items — the menu items Adaptable would show by default. Return them as they are, extend them, or leave them out.
  • menu — the context menu definition being rendered.
  • rowInfo — the Infinite Table row info of the clicked row (use rowInfo.isGroupRow to tell group rows apart). undefined when the user clicked outside a row.

The items are Infinite Table menu items: objects with a key and a label, plus optional disabled, description, menu (a submenu with its own items) and onAction, or the '-' separator.

What you return decides what the user sees:

  • an array of items — that menu is shown;
  • an empty array — no menu at all, not even the browser's;
  • null — no Adaptable menu, so the browser's own context menu opens.
Tip

Put anything that repeats across menus in contextMenu.menuDefaults — it is merged under every menu definition, and each menu's own properties win.

#Responding to clicks

Use the item's onAction to react to the user picking it. Because the item is created inside getMenuItems, onAction has access to the clicked cell — no lookups needed.

Actions have access to the cell where the user clicked
getMenuItems: ({ items, cellValue, column, rowInfo }) => [
  ...items,
  '-',
  {
    key: 'show-cell-value',
    label: 'Show cell value',
    onAction: ({ hideMenu }) => {
      hideMenu();
      if (!column || !rowInfo) {
        return;
      }
      alert(`"${cellValue}" (column "${column.id}", row ${rowInfo.id})`);
    },
  },
],

The menu stays open after an action unless you call hideMenu() (as above) or set hideMenuOnAction: true on the item.

#A default context menu for all views

To use the same menu in every view, set view.viewDefaults.contextMenu to the id of a menu from availableMenus.

Using a default context menu
const defaultState: AdaptableUserState = {
  view: {
    currentViewId: 'developers',
    // used by every view that doesn't set its own contextMenu
    viewDefaults: { contextMenu: 'cell-menu' },
    views: [{ id: 'developers', label: 'Developers' }],
  },
  contextMenu: {
    availableMenus: {
      'cell-menu': {
        label: 'Cell menu',
        // getMenuItems: ...
      },
    },
  },
};
#
The same context menu in every view

Right-click any cell: the menu keeps the built-in items and adds Show cell value. Switch to the By country view with the view picker — it uses the same menu, because it comes from viewDefaults.

open
loading demo…

#A view-specific context menu

To override the default for one view, set that view's contextMenu property to another menu id. A view's own contextMenu always wins over viewDefaults.contextMenu; a view with neither shows the built-in menu.

#
Overriding the context menu per view

The By language view is grouped and uses its own menu, which starts with a Group row info item — enabled only when you right-click a group row. The Developers view falls back to the default menu from viewDefaults.

open
loading demo…

#The built-in items

The items passed to getMenuItems are what Adaptable shows when no menu is configured:

  • Show Settings — opens the settings panel.
  • Export — a submenu listing the export reports and their destinations (disabled for users without the export_run permission — see Permissioning).
  • Remove Flashing — shown on a cell that is currently flashing (see Flashing Cells).
  • Add note / Show notes / Delete notes — on a cell, from the Cell Notes module.

Modules excluded via the modules prop don't contribute items.