Skip to content

Commit

Permalink
feat: add react-table to components
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 12, 2020
1 parent f7a822d commit 53aa0cb
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 25 deletions.
2 changes: 2 additions & 0 deletions ui/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
"react-animate-height": "^2.0.20",
"react-dom": "^16.8.3",
"react-popper-tooltip": "^2.10.1",
"react-table": "^7.0.0",
"react-tabs": "^3.1.0",
"theme-ui": "^0.3.1",
"@theme-ui/presets": "^0.3.0"
},
"devDependencies": {
"@types/jest": "^25.1.2",
"@types/markdown-to-jsx": "^6.11.0",
"@types/react-table": "^7.0.10",
"@types/react-tabs": "^2.3.1",
"@types/theme-ui": "^0.3.0",
"cross-env": "^5.2.1",
Expand Down
43 changes: 19 additions & 24 deletions ui/components/src/ActionBar/ActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,25 @@ const ActionColors = ({
}: {
theme: Theme;
disabled: boolean | undefined;
}) => {
console.log(theme);
return {
backgroundColor: transparentize(
0.15,
theme.colors?.['highlight'] as string,
),
color: disabled ? '#ddd' : 'background',
cursor: disabled ? 'not-allowed' : undefined,
px: 2,
py: 1,
lineHeight: 1,
borderRadius: 1,
display: 'inline-block',
boxShadow: `${transparentize(
0.9,
theme.colors?.text as string,
)} 0 1px 3px 1px, ${transparentize(
0.35,
theme.colors?.text as string,
)} 0 0 0 1px`,
border: `1px solid ${theme.colors?.['highlight'] as string}`,
};
};
}) => ({
backgroundColor: transparentize(0.15, theme.colors?.['highlight'] as string),
color: disabled ? '#ddd' : 'background',
cursor: disabled ? 'not-allowed' : undefined,
px: 2,
py: 1,
lineHeight: 1,
borderRadius: 1,
display: 'inline-block',
boxShadow: `${transparentize(
0.9,
theme.colors?.text as string,
)} 0 1px 3px 1px, ${transparentize(
0.35,
theme.colors?.text as string,
)} 0 0 0 1px`,
border: `1px solid ${theme.colors?.['highlight'] as string}`,
});

export const ActionBar: FunctionComponent<ActionBarProps> = ({
actionItems,
}) => {
Expand Down
62 changes: 62 additions & 0 deletions ui/components/src/Table/Table.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';
import faker from 'faker';
import { Table } from './Table';
import { ThemeProvider } from '../ThemeContext';

export default {
title: 'Components/Table',
component: Table,
};

const columns = [
{
Header: 'Name',
columns: [
{
Header: 'Name',
accessor: 'name',
},
{
Header: 'User Name',
accessor: 'username',
},
],
},
{
Header: 'Address',
columns: [
{
Header: 'Street',
accessor: 'address.street',
},
{
Header: 'Suite',
accessor: 'address.suite',
},
{
Header: 'City',
accessor: 'address.city',
},
{
Header: 'Zip Code',
accessor: 'address.zipcode',
},
],
},
];

export const simple = () => {
const data = React.useMemo(
() =>
Array.apply(null, Array(20)).map(() => ({
...faker.helpers.userCard(),
subRows: undefined,
})),
[],
);
return (
<ThemeProvider>
<Table columns={columns} data={data} />
</ThemeProvider>
);
};
69 changes: 69 additions & 0 deletions ui/components/src/Table/Table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* eslint-disable react/jsx-key */
import React, { FC } from 'react';
import { get } from '@theme-ui/css';
import { Box, useThemeUI } from 'theme-ui';
import { useTable, Column } from 'react-table';

export interface TableProps {
columns: Column[];
data: any[];
showHeader?: boolean;
}
export const Table: FC<TableProps> = ({ columns, data, showHeader = true }) => {
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({
columns,
data,
});
const { theme } = useThemeUI();
return (
<Box as="table" {...getTableProps()} css={get(theme, 'styles.table')}>
{showHeader && (
<Box as="thead" css={get(theme, 'styles.thead')}>
{headerGroups.map((headerGroup: any) => (
<Box
as="tr"
{...headerGroup.getHeaderGroupProps()}
css={get(theme, 'styles.tr')}
>
{headerGroup.headers.map((column: any) => (
<Box
as="th"
{...column.getHeaderProps()}
css={get(theme, 'styles.th')}
>
{column.render('Header')}
</Box>
))}
</Box>
))}
</Box>
)}
<Box as="tbody" {...getTableBodyProps()} css={get(theme, 'styles.tbody')}>
{rows.map((row: any) => {
prepareRow(row);
return (
<Box as="tr" {...row.getRowProps()} css={get(theme, 'styles.tr')}>
{row.cells.map((cell: any) => {
return (
<Box
as="td"
{...cell.getCellProps()}
css={get(theme, 'styles.td')}
>
{cell.render('Cell')}
</Box>
);
})}
</Box>
);
})}
</Box>
</Box>
);
};
1 change: 1 addition & 0 deletions ui/components/src/Table/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Table';
10 changes: 9 additions & 1 deletion ui/components/src/ThemeContext/ThemeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({
width: '100%',
borderSpacing: '0px',
},
th: {
border: 'none',
padding: '10px 0 10px 20px',
},
thead: {
borderBottom: '1px solid rgba(0, 0, 0, 0.8)',
},
td: {
padding: '16px 20px',
borderBottom: '1px solid rgba(0, 0, 0, 0.1)',
},
tr: {
borderBottom: '1px solid rgba(0, 0, 0, 0.1)',
borderBottom: 'none',
},
},
buttons: {
Expand Down
1 change: 1 addition & 0 deletions ui/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './Markdown';
export * from './Popover';
export * from './Source';
export * from './Subtitle';
export * from './Table';
export * from './Tabs';
export * from './ThemeContext';
export * from './Title';
Expand Down
1 change: 1 addition & 0 deletions ui/components/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare module '@theme-ui/presets';
declare module '@mdx-js/runtime';
declare module '@theme-ui/css';
declare module '@mdx-js/react' {
import * as React from 'react';
type ComponentType =
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3494,6 +3494,13 @@
dependencies:
"@types/react" "*"

"@types/react-table@^7.0.10":
version "7.0.10"
resolved "https://registry.yarnpkg.com/@types/react-table/-/react-table-7.0.10.tgz#8742cd76dfaf9857580698ca4d3a852b32377d0b"
integrity sha512-kpUiIlGUpHn4D/0xcLqHEQ0M8Tbg6pbgZqXBDejfn1oBK2cjnOaZKLlqPrh5poIRp8JMJrOv2UWDCRiXARUGBA==
dependencies:
"@types/react" "*"

"@types/react-tabs@^2.3.1":
version "2.3.1"
resolved "https://registry.yarnpkg.com/@types/react-tabs/-/react-tabs-2.3.1.tgz#62d3322667ac228c4d0f9f36ac9f86988c0b3971"
Expand Down Expand Up @@ -13577,6 +13584,11 @@ react-syntax-highlighter@^11.0.2:
prismjs "^1.8.4"
refractor "^2.4.1"

react-table@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/react-table/-/react-table-7.0.0.tgz#3297e454cbffe916626b184f5394d7e7e098fa36"
integrity sha512-/RKUYLuqrupUs0qHdjdQLmgwdQ9mgXPnpshqv2T+OQUGhTu0XuLXVc6GOIywemXNf6qjL3dj81O6zALLK74Emw==

react-tabs@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/react-tabs/-/react-tabs-3.1.0.tgz#ecc50f034c1d6da2606fab9293055bbc861b382e"
Expand Down

0 comments on commit 53aa0cb

Please sign in to comment.