Skip to content

Commit f20903c

Browse files
refactor: Update theme structure and remove default theme props (#13)
- Removed defaultThemeProps and integrated theme properties directly into the theme structure. - Updated MultiLevelTable and TableRow components to utilize the new theme structure with levelColors for row backgrounds. - Enhanced row styling logic in TableRow component using useMemo for performance optimization. - Changed package name in package-lock.json to reflect the correct organization.
1 parent 681e3b6 commit f20903c

File tree

7 files changed

+66
-162
lines changed

7 files changed

+66
-162
lines changed

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/MultiLevelTable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { PaginationProps } from "./Pagination";
1414
import { TableHeader } from "./TableHeader";
1515
import { TableRow } from "./TableRow";
1616
import { SortType } from '../constants/sort';
17-
import { defaultThemeProps } from "../defaultThemeProps";
17+
import { defaultTheme } from "../constants/theme";
1818
import { mergeThemeProps } from "../mergeThemeProps";
1919
import type { ThemeProps } from "../types/theme";
2020
import type {
@@ -62,7 +62,7 @@ export const MultiLevelTable: React.FC<MultiLevelTableProps> = ({
6262
descendingIcon,
6363
expandIcon,
6464
}) => {
65-
const mergedTheme = mergeThemeProps(defaultThemeProps, theme);
65+
const mergedTheme = mergeThemeProps(defaultTheme, theme);
6666
const [filterInput, setFilterInput] = useState("");
6767

6868
/**

src/components/TableRow.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useMemo } from "react";
22

33
import type { Cell, Row } from "react-table";
44

@@ -48,24 +48,23 @@ export const TableRow: React.FC<TableRowProps> = ({
4848
theme,
4949
expandIcon,
5050
}) => {
51-
const getRowClassName = () => {
51+
const getRowClassName = useMemo(() => {
5252
const classes = ["table-row"];
5353

5454
if (isExpanded) classes.push("table-row-expanded");
5555
if (level === 0) classes.push("table-row-main");
5656
else classes.push("table-row-nested");
5757

5858
return classes.join(" ");
59-
};
60-
61-
const getRowStyle = () => {
62-
if (level === 0)
63-
return { backgroundColor: theme.table?.row?.mainBackground };
64-
if (isExpanded)
65-
return { backgroundColor: theme.table?.row?.expandedBackground };
59+
}, [isExpanded, level]);
6660

67-
return { backgroundColor: theme.table?.row?.nestedBackground };
68-
};
61+
const getRowStyle = useMemo(() => {
62+
const rowShades = theme.table?.row?.levelColors || [];
63+
// Use the level to determine which shade to use, defaulting to the lightest shade for deeper nesting
64+
const shadeIndex = Math.min(level, rowShades.length - 1);
65+
66+
return { backgroundColor: rowShades[shadeIndex]?.background };
67+
}, [level, theme.table?.row?.levelColors]);
6968

7069
const handleExpandClick = (e: React.MouseEvent) => {
7170
e.stopPropagation();
@@ -77,7 +76,7 @@ export const TableRow: React.FC<TableRowProps> = ({
7776
const dataItem = row as DataItem;
7877

7978
return (
80-
<tr className={getRowClassName()} style={getRowStyle()}>
79+
<tr className={getRowClassName} style={getRowStyle}>
8180
{columns.map((column: Column, index: number) => {
8281
const value = dataItem[column.key as keyof DataItem];
8382
const displayValue =
@@ -121,8 +120,8 @@ export const TableRow: React.FC<TableRowProps> = ({
121120
<tr
122121
key={key}
123122
{...rowProps}
124-
className={getRowClassName()}
125-
style={getRowStyle()}
123+
className={getRowClassName}
124+
style={getRowStyle}
126125
>
127126
{tableRow.cells.map((cell: Cell<DataItem>, index: number) => (
128127
<TableCell

src/constants/theme.ts

Lines changed: 40 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,47 @@
1-
import type { Theme } from '../types/theme';
1+
import type { ThemeProps } from "../types/theme";
22

3-
export const defaultTheme: Theme = {
4-
light: {
5-
colors: {
6-
primaryColor: '#2c3e50',
7-
textColor: '#2c3e50',
8-
borderColor: '#e0e0e0',
9-
background: '#ffffff',
10-
},
11-
table: {
12-
header: {
13-
background: '#2c3e50',
14-
textColor: '#ffffff',
15-
borderColor: '#e0e0e0',
16-
},
17-
row: {
18-
mainBackground: '#ffffff',
19-
nestedBackground: '#f1f3f5',
20-
expandedBackground: '#f8f9fa',
21-
hoverBackground: '#f5f5f5',
22-
},
23-
cell: {
24-
textColor: '#2c3e50',
25-
borderColor: '#e0e0e0',
26-
nestedPadding: '16px',
27-
},
28-
},
29-
pagination: {
30-
button: {
31-
background: '#2c3e50',
32-
textColor: '#ffffff',
33-
disabledOpacity: '0.5',
34-
},
35-
select: {
36-
background: '#ffffff',
37-
textColor: '#2c3e50',
38-
borderColor: '#e0e0e0',
39-
},
40-
info: {
41-
textColor: '#2c3e50',
42-
},
43-
},
44-
expandIcon: {
45-
color: '#2c3e50',
46-
},
3+
export const defaultTheme: ThemeProps = {
4+
colors: {
5+
primaryColor: "#2c3e50",
6+
textColor: "#2c3e50",
7+
borderColor: "#e0e0e0",
8+
background: "#ffffff",
479
},
48-
dark: {
49-
colors: {
50-
primaryColor: '#3498db',
51-
textColor: '#ecf0f1',
52-
borderColor: '#34495e',
53-
background: '#2c3e50',
10+
table: {
11+
header: {
12+
background: "#2c3e50",
13+
textColor: "#ffffff",
14+
borderColor: "#e0e0e0",
15+
},
16+
row: {
17+
levelColors: [
18+
{ background: "#ffffff" },
19+
{ background: "#f1f3f5" },
20+
{ background: "#f8f9fa" },
21+
],
22+
},
23+
cell: {
24+
textColor: "#2c3e50",
25+
borderColor: "#e0e0e0",
26+
nestedPadding: "16px",
5427
},
55-
table: {
56-
header: {
57-
background: '#34495e',
58-
textColor: '#ecf0f1',
59-
borderColor: '#2c3e50',
60-
},
61-
row: {
62-
mainBackground: '#2c3e50',
63-
nestedBackground: '#34495e',
64-
expandedBackground: '#2c3e50',
65-
hoverBackground: '#3d566e',
66-
},
67-
cell: {
68-
textColor: '#ecf0f1',
69-
borderColor: '#34495e',
70-
nestedPadding: '16px',
71-
},
28+
},
29+
pagination: {
30+
button: {
31+
background: "#2c3e50",
32+
textColor: "#ffffff",
33+
disabledOpacity: "0.5",
7234
},
73-
pagination: {
74-
button: {
75-
background: '#3498db',
76-
textColor: '#ffffff',
77-
disabledOpacity: '0.5',
78-
},
79-
select: {
80-
background: '#34495e',
81-
textColor: '#ecf0f1',
82-
borderColor: '#2c3e50',
83-
},
84-
info: {
85-
textColor: '#ecf0f1',
86-
},
35+
select: {
36+
background: "#ffffff",
37+
textColor: "#2c3e50",
38+
borderColor: "#e0e0e0",
8739
},
88-
expandIcon: {
89-
color: '#ecf0f1',
40+
info: {
41+
textColor: "#2c3e50",
9042
},
9143
},
92-
};
44+
expandIcon: {
45+
color: "#2c3e50",
46+
},
47+
};

src/defaultThemeProps.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/themes.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ export const lightTheme: ThemeProps = {
1616
borderColor: '#dee2e6',
1717
},
1818
row: {
19-
mainBackground: '#ffffff',
20-
nestedBackground: '#f8f9fa',
21-
expandedBackground: '#e9ecef',
19+
levelColors: [{background: '#ffffff'}, {background: '#f1f3f5'}, {background: '#f8f9fa'}]
2220
},
2321
filter: {
2422
background: 'transparent',
@@ -64,9 +62,7 @@ export const darkTheme: ThemeProps = {
6462
borderColor: '#495057',
6563
},
6664
row: {
67-
mainBackground: '#343a40',
68-
nestedBackground: '#2b3035',
69-
expandedBackground: '#212529',
65+
levelColors: [{background: '#2c3e50'}, {background: '#34495e'}, {background: '#2c3e50'}]
7066
},
7167
filter: {
7268
background: 'transparent',
@@ -94,4 +90,5 @@ export const darkTheme: ThemeProps = {
9490
expandIcon: {
9591
color: '#e9ecef',
9692
},
97-
};
93+
};
94+

src/types/theme.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ export interface ThemeProps {
77
colors?: {
88
background?: string;
99
primaryColor?: string;
10+
textColor?: string;
1011
borderColor?: string;
1112
};
1213
table?: {
1314
header?: {
1415
background?: string;
1516
textColor?: string;
17+
borderColor?: string;
1618
};
1719
cell?: {
1820
textColor?: string;
1921
borderColor?: string;
22+
nestedPadding?: string;
2023
};
2124
row?: {
22-
mainBackground?: string;
23-
nestedBackground?: string;
24-
expandedBackground?: string;
25+
levelColors?: {background: string}[];
2526
};
2627
filter?: {
2728
background?: string;

0 commit comments

Comments
 (0)