feat(data-grid): hide selected columns from header context menu

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-09-14 12:39:06 +08:00
1 parent 974848d6e2
commit 638aa48f57
22 files changed
+460 -16

No files matched your search

@@ -138,4 +138,87 @@ describe("dataGridContextMenu", () => {
});
expect(rowItems.find((item) => item.label === "delete")?.variant).toBe("destructive");
});
function columnMenuItems(overrides: { hasColumnSelection?: boolean; selectedColumnCount?: number; visibleColumnCount?: number; hiddenColumnCount?: number }) {
const action = vi.fn();
const filter = createDataGridFilterSubmenu({
label: "filter",
icon,
labels: { equals: "equals", notEquals: "not equals", like: "like", notLike: "not like", lessThan: "less", greaterThan: "greater", isNull: "null", isNotNull: "not null", clear: "clear" },
apply: action,
clear: action,
});
return createDataGridColumnContextMenuItems({
headerColumn: true,
contextColumn: false,
canCopyAlterSql: false,
canFilter: false,
hasSort: false,
sortMode: "database",
contextVisibleColIdx: 0,
hasColumnSelection: false,
selectedColumnCount: 1,
visibleColumnCount: 3,
hiddenColumnCount: 0,
labels: {
copyName: "copy name",
copyNames: "copy names",
details: "details",
copyAlterSql: "alter",
databaseAscending: "db asc",
databaseDescending: "db desc",
localAscending: "local asc",
localDescending: "local desc",
clearSort: "clear sort",
freezeToColumn: "freeze to column",
freezeSelectedColumns: "freeze selected",
unfreezeColumns: "unfreeze",
hideColumn: "hide column",
hideSelectedColumns: "hide selected (3)",
showAllColumnsMenu: "show all columns",
},
icons: { copy: icon, columnDetails: icon, database: icon, ascending: icon, descending: icon, clearSort: icon },
actions: { copyName: action, copyNames: action, details: action, copyAlterSql: action, sort: action, freezeToColumn: action, freezeSelectedColumns: action, unfreezeColumns: action, hideColumn: action, hideSelectedColumns: action, showAllColumnsMenu: action },
filterSubmenu: filter,
...overrides,
});
}
it("offers hide-this-column on a header and hides the recovery entry until a column is hidden", () => {
const labels = columnMenuItems({}).map((item) => item.label);
expect(labels).toContain("hide column");
expect(labels).not.toContain("hide selected (3)");
expect(labels).not.toContain("show all columns");
});
it("offers batch hiding for a multi-column selection and shows the recovery entry when columns are hidden", () => {
const items = columnMenuItems({ hasColumnSelection: true, selectedColumnCount: 3, visibleColumnCount: 5, hiddenColumnCount: 2 });
const hideSelected = items.find((item) => item.label === "hide selected (3)");
expect(hideSelected?.disabled).toBe(false);
expect(items.map((item) => item.label)).toContain("show all columns");
});
it("disables batch hiding when the selection covers every visible column", () => {
const items = columnMenuItems({ hasColumnSelection: true, selectedColumnCount: 3, visibleColumnCount: 3 });
expect(items.find((item) => item.label === "hide selected (3)")?.disabled).toBe(true);
});
it("disables hiding this column when only one column is visible", () => {
const items = columnMenuItems({ visibleColumnCount: 1 });
const hideColumn = items.find((item) => item.label === "hide column");
expect(hideColumn).toBeDefined();
expect(hideColumn?.disabled).toBe(true);
});
it("keeps hiding this column enabled for callers that omit the visible column count", () => {
const items = columnMenuItems({ visibleColumnCount: undefined });
const hideColumn = items.find((item) => item.label === "hide column");
expect(hideColumn).toBeDefined();
expect(hideColumn?.disabled).toBe(false);
});
});
@@ -65,6 +65,34 @@ export function nextHiddenColumnIndexes(options: { columnIndex: number; hiddenIn
return next;
}
/**
* Batch counterpart of {@link nextHiddenColumnIndexes}: hides a whole set of
* columns in one deterministic step and keeps at least one column visible.
* When the batch would hide every visible column, the first currently visible
* column is kept instead (same rule as the single-column toggle, expressed once
* for a set). Duplicate or already-hidden indexes are ignored, and an empty
* batch is a no-op.
*/
export function hiddenColumnIndexesAfterHiding(options: { columnIndexes: Iterable<number>; hiddenIndexes: ReadonlySet<number>; availableIndexes: readonly number[] }): Set<number> {
const next = new Set(options.hiddenIndexes);
const available = new Set(options.availableIndexes);
const visibleIndexes = options.availableIndexes.filter((index) => !next.has(index));
const requestedIndexes = [...new Set(options.columnIndexes)].filter((index) => available.has(index) && !next.has(index));
if (requestedIndexes.length === 0) return next;
if (visibleIndexes.length - requestedIndexes.length >= 1) {
for (const index of requestedIndexes) next.add(index);
return next;
}
// 会隐藏全部可见列:保留最前面的一个可见列,其余全部隐藏。
const keptIndex = visibleIndexes[0];
for (const index of visibleIndexes) {
if (index !== keptIndex) next.add(index);
}
return next;
}
export function invertedHiddenColumnIndexes(availableIndexes: number[], hiddenIndexes: ReadonlySet<number>): Set<number> {
const next = new Set(availableIndexes.filter((index) => !hiddenIndexes.has(index)));
if (next.size === availableIndexes.length && availableIndexes.length > 0) {
@@ -81,9 +81,30 @@ export function createDataGridColumnContextMenuItems(options: {
frozenColumnCount?: number;
contextVisibleColIdx?: number;
hasColumnSelection?: boolean;
labels: Record<"copyName" | "copyNames" | "details" | "copyAlterSql" | "databaseAscending" | "databaseDescending" | "localAscending" | "localDescending" | "clearSort" | "freezeToColumn" | "freezeSelectedColumns" | "unfreezeColumns", string>;
/** 显式多选的可见列数量;为 undefined 时按 0 处理。 */
selectedColumnCount?: number;
/** 当前可见列总数,用于判断「选中列覆盖全部可见列」而禁用批量隐藏。 */
visibleColumnCount?: number;
/** 当前被隐藏的列数量,用于决定是否显示「显示全部列」。 */
hiddenColumnCount?: number;
labels: Record<
"copyName" | "copyNames" | "details" | "copyAlterSql" | "databaseAscending" | "databaseDescending" | "localAscending" | "localDescending" | "clearSort" | "freezeToColumn" | "freezeSelectedColumns" | "unfreezeColumns" | "hideColumn" | "hideSelectedColumns" | "showAllColumnsMenu",
string
>;
icons: Pick<DataGridContextMenuIcons, "copy" | "columnDetails" | "database" | "ascending" | "descending" | "clearSort">;
actions: { copyName: () => void; copyNames: () => void; details: () => void; copyAlterSql: () => void; sort: (direction: "asc" | "desc" | null, mode: "database" | "local") => void; freezeToColumn: () => void; freezeSelectedColumns: () => void; unfreezeColumns: () => void };
actions: {
copyName: () => void;
copyNames: () => void;
details: () => void;
copyAlterSql: () => void;
sort: (direction: "asc" | "desc" | null, mode: "database" | "local") => void;
freezeToColumn: () => void;
freezeSelectedColumns: () => void;
unfreezeColumns: () => void;
hideColumn: () => void;
hideSelectedColumns: () => void;
showAllColumnsMenu: () => void;
};
filterSubmenu: DataGridContextMenuItem;
}): DataGridContextMenuItem[] {
const items: DataGridContextMenuItem[] = [];
@@ -108,6 +129,18 @@ export function createDataGridColumnContextMenuItems(options: {
}
if (options.contextVisibleColIdx !== undefined) {
items.push({ label: "", separator: true });
const selectedColumnCount = options.selectedColumnCount ?? 0;
const visibleColumnCount = options.visibleColumnCount ?? 0;
// 只剩一列时隐藏会被 helper 拒绝;用 disabled 明确表达,而不是静默无效。
// 这里读 options.visibleColumnCount 而非本地默认值:字段可选,未传的调用方不应被误禁用。
items.push({ label: options.labels.hideColumn, action: options.actions.hideColumn, disabled: options.visibleColumnCount !== undefined && options.visibleColumnCount <= 1 });
// 仅在多选(>1)时提供批量隐藏;选中列覆盖全部可见列时禁用,而不是悄悄少隐藏一列。
if (options.hasColumnSelection && selectedColumnCount > 1) {
items.push({ label: options.labels.hideSelectedColumns, action: options.actions.hideSelectedColumns, disabled: visibleColumnCount > 0 && selectedColumnCount >= visibleColumnCount });
}
if ((options.hiddenColumnCount ?? 0) > 0) {
items.push({ label: options.labels.showAllColumnsMenu, action: options.actions.showAllColumnsMenu });
}
if ((options.frozenColumnCount ?? 0) > 0) {
items.push({ label: options.labels.unfreezeColumns, action: options.actions.unfreezeColumns });
}