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.

GroupListItem.js 2.4KB

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