Skip to content

Commit

Permalink
Add withHeader HOC
Browse files Browse the repository at this point in the history
  • Loading branch information
haruelrovix committed Dec 11, 2018
1 parent f193aa3 commit 0cf995b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Components/Commit/CommitList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import React from 'react';
import { Text, View } from 'react-native';

import styles from '../Shared.style';
import withHeader from '../../HOCs/withHeader';

const CommitList = () => (
<View style={styles.container}>
<Text>Placeholder for second screen</Text>
</View>
);

export default CommitList
export default withHeader({ title: 'Commits' })(CommitList);
65 changes: 65 additions & 0 deletions src/HOCs/withHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { PureComponent } from 'react';
import { View, TouchableOpacity } from 'react-native';
import { Header } from 'react-native-elements';
import Icon from 'react-native-vector-icons/FontAwesome';

import styles from '../Components/Shared.style';

const containerStyle = {
alignItems: 'center',
height: 50,
justifyContent: 'center',
paddingHorizontal: 0,
paddingTop: 0,
width: '100%'
};

const centerContainerStyle = { paddingRight: 20 };

const buttonStyle = {
alignItems: 'center',
height: 48,
justifyContent: 'center',
paddingRight: 5,
width: 40
};

const textStyle = { color: '#fff' };

const withHeader = ({ title = '' }) => (WrappedComponent) => {
class HOC extends PureComponent {
goBack = () => this.props.history.goBack();

goHome = () => this.props.history.replace('/');

horizontalComponent = (name, size, onPress) => (
<TouchableOpacity onPress={onPress} style={buttonStyle}>
<Icon name={name} size={size} color='#fff' />
</TouchableOpacity>
);

centerComponent = (title) => ({
text: title.toUpperCase(),
style: textStyle
});

render() {
return (
<View style={styles.container}>
<Header
containerStyle={containerStyle}
centerContainerStyle={centerContainerStyle}
leftComponent={this.horizontalComponent('chevron-left', 20, this.goBack)}
centerComponent={this.centerComponent(title)}
rightComponent={this.horizontalComponent('home', 25, this.goHome)}
/>
<WrappedComponent />
</View>
);
}
}

return HOC;
}

export default withHeader;

0 comments on commit 0cf995b

Please sign in to comment.