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.

GroupListAccordion.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 {List, withTheme} from 'react-native-paper';
  22. import {FlatList, View} from 'react-native';
  23. import {stringMatchQuery} from '../../../utils/Search';
  24. import GroupListItem from './GroupListItem';
  25. import AnimatedAccordion from '../../Animations/AnimatedAccordion';
  26. import type {
  27. PlanexGroupType,
  28. PlanexGroupCategoryType,
  29. } from '../../../screens/Planex/GroupSelectionScreen';
  30. import type {CustomThemeType} from '../../../managers/ThemeManager';
  31. import type {ListIconPropsType} from '../../../constants/PaperStyles';
  32. type PropsType = {
  33. item: PlanexGroupCategoryType,
  34. favorites: Array<PlanexGroupType>,
  35. onGroupPress: (PlanexGroupType) => void,
  36. onFavoritePress: (PlanexGroupType) => void,
  37. currentSearchString: string,
  38. height: number,
  39. theme: CustomThemeType,
  40. };
  41. const LIST_ITEM_HEIGHT = 64;
  42. const REPLACE_REGEX = /_/g;
  43. class GroupListAccordion extends React.Component<PropsType> {
  44. shouldComponentUpdate(nextProps: PropsType): boolean {
  45. const {props} = this;
  46. return (
  47. nextProps.currentSearchString !== props.currentSearchString ||
  48. nextProps.favorites.length !== props.favorites.length ||
  49. nextProps.item.content.length !== props.item.content.length
  50. );
  51. }
  52. getRenderItem = ({item}: {item: PlanexGroupType}): React.Node => {
  53. const {props} = this;
  54. const onPress = () => {
  55. props.onGroupPress(item);
  56. };
  57. const onStarPress = () => {
  58. props.onFavoritePress(item);
  59. };
  60. return (
  61. <GroupListItem
  62. height={LIST_ITEM_HEIGHT}
  63. item={item}
  64. favorites={props.favorites}
  65. onPress={onPress}
  66. onStarPress={onStarPress}
  67. />
  68. );
  69. };
  70. getData(): Array<PlanexGroupType> {
  71. const {props} = this;
  72. const originalData = props.item.content;
  73. const displayData = [];
  74. originalData.forEach((data: PlanexGroupType) => {
  75. if (stringMatchQuery(data.name, props.currentSearchString))
  76. displayData.push(data);
  77. });
  78. return displayData;
  79. }
  80. itemLayout = (
  81. data: ?Array<PlanexGroupType>,
  82. index: number,
  83. ): {length: number, offset: number, index: number} => ({
  84. length: LIST_ITEM_HEIGHT,
  85. offset: LIST_ITEM_HEIGHT * index,
  86. index,
  87. });
  88. keyExtractor = (item: PlanexGroupType): string => item.id.toString();
  89. render(): React.Node {
  90. const {props} = this;
  91. const {item} = this.props;
  92. return (
  93. <View>
  94. <AnimatedAccordion
  95. title={item.name.replace(REPLACE_REGEX, ' ')}
  96. style={{
  97. height: props.height,
  98. justifyContent: 'center',
  99. }}
  100. left={(iconProps: ListIconPropsType): React.Node =>
  101. item.id === 0 ? (
  102. <List.Icon
  103. style={iconProps.style}
  104. icon="star"
  105. color={props.theme.colors.tetrisScore}
  106. />
  107. ) : null
  108. }
  109. unmountWhenCollapsed={item.id !== 0} // Only render list if expanded for increased performance
  110. opened={props.currentSearchString.length > 0}>
  111. <FlatList
  112. data={this.getData()}
  113. extraData={props.currentSearchString + props.favorites.length}
  114. renderItem={this.getRenderItem}
  115. keyExtractor={this.keyExtractor}
  116. listKey={item.id.toString()}
  117. // Performance props, see https://reactnative.dev/docs/optimizing-flatlist-configuration
  118. getItemLayout={this.itemLayout}
  119. removeClippedSubviews
  120. />
  121. </AnimatedAccordion>
  122. </View>
  123. );
  124. }
  125. }
  126. export default withTheme(GroupListAccordion);