fix(grid): toggle context transpose view

This commit is contained in:
t8y2 2026-05-18 11:09:00 +08:00
parent 1d57bc575f
commit efdf843d2f
3 changed files with 44 additions and 9 deletions

View File

@ -83,8 +83,8 @@ import {
import { formatGridSqlLiteral } from "@/lib/dataGridSql";
import {
buildTransposeRows,
nextContextTransposeState,
nextTransposeState,
transposeAnchorRowIndex,
transposeFieldWidth,
transposeScrollLeftForRecord,
visibleTransposeRecordWindow,
@ -2029,14 +2029,21 @@ function openTranspose(rowIndex: number) {
function openContextTranspose() {
if (!contextCell.value) return;
openTranspose(
transposeAnchorRowIndex({
requestedRowIndex: contextCell.value.rowIndex,
rowIds: displayItems.value.map((item) => item.id),
selectedRowIds: selectedRowIds.value,
selectedRange: selectedRange.value,
}),
);
const next = nextContextTransposeState({
showTranspose: showTranspose.value,
transposeRowIndex: transposeRowIndex.value,
requestedRowIndex: contextCell.value.rowIndex,
rowIds: displayItems.value.map((item) => item.id),
selectedRowIds: selectedRowIds.value,
selectedRange: selectedRange.value,
});
transposeRowIndex.value = next.transposeRowIndex;
showTranspose.value = next.showTranspose;
if (next.showTranspose) {
showCellDetail.value = false;
nextTick(updateTransposeViewport);
if (next.transposeRowIndex !== null) scrollTransposeRecordIntoView(next.transposeRowIndex);
}
}
function toggleTranspose(rowIndex: number) {

View File

@ -53,6 +53,11 @@ export interface TransposeAnchorOptions {
selectedRange: TransposeSelectionRange | null;
}
export interface ContextTransposeStateOptions extends TransposeAnchorOptions {
showTranspose: boolean;
transposeRowIndex: number | null;
}
export interface TransposeFieldWidthOptions {
minWidth?: number;
maxWidth?: number;
@ -79,6 +84,11 @@ export function nextTransposeState(
return { showTranspose: true, transposeRowIndex: requestedRowIndex };
}
export function nextContextTransposeState(options: ContextTransposeStateOptions): DataGridTransposeState {
const anchorRowIndex = transposeAnchorRowIndex(options);
return nextTransposeState(options.showTranspose, options.transposeRowIndex, anchorRowIndex);
}
export function buildTransposeRows<T>(options: BuildTransposeRowsOptions<T>): Array<DataGridTransposeRow<T>> {
return options.columns.map((column, columnIndex) => {
return {

View File

@ -2,6 +2,7 @@ import { strict as assert } from "node:assert";
import test from "node:test";
import {
buildTransposeRows,
nextContextTransposeState,
nextTransposeState,
transposeAnchorRowIndex,
transposeFieldWidth,
@ -30,6 +31,23 @@ test("row number double click closes transpose for the same row", () => {
});
});
test("context menu transpose closes when invoked for the current anchor row", () => {
assert.deepEqual(
nextContextTransposeState({
showTranspose: true,
transposeRowIndex: 2,
requestedRowIndex: 3,
rowIds: [10, 11, 12, 13, 14],
selectedRowIds: new Set([12, 13]),
selectedRange: null,
}),
{
showTranspose: false,
transposeRowIndex: null,
},
);
});
test("builds one virtualizable transpose row per field with all record values", () => {
const rows = buildTransposeRows({
columns: ["id", "name", "notes"],