fix(table): align constrained widths and guard invalid fr factors

This commit is contained in:
maoyingjie
2026-10-29 11:11:10 +08:00
parent 86d76a0675
commit c019768e6a
6 changed files with 110 additions and 17 deletions
@@ -88,9 +88,8 @@ export default defineComponent({
const cssWidth = (value: string | number | undefined) => {
if (value === undefined) return undefined;
if (typeof value === "number") return `${value}px`;
// CSS Grid fr units are not valid table widths. Treat Nfr as a flexible
// column (native width:auto sharing remaining space). This does not
// implement RAC fr weight proportions across columns.
// fr is resolved by TableContent after measurement. Leave native layout
// untouched until a managed pixel width is available.
if (/^\d+(\.\d+)?fr$/.test(value)) return undefined;
return value;
};
@@ -118,7 +117,8 @@ export default defineComponent({
? `${resolvedWidth.value}px`
: cssWidth(width.value),
minWidth: cssWidth(
typeof width.value === "number"
resolvedWidth.value === undefined &&
typeof width.value === "number"
? Math.max(props.minWidth ?? 0, width.value)
: props.minWidth,
),
@@ -16,7 +16,7 @@ import {
} from "vue";
import { useTableNavigation } from "./navigation";
import { useCollectionSelection } from "../collection/selection";
import { calculateColumnSizes, isStaticWidth } from "./widths";
import { calculateColumnSizes } from "./widths";
import type { TableKey, TableColumnRegistration } from "./context";
import { tableContextKey, type TableSortDirection } from "./context";
@@ -227,14 +227,31 @@ export default defineComponent({
for (const entry of entries) entry.applyWidth?.(undefined);
return;
}
const availableWidth =
element.value?.getBoundingClientRect().width ?? 0;
const table = element.value;
let availableWidth = table?.getBoundingClientRect().width ?? 0;
const parent = table?.parentElement;
if (table && parent) {
const parentStyle = getComputedStyle(parent);
const parentWidth =
parent.clientWidth -
(parseFloat(parentStyle.paddingLeft) || 0) -
(parseFloat(parentStyle.paddingRight) || 0);
// Assigned column pixels can keep a table wider than its shrinking
// viewport. Observe that constraint too, retaining an explicit table
// min-width for deliberate horizontal overflow.
if (parentWidth > 0)
availableWidth = Math.max(
parseFloat(getComputedStyle(table).minWidth) || 0,
Math.min(availableWidth, parentWidth),
);
}
if (!(availableWidth > 0)) return;
managedLayout.value = true;
const sizes = calculateColumnSizes(availableWidth, specs);
entries.forEach((entry, index) => {
if (isStaticWidth(specs[index].width)) entry.applyWidth?.(undefined);
else entry.applyWidth?.(sizes[index]);
// Static columns must use the same clamped sizes deducted from the
// remaining space, rather than their original width/percentage.
entry.applyWidth?.(sizes[index]);
});
}
const layoutSignature = computed(() =>
@@ -254,6 +271,8 @@ export default defineComponent({
if (typeof ResizeObserver !== "undefined" && element.value) {
layoutObserver = new ResizeObserver(() => recomputeLayout());
layoutObserver.observe(element.value);
if (element.value.parentElement)
layoutObserver.observe(element.value.parentElement);
}
});
onBeforeUnmount(() => layoutObserver?.disconnect());
@@ -136,7 +136,8 @@ export default defineComponent({
if (!column?.allowsSorting.value && props.columnKey && ctx)
ctx.sort(props.columnKey);
},
onFocus: (event: FocusEvent) => forwardFocusVisible(event, true),
onFocus: (event: FocusEvent) =>
forwardFocusVisible(event, true),
onBlur: (event: FocusEvent) =>
forwardFocusVisible(event, false),
},
+10 -3
View File
@@ -2,7 +2,9 @@
* Column width resolution for managed tables (explicit `fr` widths).
*
* Ported from react-stately `TableUtils.js` / `TableColumnLayout.js`
* (Apache-2.0, https://github.com/adobe/react-spectrum) to keep the pinned
* Copyright 2022 Adobe. All rights reserved.
* Licensed under Apache-2.0 (LICENSE.react-aria in the component package).
* (https://github.com/adobe/react-spectrum) to keep the pinned
* baseline's `defaultWidth="Nfr"` weight semantics: static widths (px / %)
* are taken first, fractional widths share the remaining space by weight,
* min/max clamp with flexbox-style violation freezing, and the rounded
@@ -29,7 +31,9 @@ export function isStaticWidth(width: TableColumnWidthSpec): boolean {
export function parseFractionalUnit(width: TableColumnWidthSpec): number {
if (!width || typeof width === "number") return 1;
const match = String(width).match(/^(.+)(?=fr$)/);
return match ? parseFloat(match[0]) : 1;
const parsed = match ? Number(match[0]) : 1;
// A NaN/Infinity factor prevents violation freezing from converging.
return Number.isFinite(parsed) ? parsed : 1;
}
/** Resolve a static width against the available width (percent support). */
@@ -169,7 +173,10 @@ export function calculateColumnSizes(
}
hasNonFrozenItems = false;
for (const item of flexItems) {
if (totalViolation === 0 || Math.sign(totalViolation) === Math.sign(item.violation))
if (
totalViolation === 0 ||
Math.sign(totalViolation) === Math.sign(item.violation)
)
item.frozen = true;
else if (!item.frozen) hasNonFrozenItems = true;
}