application-amicale/components/CustomHeader.js

131 lines
4.4 KiB
JavaScript
Raw Normal View History

// @flow
import * as React from "react";
import {Body, Header, Input, Item, Left, Right, Subtitle, Title} from "native-base";
import {Platform, StyleSheet, View} from "react-native";
2019-06-25 22:20:24 +02:00
import {getStatusBarHeight} from "react-native-status-bar-height";
2019-06-27 10:17:51 +02:00
import Touchable from 'react-native-platform-touchable';
import ThemeManager from "../utils/ThemeManager";
import CustomMaterialIcon from "./CustomMaterialIcon";
import i18n from "i18n-js";
import {NavigationActions} from 'react-navigation';
2019-06-27 10:17:51 +02:00
type Props = {
hasBackButton: boolean,
hasSearchField: boolean,
searchCallback: Function,
shouldFocusSearchBar: boolean,
leftButton: React.Node,
rightButton: React.Node,
title: string,
subtitle: string,
navigation: Object,
hasTabs: boolean,
};
2019-06-29 15:43:57 +02:00
/**
* Custom component defining a header using native base
*
* @prop hasBackButton {boolean} Whether to show a back button or a burger menu. Use burger if unspecified
2019-06-29 15:43:57 +02:00
* @prop rightMenu {React.Node} Element to place at the right of the header. Use nothing if unspecified
* @prop title {string} This header title
* @prop navigation {Object} The navigation object from react navigation
*/
export default class CustomHeader extends React.Component<Props> {
static defaultProps = {
hasBackButton: false,
hasSearchField: false,
searchCallback: () => null,
shouldFocusSearchBar: false,
title: '',
subtitle: '',
leftButton: <View/>,
rightButton: <View/>,
hasTabs: false,
};
2019-06-25 22:20:24 +02:00
componentDidMount() {
2019-11-15 19:38:43 +01:00
if (this.refs.searchInput !== undefined && this.refs.searchInput._root !== undefined && this.props.shouldFocusSearchBar) {
// does not work if called to early for some reason...
2019-11-15 19:38:43 +01:00
setTimeout(() => this.refs.searchInput._root.focus(), 500);
}
}
getSearchBar() {
return (
<Body>
<Item
style={{
width: '100%',
marginBottom: 7
}}>
<CustomMaterialIcon
icon={'magnify'}
color={ThemeManager.getCurrentThemeVariables().toolbarBtnColor}/>
<Input
ref="searchInput"
placeholder={i18n.t('proximoScreen.search')}
placeholderTextColor={ThemeManager.getCurrentThemeVariables().toolbarPlaceholderColor}
onChangeText={(text) => this.props.searchCallback(text)}/>
</Item>
</Body>
);
}
getHeaderTitle() {
return (
<Body>
<Title style={{
color: ThemeManager.getCurrentThemeVariables().toolbarTextColor
}}>
{this.props.title}
</Title>
{this.props.subtitle !== '' ? <Subtitle>{this.props.subtitle}</Subtitle> : null}
</Body>
);
}
2019-06-25 22:20:24 +02:00
render() {
2019-06-27 10:17:51 +02:00
let button;
// Does the app have a back button or a burger menu ?
if (this.props.hasBackButton)
2019-06-27 10:17:51 +02:00
button =
<Touchable
style={{padding: 6}}
onPress={() => {
const backAction = NavigationActions.back();
this.props.navigation.dispatch(backAction);
}}>
<CustomMaterialIcon
color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
icon={Platform.OS === 'chevron-left' ? "" : "arrow-left"}/>
2019-06-27 10:17:51 +02:00
</Touchable>;
else
button = this.props.leftButton;
2019-06-27 10:17:51 +02:00
2019-06-25 22:20:24 +02:00
return (
<Header style={styles.header}
hasTabs={this.props.hasTabs}>
2019-11-15 16:44:10 +01:00
<Left style={{flex: 0}}>
2019-06-27 10:17:51 +02:00
{button}
2019-06-25 22:20:24 +02:00
</Left>
{this.props.hasSearchField ?
this.getSearchBar() :
this.getHeaderTitle()}
2019-11-15 16:44:10 +01:00
<Right style={{flex: this.props.hasSearchField ? 0 : 1}}>
{this.props.rightButton}
</Right>
2019-06-25 22:20:24 +02:00
</Header>);
}
};
// Fix header in status bar on Android
const styles = StyleSheet.create({
header: {
paddingTop: getStatusBarHeight(),
height: 54 + getStatusBarHeight(),
},
2019-06-27 10:17:51 +02:00
});