Skip to content

Commit aae4a67

Browse files
authored
added more style options to the column meta (#61)
1 parent fb82869 commit aae4a67

5 files changed

Lines changed: 257 additions & 43 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- option `cellClassName` added to the column meta to add class names to every cell of a column
13+
- option `headerClassName` added to the column meta to add class names to every header of a column
14+
- option `footerStyle` added to the column meta to add styles to every footer of a column
15+
- option `footerClassName` added to the column meta to add class names to every footer of a column
16+
1017
## [4.1.0] - 2024-03-13
1118

1219
### Added

src/lib/ReactDataTable/ReactDataTable.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const ReactDataTable = <TData,>(props: ReactDataTableProps<TData>) => {
7070
key={header.id}
7171
onClick={header.column.getToggleSortingHandler()}
7272
style={{ ...header.column.columnDef.meta?.headerStyle, ...(header.column.getCanSort() ? { cursor: "pointer" } : {}) }}
73+
className={header.column.columnDef.meta?.headerClassName}
7374
>
7475
{header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
7576

@@ -182,7 +183,7 @@ const ReactDataTable = <TData,>(props: ReactDataTableProps<TData>) => {
182183
{table.getRowModel().rows.map((row) => (
183184
<tr key={row.id} style={rowStyle && rowStyle(row.original)}>
184185
{row.getVisibleCells().map((cell) => (
185-
<td key={cell.id} style={cell.column.columnDef.meta?.cellStyle}>
186+
<td key={cell.id} style={cell.column.columnDef.meta?.cellStyle} className={cell.column.columnDef.meta?.cellClassName}>
186187
{flexRender(cell.column.columnDef.cell, cell.getContext())}
187188
</td>
188189
))}
@@ -195,7 +196,13 @@ const ReactDataTable = <TData,>(props: ReactDataTableProps<TData>) => {
195196
{table.getFooterGroups().map((footerGroup) => (
196197
<tr key={footerGroup.id}>
197198
{footerGroup.headers.map((header) => (
198-
<th key={header.id}>{header.isPlaceholder ? null : flexRender(header.column.columnDef.footer, header.getContext())}</th>
199+
<th
200+
key={header.id}
201+
style={header.column.columnDef.meta?.footerStyle}
202+
className={header.column.columnDef.meta?.footerClassName}
203+
>
204+
{header.isPlaceholder ? null : flexRender(header.column.columnDef.footer, header.getContext())}
205+
</th>
199206
))}
200207
</tr>
201208
))}

src/react-table.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,31 @@ declare module "@tanstack/table-core" {
2525
*/
2626
cellStyle?: CSSProperties;
2727

28+
/**
29+
* Add class name to the every cell in this column
30+
*/
31+
cellClassName?: string;
32+
2833
/**
2934
* Add a style to the header cell of this column
3035
*/
3136
headerStyle?: CSSProperties;
3237

38+
/**
39+
* Add class name to the every header in this column
40+
*/
41+
headerClassName?: string;
42+
43+
/**
44+
* Add a style to the footer cell of this column
45+
*/
46+
footerStyle?: CSSProperties;
47+
48+
/**
49+
* Add class name to the every footer in this column
50+
*/
51+
footerClassName?: string;
52+
3353
/**
3454
* Define a custom filter
3555
* @param filterValue The current value of the filter

test/DataTable/DataTable.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe("DataTable", () => {
4747
columnHelper.accessor("count", { header: "COUNT", enableSorting: true, enableColumnFilter: true }),
4848
columnHelper.accessor("status", {
4949
header: "STATUS",
50+
footer: "STATUS-FOOTER",
5051
enableSorting: true,
5152
enableColumnFilter: true,
5253
meta: {
@@ -57,6 +58,12 @@ describe("DataTable", () => {
5758
{ value: Status.Deleted, label: "Deleted" },
5859
],
5960
},
61+
headerStyle: { backgroundColor: "blue" },
62+
headerClassName: "header-class",
63+
cellStyle: { backgroundColor: "yellow" },
64+
cellClassName: "cell-class",
65+
footerStyle: { backgroundColor: "red" },
66+
footerClassName: "footer-class",
6067
},
6168
}),
6269
columnHelper.accessor("dateCreated", {

0 commit comments

Comments
 (0)