-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
133 lines (120 loc) · 3.63 KB
/
index.js
File metadata and controls
133 lines (120 loc) · 3.63 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
import React, { Component, PropTypes } from 'react'
import Tcell from './Tcell.js'
import Spinner from './spinner.js'
import CSSModules from 'react-css-modules'
import styles from './index.styl'
import 'fe-reset'
@CSSModules(styles, { allowMultiple: true, errorWhenNotFound: false })
export class Etable extends Component {
changeCell (map, value, e) {
let name = this.props.map
let m = map
m.unshift(name)
this.props.onChange(m, value, e)
}
handleClick (e) {
if (this.props.onClick) {
if (e.target.dataset.map) {
let arg = e.target.dataset.map.split('-')
arg.unshift(this.props.map)
this.props.onClick(e, ...arg)
} else {
this.props.onClick(e)
}
}
}
renderTitle (title) {
return title ? <caption data-map={'caption'}>{title}</caption> : null
}
renderHead (head) {
return (
head.map(
(h, i) =>
<th
style={Object.assign({}, {width: h.width, minWidth: h.width}, h.style)}
data-map={'head-' + i}
key={i}>
<Tcell
data-map={'head-' + i}
onChange={this.changeCell.bind(this)}
editing={this.props.data.editing}
data={h}
action={this.props.action}
loading={this.props.data.loading} />
</th>
)
)
}
renderBody () {
let data = this.props.data
if (data.error) {
return <tr><td colSpan={data.head.length} styleName='noDataMsg'>{this.props.data.errorMsg}</td></tr>
}
if (data.body.length === 0) {
return <tr><td colSpan={data.head.length} styleName='noDataMsg'>该条件下暂无数据</td></tr>
}
return data.body.map((row, i) => row.isShow !== false ? <tr style={Object.assign({}, row.style, (data.activeRow ? data.activeRow : '').indexOf(i) > -1 ? data.activeStyle : null)} key={i}>{this.renderRow(row.data, i)}</tr> : null)
}
renderRow (row, i) {
if (this.props.data.error) {
return <td colSpan={this.props.data.length} styleName='noDataMsg'>{this.props.data.errorMsg}</td>
}
if (row.length === 0) {
return <td colSpan={this.props.data.length} styleName='noDataMsg'>该条件下暂无数据</td>
}
return (
row.map(
(r, j) =>
<td
key={j}
data-map={'body-' + i + '-' + j}
colSpan={r.colSpan}
rowSpan={r.rowSpan} >
<Tcell
data-map={'body-' + i + '-' + j}
onChange={this.changeCell.bind(this)}
editing={this.props.data.editing}
action={this.props.action}
data={r} />
</td>
)
)
}
render () {
let data = this.props.data
return (
<div styleName='warp'>
{data.loading ? <div styleName='tableLoading'><Spinner /></div> : null}
<table onClickCapture={this.handleClick.bind(this)} onDoubleClickCapture={this.handleClick.bind(this)}>
{this.renderTitle(data.title)}
<thead>
<tr>
{this.renderHead(data.head)}
</tr>
</thead>
<tbody>
{this.renderBody()}
</tbody>
</table>
</div>
)
}
}
Etable.propTypes = {
onClick: PropTypes.func,
onChange: PropTypes.func,
action: PropTypes.object,
data: PropTypes.shape({
loading: PropTypes.bool,
editing: PropTypes.bool,
error: PropTypes.bool,
errorMsg: PropTypes.string,
head: PropTypes.arrayOf(PropTypes.object),
body: PropTypes.arrayOf(
PropTypes.shape({
isShow: PropTypes.bool,
data: PropTypes.arrayOf(PropTypes.object)
}))
})
}
export default Etable