Keyboard use of in-cell widgets
The grid keeps keyboard focus in one place, the grid root, and moves the current cell separately. So Tab does not go straight into a link or button inside a cell. Instead there are keys that step into the cell.
| Key | Action |
|---|---|
| Arrow keys and so on | Select a cell, as before |
| Enter or F2 | Steps into the first element in the selected cell |
| Tab, Shift+Tab | Moves in the browser's normal order if the cell has more elements |
| Enter or Space | Presses that element (opens a link, presses a button, toggles a switch) |
| Keys the widget sets | Keys a widget handles itself, such as Left and Right in the rating example, work inside the widget |
| Esc | Returns to the grid. The same cell stays selected |
The rules are the same for built-in and custom renderers. Press the widgets with only the keyboard in the gallery examples Switch cells, Rating stars and Buttons inside a row.
Where you step in
You step into the first element in the cell that can take keyboard input. That means a link (a[href]), an enabled button, an element whose role is switch, radio, checkbox or button, and an element whose tabindex is not -1.
In a group such as stars, where one element is the Tab stop (tabindex="0") and the rest are -1, you start on that stop. Use this to start on the star that matches the current score.
In editable cells
In a cell with editable, Enter and F2 start editing. There is no key to step into such a cell's widget, so avoid putting a button in an editable cell. In a cell that is not edited, the rule above applies as is (Cell editing).
Shift+Enter and Enter with Ctrl or Alt do not step in. Shift+F2 opens the cell note editor.
Per column you can leave a column out of keyboard movement (navigable: false) or intercept keys (suppressKey). See Choose key navigation while editing.
Edit start button
Where double-click or F2 is hard to use, such as on a touch screen, you can put a small pencil button at the end of a cell. Give the column editButton.
| Value | When the button shows |
|---|---|
'selected' | The focused (selected) cell |
'hover' | The cell under the pointer, or the focused cell; with a coarse pointer (pointer: coarse, such as touch) it behaves like selected |
'always' | Every editable cell |
- It shows only when that cell can be edited right now. It is absent on cells that are not editable, on soft-removed rows, and on cells whose previous edit is still pending. On a column with
editable: falseit never shows, without an error. - Cells with a checkbox, switch, button or link renderer do not get one, because they have their own click target. In merged cells only the first cell gets it.
- Pressing it focuses the cell and opens the editor like F2 or Enter (
triggerofeditStartedis'button'). AbeforeEdithook can veto it. It does not extend the selection or turn into a double-click. - The button is not a tab stop (
tabindex="-1"); keyboard users press F2 or Enter. Its name follows the grid language, for example "Edit Name". - The cell gets the class
mxg-has-edit-btnonly while the button shows, which keeps the text from running under it. The target is at least 32px, and at least 44px with a coarse pointer. Change the icon withicons.edit.
{ id: 'memo', editable: true, editButton: 'selected' }Gallery example: edit-button
Keys that add and remove rows
With keyboardRows on (1.5.0), the grid also takes Insert, Ctrl(⌘)+Plus and Ctrl+Delete (⌘+Backspace on Mac). If a widget needs those keys, return true from hooks.onKeyDown. The keys and rules are in Add and remove rows with the keyboard.
What a custom widget must do
For widgets built with Cells from components or a cellRenderer function:
- Use elements that already work with the keyboard:
a[href]for links and<button type="button">for buttons. Adivwith a click handler cannot be stepped into. - Give elements with another look, such as switches and checkmarks, a
roleandtabindex="0", and let Space and Enter toggle them. Report the value witharia-checked. - Give each one an accessible name. The same button repeats on every row, so put a value in
aria-labelthat identifies the row, such as an invoice number. - Do not intercept Esc in the widget. The grid receives it and returns to the grid.
- Receive presses through the
cellClickedevent instead of handling keys inside the widget. That follows the same path as a mouse click, so one handler covers both.
Control focus and movement yourself
The default movement stays as is; you step in only where you need to (1.3.6). If a hook throws, the default behavior runs, so focus is never trapped.
| Hook | Receives | Returns |
|---|---|---|
nextCell | from, direction (next prev up down left right), event | A cell { rowKey, columnId }: go there, null: stay, nothing: default |
nextHeader | from, direction (next prev), event | A column id, null: stay, nothing: default |
tabOut | direction, event | true: Tab does not leave the grid and the key counts as handled |
entryFocus | direction (next prev click api) | A cell, a header column id, null: default |
hooks: {
nextCell: ({ from, direction }) =>
direction === 'down' && from?.columnId === 'memo' ? { rowKey: 1, columnId: 'name' } : undefined,
tabOut: ({ direction }) => (direction === 'next' ? document.getElementById('save')?.focus() ?? true : undefined),
}From code use clearFocus(), moveFocus(direction) (true when it moved), focusHeader(columnId), getFocusedHeader() and scrollToColumn(columnId, { align }). align is start, center, end or nearest (default); pinned columns are left alone.
A header gets focus only from code (focusHeader) or from entryFocus, and it never adds a tab stop. On a focused header, Left and Right move to the neighbor header, Down goes to the first cell of that column, Enter and Space act like a header click (sort toggle) and Escape clears the focus. The header gets the mxg-hcell-focused class and the headerFocused { columnId } event fires.