feat(grid): customize zebra stripe color

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-04 10:31:39 +08:00
1 parent 4b48809c84
commit 89ecc63b9e
22 files changed
+107 -7

No files matched your search

@@ -237,4 +237,26 @@ describe("drawCanvasDataGrid with frozen columns", () => {
});
expect(() => drawCanvasDataGrid(options)).not.toThrow();
});
it("draws with zebraStriping enabled and disabled", () => {
const canvas = createMockCanvas();
const optionsWithZebra = createBaseOptions({
canvas,
zebraStriping: true,
});
expect(() => drawCanvasDataGrid(optionsWithZebra)).not.toThrow();
const optionsWithoutZebra = createBaseOptions({
canvas,
zebraStriping: false,
});
expect(() => drawCanvasDataGrid(optionsWithoutZebra)).not.toThrow();
const optionsWithCustomZebraColor = createBaseOptions({
canvas,
zebraStriping: true,
zebraRowBg: "#334455",
});
expect(() => drawCanvasDataGrid(optionsWithCustomZebraColor)).not.toThrow();
});
});
@@ -104,6 +104,8 @@ export interface DrawCanvasDataGridOptions {
flatteningMultiLineEnabled: boolean;
showWhitespace?: boolean;
stripedRows?: boolean;
zebraStriping?: boolean;
zebraRowBg?: string;
/** 行号栏取值:`view` = 当前视图序号(默认,与筛选前一致),`source` = 筛选前的原始行号 */
rowNumberMode?: "view" | "source";
}
@@ -153,12 +155,18 @@ 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;
export function resolveCanvasDataGridRowBase(
theme: Pick<DataGridPaintTheme, "rowDeleted" | "rowNew" | "rowMuted" | "background">,
item: Pick<CanvasDataGridRow, "isDeleted" | "isNew" | "isDraft" | "displayIndex">,
options: { isActive: boolean; stripedRows?: boolean; zebraStriping?: boolean; zebraRowBg?: string },
): string {
const { isActive } = options;
const stripedRows = options.stripedRows ?? options.zebraStriping ?? true;
const stripedRowBg = options.zebraRowBg?.trim() || theme.rowMuted;
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;
if (stripedRows && item.displayIndex % 2 === 1 && !isActive) return stripedRowBg;
return theme.background;
}
@@ -378,9 +386,12 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions): boolean
booleanDisplayMode = "dropdown",
flatteningMultiLineEnabled,
showWhitespace = false,
stripedRows = true,
stripedRows: rawStripedRows,
zebraStriping,
zebraRowBg,
rowNumberMode = "view",
} = options;
const stripedRows = rawStripedRows ?? zebraStriping ?? true;
// 框选热路径:整次绘制只判断一次。常见情况(单矩形 / 多列且每段都是多格)可跳过逐格 kind 查询
const paintSelectionOuterFrame = dataGridSelectionUsesOuterFrame(selectionFrames);
const suppressAllSelectedCellBorders = selectionFrames.length > 0 && selectionFrames.every(dataGridFrameIsMultiCell);
@@ -444,6 +455,8 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions): boolean
const rowBase = resolveCanvasDataGridRowBase(theme, item, {
isActive: rowIsActive,
stripedRows,
zebraStriping,
zebraRowBg,
});
const rowFill = resolveCanvasDataGridRowFill(theme, rowBase, {
isActive: rowIsActive,
@@ -143,6 +143,10 @@ describe("EDITOR_SETTINGS_DRAFT_KEYS", () => {
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridCellDetailButtonVisible");
});
it("includes zebra row background in draft keys", () => {
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridZebraRowBg");
});
it("includes dataGridCellDetailDialogDefault", () => {
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridCellDetailDialogDefault");
});
@@ -475,4 +475,17 @@ describe("settingsTransfer", () => {
expect(transferCategoryForKey("csvNullMode")).toBe("data");
expect(collectTransferCategories(["csvNullMode", "csvQuoteMode"])).toEqual(["data"]);
});
it("round-trips zebra row background in data category", () => {
expect(transferCategoryForKey("dataGridZebraRowBg")).toBe("data");
const text = serializeSettingsTransfer({
...DEFAULT_EDITOR_SETTINGS,
dataGridZebraRowBg: "#232323",
});
const result = parseSettingsTransferFile(text);
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value.editorSettings.dataGridZebraRowBg).toBe("#232323");
});
});
@@ -76,6 +76,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
"dataGridCellDetailDialogDefault",
"dataGridCrosshairHighlight",
"dataGridStripedRows",
"dataGridZebraRowBg",
"pageSize",
"tableOpenPageSize",
"tableOpenSortMode",
@@ -169,6 +169,7 @@ const SETTINGS_TRANSFER_CATEGORY_KEYS: Record<SettingsTransferCategoryId, readon
"dataGridCellDetailDialogDefault",
"dataGridCrosshairHighlight",
"dataGridStripedRows",
"dataGridZebraRowBg",
"flatteningMultiLineText",
"dataGridShowWhitespace",
"pageSize",
@@ -289,6 +290,7 @@ const PASS_THROUGH_BOOLEAN_KEYS = [
"showIndexIndicatorsInHeader",
"compactColumnHeaderActions",
"dataGridQuickEntry",
"dataGridStripedRows",
"flatteningMultiLineText",
"dataGridShowWhitespace",
"infiniteScroll",