SelectUserView.js
6.79 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
205
import React, {Component} from 'react';
import {Image, Text, TouchableOpacity, View, StyleSheet, Alert} from 'react-native';
import {xnBorderWidth} from "../utils/utils";
/**
* 选择用户组件
*/
export default class SelectUserView extends Component {
constructor(props) {
super(props);
this.state = {
usersList: []
}
// 传入参数
this.attrData = this.props.attrData;
// json解析后的传入参数
this.extendData = (this.attrData.data && this.attrData.data.length > 0) ? JSON.parse(this.attrData.data) : {};
}
// 添加用户(通过自定义的'Employee'页面)
addUser = () => {
this.props.navigation.navigate("Employee", {
status: "add", callback: (data) => {
let usersList = this.state.usersList || [];
let object = {};
object.objectName = data.name;
object.objectId = data.userId;
object.objectAvatar = data.avatar||'';
object.type = "USER";
usersList.push(object);
this.attrData.value = JSON.stringify(usersList);
this.setState({usersList});
}
})
};
deleteUser = (v, i) => {
Alert.alert(
'删除',
'确定删除?',
[
{text: '取消', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
{
text: '确定', onPress: () => {
let userList = this.state.usersList;
userList.splice(i, 1);
this.attrData.value = JSON.stringify(usersList);
this.setState({usersList});
}
},
]
)
}
// 用户UI绘制
_renderUser = (v, i) => {
return (
<View style={styles.addItem} key={i}>
<View style={styles.addItemLayout}>
<View style={[styles.addItemImg, {backgroundColor: global.homeColor}]}>
{!v.avatar && <Text
style={styles.addItemImageText}>{v.objectName.length > 2 ? v.objectName.substring(v.objectName.length - 2) : v.objectName}</Text>}
{v.avatar &&
<Image style={styles.addItemImage} source={{uri: v.avatar}} resizeMode="contain"></Image>}
</View>
<View style={styles.addItemName}><Text style={styles.addItemNameText}>{v.objectName}</Text></View>
<TouchableOpacity style={styles.addItemDelete} onPress={(v) => {
this.deleteUser(v, i)
}}>
<Image style={styles.addItemDeleteIcon} source={require('../img/delete.png')}
resizeMode="contain"/>
</TouchableOpacity>
</View>
</View>
)
};
render() {
const { name, code, type, description, isRequired, value } = this.attrData;
// 用户选择组件属性
// userType: 用户类型 固定:USER=用户|EMPLOYEE=员工|SUPPLIER=供应商|ISV=开发者
// minValue: 最少人数 maxValue: 最多人数,默认不限制
const { userType, minValue, maxValue } = this.extendData;
// 限制添加用户
let enabledAddUser = true;
if (maxValue && value && maxValue <= value.length) {
enabledAddUser = false
}
return (
// 最外层的样式区域
<View style={{
// 高度
//minHeight: 130,
// 左内边距
paddingLeft: 10,
// 右内边距
paddingRight: 15,
// 背景色
backgroundColor: '#fff',
// 上内边距
paddingTop: 6,
}}>
{/*标题显示区域*/}
<View style={{flexDirection: 'row'}}>
{/*是否必须UI设定*/}
{isRequired && <Text style={{color: "#FF3030"}}>* </Text>}
{!isRequired && <Text style={{color: "#fff"}}>* </Text>}
{/*标题设定*/}
<Text style={{fontSize: 16, color: "#000",}}>{name}:</Text>
</View>
<View style={styles.addList}>
{/*显示选择的用户*/}
{value && value.length > 0 && JSON.parse(value).map((v, i) => this._renderUser(v, i))}
{/*添加用户区域*/}
{enabledAddUser &&
<View style={styles.addItem}>
{/*用于触发添加操作*/}
<TouchableOpacity style={styles.addItemLayout} onPress={() => {
this.addUser()
}}>
{/*"+"号*/}
<View style={[styles.addItemImg, styles.addItemImgOp]}><Image
style={{width: 12, height: 12}} source={require('../img/add-icon.png')}
resizeMode="contain"/></View>
<View style={styles.addItemName}><Text
style={[styles.addItemNameText, styles.addItemNameTextOp]}>添加</Text></View>
</TouchableOpacity>
</View>}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
addList: {
flexDirection: "row",
paddingTop: 10,
flexWrap: "wrap",
},
addItem: {
width: 50,
//height: 70,
marginLeft: 10,
marginBottom: 10,
justifyContent: "center",
flexDirection: "row",
},
addItemLayout: {
//height: 70,
width: 50,
},
addItemImg: {
height: 50,
width: 50,
justifyContent: "center",
alignItems: "center",
borderRadius: 3,
backgroundColor: "#39f",
},
addItemImageText: {
color: "#fff",
fontSize: 10,
},
addItemImage: {
height: 50,
width: 50,
},
addItemName: {
//height: 20,
width: 50,
justifyContent: "center",
flexDirection: "row",
},
addItemNameText: {
fontSize: 12,
color: "#333333",
textAlign: "center",
padding: 3,
},
addItemDelete: {
position: "absolute",
width: 14,
height: 14,
right: 2,
top: 3,
},
addItemDeleteIcon: {
width: 10,
height: 10
},
addItemImgOp: {
borderWidth: xnBorderWidth(),
borderColor: "#ddd",
backgroundColor: "transparent",
},
addItemNameTextOp: {
color: "#999",
},
})