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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // @flow
  2. import * as React from 'react';
  3. import {List, withTheme} from 'react-native-paper';
  4. import {FlatList, View} from 'react-native';
  5. import {stringMatchQuery} from '../../../utils/Search';
  6. import GroupListItem from './GroupListItem';
  7. import AnimatedAccordion from '../../Animations/AnimatedAccordion';
  8. import type {
  9. PlanexGroupType,
  10. PlanexGroupCategoryType,
  11. } from '../../../screens/Planex/GroupSelectionScreen';
  12. import type {CustomThemeType} from '../../../managers/ThemeManager';
  13. type PropsType = {
  14. item: PlanexGroupCategoryType,
  15. onGroupPress: (PlanexGroupType) => void,
  16. onFavoritePress: (PlanexGroupType) => void,
  17. currentSearchString: string,
  18. favoriteNumber: number,
  19. height: number,
  20. theme: CustomThemeType,
  21. };
  22. const LIST_ITEM_HEIGHT = 64;
  23. class GroupListAccordion extends React.Component<PropsType> {
  24. shouldComponentUpdate(nextProps: PropsType): boolean {
  25. const {props} = this;
  26. return (
  27. nextProps.currentSearchString !== props.currentSearchString ||
  28. nextProps.favoriteNumber !== props.favoriteNumber ||
  29. nextProps.item.content.length !== props.item.content.length
  30. );
  31. }
  32. getRenderItem = ({item}: {item: PlanexGroupType}): React.Node => {
  33. const {props} = this;
  34. const onPress = () => {
  35. props.onGroupPress(item);
  36. };
  37. const onStarPress = () => {
  38. props.onFavoritePress(item);
  39. };
  40. return (
  41. <GroupListItem
  42. height={LIST_ITEM_HEIGHT}
  43. item={item}
  44. onPress={onPress}
  45. onStarPress={onStarPress}
  46. />
  47. );
  48. };
  49. getData(): Array<PlanexGroupType> {
  50. const {props} = this;
  51. const originalData = props.item.content;
  52. const displayData = [];
  53. originalData.forEach((data: PlanexGroupType) => {
  54. if (stringMatchQuery(data.name, props.currentSearchString))
  55. displayData.push(data);
  56. });
  57. return displayData;
  58. }
  59. itemLayout = (
  60. data: ?Array<PlanexGroupType>,
  61. index: number,
  62. ): {length: number, offset: number, index: number} => ({
  63. length: LIST_ITEM_HEIGHT,
  64. offset: LIST_ITEM_HEIGHT * index,
  65. index,
  66. });
  67. keyExtractor = (item: PlanexGroupType): string => item.id.toString();
  68. render(): React.Node {
  69. const {props} = this;
  70. const {item} = this.props;
  71. return (
  72. <View>
  73. <AnimatedAccordion
  74. title={item.name}
  75. style={{
  76. height: props.height,
  77. justifyContent: 'center',
  78. }}
  79. left={({size}: {size: number}): React.Node =>
  80. item.id === 0 ? (
  81. <List.Icon
  82. size={size}
  83. icon="star"
  84. color={props.theme.colors.tetrisScore}
  85. />
  86. ) : null
  87. }
  88. unmountWhenCollapsed // Only render list if expanded for increased performance
  89. opened={props.item.id === 0 || props.currentSearchString.length > 0}>
  90. {/* $FlowFixMe */}
  91. <FlatList
  92. data={this.getData()}
  93. extraData={props.currentSearchString}
  94. renderItem={this.getRenderItem}
  95. keyExtractor={this.keyExtractor}
  96. listKey={item.id.toString()}
  97. // Performance props, see https://reactnative.dev/docs/optimizing-flatlist-configuration
  98. getItemLayout={this.itemLayout}
  99. removeClippedSubviews
  100. />
  101. </AnimatedAccordion>
  102. </View>
  103. );
  104. }
  105. }
  106. export default withTheme(GroupListAccordion);