fix: enforce DataGrid cover boundary

This commit is contained in:
2026-07-31 04:44:24 +02:00
parent 7b8072d049
commit 5211e07d0b
4 changed files with 151 additions and 32 deletions
+5 -3
View File
@@ -135,9 +135,11 @@ type DataGridBaseProps<T> = {
/** @deprecated Use initialFit instead. */
fit?: DataGridInitialFit;
/**
* Cover keeps the right edge fixed by redistributing every resize across
* real columns. Free permits underflow and horizontal growth. Free is
* automatically promoted to cover when a right-sticky column exists.
* Cover prevents underflow while allowing horizontal overflow. Shrinking
* consumes existing overflow and then grows eligible columns to the right;
* it stops when no such capacity remains. Free permits both underflow and
* overflow. Free is automatically promoted to cover when a right-sticky
* column exists.
*/
resizeBehavior?: DataGridResizeBehavior;
className?: string;
+40 -11
View File
@@ -210,15 +210,8 @@ export function fitDataGridColumns(
resizeTargetForLayout(column, widths, widths[column.id])
)
);
// A persisted user layout is preferred, not a reason to overflow. Under
// viewport pressure every ordinary track may shrink down to its hard floor.
remaining = applyDataGridDistribution(
widths,
remaining,
coverageColumns.map((column) =>
resizeTargetForLayout(column, widths, widths[column.id])
)
);
// Explicit user widths are hard during reconciliation. Cover prevents
// underflow, but intentional user growth may remain horizontally scrollable.
}
const roundedWidths = roundDataGridWidths(widths);
@@ -268,6 +261,43 @@ export function resizeDataGridColumn(
};
}
if (fitMode === "cover") {
if (boundedDelta >= 0) {
widths[columnId] = startWidth + boundedDelta;
return {
widths: roundDataGridWidths(widths),
appliedDelta: roundWidth(boundedDelta)
};
}
const requestedShrink = -boundedDelta;
const freeShrink = Math.min(
requestedShrink,
Math.max(0, uncompensatedShrinkRoom)
);
const rightTargets = columns
.slice(activeIndex + 1)
.filter((column) => column.resizable && !column.sticky)
.map((column) =>
resizeTargetForLayout(
column,
widths,
dataGridColumnResizeWeight(column)
)
);
const compensation = distributeDataGridResizeAmount(
requestedShrink - freeShrink,
rightTargets
);
applyDataGridAmounts(widths, compensation.amounts);
const appliedShrink = freeShrink + compensation.applied;
widths[columnId] = startWidth - appliedShrink;
return {
widths: roundDataGridWidths(widths),
appliedDelta: roundWidth(-appliedShrink)
};
}
const freeShrink = boundedDelta < 0
? Math.min(-boundedDelta, Math.max(0, uncompensatedShrinkRoom))
: 0;
@@ -282,8 +312,7 @@ export function resizeDataGridColumn(
resizeTargetForLayout(
column,
widths,
dataGridColumnResizeWeight(column),
fitMode === "cover" ? DATA_GRID_MAX_TRACK_WIDTH : undefined
dataGridColumnResizeWeight(column)
)
);
const distribution = distributeDataGridResizeAmount(remainingCompensation, targets);
+96 -12
View File
@@ -94,13 +94,25 @@ assertWidths(
const overriddenOverflowLayout = fitDataGridColumns(weightedColumns, 500, {}, { primary: 450 });
assertWidths(
overriddenOverflowLayout.widths,
{ fixed: 72, primary: 328, secondary: 100 },
"a cover layout treats a persisted user width as preferred under viewport pressure"
{ fixed: 72, primary: 450, secondary: 100 },
"a cover layout preserves an explicit user width under viewport pressure"
);
assertEqual(
overriddenOverflowLayout.overflowWidth,
122,
"intentional user width may remain horizontally scrollable"
);
const overriddenUnderflowLayout = fitDataGridColumns(
weightedColumns,
800,
{},
{ fixed: 80, primary: 200, secondary: 200 }
);
assertEqual(
overriddenUnderflowLayout.underflowWidth,
0,
"a persisted user preference does not create avoidable horizontal overflow"
"a persisted cover layout is expanded when it would otherwise underflow"
);
const clampedOverrideLayout = fitDataGridColumns(weightedColumns, 800, {}, { primary: 900 }, "free");
@@ -176,15 +188,22 @@ const resizeBase = { first: 200, middle: 300, last: 300, actions: 100 };
const resizedFirst = resizeDataGridColumn(resizeColumns, resizeBase, "first", 120, "cover");
assertWidths(
resizedFirst.widths,
{ first: 320, middle: 240, last: 240, actions: 100 },
"right-side peers compensate an earlier cover-column resize"
{ first: 320, middle: 300, last: 300, actions: 100 },
"growing an earlier cover column may create horizontal overflow"
);
const shrunkFirst = resizeDataGridColumn(resizeColumns, resizeBase, "first", -80, "cover");
assertWidths(
shrunkFirst.widths,
{ first: 120, middle: 340, last: 340, actions: 100 },
"shrinking an earlier cover column grows resizable columns to its right"
);
const resizedLast = resizeDataGridColumn(resizeColumns, resizeBase, "last", 120, "cover");
assertWidths(
resizedLast.widths,
{ first: 140, middle: 240, last: 420, actions: 100 },
"left-side peers compensate the last resizable cover column"
{ first: 200, middle: 300, last: 420, actions: 100 },
"the last resizable cover column may grow into horizontal overflow"
);
const committedLayout = fitDataGridColumns(
@@ -197,7 +216,72 @@ const committedLayout = fitDataGridColumns(
assertWidths(
committedLayout.widths,
resizedLast.widths,
"committing a cover resize preserves the drag layout without a mouse-up snap"
"committing a cover resize preserves intentional overflow without a mouse-up snap"
);
const stoppedLastShrink = resizeDataGridColumn(
resizeColumns,
resizeBase,
"last",
-120,
"cover"
);
assertWidths(
stoppedLastShrink.widths,
resizeBase,
"the last resizable column stops before its shrink would require a filler"
);
assertEqual(
stoppedLastShrink.appliedDelta,
0,
"the stopped last-column shrink reports no applied movement"
);
const overflowedResizeBase = { first: 200, middle: 300, last: 420, actions: 100 };
const consumedLastOverflow = resizeDataGridColumn(
resizeColumns,
overflowedResizeBase,
"last",
-180,
"cover",
120
);
assertWidths(
consumedLastOverflow.widths,
{ first: 200, middle: 300, last: 300, actions: 100 },
"the last resizable column consumes overflow and stops at exact coverage"
);
assertEqual(
consumedLastOverflow.appliedDelta,
-120,
"last-column shrink is clamped to the available overflow"
);
const stillPastLastStop = resizeDataGridColumn(
resizeColumns,
overflowedResizeBase,
"last",
-150,
"cover",
120
);
assertWidths(
stillPastLastStop.widths,
consumedLastOverflow.widths,
"reversing the pointer while it remains beyond the stop does not move the column"
);
const crossedLastStop = resizeDataGridColumn(
resizeColumns,
overflowedResizeBase,
"last",
-110,
"cover",
120
);
assertEqual(
crossedLastStop.widths.last,
310,
"the column resumes only after the pointer crosses the cover boundary"
);
const fixedPeerResize = resizeDataGridColumn([
@@ -207,8 +291,8 @@ const fixedPeerResize = resizeDataGridColumn([
], { editable: 200, fixed: 300, actions: 100 }, "editable", 80, "cover");
assertWidths(
fixedPeerResize.widths,
{ editable: 280, fixed: 220, actions: 100 },
"non-resizable tracks may compensate layout without exposing a resize handle"
{ editable: 280, fixed: 300, actions: 100 },
"growth does not resize a fixed peer merely to avoid overflow"
);
const coverBeyondPassiveMax = resizeDataGridColumn([
@@ -218,8 +302,8 @@ const coverBeyondPassiveMax = resizeDataGridColumn([
], { first: 200, second: 300, actions: 100 }, "first", -80, "cover");
assertWidths(
coverBeyondPassiveMax.widths,
{ first: 120, second: 380, actions: 100 },
"cover compensation may exceed a peer's preferred maximum to retain full coverage"
{ first: 200, second: 300, actions: 100 },
"cover shrinking stops when right-side peers have reached their maximum"
);
const constrainedAtPassiveMax = resizeDataGridColumn([