forked from Meituan-Dianping/beeshell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableview.js
More file actions
291 lines (287 loc) · 9.36 KB
/
tableview.js
File metadata and controls
291 lines (287 loc) · 9.36 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
/*
* @Author: mengqian02
* @Date: 2017-11-06 10:00:57
* @Last Modified by: mengqian02
* @Last Modified time: 2018-04-03 15:12:48
* 用于渲染表格数据
* tableTitle: {} 字典 通过表头确定唯一顺序
* tableDirection: row/column 方向 支持整体旋转方向
* tableData: [] 数组, 每一项和一致 tableTitle 一致 是具体数据
* 支持大小相对布局
*/
import ReactNative from 'react-native';
import React from 'react';
import { TABLE_DIRECTION } from '../PropTypes';
const { Platform, View, Text, StyleSheet, PixelRatio, Dimensions, Image, } = ReactNative;
const unit = {
onePx: 1 / PixelRatio.get(),
};
const HIGHLIGHT = {
'red': '#FFF5F5',
'green': '#ECFAF0',
'blue': '#EEF6FF',
'yellow': '#FFF7EE',
'gray': '#F8F8F8',
'white': '#FFFFFF',
};
class TableDataView extends React.Component {
constructor(props) {
super(props);
this.state = {
tableTitle: props.tableTitle,
tableData: props.tableData
};
this.styles = {};
this.tableHeaderKeys = []; // 利用表头的tableTitle 的顺序约束子元素
this.verifyTableData();
// TODO: 这里怎么做 optional 的问题
this.generateStyles(props.borderColor);
this.renderRow = this.renderRow.bind(this);
this.renderCell = this.renderCell.bind(this);
}
generateStyles(borderColor) {
this.styles = StyleSheet.create({
container: {
marginBottom: 10,
},
titleContainer: {
flexDirection: 'row',
justifyContent: 'flex-start',
},
titleView: {
paddingHorizontal: 5,
paddingVertical: 8,
},
titleText: {
fontSize: 13,
},
cell: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
minHeight: 35,
paddingVertical: 5,
paddingHorizontal: 10,
},
cellBorder: {
borderColor: borderColor,
borderTopWidth: unit.onePx,
borderLeftWidth: unit.onePx,
},
cellText: {
color: '#333',
},
headerText: {
color: '#666',
},
row: {
flexDirection: 'row',
justifyContent: 'flex-start',
minHeight: 35,
},
rowBorder: {
borderColor: borderColor,
borderRightWidth: unit.onePx
},
table: {
flexDirection: 'column',
justifyContent: 'flex-start',
},
tableBorder: {
borderColor: borderColor,
borderBottomWidth: unit.onePx
},
header: {
backgroundColor: '#f0f0f0'
},
});
}
componentWillMount() {
if (this.props.tableDirection === TABLE_DIRECTION.COLUMN) {
// 选择不同的渲染方式
this.transformTableDirection();
}
}
/**
* 检查表头/表格
* 数据
*/
verifyTableData() {
const { tableData, tableTitle, flexArr, tableDirection } = this.props;
// try {
if (Object.keys(tableTitle).length !== Object.keys(tableData[0]).length) {
throw new Error("表格数据和表头数据格式不符合");
}
if (Object.keys(tableTitle).length !== flexArr.length) {
if (tableDirection === TABLE_DIRECTION.ROW) {
throw new Error("flexArr 数组长度和表头数据数量不一样");
}
else {
if (tableData.length + 1 !== flexArr.length) {
throw new Error("表格反转之后 flexArr 数组长度和表头数据数量不一样");
}
}
}
}
/**
* 旋转方向
*/
transformTableDirection() {
const { tableTitle, tableData, tableDirection } = this.props;
// 只针对 tableDirection column 的情况
if (tableDirection === TABLE_DIRECTION.ROW)
return;
let _tableTitle = {}, _tableData = [];
// 表头的第一个元素
let titleKeys = Object.keys(tableTitle);
let topKey = titleKeys.shift();
let titleOtherKeys = titleKeys;
// 第一个元素
_tableTitle[`${topKey}_0`] = tableTitle[topKey];
// 循环
tableData.forEach((item, index) => {
_tableTitle[`${topKey}_${index + 1}`] = item[topKey];
});
titleOtherKeys.forEach(otherKey => {
let valuesObj = {};
// 第一个元素
valuesObj[`${otherKey}_0`] = tableTitle[otherKey];
// 循环
tableData.forEach((data, index) => {
valuesObj[`${otherKey}_${index + 1}`] = data[otherKey];
});
_tableData.push(valuesObj);
});
console.log(_tableData);
this.tableHeaderKeys = [];
this.setState({
tableTitle: _tableTitle,
tableData: _tableData
});
}
/**
* 获取对应的高亮颜色
*
* @param {string} key
* @returns
* @memberof TableDataView
*/
getHighlight(key) {
return HIGHLIGHT[key] ? HIGHLIGHT[key] : HIGHLIGHT.white;
}
/**
* 渲染行
*/
renderRow(rowData, header, border) {
// 渲染
let viewArr = [];
// 循环渲染
let index = 0;
let rowKey;
// 通过表头确定唯一顺序
if (header || this.props.tableDirection === TABLE_DIRECTION.COLUMN) {
rowKey = Object.keys(rowData)[0];
const firstCellData = rowData[rowKey];
let highlightColor;
// 对象类型 并且有值
if (typeof firstCellData === 'object' && !Array.isArray(firstCellData) && firstCellData.highlight) {
highlightColor = this.getHighlight(firstCellData.highlight);
}
else {
highlightColor = false;
}
for (const prop in rowData) {
viewArr.push(this.renderCell(prop, rowData[prop] || '', index, border, header, highlightColor));
// 第一遍循环表头的时候确定唯一顺序
this.tableHeaderKeys.push(prop);
index++;
}
}
else {
rowKey = rowData[Object.keys(rowData)[0]];
for (const prop of this.tableHeaderKeys) {
viewArr.push(this.renderCell(prop, rowData[prop] || '', index, border, header, false));
index++;
}
}
return (<View style={[
header ? this.styles.header : '',
this.styles.row,
border ? this.styles.rowBorder : '',
]} key={rowKey}>
{viewArr}
</View>);
}
/**
*
* 渲染单元格
*
*
* @param {string} prop 属性key
* @param {*} value 值,可能是对象或者string or number
* @param {number} index 序号
* @param {boolean} border 是否渲染border
* @param {boolean} header 是否是表头行
* @param {(string|boolean)} highLightColor 高亮背景色
* @returns
* @memberof TableDataView
*/
renderCell(prop, value, index, border, header, highLightColor) {
const flexValue = this.props.flexArr[index];
// 支持红绿高亮
let trueValue;
if (typeof value === 'object' && !Array.isArray(value)) {
console.log(value);
trueValue = value.image || value.label || '';
}
else {
trueValue = value;
}
return (<View style={[
border ? this.styles.cellBorder : '',
this.styles.cell,
{ flex: flexValue || 1 },
highLightColor ? { backgroundColor: highLightColor } : null
]} key={prop}>
{value.image
? <Image source={{ uri: value.image }} style={{ width: 30, height: 30 }}/>
: <Text style={[
this.styles.cellText,
header ? this.styles.headerText : null
]}>
{trueValue || '无'}
</Text>}
</View>);
}
render() {
const self = this;
const { border } = this.props;
return (<View style={[this.styles.container, this.props.style]}>
{this.props.title
? <View style={this.styles.titleContainer}>
<View style={[this.styles.titleView]}>
<Text style={this.styles.titleText}>{this.props.title}</Text>
</View>
</View>
: null}
<View style={[this.styles.table, border ? this.styles.tableBorder : '']}>
{this.renderRow(this.state.tableTitle, true, border)}
{this.state.tableData.map(item => {
return self.renderRow(item, false, border);
})}
</View>
</View>);
}
}
// 因为使用了 泛型 所以不需要定义 static propTypes 了
// public propTypes = TableviewPropTypes;
TableDataView.defaultProps = {
title: '',
tableTitle: {},
tableDirection: TABLE_DIRECTION.ROW,
tableData: [],
border: true,
borderColor: '#E0E0E0',
flexArr: [1, 1, 1, 1],
};
export default TableDataView;