Skip to content

Commit 3945427

Browse files
committed
Add DataTable story
1 parent 3b03b19 commit 3945427

3 files changed

Lines changed: 111 additions & 0 deletions

File tree

src/DataTable/index.jsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// @flow
2+
import * as React from "react";
3+
import { Table, TableBody, TableHeader, TableRow } from "../Table";
4+
import { createUseStyles } from "react-jss";
5+
6+
type DataTableProps = {
7+
columns: Array<ColumnProp>,
8+
data: Array<Any>,
9+
};
10+
11+
type ColumnProps = {
12+
header: string,
13+
textAlign: TextAlign,
14+
width: string,
15+
};
16+
17+
type TextAlign = "left" | "right" | "center";
18+
19+
type DataElementProps = {
20+
textAlign: TextAlign,
21+
width: string,
22+
item: string,
23+
};
24+
25+
export function DataTable({ columns, data }: DataTableProps) {
26+
return (
27+
<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+
})}
46+
</Table>
47+
);
48+
}
49+
50+
function ColumnHeader({ header }: ColumnProps) {
51+
return <TableHeader>{header}</TableHeader>;
52+
}
53+
54+
function DataElement({ item, textAlign, width }: DataTableProps) {
55+
const useDataStyles = useDataTableStysles({ textAlign, width });
56+
57+
return <TableBody className={useDataStyles.tableData}>{item}</TableBody>;
58+
}
59+
60+
const useDataTableStysles = createUseStyles({
61+
tableData: {
62+
textAlign: ({ textAlign }) => textAlign,
63+
width: ({ width }) => width,
64+
},
65+
});

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22
export { Button, useButtonStyles } from "./Button";
33
export type { ButtonStyleProps } from "./Button";
4+
export { DataTable } from "./DataTable";
45
export {
56
DateInput,
67
Form,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Meta, Story } from "@storybook/addon-docs/blocks";
2+
import { action } from "@storybook/addon-actions";
3+
import { DataTable } from "../../src/";
4+
5+
<Meta
6+
title="DataTable"
7+
component={DataTable}
8+
argTypes={{
9+
columns: { table: { disable: true } },
10+
data: { table: { disable: true } },
11+
}}
12+
/>
13+
14+
# DataTable
15+
16+
A data table is an easier way to visualize arrays of data. You can add an array of data to define the column type and
17+
18+
export const Template = (args) => (
19+
<DataTable
20+
columns={[
21+
{
22+
header: "Strings",
23+
textAlign: "left",
24+
},
25+
{
26+
header: "Numbers",
27+
textAlign: "center",
28+
width: "8rem",
29+
},
30+
{
31+
header: "Dates",
32+
textAlign: "right",
33+
width: "25rem",
34+
},
35+
]}
36+
data={[
37+
["a", 1, Date(2021, 0, 1)],
38+
["b", 2, Date(2021, 1, 1)],
39+
["c", 3, Date(2021, 2, 1)],
40+
]}
41+
{...args}
42+
/>
43+
);
44+
45+
<Story name="DataTable">{Template.bind({})}</Story>

0 commit comments

Comments
 (0)