Skip to content

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

Break lines on every line break in wrapped table cells (#83773)

Aleksandr Lesnenkof87f3ab65fc9
diff --git a/frontend/src/metabase/utils/formatting/index.ts b/frontend/src/metabase/utils/formatting/index.ts
index 901d461736d..842b25453b5 100644
--- a/frontend/src/metabase/utils/formatting/index.ts
+++ b/frontend/src/metabase/utils/formatting/index.ts
@@ -32,6 +32,7 @@ export {
conjunct,
humanize,
inflect,
+ normalizeNewLines,
pluralize,
removeNewLines,
singularize,
diff --git a/frontend/src/metabase/utils/formatting/strings.ts b/frontend/src/metabase/utils/formatting/strings.ts
index e1872ec7f28..178e98b40ac 100644
--- a/frontend/src/metabase/utils/formatting/strings.ts
+++ b/frontend/src/metabase/utils/formatting/strings.ts
@@ -55,11 +55,20 @@ export function slugify(name: string) {
return name && encodeURIComponent(name.toLowerCase().replace(/\s/g, "_"));
}
+// \r\n (Windows), \r (old Mac), \n (Unix), and Unicode line/paragraph separators
+const LINE_BREAK_PATTERN = /\r\n|\r|\n|\u0085|\u2028|\u2029/g;
+
export function removeNewLines<T>(value: T) {
if (typeof value === "string") {
- // Replace all common newline sequences with a single space
- // Handles: \r\n (Windows), \r (old Mac), \n (Unix), and Unicode line/paragraph separators
- return value.replace(/\r\n|\r|\n|\u0085|\u2028|\u2029/g, " ");
+ return value.replace(LINE_BREAK_PATTERN, " ");
+ }
+ return value;
+}
+
+// white-space: pre-wrap breaks lines only on \n
+export function normalizeNewLines<T>(value: T) {
+ if (typeof value === "string") {
+ return value.replace(LINE_BREAK_PATTERN, "\n");
}
return value;
}
diff --git a/frontend/src/metabase/visualizations/components/TableInteractive/TableInteractive.tsx b/frontend/src/metabase/visualizations/components/TableInteractive/TableInteractive.tsx
index c17efd1ad5b..17c4d29ef17 100644
--- a/frontend/src/metabase/visualizations/components/TableInteractive/TableInteractive.tsx
+++ b/frontend/src/metabase/visualizations/components/TableInteractive/TableInteractive.tsx
@@ -46,6 +46,7 @@ import { useDispatch } from "metabase/redux";
import { setUIControls } from "metabase/redux/query-builder";
import { Flex, type MantineTheme } from "metabase/ui";
import { getScrollBarSize } from "metabase/utils/dom";
+import { normalizeNewLines } from "metabase/utils/formatting";
import { memoize } from "metabase/utils/memoize";
import { formatValue } from "metabase/value-formatting";
import { createPlainCellFormatter } from "metabase/visualizations/lib/plain-cell-formatter";
@@ -291,7 +292,7 @@ export const TableInteractiveInner = forwardRef(function TableInteractiveInner(
const value = tc(untranslatedValue);
- return formatValue(value, {
+ const formattedValue = formatValue(value, {
...columnSettings,
type: "cell",
jsx: true,
@@ -299,6 +300,8 @@ export const TableInteractiveInner = forwardRef(function TableInteractiveInner(
clicked,
collapseNewlines: !wrap,
});
+
+ return wrap ? normalizeNewLines(formattedValue) : formattedValue;
},
);
diff --git a/frontend/src/metabase/visualizations/visualizations/Table/Table.unit.spec.tsx b/frontend/src/metabase/visualizations/visualizations/Table/Table.unit.spec.tsx
index 3640ef72c09..a7e565ab551 100644
--- a/frontend/src/metabase/visualizations/visualizations/Table/Table.unit.spec.tsx
+++ b/frontend/src/metabase/visualizations/visualizations/Table/Table.unit.spec.tsx
@@ -326,6 +326,66 @@ describe("dashboard client-side sorting", () => {
});
describe("text_wrapping", () => {
+ describe("line breaks in cells (#6178, #64697)", () => {
+ beforeAll(() => {
+ mockGetBoundingClientRect();
+ });
+
+ const wrappedColumn = createMockCategoryColumn({
+ name: "wrapped",
+ display_name: "wrapped",
+ });
+ const unwrappedColumn = createMockCategoryColumn({
+ name: "unwrapped",
+ display_name: "unwrapped",
+ });
+
+ it.each([
+ ["\\n", "\n"],
+ ["\\r\\n", "\r\n"],
+ ["\\r", "\r"],
+ ["\\u0085", "\u0085"],
+ ["\\u2028", "
"],
+ ["\\u2029", "
"],
+ ])(
+ "should render %s as a line break when wrapped and a space when not",
+ (_name, lineBreak) => {
+ const value = `line1${lineBreak}line2`;
+ const series = [
+ createMockSingleSeries(
+ {
+ display: "table",
+ visualization_settings: {
+ column_settings: {
+ [getColumnKey(wrappedColumn)]: { text_wrapping: true },
+ },
+ },
+ },
+ {
+ data: {
+ cols: [wrappedColumn, unwrappedColumn],
+ rows: [[value, value]],
+ },
+ },
+ ),
+ ];
+
+ renderWithProviders(
+ <Visualization rawSeries={series} width={600} height={400} />,
+ );
+
+ const [_rowIdCell, wrappedCell, unwrappedCell] =
+ screen.getAllByRole("gridcell");
+ expect(wrappedCell).toHaveTextContent("line1\nline2", {
+ normalizeWhitespace: false,
+ });
+ expect(unwrappedCell).toHaveTextContent("line1 line2", {
+ normalizeWhitespace: false,
+ });
+ },
+ );
+ });
+
describe("in columnSettings", () => {
function assertDefined<T>(
value: T | undefined | null,