feat(grid): allow disabling striped row background

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-04 02:09:13 +08:00
1 parent e3ea4740c0
commit 1edbb7b935
23 files changed
+144 -6

No files matched your search

@@ -103,6 +103,7 @@ export interface DrawCanvasDataGridOptions {
booleanDisplayMode?: "checkbox" | "dropdown";
flatteningMultiLineEnabled: boolean;
showWhitespace?: boolean;
stripedRows?: boolean;
/** 行号栏取值:`view` = 当前视图序号(默认,与筛选前一致),`source` = 筛选前的原始行号 */
rowNumberMode?: "view" | "source";
}
@@ -152,6 +153,15 @@ export function resolveCanvasBackingStoreMetrics(options: { width: number; heigh
};
}
export function resolveCanvasDataGridRowBase(theme: Pick<DataGridPaintTheme, "rowDeleted" | "rowNew" | "rowMuted" | "background">, item: Pick<CanvasDataGridRow, "isDeleted" | "isNew" | "isDraft" | "displayIndex">, options: { isActive: boolean; stripedRows?: boolean }): string {
const { isActive, stripedRows = true } = options;
if (item.isDeleted) return theme.rowDeleted;
if (item.isNew && !isActive) return theme.rowNew;
if (item.isDraft && !isActive) return theme.rowMuted;
if (stripedRows && item.displayIndex % 2 === 1 && !isActive) return theme.rowMuted;
return theme.background;
}
export function resolveCanvasDataGridRowFill(theme: Pick<DataGridPaintTheme, "cellActive" | "cellSelected">, rowBase: string, options: { isActive: boolean; isDeleted: boolean; isSelected: boolean }): string {
if (options.isSelected) return theme.cellSelected;
if (options.isActive && !options.isDeleted) return theme.cellActive;
@@ -368,6 +378,7 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions): boolean
booleanDisplayMode = "dropdown",
flatteningMultiLineEnabled,
showWhitespace = false,
stripedRows = true,
rowNumberMode = "view",
} = options;
// 框选热路径:整次绘制只判断一次。常见情况(单矩形 / 多列且每段都是多格)可跳过逐格 kind 查询
@@ -430,7 +441,10 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions): boolean
const rowIsActive = isRowActive(item.displayIndex);
const rowSelectionVisual = rowCellsUseSelectionVisual(item.id);
const rowBase = item.isDeleted ? theme.rowDeleted : item.isNew && !rowIsActive ? theme.rowNew : item.isDraft && !rowIsActive ? theme.rowMuted : item.displayIndex % 2 === 1 && !rowIsActive ? theme.rowMuted : theme.background;
const rowBase = resolveCanvasDataGridRowBase(theme, item, {
isActive: rowIsActive,
stripedRows,
});
const rowFill = resolveCanvasDataGridRowFill(theme, rowBase, {
isActive: rowIsActive,
isDeleted: item.isDeleted,
@@ -114,4 +114,16 @@ describe("grid snapshot rendering", () => {
expect(regular).not.toContain("integer");
expect(regular).not.toContain("Primary key");
});
it("supports disabling striped row backgrounds", () => {
const source = {
columns: ["id"],
rows: [[1], [2]],
};
const defaultHtml = renderGridSnapshotHtml(source, { appearance: "light" });
expect(defaultHtml).toContain("background:rgb(240, 240, 240)");
const unstripedHtml = renderGridSnapshotHtml(source, { appearance: "light", stripedRows: false });
expect(unstripedHtml).not.toContain("background:rgb(240, 240, 240)");
});
});
@@ -20,6 +20,7 @@ export interface GridSnapshotStyleOptions {
showRowNumbers?: boolean;
wrapCells?: boolean;
transpose?: boolean;
stripedRows?: boolean;
fieldNameLabel?: string;
compact?: boolean;
}
@@ -152,7 +153,7 @@ export function renderGridSnapshotHtml(source: GridSnapshotSource, options: Grid
const body = displayRows
.map(
(row, rowIndex) =>
`<tr style="background:${rowIndex % 2 === 1 ? GRID_SNAPSHOT_STRIPED_ROW[appearance] : GRID_SNAPSHOT_BACKGROUND[appearance]}">${showRowNumbers ? `<td class="dbx-grid-snapshot__cell dbx-grid-snapshot__cell--row-number" style="background:${GRID_SNAPSHOT_ROW_NUMBER[appearance]};color:${GRID_SNAPSHOT_MUTED[appearance]};padding:${cellPadding}">${rowIndex + 1}</td>` : ""}${transpose && showFieldNames ? renderFieldCell(source.columns[rowIndex] ?? "", rowIndex, "td") : ""}${row
`<tr style="background:${options.stripedRows !== false && rowIndex % 2 === 1 ? GRID_SNAPSHOT_STRIPED_ROW[appearance] : GRID_SNAPSHOT_BACKGROUND[appearance]}">${showRowNumbers ? `<td class="dbx-grid-snapshot__cell dbx-grid-snapshot__cell--row-number" style="background:${GRID_SNAPSHOT_ROW_NUMBER[appearance]};color:${GRID_SNAPSHOT_MUTED[appearance]};padding:${cellPadding}">${rowIndex + 1}</td>` : ""}${transpose && showFieldNames ? renderFieldCell(source.columns[rowIndex] ?? "", rowIndex, "td") : ""}${row
.map((value) => {
const nullClass = value === null ? " dbx-grid-snapshot__cell--null" : "";
return `<td class="dbx-grid-snapshot__cell${wrapCells ? " dbx-grid-snapshot__cell--wrapped" : ""}${nullClass}" style="color:${value === null ? GRID_SNAPSHOT_MUTED[appearance] : GRID_SNAPSHOT_TEXT[appearance]};padding:${cellPadding}">${escapeHtml(formatGridSnapshotCell(value))}</td>`;
@@ -72,6 +72,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
"dataGridAutoTransposeSingleRow",
"dataGridCellDetailButtonVisible",
"dataGridCrosshairHighlight",
"dataGridStripedRows",
"pageSize",
"tableOpenPageSize",
"tableOpenSortMode",
@@ -246,6 +246,7 @@ export const SETTINGS_SEARCH_DEFINITIONS: readonly SettingsSearchDefinition[] =
{ id: "data-grid-filter-view", category: "data", titleKey: "settings.dataGridFilterView", descriptionKey: "settings.dataGridFilterViewDescription", targetId: "data-grid-filter-view" },
{ id: "data-grid-flattening-multi-line", category: "data", titleKey: "settings.flatteningMultiLineText", descriptionKey: "settings.flatteningMultiLineTextDescription", targetId: "data" },
{ id: "data-grid-show-whitespace", category: "data", titleKey: "settings.dataGridShowWhitespace", descriptionKey: "settings.dataGridShowWhitespaceDescription", targetId: "data" },
{ id: "data-grid-striped-rows", category: "data", titleKey: "settings.dataGridStripedRows", descriptionKey: "settings.dataGridStripedRowsDescription", targetId: "data" },
{ id: "sql-file-editor-max-mb", category: "editor", titleKey: "settings.externalSqlEditorMaxMb", descriptionKey: "settings.externalSqlEditorMaxMbDescription", targetId: "editor-sql-file" },
{ id: "sql-file-web-upload-max-mb", category: "data", titleKey: "settings.webSqlFileUploadMaxMb", descriptionKey: "settings.webSqlFileUploadMaxMbDescription", targetId: "data-sql-file-upload", visible: (context) => context.isWeb },
{ id: "appearance-toolbar", category: "appearance", titleKey: "settings.toolbarTitle", descriptionKey: "settings.toolbarHiddenHint", targetId: "appearance" },
@@ -167,6 +167,7 @@ const SETTINGS_TRANSFER_CATEGORY_KEYS: Record<SettingsTransferCategoryId, readon
"dataGridAutoTransposeSingleRow",
"dataGridCellDetailButtonVisible",
"dataGridCrosshairHighlight",
"dataGridStripedRows",
"flatteningMultiLineText",
"dataGridShowWhitespace",
"pageSize",