Skip to content

Commit 6a4f311

Browse files
committed
feat(UIExplorer): Adding ListView example for Windows UIExplorer (microsoft#426)
We weren't able to use the ListView example due to the missing RecyclerViewBackedScrollViewManager, which is still missing. Working around that for now.
1 parent 1b92c6f commit 6a4f311

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/**
2+
* The examples provided by Facebook are for non-commercial testing and
3+
* evaluation purposes only.
4+
*
5+
* Facebook reserves all rights not expressly granted.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
8+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
10+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
11+
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
12+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13+
*
14+
* @flow
15+
*/
16+
'use strict';
17+
18+
var React = require('react');
19+
var ReactNative = require('react-native');
20+
var {
21+
Image,
22+
ListView,
23+
TouchableHighlight,
24+
StyleSheet,
25+
Text,
26+
View,
27+
} = ReactNative;
28+
29+
var UIExplorerPage = require('./UIExplorerPage');
30+
31+
var ListViewSimpleExample = React.createClass({
32+
statics: {
33+
title: '<ListView>',
34+
description: 'Performant, scrollable list of data.'
35+
},
36+
37+
getInitialState: function() {
38+
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
39+
return {
40+
dataSource: ds.cloneWithRows(this._genRows({})),
41+
};
42+
},
43+
44+
_pressData: ({}: {[key: number]: boolean}),
45+
46+
componentWillMount: function() {
47+
this._pressData = {};
48+
},
49+
50+
render: function() {
51+
return (
52+
<UIExplorerPage
53+
title={this.props.navigator ? null : '<ListView>'}
54+
noSpacer={true}
55+
noScroll={true}>
56+
<ListView
57+
dataSource={this.state.dataSource}
58+
renderRow={this._renderRow}
59+
renderSeparator={this._renderSeperator}
60+
/>
61+
</UIExplorerPage>
62+
);
63+
},
64+
65+
_renderRow: function(rowData: string, sectionID: number, rowID: number, highlightRow: (sectionID: number, rowID: number) => void) {
66+
var rowHash = Math.abs(hashCode(rowData));
67+
var imgSource = THUMB_URLS[rowHash % THUMB_URLS.length];
68+
return (
69+
<TouchableHighlight onPress={() => {
70+
this._pressRow(rowID);
71+
highlightRow(sectionID, rowID);
72+
}}>
73+
<View>
74+
<View style={styles.row}>
75+
<Image style={styles.thumb} source={imgSource} />
76+
<Text style={styles.text}>
77+
{rowData + ' - ' + LOREM_IPSUM.substr(0, rowHash % 301 + 10)}
78+
</Text>
79+
</View>
80+
</View>
81+
</TouchableHighlight>
82+
);
83+
},
84+
85+
_genRows: function(pressData: {[key: number]: boolean}): Array<string> {
86+
var dataBlob = [];
87+
for (var ii = 0; ii < 100; ii++) {
88+
var pressedText = pressData[ii] ? ' (pressed)' : '';
89+
dataBlob.push('Row ' + ii + pressedText);
90+
}
91+
return dataBlob;
92+
},
93+
94+
_pressRow: function(rowID: number) {
95+
this._pressData[rowID] = !this._pressData[rowID];
96+
this.setState({dataSource: this.state.dataSource.cloneWithRows(
97+
this._genRows(this._pressData)
98+
)});
99+
},
100+
101+
_renderSeperator: function(sectionID: number, rowID: number, adjacentRowHighlighted: bool) {
102+
return (
103+
<View
104+
key={`${sectionID}-${rowID}`}
105+
style={{
106+
height: adjacentRowHighlighted ? 4 : 1,
107+
backgroundColor: adjacentRowHighlighted ? '#3B5998' : '#CCCCCC',
108+
}}
109+
/>
110+
);
111+
}
112+
});
113+
114+
var THUMB_URLS = [
115+
require('./Thumbnails/like.png'),
116+
require('./Thumbnails/dislike.png'),
117+
require('./Thumbnails/call.png'),
118+
require('./Thumbnails/fist.png'),
119+
require('./Thumbnails/bandaged.png'),
120+
require('./Thumbnails/flowers.png'),
121+
require('./Thumbnails/heart.png'),
122+
require('./Thumbnails/liking.png'),
123+
require('./Thumbnails/party.png'),
124+
require('./Thumbnails/poke.png'),
125+
require('./Thumbnails/superlike.png'),
126+
require('./Thumbnails/victory.png'),
127+
];
128+
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.';
129+
130+
/* eslint no-bitwise: 0 */
131+
var hashCode = function(str) {
132+
var hash = 15;
133+
for (var ii = str.length - 1; ii >= 0; ii--) {
134+
hash = ((hash << 5) - hash) + str.charCodeAt(ii);
135+
}
136+
return hash;
137+
};
138+
139+
var styles = StyleSheet.create({
140+
row: {
141+
flexDirection: 'row',
142+
justifyContent: 'center',
143+
padding: 10,
144+
backgroundColor: '#F6F6F6',
145+
},
146+
thumb: {
147+
width: 64,
148+
height: 64,
149+
},
150+
text: {
151+
flex: 1,
152+
},
153+
});
154+
155+
module.exports = ListViewSimpleExample;

Examples/UIExplorer/UIExplorerList.windows.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ var ComponentExamples: Array<UIExplorerExample> = [
2929
key: 'ImageExample',
3030
module: require('./ImageExample'),
3131
},
32+
{
33+
key: 'ListViewExample',
34+
module: require('./ListViewWindowsExample'),
35+
},
3236
{
3337
key: 'PickerWindowsExample',
3438
module: require('./PickerWindowsExample'),

0 commit comments

Comments
 (0)