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.

AnimatedAccordion.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 * as React from 'react';
  20. import {View, ViewStyle} from 'react-native';
  21. import {List, withTheme} from 'react-native-paper';
  22. import Collapsible from 'react-native-collapsible';
  23. import * as Animatable from 'react-native-animatable';
  24. type PropsType = {
  25. theme: ReactNativePaper.Theme;
  26. title: string;
  27. subtitle?: string;
  28. style: ViewStyle;
  29. left?: (props: {
  30. color: string;
  31. style?: {
  32. marginRight: number;
  33. marginVertical?: number;
  34. };
  35. }) => React.ReactNode;
  36. opened?: boolean;
  37. unmountWhenCollapsed?: boolean;
  38. children?: React.ReactNode;
  39. };
  40. type StateType = {
  41. expanded: boolean;
  42. };
  43. const AnimatedListIcon = Animatable.createAnimatableComponent(List.Icon);
  44. class AnimatedAccordion extends React.Component<PropsType, StateType> {
  45. chevronRef: {current: null | (typeof AnimatedListIcon & List.Icon)};
  46. chevronIcon: string;
  47. animStart: string;
  48. animEnd: string;
  49. constructor(props: PropsType) {
  50. super(props);
  51. this.chevronIcon = '';
  52. this.animStart = '';
  53. this.animEnd = '';
  54. this.state = {
  55. expanded: props.opened != null ? props.opened : false,
  56. };
  57. this.chevronRef = React.createRef();
  58. this.setupChevron();
  59. }
  60. shouldComponentUpdate(nextProps: PropsType): boolean {
  61. const {state, props} = this;
  62. if (nextProps.opened != null && nextProps.opened !== props.opened) {
  63. state.expanded = nextProps.opened;
  64. }
  65. return true;
  66. }
  67. setupChevron() {
  68. const {expanded} = this.state;
  69. if (expanded) {
  70. this.chevronIcon = 'chevron-up';
  71. this.animStart = '180deg';
  72. this.animEnd = '0deg';
  73. } else {
  74. this.chevronIcon = 'chevron-down';
  75. this.animStart = '0deg';
  76. this.animEnd = '180deg';
  77. }
  78. }
  79. toggleAccordion = () => {
  80. const {expanded} = this.state;
  81. if (this.chevronRef.current != null) {
  82. this.chevronRef.current.transitionTo({
  83. rotate: expanded ? this.animStart : this.animEnd,
  84. });
  85. this.setState((prevState: StateType): {expanded: boolean} => ({
  86. expanded: !prevState.expanded,
  87. }));
  88. }
  89. };
  90. render() {
  91. const {props, state} = this;
  92. const {colors} = props.theme;
  93. return (
  94. <View style={props.style}>
  95. <List.Item
  96. title={props.title}
  97. description={props.subtitle}
  98. titleStyle={state.expanded ? {color: colors.primary} : null}
  99. onPress={this.toggleAccordion}
  100. right={(iconProps) => (
  101. <AnimatedListIcon
  102. ref={this.chevronRef}
  103. style={iconProps.style}
  104. icon={this.chevronIcon}
  105. color={state.expanded ? colors.primary : iconProps.color}
  106. useNativeDriver
  107. />
  108. )}
  109. left={props.left}
  110. />
  111. <Collapsible collapsed={!state.expanded}>
  112. {!props.unmountWhenCollapsed ||
  113. (props.unmountWhenCollapsed && state.expanded)
  114. ? props.children
  115. : null}
  116. </Collapsible>
  117. </View>
  118. );
  119. }
  120. }
  121. export default withTheme(AnimatedAccordion);