

Drag and Drop List in React
source link: https://dev.to/readymadecode/drag-and-drop-list-in-react-18h9
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In our react applications we organizes some data in the form of list. We can show data in ordered list and unordered list. In our applications, sometimes we needs to set order of our list items. So, to do this we can create drag and drop list in react which is easy to set order of list from UI.
Create Drag and Drop List in React
var placeholder = document.createElement("li");
placeholder.className = "placeholder";
class List extends React.Component {
constructor(props) {
super(props);
this.state = {...props};
}
dragStart(e) {
this.dragged = e.currentTarget;
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', this.dragged);
}
dragEnd(e) {
this.dragged.style.display = 'block';
this.dragged.parentNode.removeChild(placeholder);
// update state
var data = this.state.colors;
var from = Number(this.dragged.dataset.id);
var to = Number(this.over.dataset.id);
if(from < to) to--;
data.splice(to, 0, data.splice(from, 1)[0]);
this.setState({colors: data});
}
dragOver(e) {
e.preventDefault();
this.dragged.style.display = "none";
if(e.target.className === 'placeholder') return;
this.over = e.target;
e.target.parentNode.insertBefore(placeholder, e.target);
}
render() {
var listItems = this.state.colors.map((item, i) => {
return (
<li
data-id={i}
key={i}
draggable='true'
onDragEnd={this.dragEnd.bind(this)}
onDragStart={this.dragStart.bind(this)}>{item}</li>
)
});
return (
<ul onDragOver={this.dragOver.bind(this)}>
{listItems}
</ul>
)
}
}
Enter fullscreen mode
Exit fullscreen mode
Use List Component in React
Now, we have <List /> Component we can use this in our class or functional component. Also, we can pass some props like colors props to our <List /> Component. For example, use this in our App Component.
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
colors: ['PHP', 'MYSQL', 'REACT', 'LARAVEL']
}
}
render() {
return (
<div>
<List colors={this.state.colors} />
</div>
)
}
}
Enter fullscreen mode
Exit fullscreen mode
Style List in React
ul {
list-style: none;
margin:0;
padding:0;
border: 5px solid #e1e1e1;
box-shadow: 1px 3px 8px #888;
}
li {
padding: 10px 15px;
background:#eee;
&:hover {
background: darken(#eee, 5%);
}
}
.placeholder {
background: rgb(255,240,120);
&:before {
content: "Drop here";
color: rgb(225,210,90);
}
}
Enter fullscreen mode
Exit fullscreen mode
Please like share subscribe and give positive feedback to motivate me to write more for you.
For more tutorials please visit my website.
Thanks:)
Happy Coding:)
Recommend
-
133
Files Permalink Latest commit message
-
204
atlassian/react-beautiful-dnd: Beautiful and accessible drag and drop for lists with React Files
-
37
Photo by
-
22
OverviewUse DayPilot.Scheduler.makeDraggable() to activate external DOM elements.DraggableItem React component embeds the activation logic and lets you create the draggable items tran...
-
33
Blog Post Implement Drag and Drop for PSPDFKit in a React Application
-
13
BackCreate a Drag-and-Drop Zone in React with react-dropzoneApril 18th, 2022 · 4 min read
-
6
If you already have a Next app, the first step to setting up a drag and component is to install React DND Beautiful. Install Depending on if yo...
-
5
-
13
Drag Modern and easy-to-use drag&drop library for react-native Apr 16, 2023 2 min read
-
5
Introduction In modern web development, implementing drag-and-drop upload functionality has become a common requirement for many applications. Drag and drop allows users to easily upload files by simply dragging them from their lo...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK