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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. state = {
  29. translateY: new Animated.Value(0),
  30. barSynced: false,// Is the bar synced with the header for animations?
  31. }
  32. onItemPress(route: Object, currentIndex: number, destIndex: number) {
  33. const event = this.props.navigation.emit({
  34. type: 'tabPress',
  35. target: route.key,
  36. canPreventDefault: true,
  37. });
  38. if (currentIndex !== destIndex && !event.defaultPrevented) {
  39. this.state.translateY = new Animated.Value(0);
  40. this.props.navigation.navigate(route.name);
  41. }
  42. }
  43. onItemLongPress(route: Object) {
  44. const event = this.props.navigation.emit({
  45. type: 'tabLongPress',
  46. target: route.key,
  47. canPreventDefault: true,
  48. });
  49. if (route.name === "home" && !event.defaultPrevented) {
  50. this.props.navigation.navigate('tetris');
  51. }
  52. }
  53. tabBarIcon = (route, focused) => {
  54. let icon = TAB_ICONS[route.name];
  55. icon = focused ? icon : icon + ('-outline');
  56. if (route.name !== "home")
  57. return icon;
  58. else
  59. return null;
  60. };
  61. onRouteChange = () => {
  62. this.setState({barSynced: false});
  63. }
  64. renderIcon = (route, index) => {
  65. const state = this.props.state;
  66. const {options} = this.props.descriptors[route.key];
  67. const label =
  68. options.tabBarLabel !== undefined
  69. ? options.tabBarLabel
  70. : options.title !== undefined
  71. ? options.title
  72. : route.name;
  73. const isFocused = state.index === index;
  74. const onPress = () => this.onItemPress(route, state.index, index);
  75. const onLongPress = () => this.onItemLongPress(route);
  76. if (isFocused) {
  77. const stackState = route.state;
  78. const stackRoute = route.state ? stackState.routes[stackState.index] : undefined;
  79. const params = stackRoute ? stackRoute.params : undefined;
  80. const collapsible = params ? params.collapsible : undefined;
  81. if (collapsible && !this.state.barSynced) {
  82. this.setState({
  83. translateY: Animated.multiply(-1.5, collapsible.translateY),
  84. barSynced: true,
  85. });
  86. }
  87. }
  88. const color = isFocused ? this.props.theme.colors.primary : this.props.theme.colors.tabIcon;
  89. if (route.name !== "home") {
  90. return <TabIcon
  91. onPress={onPress}
  92. onLongPress={onLongPress}
  93. icon={this.tabBarIcon(route, isFocused)}
  94. color={color}
  95. label={label}
  96. focused={isFocused}
  97. extraData={state.index > index}
  98. key={route.key}
  99. />
  100. } else
  101. return <TabHomeIcon
  102. onPress={onPress}
  103. onLongPress={onLongPress}
  104. focused={isFocused}
  105. key={route.key}
  106. tabBarHeight={CustomTabBar.TAB_BAR_HEIGHT}
  107. />
  108. };
  109. render() {
  110. this.props.navigation.addListener('state', this.onRouteChange);
  111. return (
  112. <Animated.View
  113. useNativeDriver
  114. style={{
  115. flexDirection: 'row',
  116. height: CustomTabBar.TAB_BAR_HEIGHT,
  117. width: '100%',
  118. position: 'absolute',
  119. bottom: 0,
  120. left: 0,
  121. backgroundColor: this.props.theme.colors.surface,
  122. transform: [{translateY: this.state.translateY}],
  123. }}
  124. >
  125. {this.props.state.routes.map(this.renderIcon)}
  126. </Animated.View>
  127. );
  128. }
  129. }
  130. export default withTheme(CustomTabBar);