-
Notifications
You must be signed in to change notification settings - Fork 31
/
Flex.js
106 lines (101 loc) · 2.03 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React from 'react';
import {View, StyleSheet} from 'react-native';
import PropTypes, {number} from 'prop-types';
import {useThemeContext} from '../util/ThemeProvider';
import {spaces} from '../util/prop-types';
const getContainerStyle = ({
theme,
space,
background,
flex,
flexDirection,
alignItems,
justifyContent,
flexWrap,
alignContent,
alignSelf,
}) => {
const styles = {
flex: flex,
flexDirection,
alignItems,
justifyContent,
padding: theme.space[space],
background: theme.colors[background],
};
if (flexWrap) {
styles.flexWrap = flexWrap;
}
if (alignContent) {
styles.alignContent = alignContent;
}
if (alignSelf) {
styles.alignSelf = alignSelf;
}
return styles;
};
const Flex = props => {
const theme = useThemeContext();
return (
<View
{...props}
style={StyleSheet.flatten([
getContainerStyle({...props, theme}),
props.style,
])}>
{props.children}
</View>
);
};
Flex.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
background: PropTypes.string,
space: spaces,
flexDirection: PropTypes.oneOf([
'row',
'column',
'row-reverse',
'column-reverse',
]),
alignItems: PropTypes.oneOf([
'flex-start',
'flex-end',
'center',
'stretch',
'baseline',
]),
alignSelf: PropTypes.oneOf([
'flex-start',
'flex-end',
'center',
'stretch',
'baseline',
]),
justifyContent: PropTypes.oneOf([
'flex-start',
'flex-end',
'center',
'space-between',
'space-around',
'space-evenly',
]),
alignContent: PropTypes.oneOf([
'flex-start',
'flex-end',
'center',
'space-between',
'space-around',
'space-evenly',
]),
flex: number,
flexWrap: PropTypes.oneOf(['no-wrap', 'wrap', 'wrap-reverse']),
children: PropTypes.oneOfType([PropTypes.array, PropTypes.element])
.isRequired,
};
Flex.defaultProps = {
flex: 1,
flexDirection: 'row',
space: 'none',
background: 'transparent',
};
export default Flex;