Skip to content

Commit

Permalink
feat: Add Column Index to Cell Properties
Browse files Browse the repository at this point in the history
[feature/improvement] Pass column index to Cell
  • Loading branch information
maticzav authored Aug 6, 2022
2 parents bfd219a + d071a54 commit c549c36
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type ScalarDict = {
[key: string]: Scalar
}

export type CellProps = React.PropsWithChildren<{ column: number }>

export type TableProps<T extends ScalarDict> = {
/**
* List of values (rows).
Expand All @@ -30,7 +32,7 @@ export type TableProps<T extends ScalarDict> = {
/**
* Component used to render a cell in the table.
*/
cell: (props: React.PropsWithChildren<{}>) => JSX.Element
cell: (props: CellProps) => JSX.Element
/**
* Component used to render the skeleton of the table.
*/
Expand Down Expand Up @@ -233,7 +235,7 @@ type RowConfig = {
/**
* Component used to render cells.
*/
cell: (props: React.PropsWithChildren<{}>) => JSX.Element
cell: (props: CellProps) => JSX.Element
/**
* Tells the padding of each cell.
*/
Expand Down Expand Up @@ -295,15 +297,15 @@ function row<T extends ScalarDict>(
},

// Values.
props.columns.map((column) => {
props.columns.map((column, colI) => {
// content
const value = props.data[column.column]

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

return (
<config.cell key={key}>
<config.cell key={key} column={colI}>
{skeleton.line.repeat(column.width)}
</config.cell>
)
Expand All @@ -316,7 +318,7 @@ function row<T extends ScalarDict>(

return (
/* prettier-ignore */
<config.cell key={key}>
<config.cell key={key} column={colI}>
{`${skeleton.line.repeat(ml)}${String(value)}${skeleton.line.repeat(mr)}`}
</config.cell>
)
Expand All @@ -343,7 +345,7 @@ export function Header(props: React.PropsWithChildren<{}>) {
/**
* Renders a cell in the table.
*/
export function Cell(props: React.PropsWithChildren<{}>) {
export function Cell(props: CellProps) {
return <Text>{props.children}</Text>
}

Expand Down

0 comments on commit c549c36

Please sign in to comment.