我有一个React应用程序,它创建一个小部件网格,我已经创建了一个remove函数,让您单击remove并清除可交换网格项目的内容。我想创建一个添加功能,该功能允许您选择要在小部件中显示的内容。我现在还很陌生,我知道这可能要问很多,但是任何帮助都会很棒。
import React, { Component } from 'react';
import Swappable from './components/SwappableComponent'
import './App.css';
import DataTable from './components/tableWidget';
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import Paper from "@material-ui/core/Paper";
import Grid from "@material-ui/core/Grid";
const styles = theme => ({
root: {
flexGrow: 1
},
paper: {
padding: theme.spacing.unit * 2,
textAlign: "center",
color: theme.palette.text.secondary
}
});
class App extends Component {
constructor(props) {
super(props);
this.state={
widgets:[
{id:1, content: <DataTable/>},
{id:2, content: "#2"},
{id:3, content: "#3"},
{id:4, content: "#4"}
]
}
}
deleteEvent=(index)=>{
const copyWidgets=Object.assign([],this.state.widgets);
let widget=this.state.widgets[index];
widget.content="Add Widget";
copyWidgets[index]=widget;
this.setState({
widgets:copyWidgets
})
}
render() {
const { classes } = this.props;
return (
<div className={classes.root}>
<Grid container spacing={24}>
{
this.state.widgets.map((widget,index)=>{
return(
<Grid item xs={12} sm={6}>
<Paper className={classes.paper}><Swappable id={widget.id} content={widget.content} delete={this.deleteEvent.bind(this,index)}/></Paper>
</Grid>
)
})
}
</Grid>
</div>
);
}
}
App.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(App);
import React, { Component } from 'react'
class Swappable extends Component {
constructor() {
super()
this.state = {
customFunc: null
}
}
allowDrop(ev) {
ev.preventDefault();
}
drag(ev, customFunc = null) {
ev.dataTransfer.setData("src", ev.target.id);
console.log(ev.target.parentNode, 'TARGET DRAGSTART')
this.setState({
initialParentNode: ev.target.parentNode
})
}
dragEnd(ev, customFunc = null) {
console.log(ev.target.parentNode, 'TARGET DRAGEND')
if (customFunc && (ev.target.parentNode != this.state.initialParentNode)) {
console.log('custom func')
this.props.customFunc()
}
}
drop(ev, dragableId, dropzoneId, customFunc = null, swappable = true) {
ev.preventDefault();
let src = document.getElementById(ev.dataTransfer.getData("src"));
let srcParent = src.parentNode;
let target = document.getElementById(dragableId);
console.log(src, 'dragged element');
console.log(srcParent, 'parent of dragged');
console.log(target, 'element to be swapped')
swappable ? this.swapElements(src, target, srcParent) : this.transferElement(src, dropzoneId)
}
swapElements(src, target, srcParent) {
target.replaceWith(src);
srcParent.appendChild(target);
}
transferElement(src, dropzoneId) {
let dropzone = document.getElementById(dropzoneId)
dropzone.appendChild(src);
}
render() {
const dropZoneStyle = {
width: '450px',
minHeight: '300px',
padding: '10px',
border: '1px solid #aaaaaa'
};
const draggableStyle = {
width: '400px',
height: '300px',
padding: '10px',
border: '1px solid red'
};
const { id, content, swappable, customFunc } = this.props
const dropzoneId = 'drop' + id
const dragableId = 'drag' + id
console.log(customFunc, 'customFunc')
return (
<div
id = {dropzoneId}
onDrop={(event) => this.drop(event, dragableId, dropzoneId, customFunc, swappable)}
onDragOver={(event) => this.allowDrop(event)}
style={dropZoneStyle}>
<div id={ dragableId }
draggable="true"
onDragStart={(event) => this.drag(event)}
onDragEnd = {(event) => this.dragEnd(event, customFunc)}
style={draggableStyle}>
{ content }
<div>{content==""?<button>Add Widget</button>:<button onClick={this.props.delete}>Delete</button>}</div>
</div>
</div>
)
}
}
export default Swappable;