Advertisement
Guest User

ArtistList

a guest
Sep 2nd, 2017
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import {
  3.   StyleSheet,
  4.   ListView,
  5.   TouchableOpacity,
  6.   View,
  7. } from 'react-native';
  8.  
  9. import ArtistBox from './ArtistBox';
  10. import {Actions} from 'react-native-router-flux'
  11. import SearchBar from 'react-native-material-design-searchbar';
  12.  
  13.  
  14. export default class ArtistList extends Component {
  15.  
  16.    
  17.   state = {
  18.     text: ''
  19.   }
  20.  
  21.   constructor(props){
  22.     super(props);
  23.  
  24.     const ds = new ListView.DataSource({
  25.         rowHasChanged: (r1, r2) => r1 !== r2});
  26.         this.state = {
  27.             dataSource: ds
  28.         }
  29.   }
  30.  
  31.   componentDidMount(){
  32.     this.updateDataSource(this.props.artists)
  33.   }
  34.  
  35.   componentWillReceiveProps(newProps){
  36.      
  37.     if(newProps.artists !== this.props.artists){
  38.         this.updateDataSource(newProps.artists)      
  39.     }
  40.   }
  41.  
  42.   updateDataSource = data => {
  43.     this.setState({
  44.         dataSource: this.state.dataSource.cloneWithRows(data)
  45.     })
  46.   }
  47.  
  48.   handlePress(artist){
  49.       Actions.artistDetail({artist: artist})    
  50.   }
  51.  
  52.  
  53.   filterSearch(text){
  54.     const newData = this.props.artists.filter(function(item){
  55.       const itemData =  item.artist.toUpperCase()
  56.       const textData = text.toUpperCase()
  57.       return itemData.indexOf(textData) > -1
  58.  
  59.     })
  60.     this.setState = {
  61.         dataSource: this.state.dataSource.cloneWithRows(newData),
  62.         text: text
  63.     }
  64.   }
  65.  
  66.  
  67.   render() {
  68.        
  69.     return (
  70. <View>
  71.         <SearchBar
  72.         onSearchChange={(text) => this.filterSearch(text)}
  73.         height={50}
  74.         value={this.state.text}
  75.         onFocus={() => console.log('On Focus')}
  76.         onBlur={() => console.log('On Blur')}
  77.         placeholder={'Search...'}
  78.         autoCorrect={false}
  79.         padding={5}
  80.         returnKeyType={'search'}
  81.         />
  82.         <ListView
  83.             enableEmptySections={true}
  84.             dataSource={this.state.dataSource}
  85.             renderRow={(artist) => {
  86.                 return (
  87.                     <TouchableOpacity
  88.                         onPress={ () => this.handlePress(artist)}>
  89.                         <ArtistBox artist={artist} />
  90.                     </TouchableOpacity>
  91.                 );  
  92.             }}
  93.         />
  94. </View>
  95.     );
  96.   }
  97. }
  98.  
  99. const styles = StyleSheet.create({
  100.  
  101.   container: {
  102.     flex: 1,
  103.     paddingTop: 20,
  104.     backgroundColor: 'lightgray',
  105.   },
  106. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement