Skip to content

Commit 99f8361

Browse files
committed
Refinements based on storybook
1 parent 3945427 commit 99f8361

3 files changed

Lines changed: 97 additions & 55 deletions

File tree

src/DataTable/index.jsx

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,90 @@
11
// @flow
22
import * as React from "react";
3-
import { Table, TableBody, TableHeader, TableRow } from "../Table";
4-
import { createUseStyles } from "react-jss";
3+
import {
4+
Table,
5+
TableBody,
6+
TableData,
7+
TableHead,
8+
TableHeader,
9+
TableRow,
10+
} from "../Table";
11+
import type { TextAlign } from "../commonTypes";
512

613
type DataTableProps = {
7-
columns: Array<ColumnProp>,
8-
data: Array<Any>,
14+
columns: Array<ColumnProps>,
15+
data: Array<any>,
16+
borderWidth?: string,
917
};
1018

1119
type ColumnProps = {
12-
header: string,
13-
textAlign: TextAlign,
14-
width: string,
20+
header?: string,
21+
textAlign?: TextAlign,
22+
width?: string,
1523
};
1624

17-
type TextAlign = "left" | "right" | "center";
18-
1925
type DataElementProps = {
20-
textAlign: TextAlign,
21-
width: string,
2226
item: string,
27+
borderWidth?: string,
28+
textAlign?: TextAlign,
29+
width?: string,
2330
};
2431

25-
export function DataTable({ columns, data }: DataTableProps) {
32+
export function DataTable({
33+
borderWidth,
34+
columns,
35+
data,
36+
}: DataTableProps): React.Node {
2637
return (
2738
<Table>
28-
<TableRow>
29-
{columns.map((columnProps) => (
30-
<ColumnHeader {...ColumnProps} />
31-
))}
32-
</TableRow>
33-
{data.map((row) => {
34-
return (
35-
<TableRow>
36-
{row.map((element, index) => (
37-
<DataElement
38-
item={element.item}
39-
textAlign={element.textAlign}
40-
width={element.width}
41-
/>
42-
))}
43-
</TableRow>
44-
);
45-
})}
39+
<TableHead>
40+
<TableRow>
41+
{columns.map((column, index) => (
42+
<ColumnHeader
43+
key={column.header || index}
44+
header={column.header}
45+
width={column.width}
46+
/>
47+
))}
48+
</TableRow>
49+
</TableHead>
50+
<TableBody>
51+
{data.map((row, i) => {
52+
return (
53+
<TableRow key={i}>
54+
{row.map((element, j) => (
55+
<DataElement
56+
key={`${i}_${j}`}
57+
item={element}
58+
textAlign={columns[j].textAlign}
59+
width={columns[j].width}
60+
borderWidth={borderWidth}
61+
/>
62+
))}
63+
</TableRow>
64+
);
65+
})}
66+
</TableBody>
4667
</Table>
4768
);
4869
}
4970

50-
function ColumnHeader({ header }: ColumnProps) {
51-
return <TableHeader>{header}</TableHeader>;
71+
function ColumnHeader({ header, width }: ColumnProps) {
72+
return (
73+
<TableHeader textAlign="center" width={width}>
74+
{header}
75+
</TableHeader>
76+
);
5277
}
5378

54-
function DataElement({ item, textAlign, width }: DataTableProps) {
55-
const useDataStyles = useDataTableStysles({ textAlign, width });
56-
57-
return <TableBody className={useDataStyles.tableData}>{item}</TableBody>;
79+
function DataElement({
80+
borderWidth,
81+
item,
82+
textAlign,
83+
width,
84+
}: DataElementProps) {
85+
return (
86+
<TableData borderWidth={borderWidth} textAlign={textAlign} width={width}>
87+
{item}
88+
</TableData>
89+
);
5890
}
59-
60-
const useDataTableStysles = createUseStyles({
61-
tableData: {
62-
textAlign: ({ textAlign }) => textAlign,
63-
width: ({ width }) => width,
64-
},
65-
});

src/Table/index.jsx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
// @flow
22
import * as React from "react";
33
import { createUseStyles } from "react-jss";
4+
import type { TextAlign } from "../commonTypes";
45

56
type TableProps = {
67
children?: React.Node,
78
};
89

910
type TableDataProps = {
10-
center?: boolean,
11+
borderWidth?: string,
1112
colSpan?: number,
1213
rowSpan?: number,
1314
styles?: any,
15+
textAlign?: TextAlign,
16+
width?: string,
1417
children?: React.Node,
1518
};
1619

@@ -19,7 +22,7 @@ type TableHeaderProps = TableDataProps & {
1922
};
2023

2124
export function Table({ children }: TableProps): React.Node {
22-
const classes = useTableStyles();
25+
const classes = useTableStyles({});
2326

2427
return <table className={classes.table}>{children}</table>;
2528
}
@@ -37,18 +40,19 @@ export function TableHead({ children }: TableProps): React.Node {
3740
}
3841

3942
export function TableHeader({
40-
center,
4143
colSpan,
4244
rowSpan,
4345
scope,
46+
textAlign,
47+
width,
4448
children,
4549
styles,
4650
}: TableHeaderProps): React.Node {
47-
const classes = useTableStyles();
51+
const classes = useTableStyles({ textAlign, width });
4852

4953
return (
5054
<th
51-
className={center && classes.center}
55+
className={classes.tableHeader}
5256
style={styles}
5357
colSpan={colSpan ? colSpan : 1}
5458
rowSpan={rowSpan ? rowSpan : 1}
@@ -59,18 +63,24 @@ export function TableHeader({
5963
);
6064
}
6165

66+
TableHeader.defaultProps = {
67+
textAlign: "center",
68+
};
69+
6270
export function TableData({
63-
center,
71+
borderWidth,
6472
colSpan,
6573
rowSpan,
6674
children,
75+
textAlign,
76+
width,
6777
styles,
6878
}: TableDataProps): React.Node {
69-
const classes = useTableStyles();
79+
const classes = useTableStyles({ borderWidth, textAlign, width });
7080

7181
return (
7282
<td
73-
className={`${classes.tableData} ${center ? classes.center : ""}`}
83+
className={classes.tableData}
7484
style={styles}
7585
colSpan={colSpan ? colSpan : 1}
7686
rowSpan={rowSpan ? rowSpan : 1}
@@ -80,17 +90,22 @@ export function TableData({
8090
);
8191
}
8292

93+
TableData.defaultProps = { borderWidth: "1px", textAlign: "right" };
94+
8395
const useTableStyles = createUseStyles({
84-
center: {
85-
textAlign: "center",
86-
},
8796
table: {
8897
borderCollapse: "collapse",
8998
},
9099
tableData: {
91-
border: "1px",
100+
borderWidth: ({ borderWidth }) => borderWidth,
92101
borderStyle: "solid",
93102
margin: 0,
94103
padding: "5px 5px",
104+
textAlign: ({ textAlign }) => textAlign,
105+
width: ({ width }) => width,
106+
},
107+
tableHeader: {
108+
textAlign: ({ textAlign }) => textAlign,
109+
width: ({ width }) => width,
95110
},
96111
});

src/commonTypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @flow
2+
export type TextAlign = "left" | "right" | "center";

0 commit comments

Comments
 (0)