-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,90 @@ | ||
// @flow | ||
import * as React from "react"; | ||
import { Table, TableBody, TableHeader, TableRow } from "../Table"; | ||
import { createUseStyles } from "react-jss"; | ||
import { | ||
Table, | ||
TableBody, | ||
TableData, | ||
TableHead, | ||
TableHeader, | ||
TableRow, | ||
} from "../Table"; | ||
import type { TextAlign } from "../commonTypes"; | ||
|
||
type DataTableProps = { | ||
columns: Array<ColumnProp>, | ||
data: Array<Any>, | ||
columns: Array<ColumnProps>, | ||
data: Array<any>, | ||
borderWidth?: string, | ||
}; | ||
|
||
type ColumnProps = { | ||
header: string, | ||
textAlign: TextAlign, | ||
width: string, | ||
header?: string, | ||
textAlign?: TextAlign, | ||
width?: string, | ||
}; | ||
|
||
type TextAlign = "left" | "right" | "center"; | ||
|
||
type DataElementProps = { | ||
textAlign: TextAlign, | ||
width: string, | ||
item: string, | ||
borderWidth?: string, | ||
textAlign?: TextAlign, | ||
width?: string, | ||
}; | ||
|
||
export function DataTable({ columns, data }: DataTableProps) { | ||
export function DataTable({ | ||
borderWidth, | ||
columns, | ||
data, | ||
}: DataTableProps): React.Node { | ||
return ( | ||
<Table> | ||
<TableRow> | ||
{columns.map((columnProps) => ( | ||
<ColumnHeader {...ColumnProps} /> | ||
))} | ||
</TableRow> | ||
{data.map((row) => { | ||
return ( | ||
<TableRow> | ||
{row.map((element, index) => ( | ||
<DataElement | ||
item={element.item} | ||
textAlign={element.textAlign} | ||
width={element.width} | ||
/> | ||
))} | ||
</TableRow> | ||
); | ||
})} | ||
<TableHead> | ||
<TableRow> | ||
{columns.map((column, index) => ( | ||
<ColumnHeader | ||
key={column.header || index} | ||
header={column.header} | ||
width={column.width} | ||
/> | ||
))} | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{data.map((row, i) => { | ||
return ( | ||
<TableRow key={i}> | ||
{row.map((element, j) => ( | ||
<DataElement | ||
key={`${i}_${j}`} | ||
item={element} | ||
textAlign={columns[j].textAlign} | ||
width={columns[j].width} | ||
borderWidth={borderWidth} | ||
/> | ||
))} | ||
</TableRow> | ||
); | ||
})} | ||
</TableBody> | ||
</Table> | ||
); | ||
} | ||
|
||
function ColumnHeader({ header }: ColumnProps) { | ||
return <TableHeader>{header}</TableHeader>; | ||
function ColumnHeader({ header, width }: ColumnProps) { | ||
return ( | ||
<TableHeader textAlign="center" width={width}> | ||
{header} | ||
</TableHeader> | ||
); | ||
} | ||
|
||
function DataElement({ item, textAlign, width }: DataTableProps) { | ||
const useDataStyles = useDataTableStysles({ textAlign, width }); | ||
|
||
return <TableBody className={useDataStyles.tableData}>{item}</TableBody>; | ||
function DataElement({ | ||
borderWidth, | ||
item, | ||
textAlign, | ||
width, | ||
}: DataElementProps) { | ||
return ( | ||
<TableData borderWidth={borderWidth} textAlign={textAlign} width={width}> | ||
{item} | ||
</TableData> | ||
); | ||
} | ||
|
||
const useDataTableStysles = createUseStyles({ | ||
tableData: { | ||
textAlign: ({ textAlign }) => textAlign, | ||
width: ({ width }) => width, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @flow | ||
export type TextAlign = "left" | "right" | "center"; |