Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Fix getCellElement method not working with pinned columns #7844

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,17 @@ describe('<DataGridPro /> - Column pinning', () => {
expect(apiRef.current.isColumnPinned('currencyPair')).to.equal(false);
});
});

// See https://github.com/mui/mui-x/issues/7819
describe('`getCellElement` method should return cell element', () => {
it('should return the correct value', () => {
render(
<TestCase initialState={{ pinnedColumns: { left: ['id'], right: ['price16M'] } }} />,
);
const cellElement = apiRef.current.getCellElement(0, 'currencyPair');
expect(cellElement).not.to.equal(null);
});
});
});

describe('column menu', () => {
Expand Down
20 changes: 10 additions & 10 deletions packages/grid/x-data-grid/src/utils/domUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ export function getGridColumnHeaderElement(root: Element, field: string) {
`[role="columnheader"][data-field="${escapeOperandAttributeSelector(field)}"]`,
);
}
function getGridRowElementSelector(id: GridRowId): string {
return `.${gridClasses.row}[data-id="${escapeOperandAttributeSelector(String(id))}"]`;
}

export function getGridRowElement(root: Element, id: GridRowId) {
return root.querySelector<HTMLDivElement>(
`.${gridClasses.row}[data-id="${escapeOperandAttributeSelector(String(id))}"]`,
);
return root.querySelector<HTMLDivElement>(getGridRowElementSelector(id));
}

export function getGridCellElement(root: Element, { id, field }: { id: GridRowId; field: string }) {
const row = getGridRowElement(root, id);
if (!row) {
return null;
}
return row.querySelector<HTMLDivElement>(
`.${gridClasses.cell}[data-field="${escapeOperandAttributeSelector(field)}"]`,
);
const rowSelector = getGridRowElementSelector(id);
const cellSelector = `.${gridClasses.cell}[data-field="${escapeOperandAttributeSelector(
field,
)}"]`;
const selector = `${rowSelector} ${cellSelector}`;
return root.querySelector<HTMLDivElement>(selector);
}