Skip to content

Commit b4caeda

Browse files
committed
update expanded row args
1 parent d6d12a4 commit b4caeda

File tree

7 files changed

+58
-30
lines changed

7 files changed

+58
-30
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# CHANGELOG
22

3+
## 1.2.0
4+
5+
_2024-12-18_
6+
7+
### Features
8+
9+
- **BREAKING:** instead of taking just the `index`, the following functions all also take in the row as well: `expandedRows`, `rowStyle`, and `rowClassname`.
10+
- (docs) added `contentClassname`, `contentStyle`
11+
312
## 1.1.0
413

514
_2024-12-13_

example/src/ColumnProps.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const columns: ColumnProps<PropData>[] = [
1616
{
1717
key: "prop",
1818
header: "Prop",
19-
width: 140,
19+
width: 200,
2020
content: ({ row }) => <code>{row.prop}</code>
2121
},
2222
{
@@ -48,6 +48,16 @@ const data: PropData[] = [
4848
</div>
4949
)
5050
},
51+
{
52+
prop: "headerClassname",
53+
type: "string",
54+
description: "An optional classname that can be applied to the header cell."
55+
},
56+
{
57+
prop: "headerStyle",
58+
type: "React.CSSProperties",
59+
description: "An optional style object that can be applied to the header cell."
60+
},
5161
{
5262
prop: "width",
5363
type: "number",
@@ -97,6 +107,16 @@ const data: PropData[] = [
97107
</div>
98108
)
99109
},
110+
{
111+
prop: "contentClassname",
112+
type: "string | ((props: { row: any; index: number }) => string | undefined)",
113+
description: "An optional classname that can be applied to the content cell."
114+
},
115+
{
116+
prop: "contentStyle",
117+
type: "React.CSSProperties | ((props: { row: any; index: number }) => React.CSSProperties | undefined)",
118+
description: "An optional style object that can be applied to the content cell."
119+
},
100120
{
101121
prop: "cell",
102122
type: "CellElement",

example/src/Props.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ const data: PropData[] = [
168168
},
169169
{
170170
prop: "rowStyle",
171-
type: "object | (index: number) => object",
171+
type: "object | (value: { row: any; index: number }) => object",
172172
description:
173173
"Add custom css styles to each row element. One can also pass in a function that takes in the row number in order to provide custom styling for particular rows."
174174
},
175175
{
176176
prop: "rowClassname",
177-
type: "string | (index: number) => string",
177+
type: "string | (value: { row: any; index: number }) => string",
178178
description:
179179
"Add custom css className to each row element. One can also pass in a function that takes in the row number in order to provide custom styling for particular rows."
180180
},
@@ -185,7 +185,7 @@ const data: PropData[] = [
185185
},
186186
{
187187
prop: "onRowClick",
188-
type: "(e: Event, { index: number }) => void",
188+
type: "(value: { row: any, index: number, event?: Event }) => void",
189189
description: "optional click handler for the rows in the table"
190190
},
191191
{

example/src/examples/08-header.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ from "lodash";
2-
import React, { useState } from "react";
2+
import { useState } from "react";
33
import { ColumnProps, HeaderProps, SortDirection, Table } from "react-fluid-table";
44
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
55
import { TestData, testData } from "../data";
@@ -86,10 +86,6 @@ const Example = () => {
8686
}
8787
};
8888
89-
const rowStyle = index => ({
90-
backgroundColor: index % 2 === 0 ? "#33be54" : "#21ba49"
91-
});
92-
9389
return (
9490
<Table
9591
data={data}
@@ -99,7 +95,9 @@ const Example = () => {
9995
onSort={onSort}
10096
sortColumn="firstName"
10197
sortDirection="ASC"
102-
rowStyle={rowStyle}
98+
rowStyle={({ index }) => ({
99+
backgroundColor: index % 2 === 0 ? "#33be54" : "#21ba49"
100+
})}
103101
style={{ backgroundColor: "#33be54" }}
104102
headerStyle={{ backgroundColor: "#1e9f3f" }}
105103
/>
@@ -122,10 +120,6 @@ const Example8 = () => {
122120
}
123121
};
124122

125-
const rowStyle = (index: number): React.CSSProperties => ({
126-
backgroundColor: index % 2 === 0 ? "#33be54" : "#21ba49"
127-
});
128-
129123
return (
130124
<Table
131125
data={data}
@@ -135,7 +129,9 @@ const Example8 = () => {
135129
onSort={onSort}
136130
sortColumn="firstName"
137131
sortDirection="ASC"
138-
rowStyle={rowStyle}
132+
rowStyle={({ index }) => ({
133+
backgroundColor: index % 2 === 0 ? "#33be54" : "#21ba49"
134+
})}
139135
style={{ backgroundColor: "#33be54" }}
140136
headerStyle={{ backgroundColor: "#1e9f3f" }}
141137
/>

index.d.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,14 @@ export type TableProps<T> = {
289289
headerClassname?: string;
290290
/**
291291
* React styles used for customizing each row. Could be an object or
292-
* a function that takes the index of the row and returns an object.
292+
* a function that takes the index or the row and returns an object.
293293
*/
294-
rowStyle?: CSSProperties | ((index: number) => CSSProperties | undefined);
294+
rowStyle?: CSSProperties | ((value: { row: T; index: number }) => CSSProperties | undefined);
295295
/**
296296
* React className used for customizing each row. Could be an object or
297-
* a function that takes the index of the row and returns an object.
297+
* a function that takes the index or the row and returns an object.
298298
*/
299-
rowClassname?: string | ((index: number) => string);
299+
rowClassname?: string | ((value: { row: T; index: number }) => string);
300300
/**
301301
* React styles used for customizing the footer.
302302
*/
@@ -306,10 +306,12 @@ export type TableProps<T> = {
306306
*/
307307
footerClassname?: string;
308308
/**
309-
* an object that contains the expanded rows. can also be a function that takes the index and
310-
* returns a boolean.
309+
* an object that contains the expanded rows. can also be a function that takes the index
310+
* or row and returns a boolean.
311311
*/
312-
expandedRows?: { [x: string | number]: boolean } | ((index: number) => boolean);
312+
expandedRows?:
313+
| { [x: string | number]: boolean }
314+
| ((value: { row: T; index: number }) => boolean);
313315
/**
314316
* called when a row is expanded
315317
* @param value information about the row that is expanded/shrunk

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-fluid-table",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "A React table inspired by @tanstack/react-virtual",
55
"author": "Mckervin Ceme <mckervinc@live.com>",
66
"license": "MIT",

src/components/List.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,14 @@ const List = forwardRef(function <T>(
179179
}}
180180
>
181181
<div className={cx(showRowWrapper && "rft-row-wrapper")}>
182-
{items.map(item => {
183-
const row = data[item.index];
184-
const key = generateKeyFromRow(row, item.index);
185-
const isExpanded = isRowExpanded?.(item.index) ?? !!expandedCache[key];
182+
{items.map(({ index }) => {
183+
const row = data[index];
184+
const fargs = { row, index };
185+
const key = generateKeyFromRow(row, index);
186+
const isExpanded = isRowExpanded?.(fargs) ?? !!expandedCache[key];
186187
const className =
187-
typeof rowClassname === "function" ? rowClassname(item.index) : rowClassname;
188-
const style = typeof rowStyle === "function" ? rowStyle(item.index) : rowStyle;
188+
typeof rowClassname === "function" ? rowClassname(fargs) : rowClassname;
189+
const style = typeof rowStyle === "function" ? rowStyle(fargs) : rowStyle;
189190
return (
190191
<Row
191192
ref={virtualizer.measureElement}
@@ -200,7 +201,7 @@ const List = forwardRef(function <T>(
200201
onRowClick={onRowClick as any}
201202
rowRenderer={rowRenderer as any}
202203
onExpand={onExpand as any}
203-
index={item.index}
204+
index={index}
204205
columns={columns as any}
205206
pixelWidths={pixelWidths}
206207
subComponent={subComponent as any}

0 commit comments

Comments
 (0)