Skip to content

Commit

Permalink
Add DataTable story
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbarela committed Mar 14, 2021
1 parent 3b03b19 commit 3945427
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/DataTable/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// @flow
import * as React from "react";
import { Table, TableBody, TableHeader, TableRow } from "../Table";
import { createUseStyles } from "react-jss";

type DataTableProps = {
columns: Array<ColumnProp>,
data: Array<Any>,
};

type ColumnProps = {
header: string,
textAlign: TextAlign,
width: string,
};

type TextAlign = "left" | "right" | "center";

type DataElementProps = {
textAlign: TextAlign,
width: string,
item: string,
};

export function DataTable({ columns, data }: DataTableProps) {
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>
);
})}
</Table>
);
}

function ColumnHeader({ header }: ColumnProps) {
return <TableHeader>{header}</TableHeader>;
}

function DataElement({ item, textAlign, width }: DataTableProps) {
const useDataStyles = useDataTableStysles({ textAlign, width });

return <TableBody className={useDataStyles.tableData}>{item}</TableBody>;
}

const useDataTableStysles = createUseStyles({
tableData: {
textAlign: ({ textAlign }) => textAlign,
width: ({ width }) => width,
},
});
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
export { Button, useButtonStyles } from "./Button";
export type { ButtonStyleProps } from "./Button";
export { DataTable } from "./DataTable";
export {
DateInput,
Form,
Expand Down
45 changes: 45 additions & 0 deletions stories/DataTable/DataTable.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Meta, Story } from "@storybook/addon-docs/blocks";
import { action } from "@storybook/addon-actions";
import { DataTable } from "../../src/";

<Meta
title="DataTable"
component={DataTable}
argTypes={{
columns: { table: { disable: true } },
data: { table: { disable: true } },
}}
/>

# DataTable

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

export const Template = (args) => (
<DataTable
columns={[
{
header: "Strings",
textAlign: "left",
},
{
header: "Numbers",
textAlign: "center",
width: "8rem",
},
{
header: "Dates",
textAlign: "right",
width: "25rem",
},
]}
data={[
["a", 1, Date(2021, 0, 1)],
["b", 2, Date(2021, 1, 1)],
["c", 3, Date(2021, 2, 1)],
]}
{...args}
/>
);

<Story name="DataTable">{Template.bind({})}</Story>

0 comments on commit 3945427

Please sign in to comment.