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.

CollapsibleComponent.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import React, { useCallback } from 'react';
  20. import { useCollapsibleHeader } from 'react-navigation-collapsible';
  21. import { TAB_BAR_HEIGHT } from '../Tabbar/CustomTabBar';
  22. import {
  23. NativeScrollEvent,
  24. NativeSyntheticEvent,
  25. StyleSheet,
  26. } from 'react-native';
  27. import { useTheme } from 'react-native-paper';
  28. import { useCollapsible } from '../../context/CollapsibleContext';
  29. import { useFocusEffect } from '@react-navigation/core';
  30. export type CollapsibleComponentPropsType = {
  31. children?: React.ReactNode;
  32. hasTab?: boolean;
  33. onScroll?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
  34. paddedProps?: (paddingTop: number) => Record<string, any>;
  35. headerColors?: string;
  36. };
  37. type Props = CollapsibleComponentPropsType & {
  38. component: React.ComponentType<any>;
  39. };
  40. const styles = StyleSheet.create({
  41. main: {
  42. minHeight: '100%',
  43. },
  44. });
  45. function CollapsibleComponent(props: Props) {
  46. const { paddedProps, headerColors } = props;
  47. const Comp = props.component;
  48. const theme = useTheme();
  49. const { setCollapsible } = useCollapsible();
  50. const collapsible = useCollapsibleHeader({
  51. config: {
  52. collapsedColor: headerColors ? headerColors : theme.colors.surface,
  53. useNativeDriver: true,
  54. },
  55. navigationOptions: {
  56. headerStyle: {
  57. backgroundColor: headerColors ? headerColors : theme.colors.surface,
  58. },
  59. },
  60. });
  61. useFocusEffect(
  62. useCallback(() => {
  63. setCollapsible(collapsible);
  64. }, [collapsible, setCollapsible])
  65. );
  66. const { containerPaddingTop, scrollIndicatorInsetTop, onScrollWithListener } =
  67. collapsible;
  68. const paddingBottom = props.hasTab ? TAB_BAR_HEIGHT : 0;
  69. const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
  70. if (props.onScroll) {
  71. props.onScroll(event);
  72. }
  73. };
  74. const pprops =
  75. paddedProps !== undefined ? paddedProps(containerPaddingTop) : undefined;
  76. return (
  77. <Comp
  78. {...props}
  79. {...pprops}
  80. onScroll={onScrollWithListener(onScroll)}
  81. contentContainerStyle={{
  82. paddingTop: containerPaddingTop,
  83. paddingBottom: paddingBottom,
  84. ...styles.main,
  85. }}
  86. scrollIndicatorInsets={{ top: scrollIndicatorInsetTop }}
  87. >
  88. {props.children}
  89. </Comp>
  90. );
  91. }
  92. export default CollapsibleComponent;