TabDetailView.js
21.6 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import React, {Component} from 'react';
import {Image, Modal, ScrollView, StyleSheet, Text, TouchableOpacity, View,} from 'react-native';
import {isJSONString, xnBorderWidth} from "../utils/utils";
import SelectItemView from "./SelectItemView";
import TextSingleEditView from "./TextSingleEditView";
import TextMultiEditView from "./TextMultiEditView";
import PhoneEditView from "./PhoneEditView";
import SliderView from "./SliderView";
import PickerView from "./PickerView";
import FiveStarView from "./FiveStarView";
import NumberEditView from "./NumberEditView";
import CalculatorView from "./CalculatorView";
import LabelLinkView from "./LabelLinkView";
import ContainerView from "./ContainerView";
import CheckboxView from "./CheckboxView";
import BooleanCheckView from "./BooleanCheckView";
import CascadeView from "./CascadeView";
import LocationTextView from "./LocationTextView";
// 标题默认高度
const titleViewHeight = (26);
// 默认横向内边距
const HPading = (16);
// 默认纵向内边距
const VPading = (6);
// 表格宽度
const TabItemWidth = 90;
// 表格高度
const TabItemHeight = 40;
/**
* 表格/明细 组件 (type: 'TABLE')
*/
export default class TabDetailView extends Component {
// 构造方法
constructor(props) {
super(props);
this.state = {
tableHead: [],// 表头
tableData: [],// 表格内容数据
rowList: [],// 行数
editRowsData: [],// 编辑行数据
showModal: false,// 是否以模态展示
editLineNumber: 0,// 编辑行数
};
// 传入参数
this.attrData = this.props.attrData;
// json解析后的传入参数
this.extendData = (this.attrData.data && this.attrData.data.length > 0) ? JSON.parse(this.attrData.data) : {};
// 固定:list=列表|table=表格
this.tabType = this.extendData.type;
// 过滤掉对展示表格无用的数据
this.pureTabData = [];
}
// 生命周期-组件将要展示
componentWillMount() {
// 过滤取得表头,表格内容数据
let headTabData = this.extendData.tabDataList[0];
let _tableHead = this.state.tableHead;
headTabData.map(item => {
_tableHead.push(item);
let tabData = {
name: item.name, code: item.code, type: item.type, description: item.description||'',
isRequired: item.isRequired, isPreview: item.isPreview, data: item.data, value: ''
};
this.pureTabData.push(tabData);
});
this.setState({
tableHead: _tableHead,
rowList: this.extendData.tabDataList
});
}
render() {
/*
name:标题
code:代码编号
type:控件类型('TABLE')
description:描述
isRequired:是否必须
isPreview:是否预览
*/
let {name, code, type, description, isRequired, isPreview} = this.attrData;
// 按钮文本 // // 显示合计
let {buttonText, showCount} = this.extendData;
return (
// 最外层的样式区域
<View style={{
width: '100%',
backgroundColor: 'white',
paddingLeft: 10,
paddingRight: 15,
paddingVertical: VPading
}}>
{/*标题显示区域*/}
<View style={{
flexDirection: 'row',
height: titleViewHeight,
justifyContent: 'flex-start',
alignItems: 'center',
backgroundColor: '#fff'
}}>
{/*是否必须UI设定*/}
{isRequired && <Text style={{color: "#FF3030"}}>* </Text>}
{!isRequired && <Text style={{color: "#fff"}}>* </Text>}
{/*标题设定*/}
<Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)', marginBottom: 5}}>{name || ''}</Text>
</View>
{/** 列表 */}
{this.tabType === 'list' && this.state.rowList && this.state.rowList.length > 0 &&
this.state.rowList.map((item) => item.map((data, index) => this.renderComponentsItem(data, index)))}
{/** 表格 */}
{this.tabType === 'table' &&
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
>
<View>
{this.renderTableHead()}
{this.renderTableRow()}
</View>
</ScrollView>
}
{/** TODO 显示合计 */}
{showCount && <View style={{
width: '100%', height: 44, alignItems: 'center', borderBottomWidth: xnBorderWidth(),
borderBottomColor: '#ddd', flexDirection: 'row'
}}
>
<Text style={{marginLeft: 10, fontSize: 16, color: 'black'}}>合计</Text>
</View>}
<TouchableOpacity
style={{width: '100%', height: 44, alignItems: 'center', justifyContent: 'center'}}
activeOpacity={0.8}
onPress={() => {
let tempList = this.state.rowList;
let rowList = [];
this.pureTabData.map(item => {
let itemData = {
name: item.name, code: item.code, type: item.type, description: item.description,
isRequired: item.isRequired, isPreview: item.isPreview, data: item.data, value: ''
};
rowList.push(itemData);
});
tempList.push(rowList);
this.setState({
rowList: tempList
})
}}
>
<Text style={{fontSize: 16, color: global.homeColor}}>{buttonText?`+ ${buttonText}`:'+ 添加'}</Text>
</TouchableOpacity>
{/** 创建编辑表格弹窗 */}
{this.createTableEditModal()}
</View>
);
}
// 表格头部UI绘制
renderTableHead() {
return (
<View style={{
height: TabItemHeight,
backgroundColor: 'rgb(220,220,220)',
flexDirection: 'row',
alignItems: 'center'
}}>
<Text style={{width: TabItemHeight, height: TabItemHeight}}>{''}</Text>
{this.state.tableHead && this.state.tableHead.map(((data, index) => this.renderTableHeadItem(data, index)))}
</View>
)
}
/**
* 表格头部
* @param data
* @param index
*/
renderTableHeadItem(data, index) {
return (
<View key={index}
style={{width: TabItemWidth, height: TabItemHeight, flexDirection: 'row', alignItems: 'center'}}>
<View style={{width: 1, height: 20, backgroundColor: 'rgba(0,0,0,0.4)'}}/>
<Text style={{marginLeft: 5, marginRight: 10, fontSize: 14, fontWeight: 'bold', color: 'black'}}
numberOfLines={1} ellipsizeMode={'tail'}>{data.name}</Text>
</View>
)
}
/**
* 表格行
*/
renderTableRow() {
return (
<View style={{backgroundColor: 'white', flexDirection: 'column'}}>
{this.state.rowList.map((rowData, index) => {
return this.renderTableRowItem(rowData, index)
})}
</View>
);
}
// 表格行UI绘制
renderTableRowItem(rowData, index) {
return (
<TouchableOpacity
key={index}
style={{
height: TabItemHeight,
flex: 1,
flexDirection: 'row',
borderBottomWidth: xnBorderWidth(),
borderBottomColor: 'rgb(215,215,215)'
}}
activeOpacity={0.8}
onPress={() => {
// 编辑行
this.setState({
editRowsData: rowData,
showModal: true,
editLineNumber: index
})
}}
>
{/** 行号 */}
<View style={{
width: TabItemHeight,
height: TabItemHeight,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: "rgb(245,245,245)"
}}>
<Text style={{fontSize: 14, color: 'black'}}>{(index + 1) + ""}</Text>
</View>
{/*表格行内容*/}
{rowData && rowData.length > 0 && rowData.map((item, index) => this.renderRowItem(item, index))}
</TouchableOpacity>
);
}
// 表格行UI绘制
renderRowItem(item, index) {
return (
<View key={index}
style={{width: TabItemWidth, height: TabItemHeight, flexDirection: 'row', alignItems: 'center'}}>
<View style={{width: 1, height: 20, backgroundColor: 'rgb(215,215,215)'}}/>
<Text style={{marginLeft: 5, marginRight: 10, fontSize: 14, fontWeight: 'bold', color: 'black'}}
numberOfLines={1}
ellipsizeMode={'tail'}>{this.parseRowItemValue(item)}</Text>
</View>
)
}
/**
* 解析行每项value显示
* @param item
* @returns {string|*}
*/
parseRowItemValue(item) {
if (isJSONString(item.value)) {
let data = JSON.parse(item.value);
switch (item.type) {
case "CITY":
return data.province + data.city + data.area;
case "POSITION":
return data.organizationName + "-" + data.jobName + "-" + data.employeeName;
case "LOCATION":
case "OPERATING_UNIT":
case "ORGANIZATION":
return data.name;
case "CASCADE":// 级联选择
let showText = "";
let selected = data.selected;
showText = selected.title;
let _children = selected.children;
while (!!_children) {
showText = showText + "," + _children.title;
_children = _children.children;
}
return showText;
case "CHECKBOX":// 多选框
// 多选框
let count = 0;
let checkText = '';
data.map(name => {
if (count === 0) {
checkText = name;
} else {
checkText = checkText + "," + name
}
});
return checkText;
case "CALCULATOR":// 计算公式
return data.result;
case "PERIOD":// 日期区间
return data.beginDate + "至" + data.endDate + " 共" + data.duration + "天";
}
}
if (item.type === "BOOLEAN") {
if (item.value || item.value === 'true') {
return '是';
} else {
return '否';
}
}
return item.value || "";
}
/**
* 显示编辑弹窗
*/
showModal() {
this.setState({
showModal: false,
});
}
/**
* 模态框取消操作
*/
closeModal() {
this.setState({
showModal: false,
});
}
// 模态窗口模式
createTableEditModal(name) {
return (
<Modal animationType={"fade"} transparent={true} visible={this.state.showModal}
onRequestClose={() => this.closeModal()}>
{/** 目录弹窗 */}
<View style={{flex: 1}}>
<TouchableOpacity style={styles.modalBg} activeOpacity={1} onPress={() => this.closeModal()}/>
{/** 实体内容 */}
<View style={styles.contentBg}>
{/** 标题栏 */}
<View style={styles.titleBar}>
<TouchableOpacity
style={{paddingLeft: 15, paddingRight: 5}}
activeOpacity={0.8}
onPress={() => this.closeModal()}
>
<Image style={{width: 14, height: 14}}
source={require('../img/loadBack.png')}/>
</TouchableOpacity>
<Text style={styles.modelTitle}>{name}</Text>
</View>
<View style={{
width: '100%',
height: 36,
justifyContent: 'center',
backgroundColor: 'rgba(245,245,245,1)'
}}>
<Text style={{
fontSize: 14,
color: 'black',
marginLeft: 16
}}>当前编辑的是第{(this.state.editLineNumber + 1) + '行'}</Text>
</View>
{/** 内容 */}
<ScrollView style={{flex: 1}} horizontal={false} showsVerticalScrollIndicator={false}>
{this.state.editRowsData && this.state.editRowsData.map((value, index) => this.renderComponentsItem(value, index))}
</ScrollView>
</View>
</View>
</Modal>
)
}
/**
* 组件
* @param item
* @param index
*/
renderComponentsItem(item, index) {
switch (item.type) {
case 'OPERATING_UNIT':// 经营单元
return (
<SelectItemView
key={index}
attrData={item}
navigation={this.props.navigation}
/>
)
break;
case 'TEXT': // 单行输入框
return (
<TextSingleEditView
key={index}
attrData={item}
/>
)
break;
case 'LONG_TEXT':// 多行输入框
return (
<TextMultiEditView
key={index}
attrData={item}
/>
)
break;
case 'PHONE':// 电话输入
return (
<PhoneEditView
key={index}
attrData={item}
/>
)
break;
case 'SLIDE':// 滑块
return (
<SliderView
key={index}
attrData={item}
/>
)
break;
case 'DATE':// 日期
return (
<PickerView
key={index}
attrData={item}
/>
)
break;
case 'PERIOD':// 日期区间
return (
<PickerView
key={index}
attrData={item}
/>
)
break;
case 'CITY':// 省市区
return (
<PickerView
key={index}
attrData={item}
/>
)
break
case 'SCORE':// 评分
return (
<FiveStarView
key={index}
attrData={item}
/>
)
break;
case 'MONEY':// 金额
return (
<NumberEditView
key={index}
attrData={item}
onChangeText={this.onChangeCalcs}
/>
)
break;
case 'NUMBER':// 数字输入框
return (
<NumberEditView
key={index}
attrData={item}
onChangeText={this.onChangeCalcs}
/>
)
break;
case 'IDCARD':// 身份证
return (
<NumberEditView
key={index}
attrData={item}
inputType={'default'}
/>
)
break;
case 'CALCULATOR':// 计算公式
return (
<CalculatorView
key={index}
ref={'calculate'}
attrData={item}
/>
)
break;
case 'TABLE':// 表格
return (
<TabDetailView
key={index}
attrData={item}
/>
)
break;
case 'LABEL':// 提示标签
return (
<LabelLinkView
key={index}
attrData={item}
navigation={this.props.navigation}
/>
)
break;
case 'CONTAINER':// 布局容器
return (
<ContainerView
key={index}
attrData={item}
/>
)
break;
case 'SELECT':// 单选框
return (
<CheckboxView
key={index}
attrData={item}
onChangeText={(text) => {
item.value = text;
}}
/>
)
break;
case "CHECKBOX":// 多选框
return (
<CheckboxView
key={index}
attrData={item}
/>
)
break;
case 'BOOLEAN':// 是否
return (
<BooleanCheckView
key={index}
attrData={item}
/>
)
break;
case 'ORGANIZATION':// 部门
return (
<SelectItemView
key={index}
attrData={item}
navigation={this.props.navigation}
/>
)
break;
case 'CASCADE':// 级联
return (
<CascadeView
key={index}
attrData={item}
/>
)
break;
case 'POSITION':// 岗位
return (
<SelectItemView
key={index}
attrData={item}
navigation={this.props.navigation}
/>
)
break;
case 'LOCATION':// 获取定位
return (
<LocationTextView
key={index}
attrData={item}
onStartLocation={() => {
this.setState({
startLoading: true
})
}}
onCompleteLocation={() => {
this.setState({
startLoading: false
})
}}
/>
)
break
}
}
onChangeCalcs = (amount, type) => {
if (this.refs.calculate) {
this.refs.calculate.calculateValue(amount, type)
}
}
}
const styles = StyleSheet.create({
head: {
height: 40,
backgroundColor: '#f1f8ff'
},
text: {
margin: 6,
fontSize: 14,
color: 'black'
},
modalBg: {
backgroundColor: 'rgba(0,0,0,.5)',
width: '100%',
height: '100%',
display: 'flex',
position: 'absolute'
},
contentBg: {
width: '100%',
flex: 1,
marginTop: 122,
backgroundColor: "rgba(255, 255, 255, 1)",
borderTopLeftRadius: 8,
borderTopRightRadius: 8
},
titleBar: {
flexDirection: "row",
justifyContent: "flex-start",
alignItems: "center",
marginTop: 15,
marginBottom: 10,
marginRight: 15
},
modelTitle: {
flex: 1,
color: "rgba(0, 0, 0, 1)",
fontSize: 16,
textAlign: "center",
justifyContent: "center"
},
});