Skip to content

Commit

Permalink
Merge branch 'develop' into feature/#2118-do-not-calculate-coverage-o…
Browse files Browse the repository at this point in the history
…n-external-PR
  • Loading branch information
FredLL-Avaiga authored Oct 22, 2024
2 parents c3f19cb + a900073 commit d376a13
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
21 changes: 21 additions & 0 deletions doc/gui/examples/controls/column_name_styling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Example for column name styling for header

import pandas as pd

from taipy.gui import Gui, Markdown

# Sample data in DataFrame format
df = pd.DataFrame({
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35],
"Job or Occupation": ["Engineer", "Doctor", "Artist"]
})


# Page content with table and header styling
page = Markdown("""
<|table|data={df}|columns={columns}|>
""", style={".taipy-table-name": {"color": "blue"}, ".taipy-table-job-or-occupation": {"color": "green"}})

if __name__ == "__main__":
Gui(page).run(title="Column Name Styling Example")
7 changes: 6 additions & 1 deletion frontend/taipy-gui/src/components/Taipy/AutoLoadingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import AddIcon from "@mui/icons-material/Add";
import DataSaverOn from "@mui/icons-material/DataSaverOn";
import DataSaverOff from "@mui/icons-material/DataSaverOff";
import Download from "@mui/icons-material/Download";
import { generateHeaderClassName } from "./tableUtils";

import {
createRequestInfiniteTableUpdateAction,
Expand Down Expand Up @@ -604,7 +605,11 @@ const AutoLoadingTable = (props: TaipyTableProps) => {
<TableCell
key={`head${columns[col].dfid}`}
sortDirection={orderBy === columns[col].dfid && order}
sx={columns[col].width ? { width: columns[col].width } : undefined}
sx={columns[col].width ? { width: columns[col].width } : {}}
className={col === "EDIT_COL"
? getSuffixedClassNames(className, "-action")
: getSuffixedClassNames(className, generateHeaderClassName(columns[col].dfid))
}
>
{columns[col].dfid === EDIT_COL ? (
[
Expand Down
5 changes: 5 additions & 0 deletions frontend/taipy-gui/src/components/Taipy/PaginatedTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import TableSortLabel from "@mui/material/TableSortLabel";
import Tooltip from "@mui/material/Tooltip";
import Typography from "@mui/material/Typography";
import { visuallyHidden } from "@mui/utils";
import { generateHeaderClassName } from "./tableUtils";

import { createRequestTableUpdateAction, createSendActionNameAction } from "../../context/taipyReducers";
import { emptyArray } from "../../utils";
Expand Down Expand Up @@ -528,6 +529,10 @@ const PaginatedTable = (props: TaipyPaginatedTableProps) => {
? { minWidth: `${100 / nbWidth}%` }
: undefined
}
className={col === "EDIT_COL"
? getSuffixedClassNames(className, "-action")
: getSuffixedClassNames(className, generateHeaderClassName(columns[col].dfid))
}
>
{columns[col].dfid === EDIT_COL ? (
[
Expand Down
28 changes: 28 additions & 0 deletions frontend/taipy-gui/src/components/Taipy/tableUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { generateHeaderClassName } from "./tableUtils";

describe("generateHeaderClassName", () => {
it("should generate a CSS class name with a hyphen prefix and convert to lowercase", () => {
const result = generateHeaderClassName("ColumnName");
expect(result).toBe("-columnname");
});

it("should replace spaces and special characters with hyphens", () => {
const result = generateHeaderClassName("Column Name@123!");
expect(result).toBe("-column-name-123-");
});

it("should remove multiple hyphens in a row", () => {
const result = generateHeaderClassName("Column--Name");
expect(result).toBe("-column-name");
});

it("should handle empty strings and return an empty string", () => {
const result = generateHeaderClassName("");
expect(result).toBe("");
});

it("should return empty string for the undefined", () => {
const result = generateHeaderClassName(undefined);
expect(result).toBe("");
});
});
16 changes: 16 additions & 0 deletions frontend/taipy-gui/src/components/Taipy/tableUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ import { TaipyActiveProps, TaipyMultiSelectProps, getSuffixedClassNames } from "
/**
* A column description as received by the backend.
*/

/**
* Generates a CSS class name for a table header.
* @param columnName - The name of the column.
* @returns for CSS class name.
*/

export const generateHeaderClassName = (columnName: string | undefined): string => {
// logic for the css header classname
if (!columnName){
// return an empty string if columname is undefined or empty
return "";
}
return '-' + columnName.replace(/\W+/g, '-').replace(/-+/g, '-').toLowerCase();
};

export interface ColumnDesc {
/** The unique column identifier. */
dfid: string;
Expand Down

0 comments on commit d376a13

Please sign in to comment.