Advertisement
Guest User

Untitled

a guest
May 24th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import {
  3.   Platform, LayoutAnimation, StyleSheet, View, Text, ScrollView, UIManager, TouchableOpacity,
  4.   Image
  5. } from 'react-native';
  6.  
  7. class Accordion_Panel extends Component {
  8.   constructor() {
  9.     super();
  10.     this.state = {
  11.       updated_Height: 0
  12.     }
  13.   }
  14.  
  15.   componentWillReceiveProps(update_Props) {
  16.     if (update_Props.item.expanded) {
  17.       this.setState(() => {
  18.         return {
  19.           updated_Height: null
  20.         }
  21.       });
  22.     }
  23.     else {
  24.       this.setState(() => {
  25.         return {
  26.           updated_Height: 0
  27.         }
  28.       });
  29.     }
  30.   }
  31.  
  32.   shouldComponentUpdate(update_Props, nextState) {
  33.     if (update_Props.item.expanded !== this.props.item.expanded) {
  34.       return true;
  35.     }
  36.     return false;
  37.   }
  38.   render() {
  39.     return (
  40.       <View style={styles.Panel_Holder}>
  41.         <TouchableOpacity activeOpacity={0.7} onPress={this.props.onClickFunction} style={styles.Btn}>
  42.           <Text style={styles.Panel_Button_Text_1}>{this.props.item.title} </Text>
  43.           <Text style={styles.Panel_Button_Text_2}>{this.props.item.price} </Text>
  44.         </TouchableOpacity>
  45.         <View style={{ height: this.state.updated_Height, overflow: 'hidden', flexDirection: 'row' }}>
  46.           <Image style={styles.img_style} source={this.props.item.image} />
  47.           <Text style={styles.Panel_text}>
  48.             {this.props.item.body}
  49.           </Text>
  50.         </View>
  51.       </View>
  52.     );
  53.   }
  54. }
  55.  
  56. export default class App extends Component {
  57.   constructor() {
  58.     super();
  59.     if (Platform.OS === 'android') {
  60.       UIManager.setLayoutAnimationEnabledExperimental(true)
  61.     }
  62.     const array = [
  63.       { expanded: false, title: "Panel 1", price: "70$", image: require('./img/2.jpeg'), body: "Hello Guys this is the Animated Accordion Panel." },
  64.       { expanded: false, title: "Panel 2", price: "70$", image: require('./img/2.jpeg'), body: "Hello Guys this is the Animated Accordion Panel." },
  65.       { expanded: false, title: "Panel 3", price: "70$", image: require('./img/favicon.png'), body: "Hello Guys this is the Animated Accordion Panel." },
  66.     ];
  67.     this.state = { AccordionData: [...array] }
  68.   }
  69.  
  70.   update_Layout = (index) => {
  71.     LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
  72.     const array = this.state.AccordionData.map((item) => {
  73.       const newItem = Object.assign({}, item);
  74.       newItem.expanded = false;
  75.       return newItem;
  76.     });
  77.     array[index].expanded = true;
  78.     this.setState(() => {
  79.       return {
  80.         AccordionData: array
  81.       }
  82.     });
  83.   }
  84.  
  85.   render() {
  86.     return (
  87.       <View style={styles.MainContainer}>
  88.         <ScrollView contentContainerStyle={{ paddingHorizontal: 10, paddingVertical: 5 }}>
  89.           {
  90.             this.state.AccordionData.map((item, key) =>
  91.               (
  92.                 <Accordion_Panel key={key} onClickFunction={this.update_Layout.bind(this, key)} item={item} />
  93.               ))
  94.           }
  95.         </ScrollView>
  96.       </View>
  97.     );
  98.   }
  99. }
  100.  
  101. const styles = StyleSheet.create({
  102.  
  103.   MainContainer: {
  104.     flex: 1,
  105.     justifyContent: 'center',
  106.     paddingTop: (Platform.OS === 'ios') ? 20 : 0,
  107.     backgroundColor: '#d9d9d9'
  108.   },
  109.   img_style: {
  110.     margin: 15,
  111.   },
  112.   Panel_text: {
  113.     fontSize: 18,
  114.     color: '#000',
  115.     padding: 10,
  116.     margin: 10,
  117.     flex: 1,
  118.   },
  119.  
  120.   Panel_Button_Text_1: {
  121.     textAlign: 'left',
  122.     color: '#000',
  123.     fontSize: 21,
  124.   },
  125.   Panel_Button_Text_2: {
  126.     textAlign: 'right',
  127.     color: '#000',
  128.     fontSize: 21,
  129.   },
  130.  
  131.   Panel_Holder: {
  132.     borderWidth: 1,
  133.     borderColor: '#fff',
  134.     marginVertical: 5,
  135.     backgroundColor: '#fff'
  136.   },
  137.  
  138.   Btn: {
  139.     padding: 10,
  140.     backgroundColor: '#fff',
  141.     flexDirection: 'row',
  142.     justifyContent: 'space-between'
  143.   }
  144.  
  145. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement