2016-07-28 00:51:58 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-08-23 17:27:26 +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-08-23 17:27:26 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2016-07-28 00:51:58 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2016-07-28 00:51:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
const React = require('react');
|
|
|
|
const {
|
2016-07-28 00:51:58 +00:00
|
|
|
Image,
|
|
|
|
SwipeableListView,
|
|
|
|
TouchableHighlight,
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
View,
|
|
|
|
Alert,
|
2018-09-27 21:27:08 +00:00
|
|
|
} = require('react-native');
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
const RNTesterPage = require('./RNTesterPage');
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
import type {RNTesterProps} from 'RNTesterTypes';
|
|
|
|
import type {SwipeableListViewDataSource} from 'SwipeableListViewDataSource';
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
type State = {|
|
|
|
|
dataSource: SwipeableListViewDataSource,
|
|
|
|
|};
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-12-31 16:26:00 +00:00
|
|
|
class SwipeableListViewExample extends React.Component<RNTesterProps, State> {
|
2018-09-27 21:27:08 +00:00
|
|
|
state = {
|
|
|
|
dataSource: SwipeableListView.getNewDataSource().cloneWithRowsAndSections(
|
|
|
|
...this._genDataSource({}),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
_pressData: {[key: number]: boolean} = {};
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
render(): React.Node {
|
2016-07-28 00:51:58 +00:00
|
|
|
return (
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterPage
|
2016-07-28 00:51:58 +00:00
|
|
|
title={this.props.navigator ? null : '<SwipeableListView>'}
|
|
|
|
noSpacer={true}
|
|
|
|
noScroll={true}>
|
|
|
|
<SwipeableListView
|
|
|
|
dataSource={this.state.dataSource}
|
|
|
|
maxSwipeDistance={100}
|
2018-05-11 20:32:37 +00:00
|
|
|
renderQuickActions={(
|
|
|
|
rowData: Object,
|
|
|
|
sectionID: string,
|
|
|
|
rowID: string,
|
|
|
|
) => {
|
|
|
|
return (
|
|
|
|
<View style={styles.actionsContainer}>
|
|
|
|
<TouchableHighlight
|
|
|
|
onPress={() => {
|
|
|
|
Alert.alert(
|
|
|
|
'Tips',
|
|
|
|
'You could do something with this row: ' + rowData.text,
|
|
|
|
);
|
|
|
|
}}>
|
|
|
|
<Text>Remove</Text>
|
|
|
|
</TouchableHighlight>
|
|
|
|
</View>
|
|
|
|
);
|
2016-07-28 00:51:58 +00:00
|
|
|
}}
|
|
|
|
renderRow={this._renderRow}
|
|
|
|
renderSeparator={this._renderSeperator}
|
|
|
|
/>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterPage>
|
2016-07-28 00:51:58 +00:00
|
|
|
);
|
2018-09-27 21:27:08 +00:00
|
|
|
}
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
_renderRow(rowData: Object, sectionID: string, rowID: string) {
|
|
|
|
const rowHash = Math.abs(hashCode(rowData.id));
|
|
|
|
const imgSource = THUMB_URLS[rowHash % THUMB_URLS.length];
|
2016-07-28 00:51:58 +00:00
|
|
|
return (
|
|
|
|
<TouchableHighlight onPress={() => {}}>
|
|
|
|
<View>
|
|
|
|
<View style={styles.row}>
|
|
|
|
<Image style={styles.thumb} source={imgSource} />
|
|
|
|
<Text style={styles.text}>
|
2018-06-06 12:20:40 +00:00
|
|
|
{rowData.id + ' - ' + LOREM_IPSUM.substr(0, (rowHash % 301) + 10)}
|
2016-07-28 00:51:58 +00:00
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
);
|
2018-09-27 21:27:08 +00:00
|
|
|
}
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
_genDataSource(pressData: {[key: number]: boolean}) {
|
|
|
|
const dataBlob = {};
|
|
|
|
const sectionIDs = ['Section 0'];
|
|
|
|
const rowIDs = [[]];
|
2016-07-28 00:51:58 +00:00
|
|
|
/**
|
|
|
|
* dataBlob example below:
|
2018-09-27 21:27:08 +00:00
|
|
|
* {
|
|
|
|
* 'Section 0': {
|
|
|
|
* 'Row 0': {
|
|
|
|
* id: '0',
|
|
|
|
* text: 'row 0 text'
|
|
|
|
* },
|
|
|
|
* 'Row 1': {
|
|
|
|
* id: '1',
|
|
|
|
* text: 'row 1 text'
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Only one section in this example
|
2016-07-28 00:51:58 +00:00
|
|
|
dataBlob['Section 0'] = {};
|
2018-09-27 21:27:08 +00:00
|
|
|
for (let ii = 0; ii < 100; ii++) {
|
|
|
|
const pressedText = pressData[ii] ? ' (pressed)' : '';
|
2018-05-11 20:32:37 +00:00
|
|
|
dataBlob[sectionIDs[0]]['Row ' + ii] = {
|
|
|
|
id: 'Row ' + ii,
|
|
|
|
text: 'Row ' + ii + pressedText,
|
|
|
|
};
|
2016-07-28 00:51:58 +00:00
|
|
|
rowIDs[0].push('Row ' + ii);
|
|
|
|
}
|
|
|
|
return [dataBlob, sectionIDs, rowIDs];
|
2018-09-27 21:27:08 +00:00
|
|
|
}
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
_renderSeperator(
|
|
|
|
sectionID: string,
|
|
|
|
rowID: string,
|
2018-05-11 20:32:37 +00:00
|
|
|
adjacentRowHighlighted: boolean,
|
|
|
|
) {
|
2016-07-28 00:51:58 +00:00
|
|
|
return (
|
|
|
|
<View
|
|
|
|
key={`${sectionID}-${rowID}`}
|
|
|
|
style={{
|
|
|
|
height: adjacentRowHighlighted ? 4 : 1,
|
|
|
|
backgroundColor: adjacentRowHighlighted ? '#3B5998' : '#CCCCCC',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2018-09-27 21:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
const THUMB_URLS = [
|
2016-07-28 00:51:58 +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
|
|
|
];
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
const 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.',
|
|
|
|
].join('');
|
|
|
|
|
|
|
|
/* eslint-disable no-bitwise */
|
|
|
|
const hashCode = str => {
|
|
|
|
let hash = 15;
|
|
|
|
for (let ii = str.length - 1; ii >= 0; ii--) {
|
2018-05-11 20:32:37 +00:00
|
|
|
hash = (hash << 5) - hash + str.charCodeAt(ii);
|
2016-07-28 00:51:58 +00:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
};
|
2018-09-27 21:27:08 +00:00
|
|
|
/* eslint-enable no-bitwise */
|
2016-07-28 00:51:58 +00:00
|
|
|
|
2018-09-27 21:27:08 +00:00
|
|
|
const styles = StyleSheet.create({
|
2016-07-28 00:51:58 +00:00
|
|
|
row: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'center',
|
|
|
|
padding: 10,
|
|
|
|
backgroundColor: '#F6F6F6',
|
|
|
|
},
|
|
|
|
thumb: {
|
|
|
|
width: 64,
|
|
|
|
height: 64,
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
actionsContainer: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'flex-end',
|
|
|
|
alignItems: 'center',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-12-31 16:26:00 +00:00
|
|
|
exports.title = '<SwipeableListView>';
|
|
|
|
exports.description = 'Performant, scrollable, swipeable list of data.';
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'Simple swipable list',
|
|
|
|
render: function(): React.Element<typeof SwipeableListViewExample> {
|
|
|
|
return <SwipeableListViewExample />;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|