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.

CustomTabBar.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import * as React from 'react';
  2. import {withTheme} from 'react-native-paper';
  3. import TabIcon from "./TabIcon";
  4. import TabHomeIcon from "./TabHomeIcon";
  5. import {Animated} from 'react-native';
  6. type Props = {
  7. state: Object,
  8. descriptors: Object,
  9. navigation: Object,
  10. theme: Object,
  11. collapsibleStack: Object,
  12. }
  13. type State = {
  14. translateY: AnimatedValue,
  15. barSynced: boolean,
  16. }
  17. const TAB_ICONS = {
  18. proxiwash: 'tshirt-crew',
  19. services: 'account-circle',
  20. planning: 'calendar-range',
  21. planex: 'clock',
  22. };
  23. /**
  24. * Abstraction layer for Agenda component, using custom configuration
  25. */
  26. class CustomTabBar extends React.Component<Props, State> {
  27. static TAB_BAR_HEIGHT = 48;
  28. activeColor: string;
  29. inactiveColor: string;
  30. state = {
  31. translateY: new Animated.Value(0),
  32. barSynced: false,// Is the bar synced with the header for animations?
  33. }
  34. // shouldComponentUpdate(nextProps: Props): boolean {
  35. // return (nextProps.theme.dark !== this.props.theme.dark)
  36. // || (nextProps.state.index !== this.props.state.index);
  37. // }
  38. tabRef: Object;
  39. constructor(props) {
  40. super(props);
  41. this.tabRef = React.createRef();
  42. this.activeColor = props.theme.colors.primary;
  43. this.inactiveColor = props.theme.colors.tabIcon;
  44. }
  45. onItemPress(route: Object, currentIndex: number, destIndex: number) {
  46. const event = this.props.navigation.emit({
  47. type: 'tabPress',
  48. target: route.key,
  49. canPreventDefault: true,
  50. });
  51. if (currentIndex !== destIndex && !event.defaultPrevented) {
  52. this.state.translateY = new Animated.Value(0);
  53. this.props.navigation.navigate(route.name);
  54. }
  55. }
  56. onItemLongPress(route: Object) {
  57. const event = this.props.navigation.emit({
  58. type: 'tabLongPress',
  59. target: route.key,
  60. canPreventDefault: true,
  61. });
  62. if (route.name === "home" && !event.defaultPrevented) {
  63. this.props.navigation.navigate('tetris');
  64. }
  65. }
  66. tabBarIcon = (route, focused) => {
  67. let icon = TAB_ICONS[route.name];
  68. icon = focused ? icon : icon + ('-outline');
  69. if (route.name !== "home")
  70. return icon;
  71. else
  72. return null;
  73. };
  74. onRouteChange = () => {
  75. this.setState({barSynced: false});
  76. }
  77. renderIcon = (route, index) => {
  78. const state = this.props.state;
  79. const {options} = this.props.descriptors[route.key];
  80. const label =
  81. options.tabBarLabel !== undefined
  82. ? options.tabBarLabel
  83. : options.title !== undefined
  84. ? options.title
  85. : route.name;
  86. const isFocused = state.index === index;
  87. const onPress = () => this.onItemPress(route, state.index, index);
  88. const onLongPress = () => this.onItemLongPress(route);
  89. if (isFocused) {
  90. const stackState = route.state;
  91. const stackRoute = route.state ? stackState.routes[stackState.index] : undefined;
  92. const params = stackRoute ? stackRoute.params : undefined;
  93. const collapsible = params ? params.collapsible : undefined;
  94. if (collapsible && !this.state.barSynced) {
  95. this.setState({
  96. translateY: Animated.multiply(-1.5, collapsible.translateY),
  97. barSynced: true,
  98. });
  99. }
  100. }
  101. const color = isFocused ? this.activeColor : this.inactiveColor;
  102. if (route.name !== "home") {
  103. return <TabIcon
  104. onPress={onPress}
  105. onLongPress={onLongPress}
  106. icon={this.tabBarIcon(route, isFocused)}
  107. color={color}
  108. label={label}
  109. focused={isFocused}
  110. extraData={state.index > index}
  111. key={route.key}
  112. />
  113. } else
  114. return <TabHomeIcon
  115. onPress={onPress}
  116. onLongPress={onLongPress}
  117. focused={isFocused}
  118. key={route.key}
  119. />
  120. };
  121. render() {
  122. this.props.navigation.addListener('state', this.onRouteChange);
  123. return (
  124. <Animated.View
  125. ref={this.tabRef}
  126. // animation={"fadeInUp"}
  127. // duration={500}
  128. // useNativeDriver
  129. style={{
  130. flexDirection: 'row',
  131. height: CustomTabBar.TAB_BAR_HEIGHT,
  132. width: '100%',
  133. position: 'absolute',
  134. bottom: 0,
  135. left: 0,
  136. backgroundColor: this.props.theme.colors.surface,
  137. transform: [{translateY: this.state.translateY}]
  138. }}
  139. >
  140. {this.props.state.routes.map(this.renderIcon)}
  141. </Animated.View>
  142. );
  143. }
  144. }
  145. export default withTheme(CustomTabBar);