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.4KB

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