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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. console.log(theme.dark);
  50. const { setCollapsible } = useCollapsible();
  51. const collapsible = useCollapsibleHeader({
  52. config: {
  53. collapsedColor: headerColors ? headerColors : theme.colors.surface,
  54. useNativeDriver: true,
  55. },
  56. navigationOptions: {
  57. headerStyle: { backgroundColor: theme.colors.surface },
  58. },
  59. });
  60. useFocusEffect(
  61. useCallback(() => {
  62. setCollapsible(collapsible);
  63. }, [collapsible, setCollapsible])
  64. );
  65. const {
  66. containerPaddingTop,
  67. scrollIndicatorInsetTop,
  68. onScrollWithListener,
  69. } = collapsible;
  70. const paddingBottom = props.hasTab ? TAB_BAR_HEIGHT : 0;
  71. const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
  72. if (props.onScroll) {
  73. props.onScroll(event);
  74. }
  75. };
  76. const pprops =
  77. paddedProps !== undefined ? paddedProps(containerPaddingTop) : undefined;
  78. return (
  79. <Comp
  80. {...props}
  81. {...pprops}
  82. onScroll={onScrollWithListener(onScroll)}
  83. contentContainerStyle={{
  84. paddingTop: containerPaddingTop,
  85. paddingBottom: paddingBottom,
  86. ...styles.main,
  87. }}
  88. scrollIndicatorInsets={{ top: scrollIndicatorInsetTop }}
  89. >
  90. {props.children}
  91. </Comp>
  92. );
  93. }
  94. export default CollapsibleComponent;