Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/components/datagrid/body/data_grid_body.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { DataGridSortingContext } from '../data_grid_context';
import { schemaDetectors } from '../data_grid_schema';
import { RowHeightUtils } from '../row_height_utils';

import { EuiDataGridBody } from './data_grid_body';
import { EuiDataGridBody, getParentCellContent } from './data_grid_body';

describe('EuiDataGridBody', () => {
const requiredProps = {
Expand Down Expand Up @@ -118,4 +118,39 @@ describe('EuiDataGridBody', () => {
});

// TODO: Test tabbing in Cypress

// TODO: Test column resizing in Cypress
});

describe('getParentCellContent', () => {
const doc = document.createDocumentFragment();

const body = document.createElement('body');
doc.appendChild(body);

const cell = document.createElement('div');
cell.setAttribute('data-datagrid-cellcontent', 'true');
body.appendChild(cell);

const span = document.createElement('span');
span.textContent = 'Here comes the text';
cell.appendChild(span);

const text = span.childNodes[0];

it('locates the cell element when starting with the cell itself', () => {
expect(getParentCellContent(cell)).toBe(cell);
});

it('locates the cell element when starting with an element inside the cell', () => {
expect(getParentCellContent(span!)).toBe(cell);
});

it('locates the cell element when starting with a text node inside the cell', () => {
expect(getParentCellContent(text!)).toBe(cell);
});

it('does not locate the cell element when starting outside the cell', () => {
expect(getParentCellContent(body)).toBeNull();
});
});
14 changes: 10 additions & 4 deletions src/components/datagrid/body/data_grid_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,16 @@ const IS_JEST_ENVIRONMENT = global.hasOwnProperty('_isJest');
* and search its ancestors for a div[data-datagrid-cellcontent], if any,
* which is a valid target for disabling tabbing within
*/
function getParentCellContent(_element: Node | HTMLElement) {
export function getParentCellContent(_element: Node | HTMLElement) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export for testing

let element: HTMLElement | null =
_element.nodeType === document.ELEMENT_NODE
? (_element as HTMLElement)
: _element.parentElement;

while (
element &&
element.nodeName !== 'div' &&
element.hasAttribute('data-datagrid-cellcontent')
element && // we haven't walked off the document yet
element.nodeName !== 'div' && // looking for a div
!element.hasAttribute('data-datagrid-cellcontent') // that has data-datagrid-cellcontent
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ! is the main change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♀️ I feel super bad for not noticing this/digging into it a bit more deeply in the first PR. Apologies!

) {
element = element.parentElement;
}
Expand Down Expand Up @@ -592,10 +592,16 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = (
}, [unconstrainedHeight, wrapperDimensions, isFullScreen]);

const preventTabbing = useCallback((records: MutationRecord[]) => {
// multiple mutation records can implicate the same cell
// so be sure to only check each cell once
const processedCells = new Set();

for (let i = 0; i < records.length; i++) {
const record = records[i];
// find the cell content owning this mutation
const cell = getParentCellContent(record.target);
if (processedCells.has(cell)) continue;
processedCells.add(cell);

if (cell) {
// if we found it, disable tabbable elements
Expand Down