Skip to content

Commit d071a54

Browse files
authored
[feature/improvement] Pass column index to Cell
This allows for better customization of custom cell renderers, based on a columns index. The cells column index will be passed as a `column` prop to the custom Cell component.
1 parent 3e9f738 commit d071a54

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/index.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type ScalarDict = {
1010
[key: string]: Scalar
1111
}
1212

13+
export type CellProps = React.PropsWithChildren<{ column: number }>
14+
1315
export type TableProps<T extends ScalarDict> = {
1416
/**
1517
* List of values (rows).
@@ -30,7 +32,7 @@ export type TableProps<T extends ScalarDict> = {
3032
/**
3133
* Component used to render a cell in the table.
3234
*/
33-
cell: (props: React.PropsWithChildren<{}>) => JSX.Element
35+
cell: (props: CellProps) => JSX.Element
3436
/**
3537
* Component used to render the skeleton of the table.
3638
*/
@@ -233,7 +235,7 @@ type RowConfig = {
233235
/**
234236
* Component used to render cells.
235237
*/
236-
cell: (props: React.PropsWithChildren<{}>) => JSX.Element
238+
cell: (props: CellProps) => JSX.Element
237239
/**
238240
* Tells the padding of each cell.
239241
*/
@@ -295,15 +297,15 @@ function row<T extends ScalarDict>(
295297
},
296298

297299
// Values.
298-
props.columns.map((column) => {
300+
props.columns.map((column, colI) => {
299301
// content
300302
const value = props.data[column.column]
301303

302304
if (value == undefined || value == null) {
303305
const key = `${props.key}-empty-${column.key}`
304306

305307
return (
306-
<config.cell key={key}>
308+
<config.cell key={key} column={colI}>
307309
{skeleton.line.repeat(column.width)}
308310
</config.cell>
309311
)
@@ -316,7 +318,7 @@ function row<T extends ScalarDict>(
316318

317319
return (
318320
/* prettier-ignore */
319-
<config.cell key={key}>
321+
<config.cell key={key} column={colI}>
320322
{`${skeleton.line.repeat(ml)}${String(value)}${skeleton.line.repeat(mr)}`}
321323
</config.cell>
322324
)
@@ -343,7 +345,7 @@ export function Header(props: React.PropsWithChildren<{}>) {
343345
/**
344346
* Renders a cell in the table.
345347
*/
346-
export function Cell(props: React.PropsWithChildren<{}>) {
348+
export function Cell(props: CellProps) {
347349
return <Text>{props.children}</Text>
348350
}
349351

0 commit comments

Comments
 (0)