Application Android et IOS pour l'amicale des élèves
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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // @flow
  2. import * as React from "react";
  3. import {Body, Header, Input, Item, Left, Right, 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. type Props = {
  11. hasBackButton: boolean,
  12. hasSearchField: boolean,
  13. searchCallback: Function,
  14. shouldFocusSearchBar: boolean,
  15. leftButton: React.Node,
  16. rightButton: React.Node,
  17. title: string,
  18. navigation: Object,
  19. hasTabs: boolean,
  20. };
  21. /**
  22. * Custom component defining a header using native base
  23. *
  24. * @prop hasBackButton {boolean} Whether to show a back button or a burger menu. Use burger if unspecified
  25. * @prop rightMenu {React.Node} Element to place at the right of the header. Use nothing if unspecified
  26. * @prop title {string} This header title
  27. * @prop navigation {Object} The navigation object from react navigation
  28. */
  29. export default class CustomHeader extends React.Component<Props> {
  30. static defaultProps = {
  31. hasBackButton: false,
  32. hasSearchField: false,
  33. searchCallback: () => null,
  34. shouldFocusSearchBar: false,
  35. title: '',
  36. leftButton: <View/>,
  37. rightButton: <View/>,
  38. hasTabs: false,
  39. };
  40. componentDidMount() {
  41. if (this.refs.searchInput !== undefined && this.refs.searchInput._root !== undefined && this.props.shouldFocusSearchBar) {
  42. // does not work if called to early for some reason...
  43. setTimeout(() => this.refs.searchInput._root.focus(), 500);
  44. }
  45. }
  46. getSearchBar() {
  47. return (
  48. <Item
  49. style={{
  50. width: '100%',
  51. marginBottom: 7
  52. }}>
  53. <CustomMaterialIcon
  54. icon={'magnify'}
  55. color={ThemeManager.getCurrentThemeVariables().toolbarBtnColor}/>
  56. <Input
  57. ref="searchInput"
  58. placeholder={i18n.t('proximoScreen.search')}
  59. placeholderTextColor={ThemeManager.getCurrentThemeVariables().toolbarPlaceholderColor}
  60. onChangeText={(text) => this.props.searchCallback(text)}/>
  61. </Item>
  62. );
  63. }
  64. render() {
  65. let button;
  66. // Does the app have a back button or a burger menu ?
  67. if (this.props.hasBackButton)
  68. button =
  69. <Touchable
  70. style={{padding: 6}}
  71. onPress={() => this.props.navigation.goBack()}>
  72. <CustomMaterialIcon
  73. color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
  74. icon="arrow-left"/>
  75. </Touchable>;
  76. else
  77. button = this.props.leftButton;
  78. return (
  79. <Header style={styles.header}
  80. hasTabs={this.props.hasTabs}>
  81. <Left style={{flex: 0}}>
  82. {button}
  83. </Left>
  84. <Body>
  85. {this.props.hasSearchField ?
  86. this.getSearchBar() :
  87. <Title style={{
  88. paddingLeft: 10,
  89. color: ThemeManager.getCurrentThemeVariables().toolbarTextColor
  90. }}>{this.props.title}</Title>}
  91. </Body>
  92. <Right style={{flex: this.props.hasSearchField ? 0 : 1}}>
  93. {this.props.rightButton}
  94. </Right>
  95. </Header>);
  96. }
  97. };
  98. // Fix header in status bar on Android
  99. const styles = StyleSheet.create({
  100. header: {
  101. paddingTop: getStatusBarHeight(),
  102. height: 54 + getStatusBarHeight(),
  103. },
  104. });