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

🆕 Update width only if it isn't exactly the previous value #2870

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
],
"plugins": [
"@babel/transform-runtime",
"@babel/plugin-transform-nullish-coalescing-operator",
"@babel/plugin-transform-optional-chaining",
["optimize-clsx", { "functionNames": ["getCellClassname"] }]
]
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-data-grid",
"version": "7.0.0-beta.11",
"version": "7.0.0-beta.11-3",
"license": "MIT",
"description": "Feature-rich and customizable data grid React component",
"keywords": [
Expand Down Expand Up @@ -98,7 +98,7 @@
"react-dom": "^17.0.2",
"react-refresh": "^0.11.0",
"react-router-dom": "^6.2.1",
"rollup": "^2.63.0",
"rollup": "^2.75.7",
"rollup-plugin-postcss": "^4.0.2",
"style-loader": "^3.3.1",
"typescript": "~4.6.2",
Expand Down
9 changes: 6 additions & 3 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ interface EditCellState<R> extends Position {
readonly mode: 'EDIT';
readonly row: R;
readonly originalRow: R;
readonly isMouseClick: boolean;
}

type DefaultColumnOptions<R, SR> = Pick<
Expand Down Expand Up @@ -430,7 +431,7 @@ function DataGrid<R, SR, K extends Key>(
useImperativeHandle(ref, () => ({
element: gridRef.current,
scrollToColumn(idx: number) {
scrollToCell({ idx });
scrollToCell({ idx, rowIdx: lastSelectedRowIdx.current || 0 });
},
scrollToRow(rowIdx: number) {
const { current } = gridRef;
Expand Down Expand Up @@ -689,7 +690,8 @@ function DataGrid<R, SR, K extends Key>(
rowIdx,
mode: 'EDIT',
row,
originalRow: row
originalRow: row,
isMouseClick: false
}));
}
}
Expand Down Expand Up @@ -726,7 +728,7 @@ function DataGrid<R, SR, K extends Key>(

if (enableEditor && isCellEditable(position)) {
const row = rows[position.rowIdx] as R;
setSelectedPosition({ ...position, mode: 'EDIT', row, originalRow: row });
setSelectedPosition({ ...position, mode: 'EDIT', row, originalRow: row, isMouseClick: true });
} else if (isSamePosition(selectedPosition, position)) {
// Avoid re-renders if the selected cell state is the same
// TODO: replace with a #record? https://github.com/microsoft/TypeScript/issues/39831
Expand Down Expand Up @@ -970,6 +972,7 @@ function DataGrid<R, SR, K extends Key>(
scrollToCell={() => {
scrollToCell(selectedPosition);
}}
isMouseClick={'isMouseClick' in selectedPosition && selectedPosition.isMouseClick}
/>
);
}
Expand Down
9 changes: 8 additions & 1 deletion src/EditCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function EditCell<R, SR>({
column,
colSpan,
row,
isMouseClick,
onRowChange,
closeEditor,
scrollToCell
Expand Down Expand Up @@ -119,7 +120,13 @@ export default function EditCell<R, SR>({
>
{column.editor != null && (
<>
<column.editor column={column} row={row} onRowChange={onRowChange} onClose={onClose} />
<column.editor
column={column}
row={row}
onRowChange={onRowChange}
onClose={onClose}
isMouseClick={isMouseClick}
/>
{column.editorOptions?.renderFormatter && (
<column.formatter column={column} row={row} isCellSelected onRowChange={onRowChange} />
)}
Expand Down
18 changes: 15 additions & 3 deletions src/hooks/useGridDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export function useGridDimensions(): [
height: number
] {
const gridRef = useRef<HTMLDivElement>(null);
const [gridWidth, setGridWidth] = useState(1);
// keep track of the old width and the new width to avoid twitching
const [gridWidth, setGridWidth] = useState([0, 1]);
const [gridHeight, setGridHeight] = useState(1);

useLayoutEffect(() => {
Expand All @@ -25,7 +26,18 @@ export function useGridDimensions(): [
// TODO: remove once fixed upstream
// we reduce width by 1px here to avoid layout issues in Chrome
// https://bugs.chromium.org/p/chromium/issues/detail?id=1206298
setGridWidth(clientWidth - (devicePixelRatio % 1 === 0 ? 0 : 1));
setGridWidth((prev: number[]) => {
const newValue = clientWidth - (devicePixelRatio % 1 === 0 ? 0 : 1);

// If the new value is the same as the previous value, just ignore it.
// If we change back the value to the old one, we will end up with
// and infinite loop of twitching.
if (prev[0] === newValue) {
return prev;
}

return [prev[1], newValue];
});
setGridHeight(clientHeight);
}

Expand All @@ -38,5 +50,5 @@ export function useGridDimensions(): [
};
}, []);

return [gridRef, gridWidth, gridHeight];
return [gridRef, gridWidth[1], gridHeight];
}
13 changes: 7 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface GroupFormatterProps<TRow, TSummaryRow = unknown> {
export interface EditorProps<TRow, TSummaryRow = unknown> {
column: CalculatedColumn<TRow, TSummaryRow>;
row: TRow;
isMouseClick: boolean;
onRowChange: (row: TRow, commitChanges?: boolean) => void;
onClose: (commitChanges?: boolean) => void;
}
Expand All @@ -110,10 +111,10 @@ export interface HeaderRendererProps<TRow, TSummaryRow = unknown> {

export interface CellRendererProps<TRow, TSummaryRow>
extends Pick<
RowRendererProps<TRow, TSummaryRow>,
'onRowClick' | 'onRowDoubleClick' | 'selectCell'
>,
Omit<React.HTMLAttributes<HTMLDivElement>, 'style' | 'children'> {
RowRendererProps<TRow, TSummaryRow>,
'onRowClick' | 'onRowDoubleClick' | 'selectCell'
>,
Omit<React.HTMLAttributes<HTMLDivElement>, 'style' | 'children'> {
column: CalculatedColumn<TRow, TSummaryRow>;
colSpan: number | undefined;
row: TRow;
Expand Down Expand Up @@ -214,8 +215,8 @@ export interface SortIconProps {

export interface CheckboxFormatterProps
extends Pick<
React.InputHTMLAttributes<HTMLInputElement>,
'aria-label' | 'aria-labelledby' | 'checked' | 'tabIndex' | 'disabled'
React.InputHTMLAttributes<HTMLInputElement>,
'aria-label' | 'aria-labelledby' | 'checked' | 'tabIndex' | 'disabled'
> {
onChange: (checked: boolean, shift: boolean) => void;
}
Expand Down