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 69 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 @@ -15,6 +15,7 @@ import { gridEditRowsStateSelector } from '@mui/x-data-grid/internals';
import { GridRowOrderChangeParams } from '../../../models/gridRowOrderChangeParams';
import { GridPrivateApiPro } from '../../../models/gridApiPro';
import { DataGridProProcessedProps } from '../../../models/dataGridProProps';
import { GRID_REORDER_COL_DEF } from './gridRowReorderColDef';

type OwnerState = { classes: DataGridProProcessedProps['classes'] };

Expand Down Expand Up @@ -98,6 +99,7 @@ export const useGridRowReorder = (
});

originRowIndex.current = apiRef.current.getRowIndexRelativeToVisibleRows(params.id);
apiRef.current.setCellFocus(params.id, GRID_REORDER_COL_DEF.field);
},
[isRowReorderDisabled, classes.rowDragging, logger, apiRef],
);
Expand Down
77 changes: 58 additions & 19 deletions packages/grid/x-data-grid/src/components/GridRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export interface GridRowProps extends React.HTMLAttributes<HTMLDivElement> {
tabbableCell: string | null;
row?: GridRowModel;
isLastVisible?: boolean;
focusedCellColumnIndexNotInRange?: number;
isNotVisible?: boolean;
onClick?: React.MouseEventHandler<HTMLDivElement>;
onDoubleClick?: React.MouseEventHandler<HTMLDivElement>;
onMouseEnter?: React.MouseEventHandler<HTMLDivElement>;
Expand Down Expand Up @@ -114,6 +116,8 @@ const GridRow = React.forwardRef<HTMLDivElement, GridRowProps>(function GridRow(
firstColumnToRender,
lastColumnToRender,
isLastVisible = false,
focusedCellColumnIndexNotInRange,
isNotVisible,
focusedCell,
tabbableCell,
onClick,
Expand Down Expand Up @@ -277,6 +281,14 @@ const GridRow = React.forwardRef<HTMLDivElement, GridRowProps>(function GridRow(
Object.keys(editRowsState).length > 0);

const editCellState = editRowsState[rowId]?.[column.field] ?? null;
let cellIsNotVisible = false;

if (
focusedCellColumnIndexNotInRange !== undefined &&
visibleColumns[focusedCellColumnIndexNotInRange].field === column.field
) {
cellIsNotVisible = true;
}

return (
<CellComponent
Expand All @@ -291,6 +303,7 @@ const GridRow = React.forwardRef<HTMLDivElement, GridRowProps>(function GridRow(
colSpan={cellProps.colSpan}
disableDragEvents={disableDragEvents}
editCellState={editCellState}
isNotVisible={cellIsNotVisible}
{...slotProps?.cell}
/>
);
Expand Down Expand Up @@ -322,27 +335,39 @@ const GridRow = React.forwardRef<HTMLDivElement, GridRowProps>(function GridRow(
}
}

const style = {
...styleProp,
maxHeight: rowHeight === 'auto' ? 'none' : rowHeight, // max-height doesn't support "auto"
minHeight,
};
const style = React.useMemo(() => {
if (isNotVisible) {
return {
opacity: 0,
width: 0,
height: 0,
};
}
yaredtsy marked this conversation as resolved.
Show resolved Hide resolved

if (sizes?.spacingTop) {
const property = rootProps.rowSpacingType === 'border' ? 'borderTopWidth' : 'marginTop';
style[property] = sizes.spacingTop;
}
const rowStyle = {
...styleProp,
maxHeight: rowHeight === 'auto' ? 'none' : rowHeight, // max-height doesn't support "auto"
minHeight,
};

if (sizes?.spacingBottom) {
const property = rootProps.rowSpacingType === 'border' ? 'borderBottomWidth' : 'marginBottom';
let propertyValue = style[property];
// avoid overriding existing value
if (typeof propertyValue !== 'number') {
propertyValue = parseInt(propertyValue || '0', 10);
if (sizes?.spacingTop) {
const property = rootProps.rowSpacingType === 'border' ? 'borderTopWidth' : 'marginTop';
rowStyle[property] = sizes.spacingTop;
}
propertyValue += sizes.spacingBottom;
style[property] = propertyValue;
}

if (sizes?.spacingBottom) {
const property = rootProps.rowSpacingType === 'border' ? 'borderBottomWidth' : 'marginBottom';
let propertyValue = rowStyle[property];
// avoid overriding existing value
if (typeof propertyValue !== 'number') {
propertyValue = parseInt(propertyValue || '0', 10);
}
propertyValue += sizes.spacingBottom;
rowStyle[property] = propertyValue;
}

return rowStyle;
}, [isNotVisible, rowHeight, styleProp, minHeight, sizes, rootProps.rowSpacingType]);

const rowClassNames = apiRef.current.unstable_applyPipeProcessors('rowClassName', [], rowId);

Expand Down Expand Up @@ -370,7 +395,18 @@ const GridRow = React.forwardRef<HTMLDivElement, GridRowProps>(function GridRow(

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

let indexRelativeToAllColumns = firstColumnToRender + i;

if (
focusedCellColumnIndexNotInRange !== undefined &&
visibleColumns[focusedCellColumnIndexNotInRange].field === column.field &&
focusedCell
) {
indexRelativeToAllColumns = focusedCellColumnIndexNotInRange;
} else if (focusedCellColumnIndexNotInRange !== undefined && focusedCell) {
indexRelativeToAllColumns -= 1;
}
romgrk marked this conversation as resolved.
Show resolved Hide resolved

const cellColSpanInfo = apiRef.current.unstable_getCellColSpanInfo(
rowId,
Expand All @@ -386,6 +422,7 @@ const GridRow = React.forwardRef<HTMLDivElement, GridRowProps>(function GridRow(
showRightBorder: rootProps.showCellVerticalBorder,
indexRelativeToAllColumns,
};

cells.push(getCell(column, cellProps));
} else {
const { width } = cellColSpanInfo.cellProps;
Expand Down Expand Up @@ -446,12 +483,14 @@ GridRow.propTypes = {
* If `null`, no cell in this row has focus.
*/
focusedCell: PropTypes.string,
focusedCellColumnIndexNotInRange: PropTypes.number,
/**
* Index of the row in the whole sorted and filtered dataset.
* If some rows above have expanded children, this index also take those children into account.
*/
index: PropTypes.number.isRequired,
isLastVisible: PropTypes.bool,
isNotVisible: PropTypes.bool,
lastColumnToRender: PropTypes.number.isRequired,
onClick: PropTypes.func,
onDoubleClick: PropTypes.func,
Expand Down
52 changes: 39 additions & 13 deletions packages/grid/x-data-grid/src/components/cell/GridCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
GridCellModes,
GridRowId,
GridCellMode,
GridEditCellProps,
} from '../../models';
import {
GridRenderEditCellParams,
Expand All @@ -26,7 +27,6 @@ import {
} from '../../models/params/gridCellParams';
import { GridColDef, GridAlignment } from '../../models/colDef/gridColDef';
import { GridTreeNodeWithRender } from '../../models/gridRows';
import { GridEditCellProps } from '../../models/gridEditRowModel';
import { useGridSelector, objectShallowCompare } from '../../hooks/utils/useGridSelector';
import { useGridApiContext } from '../../hooks/utils/useGridApiContext';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
Expand All @@ -45,6 +45,7 @@ type GridCellV7Props = {
width: number;
colSpan?: number;
disableDragEvents?: boolean;
isNotVisible?: boolean;
editCellState: GridEditCellProps<any> | null;
yaredtsy marked this conversation as resolved.
Show resolved Hide resolved
onClick?: React.MouseEventHandler<HTMLDivElement>;
onDoubleClick?: React.MouseEventHandler<HTMLDivElement>;
Expand Down Expand Up @@ -267,6 +268,7 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>((props, ref) =>
row,
colSpan,
disableDragEvents,
isNotVisible,
onClick,
onDoubleClick,
onMouseDown,
Expand Down Expand Up @@ -330,12 +332,22 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>((props, ref) =>
[apiRef, field, rowId],
);

const style = {
minWidth: width,
maxWidth: width,
minHeight: height,
maxHeight: height === 'auto' ? 'none' : height, // max-height doesn't support "auto"
};
const style = React.useMemo(() => {
if (isNotVisible) {
return {
padding: 0,
opacity: 0,
width: 0,
};
}
const cellStyle = {
minWidth: width,
maxWidth: width,
minHeight: height,
maxHeight: height === 'auto' ? 'none' : height, // max-height doesn't support "auto"
};
return cellStyle;
}, [width, height, isNotVisible]);

React.useEffect(() => {
if (!hasFocus || cellMode === GridCellModes.Edit) {
Expand Down Expand Up @@ -393,6 +405,7 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>((props, ref) =>
let children: React.ReactNode = childrenProp;
if (children === undefined) {
const valueString = valueToRender?.toString();

children = (
<div className={classes.content} title={valueString}>
{valueString}
Expand Down Expand Up @@ -483,6 +496,7 @@ GridCell.propTypes = {
isValidating: PropTypes.bool,
value: PropTypes.any,
}),
isNotVisible: PropTypes.bool,
height: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number]),
onClick: PropTypes.func,
onDoubleClick: PropTypes.func,
Expand Down Expand Up @@ -514,6 +528,7 @@ const GridCellV7 = React.forwardRef<HTMLDivElement, GridCellV7Props>((props, ref
row,
colSpan,
disableDragEvents,
isNotVisible,
onClick,
onDoubleClick,
onMouseDown,
Expand Down Expand Up @@ -637,12 +652,22 @@ const GridCellV7 = React.forwardRef<HTMLDivElement, GridCellV7Props>((props, ref
[apiRef, field, rowId],
);

const style = {
minWidth: width,
maxWidth: width,
minHeight: height,
maxHeight: height === 'auto' ? 'none' : height, // max-height doesn't support "auto"
};
const style = React.useMemo(() => {
if (isNotVisible) {
return {
padding: 0,
opacity: 0,
width: 0,
};
}
const cellStyle = {
minWidth: width,
maxWidth: width,
minHeight: height,
maxHeight: height === 'auto' ? 'none' : height, // max-height doesn't support "auto"
};
return cellStyle;
}, [width, height, isNotVisible]);

React.useEffect(() => {
if (!hasFocus || cellMode === GridCellModes.Edit) {
Expand Down Expand Up @@ -788,6 +813,7 @@ GridCellV7.propTypes = {
value: PropTypes.any,
}),
height: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number]).isRequired,
isNotVisible: PropTypes.bool,
onClick: PropTypes.func,
onDoubleClick: PropTypes.func,
onDragEnter: PropTypes.func,
Expand Down
Loading