Advertisement
w2wHDMPMGv8q99Ty

nested scrollviews

Nov 9th, 2018
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2. import {
  3.   View, Text, TouchableOpacity, StyleSheet, ScrollView, PanResponder,
  4. } from 'react-native'
  5. import PropTypes from 'prop-types'
  6. import FastImage from 'react-native-fast-image'
  7. import Icon from 'react-native-vector-icons/MaterialCommunityIcons'
  8. import IconSimple from 'react-native-vector-icons/SimpleLineIcons'
  9. import { ButtonGroup } from 'react-native-elements'
  10. import AlphabetListView from 'react-native-alphabetlistview'
  11. import constant from '../../utils/constant'
  12. import DetailCell from './DetailCell'
  13. import DetailCellItem from './DetailCellItem'
  14.  
  15. const styles = StyleSheet.create({
  16.   container: {
  17.     flex: 1,
  18.     flexDirection: 'column',
  19.     paddingHorizontal: 15,
  20.     backgroundColor: constant.backgroundColor,
  21.     position: 'relative',
  22.   },
  23.   top: {
  24.     flexDirection: 'row',
  25.     height: 210,
  26.     paddingTop: 10,
  27.   },
  28.   bottom: {
  29.     flex: 1,
  30.     paddingBottom: 10,
  31.   },
  32.   imgContainer: {
  33.     width: 145,
  34.     height: 189,
  35.     borderColor: `${constant.blackColor}80`,
  36.     borderWidth: 1,
  37.   },
  38.   image: {
  39.     flex: 1,
  40.     width: null,
  41.     height: null,
  42.     backgroundColor: 'transparent',
  43.   },
  44.   desc: {
  45.     flex: 1,
  46.     flexDirection: 'column',
  47.     paddingLeft: 10,
  48.   },
  49.   title: {
  50.     color: constant.greenColor,
  51.     fontSize: 18,
  52.     paddingBottom: 5,
  53.     marginTop: -5,
  54.   },
  55.   text: {
  56.     color: constant.mainColor,
  57.     fontSize: 14,
  58.     paddingVertical: 5,
  59.   },
  60.   textContent: {
  61.     color: constant.contentColor,
  62.   },
  63.   back: {
  64.     color: constant.whiteColor,
  65.     fontSize: 18,
  66.   },
  67.   headerLeftButton: {
  68.     flexDirection: 'row', alignContent: 'center', alignItems: 'center',
  69.   },
  70.   headerLeftIcon: {
  71.     color: constant.whiteColor, padding: 10,
  72.   },
  73.   headerRightIcon1: {
  74.     color: constant.whiteColor, padding: 10, marginLeft: 10,
  75.   },
  76.   headerRightIcon2: {
  77.     color: constant.whiteColor, padding: 10, marginRight: 10,
  78.   },
  79.   containerStyle: {
  80.     height: 40,
  81.     marginLeft: 0,
  82.     marginRight: 0,
  83.     borderRadius: 0,
  84.     marginTop: 0,
  85.     marginBottom: 10,
  86.     borderWidth: 1,
  87.     backgroundColor: constant.backgroundColor,
  88.     borderColor: constant.borderColor,
  89.   },
  90.   innerBorderStyle: {
  91.     color: constant.borderColor,
  92.     width: 1,
  93.   },
  94.   selectedButtonStyle: { backgroundColor: constant.greenColor },
  95.   selectedTextStyle: { color: constant.contentColor },
  96.   textStyle: {
  97.     color: constant.contentColor,
  98.     fontSize: 16,
  99.   },
  100.   description: {
  101.     color: constant.mainColor,
  102.     fontSize: 14,
  103.     lineHeight: 20,
  104.   },
  105. })
  106.  
  107. class Detail extends Component {
  108.   static navigationOptions = ({ navigation }) => ({
  109.     headerLeft: (
  110.       <TouchableOpacity style={styles.headerLeftButton} onPress={() => { navigation.goBack() }}>
  111.         <IconSimple name="arrow-left" size={25} style={styles.headerLeftIcon} />
  112.         <Text style={styles.back}>BACK</Text>
  113.       </TouchableOpacity>
  114.     ),
  115.     headerRight: (
  116.       <View style={{ flex: 1, flexDirection: 'row' }}>
  117.         <TouchableOpacity onPress={() => { }}>
  118.           <Icon name="star-outline" size={25} style={styles.headerRightIcon1} />
  119.         </TouchableOpacity>
  120.         <TouchableOpacity onPress={() => { }}>
  121.           <Icon name="download" size={25} style={styles.headerRightIcon2} />
  122.         </TouchableOpacity>
  123.       </View>
  124.     ),
  125.     headerTitleContainerStyle: {
  126.       justifyContent: 'flex-start',
  127.       alignItems: 'center',
  128.       marginLeft: -25,
  129.       marginRight: 35,
  130.     },
  131.   });
  132.  
  133.   static propTypes = {
  134.     navigation: PropTypes.object,
  135.   }
  136.  
  137.   static defaultProps = {
  138.     navigation: {},
  139.   }
  140.  
  141.   constructor(props) {
  142.     super(props)
  143.     this.state = {
  144.       selectedIndex: 0,
  145.       dataTest: {
  146.         A: ['some', 'entries', 'are here'],
  147.         B: ['some', 'entries', 'are here'],
  148.         C: ['some', 'entries', 'are here'],
  149.         D: ['some', 'entries', 'are here'],
  150.         E: ['some', 'entries', 'are here'],
  151.         F: ['some', 'entries', 'are here'],
  152.         G: ['some', 'entries', 'are here'],
  153.         H: ['some', 'entries', 'are here'],
  154.         I: ['some', 'entries', 'are here'],
  155.         J: ['some', 'entries', 'are here'],
  156.         K: ['some', 'entries', 'are here'],
  157.         L: ['some', 'entries', 'are here'],
  158.         M: ['some', 'entries', 'are here'],
  159.         N: ['some', 'entries', 'are here'],
  160.         O: ['some', 'entries', 'are here'],
  161.         P: ['some', 'entries', 'are here'],
  162.         Q: ['some', 'entries', 'are here'],
  163.         R: ['some', 'entries', 'are here'],
  164.         S: ['some', 'entries', 'are here'],
  165.         T: ['some', 'entries', 'are here'],
  166.         U: ['some', 'entries', 'are here'],
  167.         V: ['some', 'entries', 'are here'],
  168.         W: ['some', 'entries', 'are here'],
  169.         X: ['some', 'entries', 'are here'],
  170.         Y: ['some', 'entries', 'are here'],
  171.         Z: ['some', 'entries', 'are here'],
  172.       },
  173.     }
  174.   }
  175.  
  176.   updateIndex = (selectedIndex) => {
  177.     this.setState({ selectedIndex })
  178.   }
  179.  
  180.   renderImage = (img) => {
  181.     if (img !== '') {
  182.       return (
  183.         <FastImage
  184.           style={styles.image}
  185.           source={{
  186.             uri: img,
  187.             priority: FastImage.priority.high,
  188.           }}
  189.           resizeMode={FastImage.resizeMode.stretch}
  190.         />)
  191.     }
  192.     return null
  193.   }
  194.  
  195.   renderInfo = (label, list) => {
  196.     if (list.length > 0) {
  197.       return (
  198.         <Text style={styles.text}>
  199.           {label}
  200.           <Text style={styles.textContent}>{list.join(', ')}</Text>
  201.         </Text>
  202.       )
  203.     }
  204.     return null
  205.   }
  206.  
  207.   renderContent = (data) => {
  208.     const { selectedIndex } = this.state
  209.     if (selectedIndex === 0) {
  210.       return (
  211.         <ScrollView>
  212.           <Text style={styles.description}>{data.desc}</Text>
  213.         </ScrollView>
  214.       )
  215.     }
  216.     if (selectedIndex === 1) {
  217.       const dataSource = data.getChapter()
  218.       console.log(`data: ${JSON.stringify(dataSource)}`)
  219.       return (
  220.         <AlphabetListView
  221.           data={this.state.dataTest}
  222.           cell={DetailCell}
  223.           cellHeight={40}
  224.           sectionListItem={DetailCellItem}
  225.           sectionHeader={DetailCellItem}
  226.           sectionHeaderHeight={22.5}
  227.           // {...this._panResponder.panHandlers}
  228.           // onScrollEndDrag={() => this.fScroll.setNativeProps({ scrollEnabled: true })}
  229.           // onScroll={this.handleInsideScroll}
  230.         />
  231.       )
  232.     }
  233.     if (selectedIndex === 2) {
  234.       return <Text>Related</Text>
  235.     }
  236.     return null
  237.   }
  238.  
  239.   render() {
  240.     const { data } = this.props.navigation.state.params
  241.     const buttons = ['Description', 'Chapters', 'Related']
  242.     const { selectedIndex } = this.state
  243.     return (
  244.       <ScrollView
  245.         ref={(e) => { this.fScroll = e }}
  246.         style={styles.container}
  247.         stickyHeaderIndices={[1]}
  248.         // nestedScrollEnabled
  249.         // onScroll={this.handleScroll}
  250.       >
  251.         <View style={styles.top}>
  252.           <View style={styles.imgContainer}>
  253.             {this.renderImage(data.img)}
  254.           </View>
  255.           <View style={styles.desc}>
  256.             <Text style={styles.title}>{ data.title }</Text>
  257.             {this.renderInfo('Authors: ', data.author)}
  258.             {this.renderInfo('Genres: ', data.genre)}
  259.             <Text style={styles.text}>
  260.               {'Chapters: '}
  261.               <Text style={styles.textContent}>{data.chapter.length}</Text>
  262.             </Text>
  263.             <Text style={styles.text}>
  264.               {'Status: '}
  265.               <Text style={styles.textContent}>{data.status}</Text>
  266.             </Text>
  267.           </View>
  268.         </View>
  269.         <ButtonGroup
  270.           onPress={this.updateIndex}
  271.           selectedIndex={selectedIndex}
  272.           buttons={buttons}
  273.           containerBorderRadius={0}
  274.           containerStyle={styles.containerStyle}
  275.           innerBorderStyle={styles.innerBorderStyle}
  276.           selectedButtonStyle={styles.selectedButtonStyle}
  277.           selectedTextStyle={styles.selectedTextStyle}
  278.           textStyle={styles.textStyle}
  279.         />
  280.         <View style={styles.bottom}>
  281.           {this.renderContent(data)}
  282.         </View>
  283.       </ScrollView>
  284.     )
  285.   }
  286. }
  287. export default Detail
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement