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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 '../../utils/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. });
  56. useFocusEffect(
  57. useCallback(() => {
  58. setCollapsible(collapsible);
  59. }, [collapsible, setCollapsible])
  60. );
  61. const {
  62. containerPaddingTop,
  63. scrollIndicatorInsetTop,
  64. onScrollWithListener,
  65. } = collapsible;
  66. const paddingBottom = props.hasTab ? TAB_BAR_HEIGHT : 0;
  67. const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
  68. if (props.onScroll) {
  69. props.onScroll(event);
  70. }
  71. };
  72. const pprops =
  73. paddedProps !== undefined ? paddedProps(containerPaddingTop) : undefined;
  74. return (
  75. <Comp
  76. {...props}
  77. {...pprops}
  78. onScroll={onScrollWithListener(onScroll)}
  79. contentContainerStyle={{
  80. paddingTop: containerPaddingTop,
  81. paddingBottom: paddingBottom,
  82. ...styles.main,
  83. }}
  84. scrollIndicatorInsets={{ top: scrollIndicatorInsetTop }}
  85. >
  86. {props.children}
  87. </Comp>
  88. );
  89. }
  90. export default CollapsibleComponent;