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

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