Browse.js
4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';
import React, {Component} from 'react';
import ReactNative, {
View,
Text,
Image,
ListView,
StyleSheet,
Dimensions,
TouchableOpacity,
} from 'react-native';
import LoadingIndicator from '../../../common/components/LoadingIndicator';
import ProductCell from './ProductCell';
import CategorySelector from './CategorySelector';
import Swipeable from 'react-native-swipeable';
import NoDataView from '../product/NoDataView'
export default class Browse extends Component {
constructor(props) {
super(props);
this.renderRow = this.renderRow.bind(this);
this.renderHeader = this.renderHeader.bind(this);
this.handleScroll = this.handleScroll.bind(this);
this.onOpen = this.onOpen.bind(this);
this.onClose = this.onClose.bind(this);
this.dataSource = new ListView.DataSource({
rowHasChanged: (r1, r2) => !Immutable.is(r1, r2),
});
this.state = {
currentlyOpenSwipeable: null,
};
this.listView = null;
this.swipeable = {};
}
componentWillReceiveProps(nextProps) {
if (this.props.data.isDeleting
&& this.props.data.isDeleting != nextProps.data.isDeleting) {
this.state.currentlyOpenSwipeable && this.state.currentlyOpenSwipeable.recenter();
this.setState({currentlyOpenSwipeable: null});
}
}
handleScroll() {
const {currentlyOpenSwipeable} = this.state;
if (currentlyOpenSwipeable) {
currentlyOpenSwipeable.recenter();
}
}
onOpen(event, gestureState, swipeable) {
const {currentlyOpenSwipeable} = this.state;
if (currentlyOpenSwipeable && currentlyOpenSwipeable !== swipeable) {
currentlyOpenSwipeable.recenter();
}
this.setState({currentlyOpenSwipeable: swipeable});
}
onClose() {
this.setState({currentlyOpenSwipeable: null});
}
rightButtons(rowData, rowID) {
const rightButtons = [
<TouchableOpacity
activeOpacity={1}
style={{
flex: 1,
justifyContent: 'center',
backgroundColor: 'red',
}}
onPress={() => {
this.props.onPressDelete && this.props.onPressDelete(rowData, rowID);
}}
>
<Text style={{color: 'white', fontSize: 17, paddingLeft: 20}}>删除</Text>
</TouchableOpacity>,
];
return rightButtons;
}
renderRow(rowData, sectionID, rowID) {
return (
<Swipeable
rightButtons={this.rightButtons(rowData, rowID)}
rightButtonWidth={70}
onRightButtonsOpenRelease={this.onOpen}
onRightButtonsCloseRelease={this.onClose}
>
<ProductCell
key={'row' + rowID}
data={rowData}
onPressProduct={this.props.onPressProduct}
onPressFindSimilar={this.props.onPressFindSimilar}
/>
</Swipeable>
);
}
renderHeader() {
let {isFetching, selectedProductList, categoryList, selectedCategoryIndex} = this.props.data;
return (
<CategorySelector
data={categoryList}
selectedCategoryIndex={selectedCategoryIndex}
onPressCategory={(rowData, rowID) => {
this.listView && this.listView.scrollTo({x: 0, y: 0, animated: true});
this.props.onPressCategory && this.props.onPressCategory(rowData, rowID);
}}
/>
);
}
render() {
let {isFetching, productList, selectedProductList, categoryList, selectedCategoryIndex, isDeleting, showEmpty} = this.props.data;
let isLoading = (selectedProductList.size == 0 && isFetching) || isDeleting;
let showList = !isFetching && productList.size > 0;
return (
<View style={styles.container}>
{showList ? <ListView
ref={(c) => {
this.listView = c;
}}
enableEmptySections={true}
dataSource={this.dataSource.cloneWithRows(selectedProductList.toArray())}
renderRow={this.renderRow}
renderHeader={this.renderHeader}
onScroll={this.handleScroll}
/> : null}
{showEmpty ? <NoDataView type={'browse'} onPressGuangGuang={this.props.onPressGuangGuang}/> : null}
<LoadingIndicator
isVisible={isLoading}
/>
</View>
);
}
}
let {width, height} = Dimensions.get('window');
let styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f0f0f0',
},
});