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.

GroupListAccordion.tsx 4.7KB

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