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
+35 -2
View File
@@ -1,6 +1,10 @@
import { afterEach, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h, nextTick, ref } from "vue";
import {
calculateColumnSizes,
parseFractionalUnit,
} from "../packages/vue/src/components/table/widths";
import {
Table,
TableBody,
@@ -247,6 +251,7 @@ it("empty state spans every leaf column by default and honors explicit colspan",
explicit.value = 1;
await nextTick();
expect(cell().getAttribute("colspan")).toBe("1");
w.unmount();
});
it("managed fr widths distribute the remaining space by weight with min/max clamping", async () => {
@@ -271,6 +276,7 @@ it("managed fr widths distribute the remaining space by weight with min/max clam
try {
const minA = ref<number | undefined>(undefined);
const maxB = ref<number | undefined>(undefined);
const maxFixed = ref<number | undefined>(undefined);
const w = mount(
defineComponent({
setup: () => ({ minA, maxB }),
@@ -288,7 +294,11 @@ it("managed fr widths distribute the remaining space by weight with min/max clam
{ id: "b", defaultWidth: "2fr", maxWidth: maxB.value },
() => "B",
),
h(TableColumn, { id: "c", width: 300 }, () => "C"),
h(
TableColumn,
{ id: "c", width: 300, maxWidth: maxFixed.value },
() => "C",
),
]),
),
),
@@ -314,6 +324,19 @@ it("managed fr widths distribute the remaining space by weight with min/max clam
maxB.value = 150;
await flushPromises();
expect(widths()).toEqual(["450px", "150px", "300px"]);
maxFixed.value = 100;
await flushPromises();
expect(widths()).toEqual(["650px", "150px", "100px"]);
expect((w.findAll("th")[2].element as HTMLElement).style.minWidth).toBe("");
// Existing assigned pixels must not mask a narrower parent viewport.
Object.defineProperty(w.get("table").element.parentElement, "clientWidth", {
configurable: true,
value: 600,
});
maxFixed.value = 120;
await flushPromises();
expect(widths()).toEqual(["400px", "80px", "120px"]);
w.unmount();
} finally {
vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockRestore();
void original;
@@ -333,6 +356,7 @@ it("sort activates once per click and forwards focus-visible to the column heade
// The trigger owns keyboard focus; the ring is carried by the th through its
// real :focus-visible state (baseline position).
const originalMatches = HTMLElement.prototype.matches;
const matches = vi
.spyOn(HTMLElement.prototype, "matches")
.mockImplementation(function (this: HTMLElement, selector: string) {
@@ -340,7 +364,6 @@ it("sort activates once per click and forwards focus-visible to the column heade
return this.tagName === "BUTTON" && triggerFocused;
return originalMatches.call(this, selector);
});
const originalMatches = HTMLElement.prototype.matches;
let triggerFocused = false;
try {
triggerFocused = true;
@@ -351,5 +374,15 @@ it("sort activates once per click and forwards focus-visible to the column heade
expect(th.attributes("data-focus-visible")).toBeUndefined();
} finally {
matches.mockRestore();
w.unmount();
}
});
it("invalid fr factors fall back to a finite share and terminate allocation", () => {
for (const spec of ["oopsfr", "NaNfr", "Infinityfr", "1e999fr"]) {
expect(parseFractionalUnit(spec)).toBe(1);
expect(
calculateColumnSizes(900, [{ width: spec }, { width: "2fr" }]),
).toEqual([300, 600]);
}
});