Giter VIP home page Giter VIP logo

react-drag-reorder's Introduction

This is a simple React component that will enable you to reorder HTML elements. You can drag any component and change their position.

Installation

Installation is done using the npm install command:

$ npm install react-drag-reorder

Example

Import Draggable from react-drag-reorder and wrap it between the components that you would like to drag and reorder.

// ES6
import { Draggable } from "react-drag-reorder";

You would have to wrap the Draggable component to the components that you would like to reorder.

import React, { Component } from "react";
import { Draggable } from "react-drag-reorder";

class Drag extends Component {
  state = {
    words: ["Hello", "Hi", "How are you", "Cool"],
  };
  render() {
    return (
      <div className="flex-container">
        <div className="row">
          <Draggable>
            {this.state.words.map((word, idx) => {
              return (
                <div key={idx} className="flex-item">
                  {word}
                </div>
              );
            })}
          </Draggable>
        </div>
      </div>
    );
  }
}

export default Drag;

Getting the current position and new position of element

Props Description type
onPosChange subscribe to change events function (currentPos , newPos)

Example

import React, { Component } from "react";
import { Draggable } from "react-drag-reorder";

class Drag extends Component {
  state = {
    words: ["Hello", "Hi", "How are you", "Cool"],
  };

  getChangedPos = (currentPos, newPos) => {
    console.log(currentPos, newPos);
  };

  render() {
    return (
      <div className="flex-container">
        <div className="row">
          <Draggable onPosChange={this.getChangedPos}>
            {this.state.words.map((word, idx) => {
              return (
                <div key={idx} className="flex-item">
                  {word}
                </div>
              );
            })}
          </Draggable>
        </div>
      </div>
    );
  }
}

export default Drag;

Updates

1.0

Support added to contain nested draggable components.

Please make sure to sort the parent components first before reordering the child.

class Test extends Component {
  state = {
    words: ["Hello", "Hi", "How are you", "Cool"],
    languages: ["C", "C++", "Java", "JS"],
    shows: ["GOT", "Friends", "Big Bang"],
  };
  render() {
    return (
      <Draggable>
        <div className="row">
          <p className="text">Words</p>
          <Draggable>
            {this.state.words.map((word, idx) => {
              return (
                <div key={idx} className="flex-item">
                  {word}
                </div>
              );
            })}
          </Draggable>
        </div>
        <div className="row">
          <p className="text">Languages</p>
          <Draggable>
            {this.state.languages.map((lng, idx) => {
              return (
                <div key={idx} className="flex-item">
                  {lng}
                </div>
              );
            })}
          </Draggable>
        </div>
        <div className="row">
          <p className="text">Shows</p>
          <Draggable>
            {this.state.shows.map((lng, idx) => {
              return (
                <div key={idx} className="flex-item">
                  {lng}
                </div>
              );
            })}
          </Draggable>
        </div>
      </Draggable>
    );
  }
}

NOTE: this is fork of react-drag-reorder

Contributors are welcome ! ๐Ÿ˜ƒ

react-drag-reorder's People

Contributors

vaishakvk avatar carlvoller avatar dhruvanwd avatar sajanthomas01 avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.