QuestionAttItem.js
5.27 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {Alert, FlatList, Image, StyleSheet, TouchableOpacity, View} from "react-native";
import React, {PureComponent} from "react";
import {width} from "../../utils/getSize";
import _ from "lodash";
const deleteImg = require("../../img/issue_pic.png");
const addImgUrl =
"http://m.qpic.cn/psb?/ccbca58c-ff4d-4e83-8519-00cda331bf6c/OLgpXFgaWqAKV1L.MC1dx1gXfcQ9rgYEl0dKjaJdi4M!/b/dFIBAAAAAAAA&bo=QwFFAQAAAAARBzY!&rf=viewer_4";
export default class QuestionAttItem extends PureComponent {
// 初始化
constructor(...props) {
super(...props);
this.state = {
dataSource: [],
attDataSource: [],
isAttListViewShow: this.props.isAttListViewShow,
isAttAddShow: this.props.isAttAddShow
};
}
// 初始化附件显示
onInitAttShow(isShow) {
this.setState({
isAttShow: isShow
});
}
// 初始化listview dataSource
onInitAttListData(dataSources) {
if (_.isEmpty(dataSources)) {
return;
}
// 添加第九张图片时候 去除第一项
if (dataSources.length == 10) {
dataSources.splice(0, 1);
}
const dataTemp = JSON.parse(JSON.stringify(dataSources));
this.setState({
dataSource: dataTemp,
attDataSource: dataTemp
});
}
// 初始化附件添加显示
onInitAttAddShow(isShow) {
this.setState({
isAttAddShow: isShow
});
}
onInitAttListViewShow(isShow) {
this.setState({
isAttListViewShow: isShow
});
}
showAttListView = () => {
if (this.state.isAttListViewShow) {
return (
<FlatList
style={styles.attListViewStyle}
refreshing={false}
keyExtractor={this.keyExtractor}
data={this.state.attDataSource}
renderItem={this.showAttView.bind(this)}
showsVerticalScrollIndicator={false}
horizontal={false}
numColumns={3}
/>
);
}
return null;
};
// 显示附件缩略图
keyExtractor = (item, index) => index;
showAttView({ item, index }) {
return (
<TouchableOpacity
style={
index == 1 || index == 4 || index == 7
? {
width: (width - 36) * 0.33,
height: (width - 36) * 0.33,
marginTop: 3
}
: {
width: (width - 36) * 0.33,
height: (width - 36) * 0.33,
marginTop: 3,
marginLeft: 3,
marginRight: 3
}
}
onPress={() => {
const { showImage } = this.props;
showImage(index);
}}
>
{ item.path != addImgUrl ?<Image source={{ uri: item.path + '?x-oss-process=image/resize,w_500' }} style={styles.attachStyle} resizeMode="cover"/>
: <Image source={require('../../img/add.jpg')} style={styles.attachStyle} resizeMode="cover"/>}
<View
style={{
backgroundColor: "#000",
opacity:item.path != addImgUrl ? 0.1:0,
position: "absolute",
top: 0,
left: 0,
width: (width - 36) * 0.33,
height: (width - 36) * 0.33
}}
/>
{item.path != addImgUrl ? this.renderDeleteView(index) : null}
</TouchableOpacity>
);
}
renderDeleteView(rowID) {
return (
<TouchableOpacity
style={styles.deleteStyle}
onPress={() => {
this.onDeleteImg(rowID);
}}
>
<Image source={deleteImg} style={styles.deleteImgStyle} resizeMode="cover"/>
</TouchableOpacity>
);
}
// 删除图片
onDeleteImg(rowID) {
let _this = this;
Alert.alert(
'提醒',
'确认删除?',
[
{text: '确定', onPress: function(){
let imgArr = _this.state.dataSource;
imgArr.splice(rowID, 1);
if (imgArr.length == 8) {
if (imgArr[0].path != addImgUrl) {
let url = {
path: addImgUrl
};
imgArr.splice(0, 0, url);
}
}
const { updateImgArr } = _this.props;
updateImgArr(imgArr);
const dataTemp = JSON.parse(JSON.stringify(imgArr));
_this.setState({
dataSource: dataTemp,
attDataSource: dataTemp
});
}},
{text: '取消', onPress: function(){
}}
]
)
}
exportDataSource() {
return this.state.dataSource;
}
render() {
return <View style={styles.attachViewStyle}>{this.showAttListView()}</View>;
}
}
const styles = StyleSheet.create({
attachViewStyle: {
flex: 1,
flexDirection: "row",
backgroundColor: "#FFFFFF",
marginLeft: 15,
marginRight: 15
},
attListViewStyle: {
alignSelf: "center",
flexDirection: "row",
flexWrap: "wrap"
},
attachStyle: {
width: (width - 36) * 0.33,
height: (width - 36) * 0.33,
alignItems: "center"
},
deleteStyle: {
position: "absolute",
top: 0,
right: 0,
padding: 5
},
deleteImgStyle: {
width: 10,
height: 10
}
});