-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathHeaderCell.js
104 lines (94 loc) · 2.8 KB
/
HeaderCell.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
/* @flow weak */
/**
* mSupply Mobile
* Sustainable Solutions (NZ) Ltd. 2016
*/
import React from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
Text,
View,
ViewPropTypes,
TouchableOpacity,
} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
/**
* Renders a headerCell that supports being a plain View with Text or being a TouchableOpacity (with
* callback). In the latter case Sort arrows will be rendered and controlled with isSelected and
* isAscending props.
* @param {object} props Properties passed where component was created.
* @prop {boolean} isSelected When false up+down sort arrows renderHeader, otherwise as below
* @prop {boolean} isAscending Sort arrow up if true, down if false.
* @prop {StyleSheet} style Style of the headerCell (View props)
* @prop {StyleSheet} textStyle Style of the text in the HeaderCell
* @prop {number} width flexbox flex property, gives weight to the headerCell width
* @prop {func} onPress CallBack (should change sort order in parent)
* @prop {string} text Text to render in headerCell
* @return {React.Component} Return TouchableOpacity with sort arrows if onPress is given a
* function. Otherwise return a View.
*/
export function HeaderCell(props) {
const {
style,
textStyle,
width,
onPress,
text,
isSelected,
isAscending,
...containerProps
} = props;
function renderSortArrow() {
if (isSelected) {
// isAscending = true = a to z
if (isAscending) return <Icon name="sort-asc" size={16} style={defaultStyles.icon} />;
return <Icon name="sort-desc" size={16} style={defaultStyles.icon} />;
}
return <Icon name="sort" size={16} style={defaultStyles.icon} />;
}
if (typeof onPress === 'function') {
return (
<TouchableOpacity
{...containerProps}
style={[defaultStyles.headerCell, style, { flex: width }]}
onPress={onPress}
>
<Text style={textStyle}>
{text}
</Text>
{renderSortArrow()}
</TouchableOpacity>
);
}
return (
<View {...containerProps} style={[defaultStyles.headerCell, style, { flex: width }]}>
<Text style={textStyle}>
{text}
</Text>
</View>
);
}
HeaderCell.propTypes = {
isSelected: PropTypes.bool,
isAscending: PropTypes.bool,
style: ViewPropTypes.style,
textStyle: Text.propTypes.style,
width: PropTypes.number,
onPress: PropTypes.func,
text: PropTypes.string,
};
HeaderCell.defaultProps = {
width: 1,
};
const defaultStyles = StyleSheet.create({
headerCell: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
icon: {
marginRight: 10,
},
});