Skip to content

Commit

Permalink
feat: 🎸 add <ListTable> component
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 4, 2019
1 parent 61dbe24 commit c9d767a
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/ListTable/__story__/story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {createElement as h} from 'react';
import {storiesOf} from '@storybook/react';
import {ListTable} from '..';
import ShowDocs from '../../ShowDocs'

storiesOf('UI/ListTable', module)
// .add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/ListTable.md')}))
.add('Default', () =>
<ListTable>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</ListTable>
)
.add('Empty list', () =>
<ListTable>
</ListTable>
)
.add('Full two rows', () =>
<ListTable>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</ListTable>
)
.add('1 column', () =>
<ListTable cols={1}>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</ListTable>
)
.add('2 columns', () =>
<ListTable cols={2}>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</ListTable>
)
.add('3 columns', () =>
<ListTable cols={3}>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</ListTable>
)
.add('5 columns', () =>
<ListTable cols={5}>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</ListTable>
)
.add('6 columns', () =>
<ListTable cols={6}>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</ListTable>
)
.add('7 columns', () =>
<ListTable cols={7}>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</ListTable>
)
.add('User renderRow', () =>
<ListTable cols={3} renderRow={(cells) => {
return (
<tr style={{outline: '1px solid tomato'}}>
{cells}
</tr>
);
}}>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</ListTable>
)
36 changes: 36 additions & 0 deletions src/ListTable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from 'react';

const h = React.createElement;

const defaultRenderRow = cells => h('tr', null, cells);

export interface Props {
cols?: number;
renderRow?: (cells: React.ReactElement<any>[]) => React.ReactElement<any>;
}

export const ListTable: React.SFC<Props> = ({cols, renderRow, children}) => {
const list = React.Children.toArray(children);
const rows: React.ReactElement<any>[] = [];
const length = list.length;
const rowLength = Math.ceil(length / cols);

let i = 0;
for (let m = 0; m < rowLength; m++) {
const cells: React.ReactElement<any>[] = [];
for (let n = 0; n < cols; n++) {
cells.push(h('td', null, i < length ? list[i] : null));
i++;
}
rows.push(renderRow(cells));
}

return h('table', null, h('tbody', null,
rows,
));
};

ListTable.defaultProps = {
cols: 3,
renderRow: defaultRenderRow,
};

0 comments on commit c9d767a

Please sign in to comment.