Repository navigation
Expand file tree
/
Copy pathFlowTable.js
More file actions
125 lines (115 loc) · 2.76 KB
/
Copy pathFlowTable.js
File metadata and controls
125 lines (115 loc) · 2.76 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
/*
Copyright © SuperMap. All rights reserved.
Author: Yang Shanglong
E-mail: yangshanglong@supermap.com
*/
import * as React from 'react'
import { ScrollView, View } from 'react-native'
import styles from './styles'
export default class TableList extends React.Component {
props: {
data: Array,
numColumns?: number,
lineSeparator?: number,
style?: Object,
cellWidth?: Object,
cellStyle?: Object,
rowStyle?: Object,
renderCell: () => {},
device: Object,
type?: string,
}
static defaultProps = {
data: [],
numColumns: 2,
type: 'normal', // normal | scroll
lineSeparator: 10,
}
constructor(props) {
super(props)
this.state = {
height: 0,
width: 0,
}
}
_onLayout = event => {
//获取根View的宽高,以及左上角的坐标值
let { width, height } = event.nativeEvent.layout
if (height !== this.state.height || width !== this.state.width) {
this.setState({
height,
width,
})
}
}
renderRows = () => {
// this.getDeviceWidth()
// console.log(this.props)
let rows = [],
rowsView = [],
column = this.props.numColumns
if (this.props.cellWidth) {
column = Math.floor(this.state.width / this.props.cellWidth)
}
this.props.data.forEach((item, index) => {
let rowIndex = Math.floor(index / column)
if (!rows[rowIndex]) {
rows[rowIndex] = []
}
rows[rowIndex].push(this.renderCell(item, rowIndex, index))
})
rows.forEach((row, rowIndex) => {
rowsView.push(this.renderRow(row, rowIndex))
})
return rowsView
}
renderRow = (row, rowIndex) => {
return (
<View
key={'row-' + rowIndex}
style={[
styles.row,
rowIndex &&
this.props.lineSeparator >= 0 && {
marginTop: this.props.lineSeparator,
},
]}
>
{row}
</View>
)
}
renderCell = (item, rowIndex, cellIndex) => {
if (!this.props.renderCell) throw new Error('Please render cell')
let column = this.props.numColumns
return (
<View
style={{ width: this.props.device.width / column }}
key={rowIndex + '-' + cellIndex}
>
{this.props.renderCell({ item, rowIndex, cellIndex })}
</View>
)
}
render() {
if (this.props.type === 'scroll') {
return (
<ScrollView
style={[styles.scrollContainer, this.props.style]}
onLayout={this._onLayout}
>
{this.renderRows()}
</ScrollView>
)
} else {
return (
<View
style={[styles.normalContainer, this.props.style]}
onLayout={this._onLayout}
>
{this.renderRows()}
</View>
)
}
}
}