fix(table): align constrained widths and guard invalid fr factors
This commit is contained in:
Vendored
+35
-2
@@ -24,6 +24,9 @@ const widths = [620, 900, 1400];
|
||||
const hostWidth = ref(900);
|
||||
const rtl = ref(false);
|
||||
const dark = ref(false);
|
||||
const layoutCase = ref("baseline");
|
||||
const showRows = ref(true);
|
||||
const extraColumn = ref(false);
|
||||
watch(
|
||||
dark,
|
||||
(value) => {
|
||||
@@ -41,6 +44,14 @@ const rows = [
|
||||
<template>
|
||||
<main style="padding: 24px; display: grid; gap: 16px">
|
||||
<div style="display: flex; gap: 16px">
|
||||
<label>
|
||||
边界场景
|
||||
<select v-model="layoutCase">
|
||||
<option value="baseline">常规列宽</option>
|
||||
<option value="fixed-max">固定列超出上限</option>
|
||||
<option value="invalid-fr">无效 fr 回退</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
容器宽度
|
||||
<select v-model.number="hostWidth">
|
||||
@@ -57,6 +68,14 @@ const rows = [
|
||||
<input v-model="dark" type="checkbox" />
|
||||
深色主题
|
||||
</label>
|
||||
<label>
|
||||
<input v-model="showRows" type="checkbox" />
|
||||
显示数据
|
||||
</label>
|
||||
<label>
|
||||
<input v-model="extraColumn" type="checkbox" />
|
||||
增加列
|
||||
</label>
|
||||
</div>
|
||||
<div
|
||||
id="table-host"
|
||||
@@ -72,6 +91,7 @@ const rows = [
|
||||
id="name"
|
||||
allows-sorting
|
||||
default-width="1fr"
|
||||
:width="layoutCase === 'invalid-fr' ? 'oopsfr' : undefined"
|
||||
:min-width="120"
|
||||
>
|
||||
<TableSortableColumnHeader>姓名</TableSortableColumnHeader>
|
||||
@@ -81,18 +101,31 @@ const rows = [
|
||||
角色
|
||||
<TableColumnResizer />
|
||||
</TableColumn>
|
||||
<TableColumn id="status" :width="180">状态</TableColumn>
|
||||
<TableColumn
|
||||
id="status"
|
||||
:width="180"
|
||||
:max-width="layoutCase === 'fixed-max' ? 100 : undefined"
|
||||
>
|
||||
状态
|
||||
</TableColumn>
|
||||
<TableColumn id="email" width="20%" :max-width="240">
|
||||
邮箱
|
||||
</TableColumn>
|
||||
<TableColumn v-if="extraColumn" id="extra">附加列</TableColumn>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
<TableRow v-for="row in rows" :key="row.id" :row-key="row.id">
|
||||
<TableRow
|
||||
v-for="row in showRows ? rows : []"
|
||||
:key="row.id"
|
||||
:row-key="row.id"
|
||||
>
|
||||
<TableCell>{{ row.name }}</TableCell>
|
||||
<TableCell>{{ row.role }}</TableCell>
|
||||
<TableCell>在职</TableCell>
|
||||
<TableCell>{{ row.email }}</TableCell>
|
||||
<TableCell v-if="extraColumn">附加内容</TableCell>
|
||||
</TableRow>
|
||||
<template #empty>未找到结果</template>
|
||||
</TableBody>
|
||||
</TableContent>
|
||||
</TableScrollContainer>
|
||||
|
||||
+35
-2
@@ -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]);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user