forked from rajeshpillai/youtube-react-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDragDropDemo.js
More file actions
77 lines (66 loc) · 2.07 KB
/
AppDragDropDemo.js
File metadata and controls
77 lines (66 loc) · 2.07 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
import React, { Component } from 'react';
import './App.css';
export default class AppDragDropDemo extends Component {
state = {
tasks: [
{name:"Learn Angular",category:"wip", bgcolor: "yellow"},
{name:"React", category:"wip", bgcolor:"pink"},
{name:"Vue", category:"complete", bgcolor:"skyblue"}
]
}
onDragStart = (ev, id) => {
console.log('dragstart:',id);
ev.dataTransfer.setData("id", id);
}
onDragOver = (ev) => {
ev.preventDefault();
}
onDrop = (ev, cat) => {
let id = ev.dataTransfer.getData("id");
let tasks = this.state.tasks.filter((task) => {
if (task.name == id) {
task.category = cat;
}
return task;
});
this.setState({
...this.state,
tasks
});
}
render() {
var tasks = {
wip: [],
complete: []
}
this.state.tasks.forEach ((t) => {
tasks[t.category].push(
<div key={t.name}
onDragStart = {(e) => this.onDragStart(e, t.name)}
draggable
className="draggable"
style = {{backgroundColor: t.bgcolor}}
>
{t.name}
</div>
);
});
return (
<div className="container-drag">
<h2 className="header">DRAG & DROP DEMO</h2>
<div className="wip"
onDragOver={(e)=>this.onDragOver(e)}
onDrop={(e)=>{this.onDrop(e, "wip")}}>
<span className="task-header">WIP</span>
{tasks.wip}
</div>
<div className="droppable"
onDragOver={(e)=>this.onDragOver(e)}
onDrop={(e)=>this.onDrop(e, "complete")}>
<span className="task-header">COMPLETED</span>
{tasks.complete}
</div>
</div>
);
}
}