ElementGroup.js
1.66 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
import React from 'react'
import Element from './Element'
import ReactAddonsUpdate from 'react-addons-update'
import autobind from 'autobind-decorator'
import { connect } from 'react-redux'
@autobind
class ElementGroup extends React.Component {
cloneElement(element) {
const elements = ReactAddonsUpdate(this.state.elements, { $push: [element] })
this.setState({elements: elements})
}
deleteElement(element) {
const index = this.state.elements.indexOf(element)
const elements = ReactAddonsUpdate(this.state.elements, { $splice: [[index, 1]] })
this.setState({elements: elements})
}
// 返回element的index为-1, 删除最后一个元素
deleteLastElement() {
const elements = ReactAddonsUpdate(this.state.elements, { $splice: [[-1, 1]] })
this.setState({elements: elements})
}
sidebar() {
return document.getElementById('sidebar')
}
render() {
const { elements } = this.props
return (
<div>
{
elements.map(function(element) {
return (
<Element meta_element={ element } key={ "key_" + element.id } id={ element.id } name={ element.name } initialPos={ element.initialPos } target_type={ element.target_type } handleCloneElement={ this.cloneElement } handleDeleteElement={ this.deleteElement } handleDeleteLastElement={ this.deleteLastElement } />
)
}.bind(this))
}
</div>
)
}
}
const mapStateToProps = (state, ownProps) => ({
elements: state.headergroup
})
const mapDispatchToProps = (dispatch, ownProps) => ({
})
const VisibleElementGroup = connect(
mapStateToProps,
mapDispatchToProps
)(ElementGroup)
export default VisibleElementGroup