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.

CardList.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. // @flow
  20. import * as React from 'react';
  21. import {Animated, Dimensions} from 'react-native';
  22. import type {ViewStyle} from 'react-native/Libraries/StyleSheet/StyleSheet';
  23. import ImageListItem from './ImageListItem';
  24. import CardListItem from './CardListItem';
  25. import type {ServiceItemType} from '../../../managers/ServicesManager';
  26. type PropsType = {
  27. dataset: Array<ServiceItemType>,
  28. isHorizontal?: boolean,
  29. contentContainerStyle?: ViewStyle | null,
  30. };
  31. export default class CardList extends React.Component<PropsType> {
  32. static defaultProps = {
  33. isHorizontal: false,
  34. contentContainerStyle: null,
  35. };
  36. windowWidth: number;
  37. horizontalItemSize: number;
  38. constructor(props: PropsType) {
  39. super(props);
  40. this.windowWidth = Dimensions.get('window').width;
  41. this.horizontalItemSize = this.windowWidth / 4; // So that we can fit 3 items and a part of the 4th => user knows he can scroll
  42. }
  43. getRenderItem = ({item}: {item: ServiceItemType}): React.Node => {
  44. const {props} = this;
  45. if (props.isHorizontal)
  46. return (
  47. <ImageListItem
  48. item={item}
  49. key={item.title}
  50. width={this.horizontalItemSize}
  51. />
  52. );
  53. return <CardListItem item={item} key={item.title} />;
  54. };
  55. keyExtractor = (item: ServiceItemType): string => item.key;
  56. render(): React.Node {
  57. const {props} = this;
  58. let containerStyle = {};
  59. if (props.isHorizontal) {
  60. containerStyle = {
  61. height: this.horizontalItemSize + 50,
  62. justifyContent: 'space-around',
  63. };
  64. }
  65. return (
  66. <Animated.FlatList
  67. data={props.dataset}
  68. renderItem={this.getRenderItem}
  69. keyExtractor={this.keyExtractor}
  70. numColumns={props.isHorizontal ? undefined : 2}
  71. horizontal={props.isHorizontal}
  72. contentContainerStyle={
  73. props.isHorizontal ? containerStyle : props.contentContainerStyle
  74. }
  75. pagingEnabled={props.isHorizontal}
  76. snapToInterval={
  77. props.isHorizontal ? (this.horizontalItemSize + 5) * 3 : null
  78. }
  79. />
  80. );
  81. }
  82. }