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] Keep focused cell in the DOM #7357

Merged
merged 70 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
2b7e18a
fix: focused row should always be rendered
yaredtsy Dec 31, 2022
259be17
fix: dragStart should update state.focus
yaredtsy Dec 31, 2022
1bb1f06
refa: code clean
yaredtsy Jan 1, 2023
a806328
fix: lastVisibleRowIndex fix
yaredtsy Jan 2, 2023
a110332
fix: cell select horizontal virtualization added
yaredtsy Jan 6, 2023
6b73fd9
fix: pinned column fix
yaredtsy Jan 7, 2023
f50b865
fix: column pin fix
yaredtsy Jan 15, 2023
93b772b
fix: hide added cell and row
yaredtsy Jan 15, 2023
86f74f9
fix: focused row should always be rendered
yaredtsy Dec 31, 2022
922596c
fix: dragStart should update state.focus
yaredtsy Dec 31, 2022
46fafd1
refa: code clean
yaredtsy Jan 1, 2023
d23d73a
fix: lastVisibleRowIndex fix
yaredtsy Jan 2, 2023
74f767d
fix: cell select horizontal virtualization added
yaredtsy Jan 6, 2023
49436c2
fix: pinned column fix
yaredtsy Jan 7, 2023
7912905
fix: column pin fix
yaredtsy Jan 15, 2023
35f3321
fix: hide added cell and row
yaredtsy Jan 15, 2023
bc7f87c
Merge branch 'focused_cell_fix' of https://github.com/yaredtsy/mui-x …
yaredtsy Jan 15, 2023
65f342b
fix: focused row should always be rendered
yaredtsy Dec 31, 2022
fe131ef
fix: cell select horizontal virtualization added
yaredtsy Jan 6, 2023
7ff3c50
fix: pinned column fix
yaredtsy Jan 7, 2023
915020e
fix: hide added cell and row
yaredtsy Jan 15, 2023
6c59e65
Merge branch 'focused_cell_fix' of https://github.com/yaredtsy/mui-x …
yaredtsy Jan 15, 2023
2a6de29
Merge branch 'next' into focused_cell_fix
yaredtsy Jan 15, 2023
6e61646
fix: merge fix
yaredtsy Jan 15, 2023
6340a94
fix: propTypes generated
yaredtsy Jan 15, 2023
4af30fd
refa: code cleaned
yaredtsy Jan 19, 2023
b91fcf2
Merge branch 'mui:next' into focused_cell_fix
yaredtsy Feb 15, 2023
face9e2
style: style refactored
yaredtsy Feb 15, 2023
4c6ad85
fix: row height set to zero
yaredtsy Feb 15, 2023
3371387
Merge branch 'next' into focused_cell_fix
yaredtsy Feb 23, 2023
31e9ddc
Merge branch 'master' into focused_cell_fix
yaredtsy Apr 12, 2023
4291751
fix: merge fix
Apr 12, 2023
d9b87cc
fix: proptypes added
Apr 13, 2023
9899329
refa: fix memoization
yaredtsy Apr 13, 2023
fd7f636
refa: removed unnecessary if
yaredtsy Apr 15, 2023
e5641a5
Merge branch 'mui:master' into focused_cell_fix
yaredtsy Apr 20, 2023
26e6c5f
test: failing test added
yaredtsy Apr 20, 2023
2341fa3
fix: fix for failing test added
yaredtsy Apr 20, 2023
1b6ca70
fix: test fixed
yaredtsy Apr 20, 2023
e612a3e
refa: code cleanup
yaredtsy Apr 22, 2023
9308a50
refa: code cleaned
yaredtsy Apr 22, 2023
cb4e71e
Merge branch 'mui:master' into focused_cell_fix
yaredtsy May 26, 2023
2314cfc
Merge branch 'master' into focused_cell_fix
yaredtsy Jun 14, 2023
b1fc96f
fix: merge clean up
yaredtsy Jun 14, 2023
4099869
fix: proptypes fix
yaredtsy Jun 14, 2023
392f5cb
fix: check for undefined fix
yaredtsy Jun 15, 2023
3083648
refa: removed hardcode string
yaredtsy Jun 15, 2023
b72cdec
fix: undefined check fix
yaredtsy Jun 16, 2023
839dc1e
Merge branch 'mui:master' into focused_cell_fix
yaredtsy Jun 16, 2023
8d7d97e
fix
yaredtsy Jun 16, 2023
47eab4e
refa: var name changed
yaredtsy Jun 16, 2023
f25ccdc
fix: proptype
yaredtsy Jun 16, 2023
434ebb0
Merge branch 'master' into focused_cell_fix
yaredtsy Jun 20, 2023
65cf472
fixes
yaredtsy Jun 20, 2023
a6ec44e
proptypes
yaredtsy Jun 20, 2023
cf76593
fix: undefined check added
yaredtsy Jun 20, 2023
bdcee3a
fix: yarn prop type fix
yaredtsy Jun 22, 2023
35830a7
test: test added
yaredtsy Jul 8, 2023
7db6d37
Merge branch 'mui:master' into focused_cell_fix
yaredtsy Jul 8, 2023
8cba543
eslint fix
yaredtsy Jul 9, 2023
bca5e68
refa: code refactored
yaredtsy Jul 9, 2023
c325dc1
fix: eslint fix
yaredtsy Jul 9, 2023
59b37fc
test: fix typo
yaredtsy Jul 10, 2023
9d68ff3
refa: fireEvent implemted
yaredtsy Jul 10, 2023
b0c3faa
removed unceary comments
yaredtsyg Jul 10, 2023
269f190
comment refactored
yaredtsy Jul 10, 2023
32bc12d
prettier
yaredtsy Jul 10, 2023
ef75b50
refactored
yaredtsy Jul 10, 2023
b9c7537
Merge branch 'mui:master' into focused_cell_fix
yaredtsy Jul 17, 2023
22ce1e5
Update packages/grid/x-data-grid/src/components/GridRow.tsx
romgrk Jul 21, 2023
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 @@ -81,6 +81,7 @@ export const useGridRowReorder = (
});

originRowIndex.current = apiRef.current.getRowIndexRelativeToVisibleRows(params.id);
apiRef.current.setCellFocus(params.id, '__reorder__');
romgrk marked this conversation as resolved.
Show resolved Hide resolved
},
[isRowReorderDisabled, classes.rowDragging, logger, apiRef],
);
Expand Down
5 changes: 4 additions & 1 deletion packages/grid/x-data-grid/src/components/GridRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,14 @@ const GridRow = React.forwardRef<
const rowType = apiRef.current.getRowNode(rowId)!.type;
const cells: JSX.Element[] = [];

const isAdditionalColumnAdded = renderedColumns.length < lastColumnToRender - firstColumnToRender;
for (let i = 0; i < renderedColumns.length; i += 1) {
const column = renderedColumns[i];
const indexRelativeToAllColumns = firstColumnToRender + i;

const isLastColumn = indexRelativeToAllColumns === visibleColumns.length - 1;
const isLastColumn = isAdditionalColumnAdded
? indexRelativeToAllColumns === visibleColumns.length
: indexRelativeToAllColumns === visibleColumns.length - 1;
const removeLastBorderRight = isLastColumn && hasScrollX && !hasScrollY;
const showRightBorder = !isLastColumn
? rootProps.showCellVerticalBorder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
const rowBuffer = !disableVirtualization ? rootProps.rowBuffer : 0;
const columnBuffer = !disableVirtualization ? rootProps.columnBuffer : 0;

let isFocusedCellRendered = false;
yaredtsy marked this conversation as resolved.
Show resolved Hide resolved
const { cell } = apiRef.current.state.focus;
yaredtsy marked this conversation as resolved.
Show resolved Hide resolved

const [firstRowToRender, lastRowToRender] = getRenderableIndexes({
firstIndex: nextRenderContext.firstRowIndex,
lastIndex: nextRenderContext.lastRowIndex,
Expand Down Expand Up @@ -445,6 +448,10 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {

for (let i = firstRowToRender; i < lastRowToRender; i += 1) {
const row = currentPage.rows[i];
if (cell && row.id === cell?.id) {
isFocusedCellRendered = true;
}

renderedRows.push(row);
apiRef.current.calculateColSpan({
rowId: row.id,
Expand All @@ -453,6 +460,29 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
columns: visibleColumns,
});
}

// If the selected row is not within the current range of rows being displayed,
yaredtsy marked this conversation as resolved.
Show resolved Hide resolved
// we need to render it at either the top or bottom of the rows,
// depending on whether it is above or below the range.
if (!isFocusedCellRendered && cell && !disableVirtualization) {
const rows = currentPage.rows.filter((row) => row.id === cell.id);
const focusedRow = rows.length > 0 && rows[0];

if (focusedRow) {
const index = currentPage.rows.indexOf(focusedRow);
if (index > firstRowToRender) {
yaredtsy marked this conversation as resolved.
Show resolved Hide resolved
renderedRows.push(focusedRow);
} else {
renderedRows.unshift(focusedRow);
}
apiRef.current.calculateColSpan({
yaredtsy marked this conversation as resolved.
Show resolved Hide resolved
rowId: focusedRow.id,
minFirstColumn,
maxLastColumn,
columns: visibleColumns,
});
}
}
}

const [initialFirstColumnToRender, lastColumnToRender] = getRenderableIndexes({
Expand All @@ -473,11 +503,34 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {

const renderedColumns = visibleColumns.slice(firstColumnToRender, lastColumnToRender);

// If the selected column is not within the current range of columns being displayed,
yaredtsy marked this conversation as resolved.
Show resolved Hide resolved
// we need to render it at either the left or right of the columns,
// depending on whether it is above or below the range.
const isAllColumnRendered =
maxLastColumn - minFirstColumn === lastColumnToRender - firstColumnToRender;
if (cell && !disableVirtualization && !isAllColumnRendered) {
const selectedColumns = visibleColumns.filter((column) => column.field === cell.field);
if (selectedColumns.length > 0) {
const focusedColumn = selectedColumns[0];
const focusedColumnIndex = visibleColumns.indexOf(focusedColumn);

if (firstColumnToRender > focusedColumnIndex) {
renderedColumns.unshift(focusedColumn);
} else if (lastColumnToRender < focusedColumnIndex) {
renderedColumns.push(focusedColumn);
}
}
}

const rows: JSX.Element[] = [];

for (let i = 0; i < renderedRows.length; i += 1) {
const { id, model } = renderedRows[i];
const lastVisibleRowIndex = firstRowToRender + i === currentPage.rows.length - 1;

const lastVisibleRowIndex =
!isFocusedCellRendered && cell
? firstRowToRender + i === currentPage.rows.length
: firstRowToRender + i === currentPage.rows.length - 1;
const baseRowHeight = !apiRef.current.rowHasAutoHeight(id)
? apiRef.current.unstable_getRowHeight(id)
: 'auto';
Expand Down