Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomHeader.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // @flow
  2. import * as React from "react";
  3. import {Body, Header, Input, Item, Left, Right, Subtitle, Title} from "native-base";
  4. import {Platform, StyleSheet, View} from "react-native";
  5. import {getStatusBarHeight} from "react-native-status-bar-height";
  6. import Touchable from 'react-native-platform-touchable';
  7. import ThemeManager from "../utils/ThemeManager";
  8. import CustomMaterialIcon from "./CustomMaterialIcon";
  9. import i18n from "i18n-js";
  10. import {NavigationActions} from 'react-navigation';
  11. type Props = {
  12. hasBackButton: boolean,
  13. hasSearchField: boolean,
  14. searchCallback: Function,
  15. shouldFocusSearchBar: boolean,
  16. leftButton: React.Node,
  17. rightButton: React.Node,
  18. title: string,
  19. subtitle: string,
  20. navigation: Object,
  21. hasTabs: boolean,
  22. };
  23. /**
  24. * Custom component defining a header using native base
  25. *
  26. * @prop hasBackButton {boolean} Whether to show a back button or a burger menu. Use burger if unspecified
  27. * @prop rightMenu {React.Node} Element to place at the right of the header. Use nothing if unspecified
  28. * @prop title {string} This header title
  29. * @prop navigation {Object} The navigation object from react navigation
  30. */
  31. export default class CustomHeader extends React.Component<Props> {
  32. static defaultProps = {
  33. hasBackButton: false,
  34. hasSearchField: false,
  35. searchCallback: () => null,
  36. shouldFocusSearchBar: false,
  37. title: '',
  38. subtitle: '',
  39. leftButton: <View/>,
  40. rightButton: <View/>,
  41. hasTabs: false,
  42. };
  43. onPressBack: Function;
  44. constructor() {
  45. super();
  46. this.onPressBack = this.onPressBack.bind(this);
  47. }
  48. shouldComponentUpdate(nextProps: Props): boolean {
  49. return nextProps.title !== this.props.title ||
  50. nextProps.subtitle !== this.props.subtitle ||
  51. nextProps.hasBackButton !== this.props.hasBackButton ||
  52. nextProps.hasSearchField !== this.props.hasSearchField ||
  53. nextProps.shouldFocusSearchBar !== this.props.shouldFocusSearchBar ||
  54. nextProps.hasTabs !== this.props.hasTabs ||
  55. nextProps.rightButton !== this.props.rightButton ||
  56. nextProps.leftButton !== this.props.leftButton;
  57. }
  58. componentDidMount() {
  59. if (this.refs.searchInput !== undefined && this.refs.searchInput._root !== undefined && this.props.shouldFocusSearchBar) {
  60. // does not work if called too early for some reason...
  61. setTimeout(() => this.refs.searchInput._root.focus(), 500);
  62. }
  63. }
  64. getSearchBar() {
  65. return (
  66. <Body>
  67. <Item
  68. style={{
  69. width: '100%',
  70. marginBottom: 7
  71. }}>
  72. <CustomMaterialIcon
  73. icon={'magnify'}
  74. color={ThemeManager.getCurrentThemeVariables().toolbarBtnColor}/>
  75. <Input
  76. ref="searchInput"
  77. placeholder={i18n.t('proximoScreen.onSearchStringChange')}
  78. placeholderTextColor={ThemeManager.getCurrentThemeVariables().toolbarPlaceholderColor}
  79. onChangeText={this.props.searchCallback}/>
  80. </Item>
  81. </Body>
  82. );
  83. }
  84. getHeaderTitle() {
  85. return (
  86. <Body>
  87. <Title style={{
  88. color: ThemeManager.getCurrentThemeVariables().toolbarTextColor
  89. }}>
  90. {this.props.title}
  91. </Title>
  92. {this.props.subtitle !== '' ? <Subtitle>{this.props.subtitle}</Subtitle> : null}
  93. </Body>
  94. );
  95. }
  96. onPressBack() {
  97. const backAction = NavigationActions.back();
  98. this.props.navigation.dispatch(backAction);
  99. }
  100. render() {
  101. // console.log("rendering CustomHeader");
  102. let button;
  103. // Does the app have a back button or a burger menu ?
  104. if (this.props.hasBackButton)
  105. button =
  106. <Touchable
  107. style={{padding: 6}}
  108. onPress={this.onPressBack}>
  109. <CustomMaterialIcon
  110. color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
  111. icon={Platform.OS === 'ios' ? 'chevron-left' : "arrow-left"}/>
  112. </Touchable>;
  113. else
  114. button = this.props.leftButton;
  115. return (
  116. <Header style={styles.header}
  117. hasTabs={this.props.hasTabs}>
  118. <Left style={{flex: 0}}>
  119. {button}
  120. </Left>
  121. {this.props.hasSearchField ?
  122. this.getSearchBar() :
  123. this.getHeaderTitle()}
  124. <Right style={{flex: this.props.hasSearchField ? 0 : 1}}>
  125. {this.props.rightButton}
  126. </Right>
  127. </Header>);
  128. }
  129. };
  130. // Fix header in status bar on Android
  131. const styles = StyleSheet.create({
  132. header: {
  133. paddingTop: getStatusBarHeight(),
  134. height: 54 + getStatusBarHeight(),
  135. },
  136. });