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.js 3.7KB

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