Skip to content

Commit aabcd4d

Browse files
committed
fix(cellNav): Allow tabbing out of grid
Shift-tabbing at top-left and tabbing at bottom right will now all tabbing out of the grid to the next/previous element. This doesn't fix all the problems with cellnav but it shuld fix this one. Fixes #2339
1 parent 4c32e3d commit aabcd4d

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

src/features/cellnav/js/cellnav.js

+48-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
direction: {LEFT: 0, RIGHT: 1, UP: 2, DOWN: 3, PG_UP: 4, PG_DOWN: 5},
2020
EVENT_TYPE: {
2121
KEYDOWN: 0,
22-
CLICK: 1
22+
CLICK: 1,
23+
CLEAR: 2
2324
}
2425
});
2526

@@ -108,6 +109,10 @@
108109

109110
//get column to left
110111
if (nextColIndex > curColIndex) {
112+
// On the first row
113+
// if (curRowIndex === 0 && curColIndex === 0) {
114+
// return null;
115+
// }
111116
if (curRowIndex === 0) {
112117
return new RowCol(curRow, focusableCols[nextColIndex]); //return same row
113118
}
@@ -800,8 +805,8 @@
800805
</file>
801806
</example>
802807
*/
803-
module.directive('uiGridCellnav', ['gridUtil', 'uiGridCellNavService', 'uiGridCellNavConstants',
804-
function (gridUtil, uiGridCellNavService, uiGridCellNavConstants) {
808+
module.directive('uiGridCellnav', ['gridUtil', 'uiGridCellNavService', 'uiGridCellNavConstants', 'uiGridConstants',
809+
function (gridUtil, uiGridCellNavService, uiGridCellNavConstants, uiGridConstants) {
805810
return {
806811
replace: true,
807812
priority: -150,
@@ -829,6 +834,10 @@
829834
_scope.$broadcast(uiGridCellNavConstants.CELL_NAV_EVENT, newRowCol, modifierDown);
830835
};
831836

837+
uiGridCtrl.cellNav.clearFocus = grid.cellNav.clearFocus = function () {
838+
_scope.$broadcast(uiGridCellNavConstants.CELL_NAV_EVENT, { eventType: uiGridCellNavConstants.EVENT_TYPE.CLEAR });
839+
};
840+
832841
uiGridCtrl.cellNav.broadcastFocus = function (rowCol, modifierDown) {
833842
modifierDown = !(modifierDown === undefined || !modifierDown);
834843

@@ -870,6 +879,30 @@
870879
// Figure out which new row+combo we're navigating to
871880
var rowCol = uiGridCtrl.grid.renderContainers[containerId].cellNav.getNextRowCol(direction, lastRowCol.row, lastRowCol.col);
872881

882+
// Shift+tab on top-left cell should exit cellnav on render container
883+
if (
884+
// Navigating left
885+
direction === uiGridCellNavConstants.direction.LEFT &&
886+
// Trying to stay on same row
887+
rowCol.row === lastRowCol.row &&
888+
evt.keyCode === uiGridConstants.keymap.TAB &&
889+
evt.shiftKey
890+
) {
891+
uiGridCtrl.cellNav.clearFocus();
892+
return true;
893+
}
894+
// Tab on bottom-right cell should exit cellnav on render container
895+
else if (
896+
direction === uiGridCellNavConstants.direction.RIGHT &&
897+
rowCol.row === lastRowCol.row &&
898+
evt.keyCode === uiGridConstants.keymap.TAB &&
899+
!evt.shiftKey
900+
) {
901+
uiGridCtrl.cellNav.clearFocus();
902+
return true;
903+
}
904+
905+
873906
rowCol.eventType = uiGridCellNavConstants.EVENT_TYPE.KEYDOWN;
874907

875908
// Broadcast the navigation
@@ -1045,8 +1078,18 @@
10451078
evt.stopPropagation();
10461079
});
10471080

1081+
$elm.find('div').on('focus', function (evt) {
1082+
console.log('cellNav focus');
1083+
uiGridCtrl.cellNav.broadcastCellNav(new RowCol($scope.row, $scope.col), evt.ctrlKey || evt.metaKey);
1084+
});
1085+
10481086
// This event is fired for all cells. If the cell matches, then focus is set
10491087
$scope.$on(uiGridCellNavConstants.CELL_NAV_EVENT, function (evt, rowCol, modifierDown) {
1088+
if (evt.eventType === uiGridCellNavConstants.EVENT_TYPE.CLEAR) {
1089+
clearFocus();
1090+
return;
1091+
}
1092+
10501093
if (rowCol.row === $scope.row &&
10511094
rowCol.col === $scope.col) {
10521095
if (uiGridCtrl.grid.options.modifierKeysToMultiSelectCells && modifierDown &&
@@ -1058,6 +1101,7 @@
10581101

10591102
// This cellNav event came from a keydown event so we can safely refocus
10601103
if (rowCol.hasOwnProperty('eventType') && rowCol.eventType === uiGridCellNavConstants.EVENT_TYPE.KEYDOWN) {
1104+
console.log('focus from navEvent');
10611105
$elm.find('div')[0].focus();
10621106
}
10631107
}
@@ -1067,7 +1111,7 @@
10671111
});
10681112

10691113
function setTabEnabled() {
1070-
$elm.find('div').attr("tabindex", -1);
1114+
$elm.find('div').attr("tabindex", 0);
10711115
}
10721116

10731117
function setFocused() {

0 commit comments

Comments
 (0)