-
Notifications
You must be signed in to change notification settings - Fork 376
/
Flex.js
80 lines (71 loc) · 1.71 KB
/
Flex.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React from 'react'
import { View, TouchableOpacity } from 'react-native'
const getDirection = (key, directions = { rows: 'column', cols: 'row' }) =>
directions[key] || null
const getAlign = (
key,
align = {
start: 'flex-start',
end: 'flex-end',
center: 'center',
stretch: 'stretch',
}
) => align[key] || align['stretch']
const getJustify = (
key,
justify = {
start: 'flex-start',
center: 'center',
end: 'flex-end',
around: 'space-around',
between: 'space-between',
evenly: 'space-evenly',
}
) => justify[key] || justify['center']
const getStyle = ({ size = 1, direction, align, self, justify, style }) => [
{
flex: size,
flexDirection: direction && getDirection(direction),
alignItems: align && getAlign(align),
alignSelf: self && getAlign(self),
justifyContent: justify && getJustify(justify),
},
style,
]
const getProps = ({
size = 1,
direction,
align,
self,
justify,
style,
...props
}) => props
export const Block = props => {
return props.onPress ? (
<TouchableOpacity {...getProps(props)} style={getStyle(props)} />
) : (
<View {...getProps(props)} style={getStyle(props)} />
)
}
export const Rows = ({ direction, ...props }) => {
props.direction = 'rows'
return props.onPress ? (
<TouchableOpacity {...getProps(props)} style={getStyle(props)} />
) : (
<View {...getProps(props)} style={getStyle(props)} />
)
}
export const Cols = ({ direction, ...props }) => {
props.direction = 'cols'
return props.onPress ? (
<TouchableOpacity {...getProps(props)} style={getStyle(props)} />
) : (
<View {...getProps(props)} style={getStyle(props)} />
)
}
export default {
Rows,
Cols,
Block,
}