Skip to content

Commit ecad476

Browse files
authored
Merge pull request #38 from streamich/list-table
List table
2 parents 61dbe24 + d95444d commit ecad476

File tree

6 files changed

+181
-0
lines changed

6 files changed

+181
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const MyComponent = mock();
9494
- [`<OutsideClick>`](./docs/en/OutsideClick.md)
9595
- [`<Ripple>`](./docs/en/Ripple.md) and [`withRipple()`](./docs/en/Ripple.md#withripple) &mdash; [**example**](https://codesandbox.io/s/983q7jr80o)
9696
- [`<Img>`](./docs/en/Img.md)
97+
- [`<ListTable>`](./docs/en/ListTable.md)
9798
- [`<WidthQuery>`](./docs/en/WidthQuery.md), [`<View>`](./docs/en/View.md), [`<WindowWidthQuery>`](./docs/en/WindowWidthQuery.md), and [`<InlineWidthQuery>`](./docs/en/InlineWidthQuery.md)
9899
- [`<Audio>`](./docs/en/Audio.md) and [`<Video>`](./docs/en/Video.md)
99100
- [`<Speak>`](./docs/en/Speak.md), [`<Vibrate>`](./docs/en/Vibrate.md), [`<Alert>`](./docs/en/Alert.md)

Diff for: docs/en/ListTable.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# `<ListTable>`
2+
3+
Wraps `children` into a table with specified column number.
4+
5+
## Usage
6+
7+
Below renders 2x3 table.
8+
9+
```jsx
10+
import {ListTable} from 'libreact/lib/ListTable';
11+
12+
<ListTable cols={2}>
13+
<div>1</div>
14+
<div>2</div>
15+
<div>3</div>
16+
<div>4</div>
17+
<div>5</div>
18+
<div>6</div>
19+
</ListTable>
20+
```
21+
22+
23+
## Props
24+
25+
- `cols` &mdash; optional, number or columns table should have, defaults to `3`.
26+
- `renderRow` &mdash; optional, function that receives an array of `<td>` cells as React
27+
elements and should return a `<tr>` React element, defaults to `cells => h('tr', null, ...cells)`.

Diff for: docs/en/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
- [`<OutsideClick>`](./OutsideClick.md)
6161
- [`<Ripple>`](./Ripple.md) and [`withRipple()`](./Ripple.md#withripple) &mdash; [**example**](https://codesandbox.io/s/983q7jr80o)
6262
- [`<Img>`](./Img.md)
63+
- [`<ListTable>`](./ListTable.md)
6364
- [`<WidthQuery>`](./WidthQuery.md), [`<View>`](./View.md), [`<WindowWidthQuery>`](./WindowWidthQuery.md), and [`<InlineWidthQuery>`](./InlineWidthQuery.md)
6465
- [`<Audio>`](./Audio.md) and [`<Video>`](./Video.md)
6566
- [`<Speak>`](./Speak.md), [`<Vibrate>`](./Vibrate.md), [`<Alert>`](./Alert.md), `<Prompt>`, `<Confirm>`

Diff for: docs/en/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
* [OutsideClick](OutsideClick.md)
6969
* [Ripple](Ripple.md)
7070
* [Img](Img.md)
71+
* [ListTable](ListTable.md)
7172
* [Group](Group.md)
7273
* [InfiniteScroll](InfiniteScroll.md)
7374
* [WidthQuery](WidthQuery.md)

Diff for: src/ListTable/__story__/story.tsx

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import {createElement as h} from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import {ListTable} from '..';
4+
import ShowDocs from '../../ShowDocs'
5+
6+
storiesOf('UI/ListTable', module)
7+
.add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/ListTable.md')}))
8+
.add('Default', () =>
9+
<ListTable>
10+
<div>1</div>
11+
<div>2</div>
12+
<div>3</div>
13+
<div>4</div>
14+
</ListTable>
15+
)
16+
.add('Empty list', () =>
17+
<ListTable>
18+
</ListTable>
19+
)
20+
.add('Full two rows', () =>
21+
<ListTable>
22+
<div>1</div>
23+
<div>2</div>
24+
<div>3</div>
25+
<div>4</div>
26+
<div>5</div>
27+
<div>6</div>
28+
</ListTable>
29+
)
30+
.add('1 column', () =>
31+
<ListTable cols={1}>
32+
<div>1</div>
33+
<div>2</div>
34+
<div>3</div>
35+
<div>4</div>
36+
<div>5</div>
37+
<div>6</div>
38+
</ListTable>
39+
)
40+
.add('2 columns', () =>
41+
<ListTable cols={2}>
42+
<div>1</div>
43+
<div>2</div>
44+
<div>3</div>
45+
<div>4</div>
46+
<div>5</div>
47+
<div>6</div>
48+
</ListTable>
49+
)
50+
.add('3 columns', () =>
51+
<ListTable cols={3}>
52+
<div>1</div>
53+
<div>2</div>
54+
<div>3</div>
55+
<div>4</div>
56+
<div>5</div>
57+
<div>6</div>
58+
</ListTable>
59+
)
60+
.add('5 columns', () =>
61+
<ListTable cols={5}>
62+
<div>1</div>
63+
<div>2</div>
64+
<div>3</div>
65+
<div>4</div>
66+
<div>5</div>
67+
<div>6</div>
68+
</ListTable>
69+
)
70+
.add('6 columns', () =>
71+
<ListTable cols={6}>
72+
<div>1</div>
73+
<div>2</div>
74+
<div>3</div>
75+
<div>4</div>
76+
<div>5</div>
77+
<div>6</div>
78+
</ListTable>
79+
)
80+
.add('7 columns', () =>
81+
<ListTable cols={7}>
82+
<div>1</div>
83+
<div>2</div>
84+
<div>3</div>
85+
<div>4</div>
86+
<div>5</div>
87+
<div>6</div>
88+
</ListTable>
89+
)
90+
.add('User renderRow', () =>
91+
<ListTable cols={3} renderRow={(cells) => {
92+
return (
93+
<tr style={{outline: '1px solid tomato'}}>
94+
{cells}
95+
</tr>
96+
);
97+
}}>
98+
<div>1</div>
99+
<div>2</div>
100+
<div>3</div>
101+
<div>4</div>
102+
<div>5</div>
103+
<div>6</div>
104+
</ListTable>
105+
)
106+
.add('Cells as text', () =>
107+
<ListTable cols={2}>
108+
{'1'}
109+
{'2'}
110+
{'3'}
111+
{'4'}
112+
{'5'}
113+
{'6'}
114+
</ListTable>
115+
)

Diff for: src/ListTable/index.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as React from 'react';
2+
3+
const h = React.createElement;
4+
5+
const defaultRenderRow = cells => h('tr', null, ...cells);
6+
7+
export interface Props extends React.AllHTMLAttributes<any> {
8+
cols?: number;
9+
renderRow?: (cells: React.ReactElement<any>[]) => React.ReactElement<any>;
10+
}
11+
12+
export const ListTable: React.SFC<Props> = ({cols, renderRow, children, ...rest}) => {
13+
const list = React.Children.toArray(children);
14+
const rows: React.ReactElement<any>[] = [];
15+
const length = list.length;
16+
const rowLength = Math.ceil(length / cols);
17+
18+
let i = 0;
19+
for (let m = 0; m < rowLength; m++) {
20+
const cells: React.ReactElement<any>[] = [];
21+
for (let n = 0; n < cols; n++) {
22+
cells.push(h('td', null, i < length ? list[i] : null));
23+
i++;
24+
}
25+
rows.push(renderRow(cells));
26+
}
27+
28+
return h('table', rest, h('tbody', null,
29+
...rows,
30+
));
31+
};
32+
33+
ListTable.defaultProps = {
34+
cols: 3,
35+
renderRow: defaultRenderRow,
36+
};

0 commit comments

Comments
 (0)