Browse Source

line breaks in table cells (#2525)

* line breaks in table cells
resolves #2524

* add lineBreak to column Description

---------

Co-authored-by: Fred Lefévère-Laoide <Fred.Lefevere-Laoide@Taipy.io>
Fred Lefévère-Laoide 1 tháng trước cách đây
mục cha
commit
a0065c4634

+ 7 - 2
frontend/taipy-gui/packaging/taipy-gui.d.ts

@@ -364,7 +364,7 @@ export interface ColumnDesc {
     width?: number | string;
     /** If true, the column cannot be edited. */
     notEditable?: boolean;
-    /** The name of the column that holds the CSS classname to
+    /** The name of the column that holds the CSS className to
      *  apply to the cells. */
     className?: string;
     /** The name of the column that holds the tooltip to
@@ -383,13 +383,18 @@ export interface ColumnDesc {
     apply?: string;
     /** The flag that allows the user to aggregate the column. */
     groupBy?: boolean;
-    widthHint?: number;
     /** The list of values that can be used on edit. */
     lov?: string[];
     /** If true the user can enter any value besides the lov values. */
     freeLov?: boolean;
     /** If false, the column cannot be sorted */
     sortable?: boolean;
+    /** The column headers if more than one. */
+    headers?: string[];
+    /** The index of the multi index if exists. */
+    multi?: number;
+    /** If true or not set, line breaks are transformed into <BR>. */
+    lineBreak?: boolean;
 }
 /**
  * A cell value type.

+ 27 - 5
frontend/taipy-gui/src/components/Taipy/tableUtils.tsx

@@ -20,6 +20,7 @@ import React, {
     MouseEvent,
     ChangeEvent,
     SyntheticEvent,
+    ReactNode,
 } from "react";
 import { FilterOptionsState } from "@mui/material";
 import Autocomplete, { createFilterOptions } from "@mui/material/Autocomplete";
@@ -108,6 +109,8 @@ export interface ColumnDesc {
     headers?: string[];
     /** The index of the multi index if exists. */
     multi?: number;
+    /** If true or not set, line breaks are transformed into <BR>. */
+    lineBreak?: boolean;
 }
 
 export const DEFAULT_SIZE = "small";
@@ -245,7 +248,13 @@ export const getSortByIndex = (cols: Record<string, ColumnDesc>) => (key1: strin
         ? 1
         : cols[key1].index - cols[key2].index;
 
-const formatValue = (val: RowValue, col: ColumnDesc, formatConf: FormatConfig, nanValue?: string): string => {
+const formatValue = (
+    val: RowValue,
+    col: ColumnDesc,
+    formatConf: FormatConfig,
+    nanValue: string | undefined,
+    lineBreak: boolean = true
+): ReactNode => {
     if (val === undefined) {
         return "";
     }
@@ -262,7 +271,20 @@ const formatValue = (val: RowValue, col: ColumnDesc, formatConf: FormatConfig, n
             }
             return getNumberString(val as number, col.format, formatConf);
         default:
-            return val ? (val as string) : "";
+            return val
+                ? lineBreak && (col.lineBreak === undefined || col.lineBreak)
+                    ? (val as string).split("\n").map((p, i) =>
+                          i == 0 ? (
+                              p
+                          ) : (
+                              <>
+                                  <br />
+                                  {p}
+                              </>
+                          )
+                      )
+                    : val
+                : "";
     }
 };
 
@@ -277,11 +299,11 @@ const defaultCursorIcon = { ...iconInRowSx, "& .MuiSwitch-input": defaultCursor
 const getCellProps = (col: ColumnDesc, base: Partial<TableCellProps> = {}): Partial<TableCellProps> => {
     switch (col.type) {
         case "bool":
-            base = {...base, align: "center"};
+            base = { ...base, align: "center" };
             break;
     }
     if (col.width) {
-        base = {...base, width: col.width};
+        base = { ...base, width: col.width };
     }
     return base;
 };
@@ -577,7 +599,7 @@ export const EditableCell = (props: EditableCellProps) => {
             title={
                 tooltip || comp
                     ? `${tooltip ? tooltip : ""}${
-                          comp ? " " + formatValue(comp as RowValue, colDesc, formatConfig, nanValue) : ""
+                          comp ? " " + formatValue(comp as RowValue, colDesc, formatConfig, nanValue, false) : ""
                       }`
                     : undefined
             }