Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tools): Implement basic functionality of table component #47263

Merged
merged 9 commits into from Nov 2, 2022
1 change: 1 addition & 0 deletions tools/ui-components/src/index.ts
Expand Up @@ -2,3 +2,4 @@
export { Button } from './button';
export { Alert } from './alert';
export { Image } from './image';
export { Table } from './table';
2 changes: 2 additions & 0 deletions tools/ui-components/src/table/index.ts
@@ -0,0 +1,2 @@
export { Table } from './table';
export type { TableProps } from './types';
83 changes: 83 additions & 0 deletions tools/ui-components/src/table/table.stories.tsx
@@ -0,0 +1,83 @@
import React from 'react';
import { Story } from '@storybook/react';
import { Table, TableProps } from '.';
const exampleTable = (
<>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<td>2</td>
<td>John</td>
<td>Loos</td>
<td>@mlos</td>
</tr>
<tr>
<td>3</td>
<td>Joe</td>
<td>Kot</td>
<td>@mko</td>
</tr>
</tbody>
</>
);

const story = {
title: 'Example/Table',
component: Table,
parameters: {
controls: {
include: [
'variant',
'size',
'bordered',
'borderless',
'hover',
'striped',
'condensed',
'responsive'
]
}
},
argTypes: {
striped: {
options: [true, false],
control: { type: 'radio' }
},
condensed: {
options: [true, false],
control: { type: 'radio' }
}
}
};

const Template: Story<TableProps> = args => (
<Table {...args}>{exampleTable}</Table>
);
export const Default = Template.bind({});
Default.args = {
condensed: false,
striped: false
};
export const Condensed = Template.bind({});
Condensed.args = {
condensed: true
};
export const Striped = Template.bind({});
Striped.args = {
striped: true
};

export default story;
45 changes: 45 additions & 0 deletions tools/ui-components/src/table/table.tsx
@@ -0,0 +1,45 @@
import React from 'react';

import { TableProps } from './types';

const defaultClassNames = [
'table',
'table-auto',
'w-full',
'max-w-full',
'border-collapse',
'text-left',
'text-foreground-tertiary',
'[&_th]:font-normal'
];

const computeClassNames = ({
condensed,
striped
}: {
condensed: boolean;
striped: boolean;
}) => {
const classNames = [...defaultClassNames];
if (condensed) classNames.push('[&_td]:p-1 [&_th]:p-1');
else classNames.push('[&_td]:p-2 [&_th]:p-2');
if (striped)
classNames.push('[&>tbody>tr:nth-of-type(odd)]:bg-background-tertiary');

return classNames.join(' ');
};

export const Table = React.forwardRef<HTMLTableElement, TableProps>(
({ striped = false, condensed = false, ...props }, ref) => {
const classNames = React.useMemo(
() => computeClassNames({ condensed, striped }),
[condensed, striped]
);

const table = <table {...props} ref={ref} className={classNames} />;

return table;
}
);

Table.displayName = 'Table';
7 changes: 7 additions & 0 deletions tools/ui-components/src/table/types.ts
@@ -0,0 +1,7 @@
import React from 'react';

export interface TableProps
extends React.TableHTMLAttributes<HTMLTableElement> {
condensed?: boolean;
striped?: boolean;
}