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 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 {group, groupCategory} from "../../../screens/Planex/GroupSelectionScreen";
  9. import type {CustomTheme} from "../../../managers/ThemeManager";
  10. type Props = {
  11. item: groupCategory,
  12. onGroupPress: (group) => void,
  13. onFavoritePress: (group) => void,
  14. currentSearchString: string,
  15. favoriteNumber: number,
  16. height: number,
  17. theme: CustomTheme,
  18. }
  19. const LIST_ITEM_HEIGHT = 64;
  20. class GroupListAccordion extends React.Component<Props> {
  21. shouldComponentUpdate(nextProps: Props) {
  22. return (nextProps.currentSearchString !== this.props.currentSearchString)
  23. || (nextProps.favoriteNumber !== this.props.favoriteNumber)
  24. || (nextProps.item.content.length !== this.props.item.content.length);
  25. }
  26. keyExtractor = (item: group) => item.id.toString();
  27. renderItem = ({item}: { item: group }) => {
  28. const onPress = () => this.props.onGroupPress(item);
  29. const onStarPress = () => this.props.onFavoritePress(item);
  30. return (
  31. <GroupListItem
  32. height={LIST_ITEM_HEIGHT}
  33. item={item}
  34. onPress={onPress}
  35. onStarPress={onStarPress}/>
  36. );
  37. }
  38. getData() {
  39. const originalData = this.props.item.content;
  40. let displayData = [];
  41. for (let i = 0; i < originalData.length; i++) {
  42. if (stringMatchQuery(originalData[i].name, this.props.currentSearchString))
  43. displayData.push(originalData[i]);
  44. }
  45. return displayData;
  46. }
  47. itemLayout = (data, index) => ({length: LIST_ITEM_HEIGHT, offset: LIST_ITEM_HEIGHT * index, index});
  48. render() {
  49. const item = this.props.item;
  50. return (
  51. <View>
  52. <AnimatedAccordion
  53. title={item.name}
  54. style={{
  55. height: this.props.height,
  56. justifyContent: 'center',
  57. }}
  58. left={props =>
  59. item.id === 0
  60. ? <List.Icon
  61. {...props}
  62. icon={"star"}
  63. color={this.props.theme.colors.tetrisScore}
  64. />
  65. : null}
  66. unmountWhenCollapsed={true}// Only render list if expanded for increased performance
  67. opened={this.props.item.id === 0 || this.props.currentSearchString.length > 0}
  68. >
  69. {/*$FlowFixMe*/}
  70. <FlatList
  71. data={this.getData()}
  72. extraData={this.props.currentSearchString}
  73. renderItem={this.renderItem}
  74. keyExtractor={this.keyExtractor}
  75. listKey={item.id.toString()}
  76. // Performance props, see https://reactnative.dev/docs/optimizing-flatlist-configuration
  77. getItemLayout={this.itemLayout}
  78. removeClippedSubviews={true}
  79. />
  80. </AnimatedAccordion>
  81. </View>
  82. );
  83. }
  84. }
  85. export default withTheme(GroupListAccordion)