Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Measure all columns to provide expand option (#67429)

Fraser286b89bf92e4
diff --git a/frontend/src/metabase/data-grid/hooks/use-data-grid-instance.tsx b/frontend/src/metabase/data-grid/hooks/use-data-grid-instance.tsx
index 598aeedbeef..99a449468fa 100644
--- a/frontend/src/metabase/data-grid/hooks/use-data-grid-instance.tsx
+++ b/frontend/src/metabase/data-grid/hooks/use-data-grid-instance.tsx
@@ -336,10 +336,7 @@ export const useDataGridInstance = <TData, TValue>({
setColumnSizingMap(newWidths);
};
- measureColumnWidths(
- handleColumnsMeasured,
- Object.keys(preserveColumnSizingMap),
- );
+ measureColumnWidths(handleColumnsMeasured);
},
[
measureColumnWidths,
diff --git a/frontend/src/metabase/data-grid/hooks/use-measure-column-widths.tsx b/frontend/src/metabase/data-grid/hooks/use-measure-column-widths.tsx
index 0977a5c9ed1..25d9c2df45f 100644
--- a/frontend/src/metabase/data-grid/hooks/use-measure-column-widths.tsx
+++ b/frontend/src/metabase/data-grid/hooks/use-measure-column-widths.tsx
@@ -41,15 +41,10 @@ export const useMeasureColumnWidths = <TData, TValue>(
) => React.ReactElement,
) => {
const measureColumnWidths = useCallback(
- (
- onMeasured: (columnSizingState: ColumnSizingState) => void,
- skipColumnIds: string[] = [],
- ) => {
+ (onMeasured: (columnSizingState: ColumnSizingState) => void) => {
const measureRoot = createMeasurementContainer();
let measureRootTree: Root | undefined = undefined;
- const skipColumnIdsSet = new Set(skipColumnIds);
-
const onMeasureHeaderRender = (div: HTMLDivElement) => {
if (div === null) {
return;
@@ -107,7 +102,6 @@ export const useMeasureColumnWidths = <TData, TValue>(
{table
.getHeaderGroups()
.flatMap((headerGroup) => headerGroup.headers)
- .filter((header) => !skipColumnIdsSet.has(header.column.id))
.map((header) => {
const headerCell = flexRender(
header.column.columnDef.header,
@@ -138,11 +132,7 @@ export const useMeasureColumnWidths = <TData, TValue>(
(rowIndex) => {
const cell = rows[rowIndex]
.getVisibleCells()
- .find(
- (cell) =>
- cell.column.id === columnOptions.id &&
- !skipColumnIdsSet.has(cell.column.id),
- );
+ .find((cell) => cell.column.id === columnOptions.id);
if (!cell) {
return null;
diff --git a/frontend/src/metabase/data-grid/utils/columns/data-column.unit.spec.tsx b/frontend/src/metabase/data-grid/utils/columns/data-column.unit.spec.tsx
new file mode 100644
index 00000000000..5f07ffaa490
--- /dev/null
+++ b/frontend/src/metabase/data-grid/utils/columns/data-column.unit.spec.tsx
@@ -0,0 +1,107 @@
+import { screen } from "@testing-library/react";
+
+import { renderWithProviders } from "__support__/ui";
+
+import { getDataColumn } from "./data-column";
+
+const setup = ({
+ columnSize,
+ measuredColumnSize,
+ expandColumn = false,
+ truncateWidth = 100,
+ columnWrap = false,
+}: {
+ columnSize: number;
+ measuredColumnSize: number;
+ expandColumn?: boolean;
+ truncateWidth?: number;
+ columnWrap?: boolean;
+}) => {
+ const onExpand = jest.fn();
+ const column = getDataColumn(
+ {
+ id: "test-column",
+ name: "Test Column",
+ accessorFn: (row: { value: string }) => row.value,
+ wrap: columnWrap,
+ },
+ { "test-column": columnSize },
+ { "test-column": measuredColumnSize },
+ { "test-column": expandColumn },
+ truncateWidth,
+ onExpand,
+ );
+
+ const CellComponent = column.cell as React.ComponentType<{
+ getValue: () => string;
+ row: { index: number; original: { value: string } };
+ }>;
+
+ renderWithProviders(
+ <CellComponent
+ getValue={() => "test value"}
+ row={{ index: 0, original: { value: "test value" } }}
+ />,
+ );
+};
+
+describe("getDataColumn", () => {
+ describe("expand button", () => {
+ it("shows expand button when column is truncated", () => {
+ setup({
+ columnSize: 150,
+ measuredColumnSize: 300,
+ });
+
+ expect(screen.getByTestId("expand-column")).toBeInTheDocument();
+ });
+
+ it("does not show expand button when column width equals measured width", () => {
+ setup({
+ columnSize: 200,
+ measuredColumnSize: 200,
+ });
+
+ expect(screen.queryByTestId("expand-column")).not.toBeInTheDocument();
+ });
+
+ it("does not show expand button when column width exceeds measured width", () => {
+ setup({
+ columnSize: 300,
+ measuredColumnSize: 200,
+ });
+
+ expect(screen.queryByTestId("expand-column")).not.toBeInTheDocument();
+ });
+
+ it("does not show expand button when measured width is below truncate threshold", () => {
+ setup({
+ columnSize: 50,
+ measuredColumnSize: 80,
+ truncateWidth: 100,
+ });
+
+ expect(screen.queryByTestId("expand-column")).not.toBeInTheDocument();
+ });
+
+ it("does not show expand button when column is already expanded", () => {
+ setup({
+ columnSize: 150,
+ measuredColumnSize: 300,
+ expandColumn: true,
+ });
+
+ expect(screen.queryByTestId("expand-column")).not.toBeInTheDocument();
+ });
+
+ it("does not show expand button when wrap is enabled", () => {
+ setup({
+ columnSize: 150,
+ measuredColumnSize: 300,
+ columnWrap: true,
+ });
+
+ expect(screen.queryByTestId("expand-column")).not.toBeInTheDocument();
+ });
+ });
+});