-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make the preview table column width reactive (#4864)
* Make the preview table column width reactive * Add test
- Loading branch information
1 parent
38796b4
commit 02515c3
Showing
3 changed files
with
116 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
web-common/src/components/virtualized-table/columnSizes.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { VirtualizedTableColumnSizes } from "@rilldata/web-common/components/virtualized-table/columnSizes"; | ||
import { describe, it, expect } from "vitest"; | ||
|
||
describe("VirtualizedTableColumnSizes", () => { | ||
it("Sanity check", () => { | ||
const sizes = new VirtualizedTableColumnSizes(); | ||
expect( | ||
sizes.get( | ||
"model", | ||
[{ name: "col1" }, { name: "col2" }, { name: "col3" }], | ||
"name", | ||
() => [50, 50, 50], | ||
), | ||
).toEqual([50, 50, 50]); | ||
|
||
// set the width for col2 to something specific | ||
sizes.set("model", "col2", 100); | ||
// the width is saved | ||
expect( | ||
sizes.get( | ||
"model", | ||
[{ name: "col1" }, { name: "col2" }, { name: "col3" }], | ||
"name", | ||
() => [50, 50, 50], | ||
), | ||
).toEqual([50, 100, 50]); | ||
|
||
// the width is saved when one of the columns is removed | ||
expect( | ||
sizes.get("model", [{ name: "col1" }, { name: "col2" }], "name", () => [ | ||
50, 50, | ||
]), | ||
).toEqual([50, 100]); | ||
|
||
expect( | ||
sizes.get("model", [{ name: "col1" }, { name: "col4" }], "name", () => [ | ||
50, 50, | ||
]), | ||
).toEqual([50, 50]); | ||
// removing a column will remove any saved sizes. this is for cleaning up leaks | ||
expect( | ||
sizes.get( | ||
"model", | ||
[{ name: "col1" }, { name: "col2" }, { name: "col4" }], | ||
"name", | ||
() => [50, 50, 50], | ||
), | ||
).toEqual([50, 50, 50]); | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
web-common/src/components/virtualized-table/columnSizes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { VirtualizedTableColumns } from "@rilldata/web-common/components/virtualized-table/types"; | ||
import { V1MetricsViewColumn } from "@rilldata/web-common/runtime-client"; | ||
|
||
export class VirtualizedTableColumnSizes { | ||
private readonly sizesCache = new Map<string, Map<string, number>>(); | ||
|
||
public get( | ||
key: string, | ||
columns: (VirtualizedTableColumns | V1MetricsViewColumn)[], | ||
columnAccessor: keyof VirtualizedTableColumns, | ||
calculator: () => number[], | ||
): number[] { | ||
const cache = this.sizesCache.get(key); | ||
if (!cache) { | ||
const sizes = calculator(); | ||
this.sizesCache.set( | ||
key, | ||
new Map<string, number>( | ||
sizes.map((s, i) => [columns[i][columnAccessor], s]), | ||
), | ||
); | ||
return sizes; | ||
} | ||
|
||
let missingSize = false; | ||
const sizes = columns.map((column) => { | ||
if (!cache.has(column[columnAccessor])) { | ||
missingSize = true; | ||
return 0; | ||
} | ||
return cache.get(column[columnAccessor]) as number; | ||
}); | ||
if (!missingSize) return sizes; | ||
|
||
const newSizes = calculator(); | ||
// retain sizes from cache | ||
newSizes.forEach((_, i) => { | ||
if (cache.has(columns[i][columnAccessor])) { | ||
newSizes[i] = cache.get(columns[i][columnAccessor]) as number; | ||
} | ||
}); | ||
// reset the cache | ||
this.sizesCache.set( | ||
key, | ||
new Map<string, number>( | ||
newSizes.map((s, i) => [columns[i][columnAccessor], s]), | ||
), | ||
); | ||
|
||
return newSizes; | ||
} | ||
|
||
public set(name: string, colName: string, value: number) { | ||
this.sizesCache.get(name)?.set(colName, value); | ||
} | ||
} |
02515c3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://ui.rilldata.in as production
🚀 Deployed on https://663cdf21c63fcd008b8e043e--rill-ui-dev.netlify.app