-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathStackList.js
More file actions
76 lines (71 loc) · 1.7 KB
/
StackList.js
File metadata and controls
76 lines (71 loc) · 1.7 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React from 'react';
import {View, FlatList, StyleSheet} from 'react-native';
import PropTypes from 'prop-types';
import {useThemeContext} from '../util/ThemeProvider';
import {spaces} from '../util/prop-types';
const getChildrenStyle = (
{theme, space, horizontalSpace, cropEndSpace, data},
index,
) => {
const childStyle = [
{
marginBottom: theme.space[space],
},
];
if (index === 0) {
childStyle.push({
marginTop: theme.space[space],
});
}
if (horizontalSpace) {
childStyle.push({
marginHorizontal: theme.space[horizontalSpace],
});
}
if (cropEndSpace) {
if (index === 0) {
childStyle.push({
marginTop: 0,
});
}
if (index === data.length - 1) {
childStyle.push({
marginBottom: 0,
});
}
}
return childStyle;
};
const StackList = React.forwardRef((props, ref) => {
const theme = useThemeContext();
return (
<FlatList
ref={ref}
{...props}
style={StyleSheet.flatten([
{backgroundColor: theme.colors[props.background]},
props.style,
])}
renderItem={child => (
<View style={getChildrenStyle({...props, theme}, child.index)}>
{props.renderItem(child)}
</View>
)}
/>
);
});
StackList.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
space: spaces,
horizontalSpace: spaces,
background: PropTypes.string,
cropEndSpace: PropTypes.bool,
...FlatList.propTypes,
};
StackList.defaultProps = {
space: 'md',
horizontalSpace: 'none',
cropEndSpace: true,
background: 'bg300',
};
export default StackList;