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.

GroupListItem.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // @flow
  2. import * as React from 'react';
  3. import {List, TouchableRipple, withTheme} from 'react-native-paper';
  4. import * as Animatable from 'react-native-animatable';
  5. import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
  6. import type {CustomThemeType} from '../../../managers/ThemeManager';
  7. import type {PlanexGroupType} from '../../../screens/Planex/GroupSelectionScreen';
  8. import type {ListIconPropsType} from '../../../constants/PaperStyles';
  9. type PropsType = {
  10. theme: CustomThemeType,
  11. onPress: () => void,
  12. onStarPress: () => void,
  13. item: PlanexGroupType,
  14. favorites: Array<PlanexGroupType>,
  15. height: number,
  16. };
  17. const REPLACE_REGEX = /_/g;
  18. class GroupListItem extends React.Component<PropsType> {
  19. isFav: boolean;
  20. starRef: null | Animatable.View;
  21. constructor(props: PropsType) {
  22. super(props);
  23. this.isFav = this.isGroupInFavorites(props.favorites);
  24. }
  25. shouldComponentUpdate(nextProps: PropsType): boolean {
  26. const {favorites} = this.props;
  27. const favChanged = favorites.length !== nextProps.favorites.length;
  28. let newFavState = this.isFav;
  29. if (favChanged) newFavState = this.isGroupInFavorites(nextProps.favorites);
  30. const shouldUpdate = this.isFav !== newFavState;
  31. this.isFav = newFavState;
  32. return shouldUpdate;
  33. }
  34. onStarPress = () => {
  35. const {props} = this;
  36. const ref = this.starRef;
  37. if (ref != null) {
  38. if (this.isFav) ref.rubberBand();
  39. else ref.swing();
  40. }
  41. props.onStarPress();
  42. };
  43. isGroupInFavorites(favorites: Array<PlanexGroupType>): boolean {
  44. const {item} = this.props;
  45. for (let i = 0; i < favorites.length; i += 1) {
  46. if (favorites[i].id === item.id) return true;
  47. }
  48. return false;
  49. }
  50. render(): React.Node {
  51. const {props} = this;
  52. const {colors} = props.theme;
  53. return (
  54. <List.Item
  55. title={props.item.name.replace(REPLACE_REGEX, ' ')}
  56. onPress={props.onPress}
  57. left={(iconProps: ListIconPropsType): React.Node => (
  58. <List.Icon
  59. color={iconProps.color}
  60. style={iconProps.style}
  61. icon="chevron-right"
  62. />
  63. )}
  64. right={(iconProps: ListIconPropsType): React.Node => (
  65. <Animatable.View
  66. ref={(ref: Animatable.View) => {
  67. this.starRef = ref;
  68. }}
  69. useNativeDriver>
  70. <TouchableRipple
  71. onPress={this.onStarPress}
  72. style={{
  73. marginRight: 10,
  74. marginLeft: 'auto',
  75. marginTop: 'auto',
  76. marginBottom: 'auto',
  77. }}>
  78. <MaterialCommunityIcons
  79. size={30}
  80. style={{padding: 10}}
  81. name="star"
  82. color={this.isFav ? colors.tetrisScore : iconProps.color}
  83. />
  84. </TouchableRipple>
  85. </Animatable.View>
  86. )}
  87. style={{
  88. height: props.height,
  89. justifyContent: 'center',
  90. }}
  91. />
  92. );
  93. }
  94. }
  95. export default withTheme(GroupListItem);