思想
在react的世界里,都是状态变更(数据变更)来驱动dom变化,动态添加dom不像以前用jquery一样append一个<li>
或者一个<td>
这样了,而是通过装载<li>
或者<td>
数据数组[]来动态添加dom,下面的例子实现使用了antd的组件库
参考:https://ant.design/components/form-cn/#components-form-demo-dynamic-form-item
实例
实现
1.引用
import React, { Component, PropTypes } from 'react';
import { Form, Input, Radio, Row, Col, Upload, Icon, Button, message, Select, DatePicker,Modal} from 'antd';
2.初始化
constructor(props) {
super(props);
this.state = {
hover: [],
};
}
3.内容组件
内容组件其实是一个<ul><li>
,遍历了一个<li>
的数组articleContent,CSS样式见最后
render(){
const liClass = this.state.hover[index] ? "rectanglesON " : "rectanglesOFF ";
const formItems = this.state.articleData.articleContent.map((k, index) => {
return (
<li key={index} ref={"li_" + index} datatype={k.contentType} className={liClass} onMouseEnter={this.handleMouseEnter.bind(this,index)} onMouseLeave{this.handleMouseLeave.bind(this,index)}
onClick={this.handleClickByModule.bind(this,index,k.contentType)}>
//业务需要显示的东西
<div>li标签里要显示的东西</div>
//悬停时右下角的按钮
<div className={this.state.hover[index]?'actionShow':'actionHide'}>
<div className={(index > 0)?'domShow':'domHide'}>
<Button type="primary" size="small" className="mr-5" onClick={this.handleClickBySortUp.bind(this,index)}><Icon type="caret-up" />上移</Button>
</div>
<div className={(index < this.state.articleData.articleContent.length - 1)?'domShow':'domHide'}>
<Button type="primary" size="small" className="mr-5" onClick={this.handleClickBySortDown.bind(this,index)}><Icon type="caret-down" />下移</Button>
</div>
<div>
<Button type="primary" size="small" className="mr-5" onClick={this.handleClickByDelete.bind(this,index)}><Icon type="delete" />删除</Button>
</div>
</div>
</li>
)
});
//ul
return (
<FormItem label="内容">
<ul className='listContent'>
{formItems}
</ul>
</FormItem>
)
}
4.事件
//鼠标移到内容模块上时触发的事件
handleMouseEnter(key) {
let arg = this.state.hover;
arg[key] = true;
this.setState({
hover: arg
});
}
//鼠标离开内容模块时触发的事件
handleMouseLeave(key) {
let arg = this.state.hover;
arg[key] = false;
this.setState({
hover: arg
});
}
//点击向上排序按钮事件
handleClickBySortUp(index, e) {
e.stopPropagation();
let arr = this.state.articleData.articleContent;
if (index != 0) {
let temp = arr[index - 1];
arr[index - 1] = arr[index];
arr[index] = temp;
this.setState({
articleData: this.state.articleData,
});
}
}
//点击向下排序按钮事件
handleClickBySortDown(index, e) {
e.stopPropagation();
let arr = this.state.articleData.articleContent;
if (index != arr.length) {
let temp = arr[index + 1];
arr[index + 1] = arr[index];
arr[index] = temp;
this.setState({
articleData: this.state.articleData,
});
}
}
//点击删除按钮事件
handleClickByDelete(index, e) {
e.stopPropagation();
let arr = this.state.articleData.articleContent;
arr.splice(index, 1);
this.setState({
articleData: this.state.articleData,
});
}
5.CSS样式
.rectanglesON {
word-wrap: break-word;
min-height: 200px;
border: 1px dashed #808080;
list-style-type: none;
margin-bottom: 10px;
cursor: pointer;
}
.rectanglesOFF {
word-wrap: break-word;
min-height: 200px;
border: 1px dashed #d3d3d3;
list-style-type: none;
margin-bottom: 10px;
}
.listContent li {
position: relative;
.actionShow {
position: absolute;
bottom: 0;
right: 0;
padding-left: 10px;
display: flex;
}
.actionHide {
position: absolute;
bottom: 0;
right: 0;
padding-left: 10px;
display: none;
}
}
.mr-5{
margin-right:5px;
}