Skip to content

Commit 0cf995b

Browse files
committed
Add withHeader HOC
1 parent f193aa3 commit 0cf995b

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/Components/Commit/CommitList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import React from 'react';
22
import { Text, View } from 'react-native';
33

44
import styles from '../Shared.style';
5+
import withHeader from '../../HOCs/withHeader';
56

67
const CommitList = () => (
78
<View style={styles.container}>
89
<Text>Placeholder for second screen</Text>
910
</View>
1011
);
1112

12-
export default CommitList
13+
export default withHeader({ title: 'Commits' })(CommitList);

src/HOCs/withHeader.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React, { PureComponent } from 'react';
2+
import { View, TouchableOpacity } from 'react-native';
3+
import { Header } from 'react-native-elements';
4+
import Icon from 'react-native-vector-icons/FontAwesome';
5+
6+
import styles from '../Components/Shared.style';
7+
8+
const containerStyle = {
9+
alignItems: 'center',
10+
height: 50,
11+
justifyContent: 'center',
12+
paddingHorizontal: 0,
13+
paddingTop: 0,
14+
width: '100%'
15+
};
16+
17+
const centerContainerStyle = { paddingRight: 20 };
18+
19+
const buttonStyle = {
20+
alignItems: 'center',
21+
height: 48,
22+
justifyContent: 'center',
23+
paddingRight: 5,
24+
width: 40
25+
};
26+
27+
const textStyle = { color: '#fff' };
28+
29+
const withHeader = ({ title = '' }) => (WrappedComponent) => {
30+
class HOC extends PureComponent {
31+
goBack = () => this.props.history.goBack();
32+
33+
goHome = () => this.props.history.replace('/');
34+
35+
horizontalComponent = (name, size, onPress) => (
36+
<TouchableOpacity onPress={onPress} style={buttonStyle}>
37+
<Icon name={name} size={size} color='#fff' />
38+
</TouchableOpacity>
39+
);
40+
41+
centerComponent = (title) => ({
42+
text: title.toUpperCase(),
43+
style: textStyle
44+
});
45+
46+
render() {
47+
return (
48+
<View style={styles.container}>
49+
<Header
50+
containerStyle={containerStyle}
51+
centerContainerStyle={centerContainerStyle}
52+
leftComponent={this.horizontalComponent('chevron-left', 20, this.goBack)}
53+
centerComponent={this.centerComponent(title)}
54+
rightComponent={this.horizontalComponent('home', 25, this.goHome)}
55+
/>
56+
<WrappedComponent />
57+
</View>
58+
);
59+
}
60+
}
61+
62+
return HOC;
63+
}
64+
65+
export default withHeader;

0 commit comments

Comments
 (0)