Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. class DetailedView extends Component{
  2.  
  3. constructor(props){
  4. super(props);
  5.  
  6. this.icons = {
  7. 'up' : require('../Images/Arrowhead.png'),
  8. 'down' : require('../Images/Arrowhead-Down.png')
  9. };
  10.  
  11. this.state = {
  12. title : props.title,
  13. expanded : true,
  14. animation : new Animated.Value(),
  15. details: []
  16. };
  17. }
  18. toggle(){
  19. let initialValue = this.state.expanded? this.state.maxHeight + this.state.minHeight : this.state.minHeight,
  20. finalValue = this.state.expanded? this.state.minHeight : this.state.maxHeight + this.state.minHeight;
  21.  
  22. this.setState({
  23. expanded : !this.state.expanded
  24. });
  25.  
  26. this.state.animation.setValue(initialValue);
  27. Animated.spring(
  28. this.state.animation,
  29. {
  30. toValue: finalValue
  31. }
  32. ).start();
  33.  
  34. }
  35.  
  36. _setMaxHeight(event){
  37. this.setState({
  38. maxHeight : event.nativeEvent.layout.height
  39. });
  40. }
  41.  
  42. _setMinHeight(event){
  43. this.setState({
  44. minHeight : event.nativeEvent.layout.height
  45. });
  46. }
  47.  
  48. componentWillMount(){
  49. fetch('https://www.mywebsite.com' + this.props.navigation.state.params.id )
  50. .then((response) => response.json())
  51. .then((responseData) =>
  52. this.setState({
  53. details:responseData
  54. })
  55. );
  56. }
  57.  
  58. showDetailsFunction(){
  59. let icon = this.icons['down'];
  60.  
  61. if(this.state.expanded){
  62. icon = this.icons['up'];
  63. }
  64.  
  65. return this.state.details.map(detail =>
  66. <ScrollView>
  67. {detail.data.curriculum.map(curr =>
  68. <Animated.View
  69. style={[styles.container,{height: this.state.animation}]}>
  70. {curr.type == 'section'? (
  71. <View onLayout={this._setMinHeight.bind(this)}>
  72. <Card>
  73. <CardSection>
  74. <TouchableHighlight onPress={this.toggle.bind(this)} underlayColor="#f1f1f1">
  75. <Image style={styles.buttonImage} source={icon}></Image>
  76. </TouchableHighlight>
  77. <View style={styles.thumbnailContainerStyle}>
  78. <Text style={styles.userStyle}>
  79. {curr.title}
  80. </Text>
  81. </View>
  82.  
  83. </CardSection>
  84. </Card>
  85. </View>
  86. ): (<Text></Text>)}
  87. <View style={styles.body} onLayout={this._setMaxHeight.bind(this)}>
  88. {this.props.children}
  89. {curr.type== 'unit'? (
  90. <Card>
  91. <CardSection>
  92. <Text>{curr.title}</Text>
  93. </CardSection>
  94. </Card>
  95. ): (<Text></Text>)}
  96. </View>
  97.  
  98. </Animated.View>
  99. )}
  100. </ScrollView>
  101. );
  102. }
  103. render(){
  104.  
  105. return(
  106. <View>
  107. {this.showDetailsFunction()}
  108. </View>
  109. );
  110. }
  111. }
  112. const styles= StyleSheet.create({
  113. thumbnailContainerStyle:{
  114. marginLeft:10,
  115. marginRight:10,
  116. padding:10,
  117. },
  118. thumbnail_style :{
  119. height: 50,
  120. width:50
  121. },
  122. headerTextStyle :{
  123. fontSize :15
  124. },
  125. headerContentStyle:{
  126. flexDirection:'column' ,
  127. justifyContent: 'space-around'
  128. },
  129. userStyle:{
  130. fontSize:16,
  131. color:'#000000',
  132. fontWeight:'bold',
  133. marginLeft:-5
  134.  
  135. },
  136. container : {
  137. backgroundColor: '#fff',
  138. margin:10,
  139. },
  140. titleContainer : {
  141. flexDirection: 'row'
  142. },
  143. title : {
  144. flex : 1,
  145. padding : 10,
  146. color :'#2a2f43',
  147. fontWeight:'bold'
  148. },
  149.  
  150. buttonImage : {
  151. width : 30,
  152. height : 25,
  153. alignItems: 'flex-end'
  154.  
  155. },
  156. body : {
  157. padding : 10,
  158. paddingTop : 0
  159. }
  160. });
  161. export default DetailedView;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement