2015-01-29 17:10:49 -08:00
/ * *
2018-09-11 15:27:47 -07:00
* Copyright ( c ) Facebook , Inc . and its affiliates .
2016-07-12 05:51:57 -07:00
*
2018-02-16 18:24:55 -08:00
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree .
2016-07-12 05:51:57 -07:00
*
2018-05-11 13:32:37 -07:00
* @ format
2018-08-09 08:32:04 -07:00
* @ flow strict - local
2015-03-23 15:07:33 -07:00
* /
2018-05-11 13:32:37 -07:00
2015-01-29 17:10:49 -08:00
'use strict' ;
2018-10-09 16:55:13 -07:00
const React = require ( 'react' ) ;
const ReactNative = require ( 'react-native' ) ;
const {
Image ,
ListView ,
TouchableHighlight ,
StyleSheet ,
Text ,
View ,
} = ReactNative ;
const ListViewDataSource = require ( 'ListViewDataSource' ) ;
const RNTesterPage = require ( './RNTesterPage' ) ;
2015-01-29 17:10:49 -08:00
2018-10-09 16:55:13 -07:00
import type { RNTesterProps } from 'RNTesterTypes' ;
2015-01-29 17:10:49 -08:00
2018-10-09 16:55:13 -07:00
type State = { |
dataSource : ListViewDataSource ,
| } ;
2015-01-29 17:10:49 -08:00
2018-10-09 16:55:13 -07:00
class ListViewSimpleExample extends React . Component < RNTesterProps , State > {
static title = '<ListView>' ;
static description = 'Performant, scrollable list of data.' ;
2015-01-29 17:10:49 -08:00
2018-10-09 16:55:13 -07:00
state = {
dataSource : this . getInitialDataSource ( ) ,
} ;
2015-03-23 10:06:16 -07:00
2018-10-09 16:55:13 -07:00
_pressData : { [ key : number ] : boolean } = { } ;
getInitialDataSource ( ) {
const ds = new ListView . DataSource ( { rowHasChanged : ( r1 , r2 ) => r1 !== r2 } ) ;
return ds . cloneWithRows ( this . _genRows ( { } ) ) ;
}
UNSAFE _componentWillMount ( ) {
2015-01-29 17:10:49 -08:00
this . _pressData = { } ;
2018-10-09 16:55:13 -07:00
}
2015-01-29 17:10:49 -08:00
2018-10-09 16:55:13 -07:00
render ( ) {
2015-01-29 17:10:49 -08:00
return (
2017-05-05 20:50:47 -07:00
< RNTesterPage
2016-01-26 10:29:47 -08:00
title = { this . props . navigator ? null : '<ListView>' }
2015-01-29 17:10:49 -08:00
noSpacer = { true }
noScroll = { true } >
< ListView
dataSource = { this . state . dataSource }
renderRow = { this . _renderRow }
2016-07-18 10:19:56 -07:00
renderSeparator = { this . _renderSeparator }
2015-01-29 17:10:49 -08:00
/ >
2017-05-05 20:50:47 -07:00
< / R N T e s t e r P a g e >
2015-01-29 17:10:49 -08:00
) ;
2018-10-09 16:55:13 -07:00
}
2015-01-29 17:10:49 -08:00
2018-10-09 16:55:13 -07:00
_renderRow = (
2018-05-11 13:32:37 -07:00
rowData : string ,
sectionID : number ,
rowID : number ,
highlightRow : ( sectionID : number , rowID : number ) => void ,
2018-10-09 16:55:13 -07:00
) => {
2018-10-30 12:55:17 -07:00
const rowHash = Math . abs ( hashCode ( rowData ) ) ;
const imgSource = THUMB _URLS [ rowHash % THUMB _URLS . length ] ;
2015-01-29 17:10:49 -08:00
return (
2018-05-11 13:32:37 -07:00
< TouchableHighlight
onPress = { ( ) => {
2016-05-05 03:42:02 -07:00
this . _pressRow ( rowID ) ;
highlightRow ( sectionID , rowID ) ;
} } >
2015-01-29 17:10:49 -08:00
< View >
< View style = { styles . row } >
< Image style = { styles . thumb } source = { imgSource } / >
< Text style = { styles . text } >
2018-06-06 05:20:40 -07:00
{ rowData + ' - ' + LOREM _IPSUM . substr ( 0 , ( rowHash % 301 ) + 10 ) }
2015-01-29 17:10:49 -08:00
< / T e x t >
< / V i e w >
< / V i e w >
< / T o u c h a b l e H i g h l i g h t >
) ;
2018-10-09 16:55:13 -07:00
} ;
2015-01-29 17:10:49 -08:00
2018-10-09 16:55:13 -07:00
_genRows ( pressData : { [ key : number ] : boolean } ) : Array < string > {
2018-10-30 12:55:17 -07:00
const dataBlob = [ ] ;
for ( let ii = 0 ; ii < 100 ; ii ++ ) {
const pressedText = pressData [ ii ] ? ' (pressed)' : '' ;
2015-01-29 17:10:49 -08:00
dataBlob . push ( 'Row ' + ii + pressedText ) ;
}
return dataBlob ;
2018-10-09 16:55:13 -07:00
}
2015-01-29 17:10:49 -08:00
2018-10-09 16:55:13 -07:00
_pressRow = ( rowID : number ) => {
2015-01-29 17:10:49 -08:00
this . _pressData [ rowID ] = ! this . _pressData [ rowID ] ;
2018-05-11 13:32:37 -07:00
this . setState ( {
dataSource : this . state . dataSource . cloneWithRows (
this . _genRows ( this . _pressData ) ,
) ,
} ) ;
2018-10-09 16:55:13 -07:00
} ;
2016-05-05 03:42:02 -07:00
2018-10-09 16:55:13 -07:00
_renderSeparator (
2018-05-11 13:32:37 -07:00
sectionID : number ,
rowID : number ,
adjacentRowHighlighted : boolean ,
) {
2016-05-05 03:42:02 -07:00
return (
< View
key = { ` ${ sectionID } - ${ rowID } ` }
style = { {
height : adjacentRowHighlighted ? 4 : 1 ,
backgroundColor : adjacentRowHighlighted ? '#3B5998' : '#CCCCCC' ,
} }
/ >
) ;
2018-10-09 16:55:13 -07:00
}
}
2015-01-29 17:10:49 -08:00
2018-10-30 12:55:17 -07:00
const THUMB _URLS = [
2015-12-22 09:02:12 -08:00
require ( './Thumbnails/like.png' ) ,
require ( './Thumbnails/dislike.png' ) ,
require ( './Thumbnails/call.png' ) ,
require ( './Thumbnails/fist.png' ) ,
require ( './Thumbnails/bandaged.png' ) ,
require ( './Thumbnails/flowers.png' ) ,
require ( './Thumbnails/heart.png' ) ,
require ( './Thumbnails/liking.png' ) ,
require ( './Thumbnails/party.png' ) ,
require ( './Thumbnails/poke.png' ) ,
require ( './Thumbnails/superlike.png' ) ,
require ( './Thumbnails/victory.png' ) ,
2018-05-11 13:32:37 -07:00
] ;
2018-10-30 12:55:17 -07:00
const LOREM _IPSUM =
2018-05-11 13:32:37 -07:00
'Lorem ipsum dolor sit amet, ius ad pertinax oportere accommodare, an vix civibus corrumpit referrentur. Te nam case ludus inciderint, te mea facilisi adipiscing. Sea id integre luptatum. In tota sale consequuntur nec. Erat ocurreret mei ei. Eu paulo sapientem vulputate est, vel an accusam intellegam interesset. Nam eu stet pericula reprimique, ea vim illud modus, putant invidunt reprehendunt ne qui.' ;
2015-01-29 17:10:49 -08:00
/* eslint no-bitwise: 0 */
2018-10-30 12:55:17 -07:00
const hashCode = function ( str ) {
let hash = 15 ;
for ( let ii = str . length - 1 ; ii >= 0 ; ii -- ) {
2018-05-11 13:32:37 -07:00
hash = ( hash << 5 ) - hash + str . charCodeAt ( ii ) ;
2015-01-29 17:10:49 -08:00
}
return hash ;
} ;
2018-10-30 12:55:17 -07:00
const styles = StyleSheet . create ( {
2015-01-29 17:10:49 -08:00
row : {
flexDirection : 'row' ,
justifyContent : 'center' ,
padding : 10 ,
backgroundColor : '#F6F6F6' ,
} ,
thumb : {
width : 64 ,
height : 64 ,
} ,
text : {
flex : 1 ,
} ,
} ) ;
module . exports = ListViewSimpleExample ;