-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathtiles.native.js
More file actions
59 lines (50 loc) · 1.27 KB
/
tiles.native.js
File metadata and controls
59 lines (50 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* External dependencies
*/
import { View } from 'react-native';
/**
* WordPress dependencies
*/
import { Children } from '@wordpress/element';
const containerStyle = {
flexDirection: 'row',
flexWrap: 'wrap',
};
const tileStyle = {
overflow: 'hidden',
flexDirection: 'row',
alignItems: 'center',
borderColor: 'transparent',
};
function Tiles( props ) {
const {
columns,
children,
groutSpacing = 10,
} = props;
const tileCount = Children.count( children );
const lastTile = tileCount - 1;
const lastRow = Math.floor( lastTile / columns );
const wrappedChildren = Children.map( children, ( child, index ) => {
const row = Math.floor( index / columns );
const rowLength = row === lastRow ? lastTile % columns + 1: columns;
const indexInRow = index % columns;
return (
<View style={ [ tileStyle, {
flexBasis: ( ( 1 / rowLength ) * 100 ) + '%',
borderLeftWidth: groutSpacing * ( indexInRow / rowLength ),
borderRightWidth: groutSpacing * ( 1 - ( indexInRow + 1 ) / rowLength ),
borderTopWidth: row === 0 ? 0 : groutSpacing / 2,
borderBottomWidth: row === lastRow ? 0 : groutSpacing / 2,
} ]}>
{ child }
</View>
);
} );
return (
<View style={ containerStyle }>
{ wrappedChildren }
</View>
);
}
export default Tiles;