feat(ui): progressively shrink data grid toolbar

This commit is contained in:
zipg authored and GitHub committed 2026-09-04 17:10:50 +08:00
1 parent f861776c3c
commit 6e9e8d1818
15 files changed
+188 -67

No files matched your search

@@ -1,9 +1,12 @@
import { describe, expect, it, vi } from "vitest";
import {
DATA_GRID_CONDITION_TOOLBAR_MIN_WIDTH,
DATA_GRID_TOOLBAR_ACTION_COLLAPSE_ORDER,
dataGridDeleteRowToolbarState,
dataGridToolbarActionCollapseCount,
dataGridToolbarCompactBreakpoint,
dataGridToolbarIntervalOptions,
isDataGridToolbarActionCompact,
isDataGridToolbarCompact,
selectDataGridToolbarAddRowItem,
selectDataGridToolbarAutoRefreshInterval,
@@ -63,6 +66,29 @@ describe("data grid toolbar capabilities", () => {
expect(isDataGridToolbarCompact(1000, 1100)).toBe(false);
});
it("progressively compacts toolbar actions as the available width shrinks", () => {
expect(dataGridToolbarActionCollapseCount(1050, 1440, DATA_GRID_CONDITION_TOOLBAR_MIN_WIDTH)).toBe(0);
expect(dataGridToolbarActionCollapseCount(1049, 1440, DATA_GRID_CONDITION_TOOLBAR_MIN_WIDTH)).toBe(1);
expect(dataGridToolbarActionCollapseCount(1010, 1440, DATA_GRID_CONDITION_TOOLBAR_MIN_WIDTH)).toBe(1);
expect(dataGridToolbarActionCollapseCount(1009, 1440, DATA_GRID_CONDITION_TOOLBAR_MIN_WIDTH)).toBe(2);
expect(dataGridToolbarActionCollapseCount(0, 1440, DATA_GRID_CONDITION_TOOLBAR_MIN_WIDTH)).toBe(0);
expect(dataGridToolbarActionCollapseCount(100, 1440, DATA_GRID_CONDITION_TOOLBAR_MIN_WIDTH)).toBe(DATA_GRID_TOOLBAR_ACTION_COLLAPSE_ORDER.length);
});
it("compacts visible actions from left to right while skipping unavailable actions", () => {
const visibleActions = ["refresh", "navigation", "addRow", "preview", "save", "rollback"] as const;
expect(DATA_GRID_TOOLBAR_ACTION_COLLAPSE_ORDER.slice(-3)).toEqual(["preview", "save", "rollback"]);
expect(isDataGridToolbarActionCompact("refresh", visibleActions, 3)).toBe(true);
expect(isDataGridToolbarActionCompact("navigation", visibleActions, 3)).toBe(true);
expect(isDataGridToolbarActionCompact("addRow", visibleActions, 3)).toBe(true);
expect(isDataGridToolbarActionCompact("preview", visibleActions, 3)).toBe(false);
expect(isDataGridToolbarActionCompact("save", visibleActions, 3)).toBe(false);
expect(isDataGridToolbarActionCompact("rollback", visibleActions, 3)).toBe(false);
expect(isDataGridToolbarActionCompact("autoRefresh", visibleActions, 3)).toBe(false);
expect(isDataGridToolbarActionCompact("rollback", visibleActions, 0, true)).toBe(true);
});
it("does not invoke hidden or disabled actions", async () => {
const onTrigger = vi.fn();
@@ -1,8 +1,13 @@
const DATA_GRID_COMPACT_TOPBAR_MIN_WIDTH = 900;
const DATA_GRID_COMPACT_TOPBAR_MAX_WIDTH = 1050;
const DATA_GRID_COMPACT_TOPBAR_VIEWPORT_RATIO = 0.75;
const DATA_GRID_TOOLBAR_ACTION_COLLAPSE_STEP = 40;
export const DATA_GRID_CONDITION_TOOLBAR_MIN_WIDTH = DATA_GRID_COMPACT_TOPBAR_MAX_WIDTH;
export const DATA_GRID_TOOLBAR_ACTION_COLLAPSE_ORDER = ["refresh", "autoRefresh", "navigation", "copyData", "addRow", "deleteRow", "exportData", "transpose", "tableInfo", "layerPreview", "preview", "save", "rollback"] as const;
export type DataGridToolbarActionKey = (typeof DATA_GRID_TOOLBAR_ACTION_COLLAPSE_ORDER)[number];
export function dataGridToolbarCompactBreakpoint(viewportWidth: number, minimumWidth = DATA_GRID_COMPACT_TOPBAR_MIN_WIDTH): number {
const normalizedViewportWidth = Number.isFinite(viewportWidth) ? Math.max(0, viewportWidth) : DATA_GRID_COMPACT_TOPBAR_MAX_WIDTH;
const normalizedMinimumWidth = Math.min(DATA_GRID_COMPACT_TOPBAR_MAX_WIDTH, Math.max(DATA_GRID_COMPACT_TOPBAR_MIN_WIDTH, minimumWidth));
@@ -13,6 +18,20 @@ export function isDataGridToolbarCompact(toolbarWidth: number, viewportWidth: nu
return toolbarWidth > 0 && toolbarWidth < dataGridToolbarCompactBreakpoint(viewportWidth, minimumWidth);
}
export function dataGridToolbarActionCollapseCount(toolbarWidth: number, viewportWidth: number, minimumWidth?: number): number {
if (!Number.isFinite(toolbarWidth) || toolbarWidth <= 0) return 0;
const deficit = dataGridToolbarCompactBreakpoint(viewportWidth, minimumWidth) - toolbarWidth;
if (deficit <= 0) return 0;
return Math.min(DATA_GRID_TOOLBAR_ACTION_COLLAPSE_ORDER.length, Math.ceil(deficit / DATA_GRID_TOOLBAR_ACTION_COLLAPSE_STEP));
}
export function isDataGridToolbarActionCompact(action: DataGridToolbarActionKey, visibleActions: readonly DataGridToolbarActionKey[], compactActionCount: number, forceCompact = false): boolean {
if (forceCompact) return true;
const actionIndex = visibleActions.indexOf(action);
if (actionIndex < 0) return false;
return actionIndex < Math.max(0, Math.floor(compactActionCount));
}
export type DataGridReloadIntent = "refresh";
export interface DataGridToolbarActionCapability {