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 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // @flow
  2. import * as React from 'react';
  3. import {withTheme} from 'react-native-paper';
  4. import Animated from 'react-native-reanimated';
  5. import {Collapsible} from 'react-navigation-collapsible';
  6. import {StackNavigationProp} from '@react-navigation/stack';
  7. import TabIcon from './TabIcon';
  8. import TabHomeIcon from './TabHomeIcon';
  9. import type {CustomTheme} from '../../managers/ThemeManager';
  10. type RouteType = {
  11. name: string,
  12. key: string,
  13. params: {collapsible: Collapsible},
  14. state: {
  15. index: number,
  16. routes: Array<RouteType>,
  17. },
  18. };
  19. type PropsType = {
  20. state: {
  21. index: number,
  22. routes: Array<RouteType>,
  23. },
  24. descriptors: {
  25. [key: string]: {
  26. options: {
  27. tabBarLabel: string,
  28. title: string,
  29. },
  30. },
  31. },
  32. navigation: StackNavigationProp,
  33. theme: CustomTheme,
  34. };
  35. type StateType = {
  36. // eslint-disable-next-line flowtype/no-weak-types
  37. translateY: any,
  38. };
  39. const TAB_ICONS = {
  40. proxiwash: 'tshirt-crew',
  41. services: 'account-circle',
  42. planning: 'calendar-range',
  43. planex: 'clock',
  44. };
  45. class CustomTabBar extends React.Component<PropsType, StateType> {
  46. static TAB_BAR_HEIGHT = 48;
  47. constructor() {
  48. super();
  49. this.state = {
  50. translateY: new Animated.Value(0),
  51. };
  52. }
  53. /**
  54. * Navigates to the given route if it is different from the current one
  55. *
  56. * @param route Destination route
  57. * @param currentIndex The current route index
  58. * @param destIndex The destination route index
  59. */
  60. onItemPress(route: RouteType, currentIndex: number, destIndex: number) {
  61. const {navigation} = this.props;
  62. const event = navigation.emit({
  63. type: 'tabPress',
  64. target: route.key,
  65. canPreventDefault: true,
  66. });
  67. if (currentIndex !== destIndex && !event.defaultPrevented)
  68. navigation.navigate(route.name);
  69. }
  70. /**
  71. * Navigates to tetris screen on home button long press
  72. *
  73. * @param route
  74. */
  75. onItemLongPress(route: RouteType) {
  76. const {navigation} = this.props;
  77. const event = navigation.emit({
  78. type: 'tabLongPress',
  79. target: route.key,
  80. canPreventDefault: true,
  81. });
  82. if (route.name === 'home' && !event.defaultPrevented)
  83. navigation.navigate('game-start');
  84. }
  85. /**
  86. * Finds the active route and syncs the tab bar animation with the header bar
  87. */
  88. onRouteChange = () => {
  89. const {props} = this;
  90. props.state.routes.map(this.syncTabBar);
  91. };
  92. /**
  93. * Gets an icon for the given route if it is not the home one as it uses a custom button
  94. *
  95. * @param route
  96. * @param focused
  97. * @returns {null}
  98. */
  99. getTabBarIcon = (route: RouteType, focused: boolean): React.Node => {
  100. let icon = TAB_ICONS[route.name];
  101. icon = focused ? icon : `${icon}-outline`;
  102. if (route.name !== 'home') return icon;
  103. return null;
  104. };
  105. /**
  106. * Gets a tab icon render.
  107. * If the given route is focused, it syncs the tab bar and header bar animations together
  108. *
  109. * @param route The route for the icon
  110. * @param index The index of the current route
  111. * @returns {*}
  112. */
  113. getRenderIcon = (route: RouteType, index: number): React.Node => {
  114. const {props} = this;
  115. const {state} = props;
  116. const {options} = props.descriptors[route.key];
  117. let label;
  118. if (options.tabBarLabel != null) label = options.tabBarLabel;
  119. else if (options.title != null) label = options.title;
  120. else label = route.name;
  121. const onPress = () => {
  122. this.onItemPress(route, state.index, index);
  123. };
  124. const onLongPress = () => {
  125. this.onItemLongPress(route);
  126. };
  127. const isFocused = state.index === index;
  128. const color = isFocused
  129. ? props.theme.colors.primary
  130. : props.theme.colors.tabIcon;
  131. if (route.name !== 'home') {
  132. return (
  133. <TabIcon
  134. onPress={onPress}
  135. onLongPress={onLongPress}
  136. icon={this.getTabBarIcon(route, isFocused)}
  137. color={color}
  138. label={label}
  139. focused={isFocused}
  140. extraData={state.index > index}
  141. key={route.key}
  142. />
  143. );
  144. }
  145. return (
  146. <TabHomeIcon
  147. onPress={onPress}
  148. onLongPress={onLongPress}
  149. focused={isFocused}
  150. key={route.key}
  151. tabBarHeight={CustomTabBar.TAB_BAR_HEIGHT}
  152. />
  153. );
  154. };
  155. getIcons(): React.Node {
  156. const {props} = this;
  157. return props.state.routes.map(this.getRenderIcon);
  158. }
  159. syncTabBar = (route: RouteType, index: number) => {
  160. const {state} = this.props;
  161. const isFocused = state.index === index;
  162. if (isFocused) {
  163. const stackState = route.state;
  164. const stackRoute =
  165. stackState != null ? stackState.routes[stackState.index] : null;
  166. const params: {collapsible: Collapsible} | null =
  167. stackRoute != null ? stackRoute.params : null;
  168. const collapsible = params != null ? params.collapsible : null;
  169. if (collapsible != null) {
  170. this.setState({
  171. translateY: Animated.multiply(-1.5, collapsible.translateY), // Hide tab bar faster than header bar
  172. });
  173. }
  174. }
  175. };
  176. render(): React.Node {
  177. const {props, state} = this;
  178. props.navigation.addListener('state', this.onRouteChange);
  179. const icons = this.getIcons();
  180. // $FlowFixMe
  181. return (
  182. <Animated.View
  183. useNativeDriver
  184. style={{
  185. flexDirection: 'row',
  186. height: CustomTabBar.TAB_BAR_HEIGHT,
  187. width: '100%',
  188. position: 'absolute',
  189. bottom: 0,
  190. left: 0,
  191. backgroundColor: props.theme.colors.surface,
  192. transform: [{translateY: state.translateY}],
  193. }}>
  194. {icons}
  195. </Animated.View>
  196. );
  197. }
  198. }
  199. export default withTheme(CustomTabBar);