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

feat: Freeze right column #973

Open
wants to merge 4 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
70 changes: 53 additions & 17 deletions packages/core/src/data-editor/data-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,9 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
const maxColumnWidth = Math.max(maxColumnWidthIn, minColumnWidth);
const maxColumnAutoWidth = Math.max(maxColumnAutoWidthIn ?? maxColumnWidth, minColumnWidth);

const freezeLeftColumns = typeof freezeColumns === "number" ? freezeColumns : freezeColumns[0];
const freezeRightColumns = typeof freezeColumns === "number" ? 0 : freezeColumns[1];

const docStyle = React.useMemo(() => {
if (typeof window === "undefined") return { fontSize: "16px" };
return window.getComputedStyle(document.documentElement);
Expand Down Expand Up @@ -1533,9 +1536,13 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
height: targetRect.height + 2 * paddingY,
};

let frozenWidth = 0;
for (let i = 0; i < freezeColumns; i++) {
frozenWidth += columns[i].width;
let frozenLeftWidth = 0;
for (let i = 0; i < freezeLeftColumns; i++) {
frozenLeftWidth += columns[i].width;
}
let frozenRightWidth = 0;
for (let i = mangledCols.length - 1; i >= mangledCols.length - freezeRightColumns; i--) {
frozenRightWidth += columns[i].width;
}
let trailingRowHeight = 0;
const freezeTrailingRowsEffective = freezeTrailingRows + (lastRowSticky ? 1 : 0);
Expand All @@ -1548,8 +1555,9 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
}

// scrollBounds is already scaled
let sLeft = frozenWidth * scale + scrollBounds.left + rowMarkerOffset * rowMarkerWidth * scale;
let sRight = scrollBounds.right;
let sLeft =
frozenLeftWidth * scale + scrollBounds.left + rowMarkerOffset * rowMarkerWidth * scale;
let sRight = scrollBounds.right - frozenRightWidth * scale;
let sTop = scrollBounds.top + totalHeaderHeight * scale;
let sBottom = scrollBounds.bottom - trailingRowHeight * scale;

Expand Down Expand Up @@ -1593,7 +1601,11 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
scrollY = bounds.y + bounds.height - sBottom;
}

if (dir === "vertical" || (typeof col === "number" && col < freezeColumns)) {
if (
dir === "vertical" ||
(typeof col === "number" &&
(col < freezeLeftColumns || col >= mangledCols.length - freezeRightColumns))
) {
scrollX = 0;
} else if (
dir === "horizontal" ||
Expand Down Expand Up @@ -1623,7 +1635,9 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
rowMarkerWidth,
scrollRef,
totalHeaderHeight,
freezeColumns,
freezeLeftColumns,
freezeRightColumns,
mangledCols.length,
columns,
mangledRows,
lastRowSticky,
Expand Down Expand Up @@ -2468,18 +2482,29 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
selected = [selected[0] - rowMarkerOffset, selected[1]];
}

const freezeRegion =
freezeColumns === 0
const freezeLeftRegion =
freezeLeftColumns === 0
? undefined
: {
x: 0,
y: region.y,
width: freezeColumns,
width: freezeLeftColumns,
height: region.height,
};

const freezeRightRegion =
freezeRightColumns === 0
? undefined
: {
x: columns.length - freezeRightColumns,
y: region.y,
width: freezeRightColumns,
height: region.height,
};

const freezeRegions: Rectangle[] = [];
if (freezeRegion !== undefined) freezeRegions.push(freezeRegion);
if (freezeLeftRegion !== undefined) freezeRegions.push(freezeLeftRegion);
if (freezeRightRegion !== undefined) freezeRegions.push(freezeRightRegion);
if (freezeTrailingRows > 0) {
freezeRegions.push({
x: region.x - rowMarkerOffset,
Expand All @@ -2488,11 +2513,20 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
height: freezeTrailingRows,
});

if (freezeColumns > 0) {
if (freezeLeftColumns > 0) {
freezeRegions.push({
x: 0,
y: rows - freezeTrailingRows,
width: freezeColumns,
width: freezeLeftColumns,
height: freezeTrailingRows,
});
}

if (freezeRightColumns > 0) {
freezeRegions.push({
x: columns.length - freezeRightColumns,
y: rows - freezeTrailingRows,
width: freezeRightColumns,
height: freezeTrailingRows,
});
}
Expand All @@ -2507,7 +2541,7 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
ty,
extras: {
selected,
freezeRegion,
freezeRegion: freezeLeftRegion,
freezeRegions,
},
};
Expand All @@ -2521,7 +2555,9 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
rowMarkerOffset,
showTrailingBlankRow,
rows,
freezeColumns,
freezeLeftColumns,
freezeRightColumns,
columns.length,
freezeTrailingRows,
setVisibleRegion,
onVisibleRegionChanged,
Expand Down Expand Up @@ -3817,7 +3853,7 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
);
}, [onGroupHeaderRenamed, renameGroup]);

const mangledFreezeColumns = Math.min(mangledCols.length, freezeColumns + (hasRowMarkers ? 1 : 0));
const mangledFreezeColumns = Math.min(mangledCols.length, freezeLeftColumns + (hasRowMarkers ? 1 : 0));

React.useImperativeHandle(
forwardedRef,
Expand Down Expand Up @@ -4055,7 +4091,7 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
onColumnProposeMove={onColumnProposeMove}
drawCell={drawCell}
disabledRows={disabledRows}
freezeColumns={mangledFreezeColumns}
freezeColumns={[mangledFreezeColumns, freezeRightColumns]}
lockColumns={rowMarkerOffset}
firstColAccessible={rowMarkerOffset === 0}
getCellContent={getMangledCellContent}
Expand Down
16 changes: 12 additions & 4 deletions packages/core/src/docs/examples/freeze-columns.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export default {
],
};

export const FreezeColumns: React.VFC<any> = (p: { freezeColumns: number }) => {
export const FreezeColumns: React.VFC<any> = (p: { freezeLeftColumns: number, freezeRightColumns: number }) => {
const { cols, getCellContent } = useMockDataGenerator(100);

return (
<DataEditor
{...defaultProps}
rowMarkers="both"
freezeColumns={p.freezeColumns}
freezeColumns={[p.freezeLeftColumns, p.freezeRightColumns]}
getCellContent={getCellContent}
columns={cols}
verticalBorder={false}
Expand All @@ -46,7 +46,14 @@ export const FreezeColumns: React.VFC<any> = (p: { freezeColumns: number }) => {
);
};
(FreezeColumns as any).argTypes = {
freezeColumns: {
freezeLeftColumns: {
control: {
type: "range",
min: 0,
max: 10,
},
},
freezeRightColumns: {
control: {
type: "range",
min: 0,
Expand All @@ -55,5 +62,6 @@ export const FreezeColumns: React.VFC<any> = (p: { freezeColumns: number }) => {
},
};
(FreezeColumns as any).args = {
freezeColumns: 1,
freezeLeftColumns: 1,
freezeRightColumns: 1,
};
Loading