2015-01-30 01:10:49 +00:00
/ * *
2017-05-06 03:50:47 +00:00
* Copyright ( c ) 2015 - present , Facebook , Inc .
2016-07-12 12:51:57 +00:00
*
2018-02-17 02:24:55 +00: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 12:51:57 +00:00
*
2018-05-11 20:32:37 +00:00
* @ format
2018-08-09 15:32:04 +00:00
* @ flow strict - local
2015-03-23 22:07:33 +00:00
* /
2018-05-11 20:32:37 +00:00
2015-01-30 01:10:49 +00:00
'use strict' ;
2016-04-09 03:36:40 +00:00
var React = require ( 'react' ) ;
2017-07-07 21:24:25 +00:00
var createReactClass = require ( 'create-react-class' ) ;
2016-04-09 03:36:40 +00:00
var ReactNative = require ( 'react-native' ) ;
2018-05-11 20:32:37 +00:00
var { Image , ListView , TouchableHighlight , StyleSheet , Text , View } = ReactNative ;
2015-01-30 01:10:49 +00:00
2017-05-06 03:50:47 +00:00
var RNTesterPage = require ( './RNTesterPage' ) ;
2015-01-30 01:10:49 +00:00
2017-07-07 21:24:25 +00:00
var ListViewSimpleExample = createReactClass ( {
displayName : 'ListViewSimpleExample' ,
2015-01-30 01:10:49 +00:00
statics : {
2016-01-26 18:29:47 +00:00
title : '<ListView>' ,
2018-05-11 20:32:37 +00:00
description : 'Performant, scrollable list of data.' ,
2015-01-30 01:10:49 +00:00
} ,
getInitialState : function ( ) {
2015-03-03 16:38:50 +00:00
var ds = new ListView . DataSource ( { rowHasChanged : ( r1 , r2 ) => r1 !== r2 } ) ;
2015-01-30 01:10:49 +00:00
return {
dataSource : ds . cloneWithRows ( this . _genRows ( { } ) ) ,
} ;
} ,
2015-03-23 17:06:16 +00:00
_pressData : ( { } : { [ key : number ] : boolean } ) ,
2018-02-08 18:26:45 +00:00
UNSAFE _componentWillMount : function ( ) {
2015-01-30 01:10:49 +00:00
this . _pressData = { } ;
} ,
render : function ( ) {
return (
2017-05-06 03:50:47 +00:00
< RNTesterPage
2016-01-26 18:29:47 +00:00
title = { this . props . navigator ? null : '<ListView>' }
2015-01-30 01:10:49 +00:00
noSpacer = { true }
noScroll = { true } >
< ListView
dataSource = { this . state . dataSource }
renderRow = { this . _renderRow }
2016-07-18 17:19:56 +00:00
renderSeparator = { this . _renderSeparator }
2015-01-30 01:10:49 +00:00
/ >
2017-05-06 03:50:47 +00:00
< / R N T e s t e r P a g e >
2015-01-30 01:10:49 +00:00
) ;
} ,
2018-05-11 20:32:37 +00:00
_renderRow : function (
rowData : string ,
sectionID : number ,
rowID : number ,
highlightRow : ( sectionID : number , rowID : number ) => void ,
) {
2015-01-30 01:10:49 +00:00
var rowHash = Math . abs ( hashCode ( rowData ) ) ;
2015-12-22 17:02:12 +00:00
var imgSource = THUMB _URLS [ rowHash % THUMB _URLS . length ] ;
2015-01-30 01:10:49 +00:00
return (
2018-05-11 20:32:37 +00:00
< TouchableHighlight
onPress = { ( ) => {
2016-05-05 10:42:02 +00:00
this . _pressRow ( rowID ) ;
highlightRow ( sectionID , rowID ) ;
} } >
2015-01-30 01:10:49 +00:00
< View >
< View style = { styles . row } >
< Image style = { styles . thumb } source = { imgSource } / >
< Text style = { styles . text } >
2018-06-06 12:20:40 +00:00
{ rowData + ' - ' + LOREM _IPSUM . substr ( 0 , ( rowHash % 301 ) + 10 ) }
2015-01-30 01:10:49 +00: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 >
) ;
} ,
2015-03-23 17:06:16 +00:00
_genRows : function ( pressData : { [ key : number ] : boolean } ) : Array < string > {
2015-01-30 01:10:49 +00:00
var dataBlob = [ ] ;
for ( var ii = 0 ; ii < 100 ; ii ++ ) {
var pressedText = pressData [ ii ] ? ' (pressed)' : '' ;
dataBlob . push ( 'Row ' + ii + pressedText ) ;
}
return dataBlob ;
} ,
2015-03-23 17:06:16 +00:00
_pressRow : function ( rowID : number ) {
2015-01-30 01:10:49 +00:00
this . _pressData [ rowID ] = ! this . _pressData [ rowID ] ;
2018-05-11 20:32:37 +00:00
this . setState ( {
dataSource : this . state . dataSource . cloneWithRows (
this . _genRows ( this . _pressData ) ,
) ,
} ) ;
2015-01-30 01:10:49 +00:00
} ,
2016-05-05 10:42:02 +00:00
2018-05-11 20:32:37 +00:00
_renderSeparator : function (
sectionID : number ,
rowID : number ,
adjacentRowHighlighted : boolean ,
) {
2016-05-05 10:42:02 +00:00
return (
< View
key = { ` ${ sectionID } - ${ rowID } ` }
style = { {
height : adjacentRowHighlighted ? 4 : 1 ,
backgroundColor : adjacentRowHighlighted ? '#3B5998' : '#CCCCCC' ,
} }
/ >
) ;
2018-05-11 20:32:37 +00:00
} ,
2015-01-30 01:10:49 +00:00
} ) ;
2015-11-17 16:48:22 +00:00
var THUMB _URLS = [
2015-12-22 17:02:12 +00: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 20:32:37 +00:00
] ;
var LOREM _IPSUM =
'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-30 01:10:49 +00:00
/* eslint no-bitwise: 0 */
var hashCode = function ( str ) {
var hash = 15 ;
for ( var ii = str . length - 1 ; ii >= 0 ; ii -- ) {
2018-05-11 20:32:37 +00:00
hash = ( hash << 5 ) - hash + str . charCodeAt ( ii ) ;
2015-01-30 01:10:49 +00:00
}
return hash ;
} ;
var styles = StyleSheet . create ( {
row : {
flexDirection : 'row' ,
justifyContent : 'center' ,
padding : 10 ,
backgroundColor : '#F6F6F6' ,
} ,
thumb : {
width : 64 ,
height : 64 ,
} ,
text : {
flex : 1 ,
} ,
} ) ;
module . exports = ListViewSimpleExample ;