This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
[terra-data-grid] Flowsheet keyboard navigation #1927
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
19bd77e
[terra-data-grid] Flowsheet Keyboard Navigation
cm9361 a999f2e
Merge remote-tracking branch 'origin' into flowsheet-keyboard-navigation
cm9361 e12bd27
[terra-data-grid] Flowsheet Keyboard Navigation
cm9361 ffa90cc
Fix navigation issues with collapsed sections
cm9361 c895c5f
Merge remote-tracking branch 'origin' into flowsheet-keyboard-navigation
cm9361 b1ba82e
Merge remote-tracking branch 'origin' into flowsheet-keyboard-navigation
cm9361 cf7d879
Fix test failures
cm9361 939d54f
Update JEST snapshots.
cm9361 e80ec80
Merge remote-tracking branch 'origin' into flowsheet-keyboard-navigation
cm9361 8c2df99
Merge remote-tracking branch 'origin' into flowsheet-keyboard-navigation
cm9361 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,7 +202,6 @@ const DataGrid = forwardRef((props, ref) => { | |
// if columns are not visible then set the first selectable row index to 1 | ||
const [focusedRow, setFocusedRow] = useState(hasVisibleColumnHeaders ? 0 : 1); | ||
const [focusedCol, setFocusedCol] = useState(0); | ||
const [gridHasFocus, setGridHasFocus] = useState(false); | ||
|
||
// Aria live region message management | ||
const [cellAriaLiveMessage, setCellAriaLiveMessage] = useState(null); | ||
|
@@ -221,6 +220,10 @@ const DataGrid = forwardRef((props, ref) => { | |
hasSelectableRows && columnIndex < displayedColumns.length && displayedColumns[columnIndex].id === WorklistDataGridUtils.ROW_SELECTION_COLUMN.id | ||
), [displayedColumns, hasSelectableRows]); | ||
|
||
const isSection = useCallback((rowIndex) => ( | ||
grid.current.rows[rowIndex].hasAttribute('data-section-id') | ||
), []); | ||
|
||
const setFocusedRowCol = useCallback((newRowIndex, newColIndex, makeActiveElement) => { | ||
setCellAriaLiveMessage(null); | ||
setFocusedRow(newRowIndex); | ||
|
@@ -232,20 +235,29 @@ const DataGrid = forwardRef((props, ref) => { | |
}; | ||
|
||
if (makeActiveElement) { | ||
// Set focus on input field (checkbox) of row selection cells. | ||
let focusedCell = grid.current.rows[newRowIndex].cells[newColIndex]; | ||
if (isRowSelectionCell(newColIndex) && focusedCell.getElementsByTagName('input').length > 0) { | ||
[focusedCell] = focusedCell.getElementsByTagName('input'); | ||
} | ||
let focusedCell; | ||
if (isSection(newRowIndex)) { | ||
[focusedCell] = grid.current.rows[newRowIndex].cells; | ||
|
||
if (!focusedCell.hasAttribute('tabindex')) { | ||
focusedCell = grid.current.rows[newRowIndex].querySelector('button'); | ||
} | ||
} else { | ||
// Set focus on input field (checkbox) of row selection cells. | ||
focusedCell = grid.current.rows[newRowIndex].cells[newColIndex]; | ||
if (isRowSelectionCell(newColIndex) && focusedCell.getElementsByTagName('input').length > 0) { | ||
[focusedCell] = focusedCell.getElementsByTagName('input'); | ||
} | ||
|
||
// Set focus to column header button, if it exists | ||
if (newRowIndex === 0 && !focusedCell.hasAttribute('tabindex')) { | ||
focusedCell = focusedCell.querySelector('[role="button"]'); | ||
// Set focus to column header button, if it exists | ||
if (newRowIndex === 0 && !focusedCell.hasAttribute('tabindex')) { | ||
focusedCell = focusedCell.querySelector('[role="button"]'); | ||
} | ||
} | ||
|
||
focusedCell?.focus(); | ||
} | ||
}, [isRowSelectionCell, displayedColumns]); | ||
}, [displayedColumns, isSection, isRowSelectionCell]); | ||
|
||
// The focus is handled by the DataGrid. However, there are times | ||
// when the other components may want to change the currently focus | ||
|
@@ -264,66 +276,44 @@ const DataGrid = forwardRef((props, ref) => { | |
// ------------------------------------- | ||
|
||
const handleMoveCellFocus = (fromCell, toCell) => { | ||
if (!isSection(toCell.row)) { | ||
// Obtain coordinate rectangles for grid container, column header, and new cell selection | ||
const gridContainerRect = tableContainerRef.current.getBoundingClientRect(); | ||
const columnHeaderRect = grid.current.rows[0].cells[toCell.col].getBoundingClientRect(); | ||
const nextCellRect = grid.current.rows[toCell.row].cells[toCell.col].getBoundingClientRect(); | ||
|
||
// Calculate horizontal scroll offset for right boundary | ||
if (nextCellRect.right > gridContainerRect.right) { | ||
tableContainerRef.current.scrollBy(nextCellRect.right - gridContainerRect.right, 0); | ||
} else { | ||
const gridContainerRect = tableContainerRef.current.getBoundingClientRect(); | ||
const columnHeaderRect = grid.current.rows[0].cells[toCell.col].getBoundingClientRect(); | ||
const nextCellRect = grid.current.rows[toCell.row].cells[toCell.col].getBoundingClientRect(); | ||
|
||
// Calculate horizontal scroll offset for right boundary | ||
if (nextCellRect.right > gridContainerRect.right) { | ||
tableContainerRef.current.scrollBy(nextCellRect.right - gridContainerRect.right, 0); | ||
} else { | ||
// Calculate horizontal scroll offset for left boundary | ||
let scrollOffsetX = 0; | ||
const pinnedColumnOffset = hasSelectableRows ? 1 : 0; | ||
const lastPinnedColumnIndex = pinnedColumns.length - 1 + pinnedColumnOffset; | ||
if (lastPinnedColumnIndex >= 0) { | ||
if (toCell.col > lastPinnedColumnIndex) { | ||
const lastPinnedColumnRect = grid.current.rows[toCell.row].cells[lastPinnedColumnIndex].getBoundingClientRect(); | ||
scrollOffsetX = nextCellRect.left - lastPinnedColumnRect.right; | ||
let scrollOffsetX = 0; | ||
const pinnedColumnOffset = hasSelectableRows ? 1 : 0; | ||
const lastPinnedColumnIndex = pinnedColumns.length - 1 + pinnedColumnOffset; | ||
if (lastPinnedColumnIndex >= 0) { | ||
if (toCell.col > lastPinnedColumnIndex) { | ||
const lastPinnedColumnRect = grid.current.rows[toCell.row].cells[lastPinnedColumnIndex].getBoundingClientRect(); | ||
scrollOffsetX = nextCellRect.left - lastPinnedColumnRect.right; | ||
} | ||
} else { | ||
scrollOffsetX = nextCellRect.left - gridContainerRect.left; | ||
} | ||
} else { | ||
scrollOffsetX = nextCellRect.left - gridContainerRect.left; | ||
} | ||
|
||
if (scrollOffsetX < 0) { | ||
tableContainerRef.current.scrollBy(scrollOffsetX, 0); | ||
if (scrollOffsetX < 0) { | ||
tableContainerRef.current.scrollBy(scrollOffsetX, 0); | ||
} | ||
} | ||
} | ||
|
||
// Calculate vertical scroll offset | ||
const scrollOffsetY = nextCellRect.top - columnHeaderRect.bottom; | ||
if (scrollOffsetY < 0) { | ||
tableContainerRef.current.scrollBy(0, scrollOffsetY); | ||
// Calculate vertical scroll offset | ||
const scrollOffsetY = nextCellRect.top - columnHeaderRect.bottom; | ||
if (scrollOffsetY < 0) { | ||
tableContainerRef.current.scrollBy(0, scrollOffsetY); | ||
} | ||
} | ||
|
||
setFocusedRowCol(toCell.row, toCell.col, true); | ||
}; | ||
|
||
const handleColumnSelect = useCallback((columnId) => { | ||
const columnIndex = displayedColumns.findIndex(column => column.id === columnId); | ||
setFocusedRowCol(0, columnIndex); | ||
|
||
if (onColumnSelect) { | ||
onColumnSelect(columnId); | ||
} | ||
}, [onColumnSelect, displayedColumns, setFocusedRowCol]); | ||
|
||
const handleRowSelectionHeaderSelect = useCallback(() => { | ||
setFocusedRowCol(0, 0); | ||
if (onRowSelectionHeaderSelect) { | ||
onRowSelectionHeaderSelect(); | ||
} | ||
}, [onRowSelectionHeaderSelect, setFocusedRowCol]); | ||
|
||
const handleCellSelection = useCallback((selectionDetails) => { | ||
const { columnIndex, rowIndex } = selectionDetails; | ||
setFocusedRowCol(rowIndex, columnIndex); | ||
if (onCellSelect) { | ||
onCellSelect(selectionDetails); | ||
} | ||
}, [onCellSelect, setFocusedRowCol]); | ||
|
||
// ------------------------------------- | ||
// event handlers | ||
|
||
|
@@ -388,17 +378,26 @@ const DataGrid = forwardRef((props, ref) => { | |
setCheckResizable(false); | ||
|
||
const targetElement = event.target; | ||
const key = event.keyCode; | ||
|
||
// Allow default behavior if the event target is an editable field | ||
|
||
if (event.keyCode !== KeyCode.KEY_TAB | ||
if (key !== KeyCode.KEY_TAB | ||
&& (isTextInput(targetElement) | ||
|| ['textarea', 'select'].indexOf(targetElement.tagName.toLowerCase()) >= 0 | ||
|| (targetElement.hasAttribute('contentEditable') && targetElement.getAttribute('contentEditable') !== false))) { | ||
return; | ||
} | ||
|
||
const key = event.keyCode; | ||
// Disable horizontal navigation when section has focus | ||
if ((key === KeyCode.KEY_RIGHT || key === KeyCode.KEY_LEFT) | ||
&& isSection(cellCoordinates.row)) { | ||
event.preventDefault(); | ||
return; | ||
} | ||
|
||
// Get grid row count | ||
const gridRowCount = grid.current.rows.length; | ||
|
||
switch (key) { | ||
case KeyCode.KEY_UP: | ||
nextRow -= 1; | ||
|
@@ -432,7 +431,7 @@ const DataGrid = forwardRef((props, ref) => { | |
if (event.ctrlKey) { | ||
// Mac: Ctrl + Cmd + Right | ||
// Windows: Ctrl + End | ||
nextRow = rows.length; | ||
nextRow = gridRowCount - 1; | ||
} | ||
} else { | ||
// Right key | ||
|
@@ -450,7 +449,7 @@ const DataGrid = forwardRef((props, ref) => { | |
if (event.ctrlKey) { | ||
// Though rows are zero based, the header is the first row so the rowsLength will | ||
// always be one more than then actual number of data rows. | ||
nextRow = rows.length; | ||
nextRow = gridRowCount - 1; | ||
} | ||
break; | ||
case KeyCode.KEY_ESCAPE: | ||
|
@@ -476,7 +475,7 @@ const DataGrid = forwardRef((props, ref) => { | |
onCellRangeSelect(cellCoordinates.row, cellCoordinates.col, event.keyCode); | ||
} | ||
|
||
if (nextRow > rows.length || nextCol >= displayedColumns.length) { | ||
if (nextRow >= gridRowCount || nextCol >= displayedColumns.length) { | ||
event.preventDefault(); // prevent the page from moving with the arrow keys. | ||
return; | ||
} | ||
|
@@ -489,9 +488,20 @@ const DataGrid = forwardRef((props, ref) => { | |
event.preventDefault(); // prevent the page from moving with the arrow keys. | ||
}; | ||
|
||
const onMouseDown = () => { | ||
// Prevent focus event updates when triggered by mouse | ||
handleFocus.current = false; | ||
const handleMouseDown = (event) => { | ||
// Determine cell containing click event | ||
const clickTarget = event.target; | ||
const targetCell = clickTarget.closest('td, th'); | ||
|
||
// Store focused cell position | ||
if (targetCell) { | ||
// Prevent focus event updates when triggered by mouse | ||
handleFocus.current = false; | ||
|
||
setCheckResizable(false); | ||
|
||
setFocusedRowCol(targetCell.parentElement.rowIndex, targetCell.cellIndex); | ||
} | ||
}; | ||
|
||
/** | ||
|
@@ -522,31 +532,25 @@ const DataGrid = forwardRef((props, ref) => { | |
} | ||
|
||
setFocusedRowCol(newRowIndex, newColumnIndex, true); | ||
setGridHasFocus(true); | ||
} | ||
} | ||
|
||
handleFocus.current = true; | ||
}; | ||
|
||
const onBlur = (event) => { | ||
if (!event.currentTarget.contains(event.relatedTarget)) { | ||
setGridHasFocus(false); | ||
} | ||
}; | ||
|
||
// ------------------------------------- | ||
|
||
const isGridActive = grid.current?.contains(document.activeElement); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💪 Nice use of JS to track the grid active state! |
||
|
||
return ( | ||
// eslint-disable-next-line jsx-a11y/no-static-element-interactions | ||
<div | ||
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex | ||
tabIndex={0} | ||
ref={gridContainerRef} | ||
onKeyDown={handleKeyDown} | ||
onMouseDown={onMouseDown} | ||
onMouseDown={handleMouseDown} | ||
onFocus={onFocus} | ||
onBlur={onBlur} | ||
id={id} | ||
className={cx('data-grid-container')} | ||
> | ||
|
@@ -557,7 +561,7 @@ const DataGrid = forwardRef((props, ref) => { | |
sections={sections} | ||
ariaLabelledBy={ariaLabelledBy} | ||
ariaLabel={ariaLabel} | ||
activeColumnIndex={(gridHasFocus && focusedRow === 0) ? focusedCol : undefined} | ||
activeColumnIndex={(isGridActive && focusedRow === 0) ? focusedCol : undefined} | ||
isActiveColumnResizing={focusedRow === 0 && checkResizable} | ||
columnResizeIncrement={columnResizeIncrement} | ||
pinnedColumns={pinnedColumns} | ||
|
@@ -567,10 +571,10 @@ const DataGrid = forwardRef((props, ref) => { | |
rowHeight={rowHeight} | ||
rowHeaderIndex={rowHeaderIndex} | ||
onColumnResize={onColumnResize} | ||
onColumnSelect={handleColumnSelect} | ||
onColumnSelect={onColumnSelect} | ||
onSectionSelect={onSectionSelect} | ||
onCellSelect={handleCellSelection} | ||
onRowSelectionHeaderSelect={handleRowSelectionHeaderSelect} | ||
onRowSelectionHeaderSelect={onRowSelectionHeaderSelect} | ||
onCellSelect={onCellSelect} | ||
rowSelectionMode={hasSelectableRows ? 'multiple' : undefined} | ||
hasVisibleColumnHeaders={hasVisibleColumnHeaders} | ||
isStriped | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Will we have to account for scenarios where a cell/column header can be selected without having been reached from a mousedown or keyboard navigation? I would not think so but removing these could affect that scenario if it is a concern. I know that JAWS has a quick navigation ability so I was wondering about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This event only triggered for the mouse down and spacebar being pressed. I think this new implementation is equivalent.