Javascript Drag And Drop But Can't Move to the bottom (With React)












0


















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>
)
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
colors: ['Red', 'Green', 'Blue', 'Yellow', 'Black', 'White', 'Orange']
}
}
render() {
return (
<div>
<List colors={this.state.colors} />
</div>
)
}
}

ReactDOM.render(
<App />,
document.getElementById('app')
);

body {
width: 100%;
max-width: 380px;
margin: 0 auto;
}

h1 {
border-left: 5px solid #e1e1e1;
padding-left: 5px;
}

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);
}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
<h1>React Drag &amp; Drop</h1>
<div id="app"></div>





There Is a simple Drag And Drop Source With react.



There are some errors here.



1.If you drag an elemnt to the bottom, an error occurs.



2.You cannot Swtich for the last item.



There seems to be a problem with the code below.



e.target.parentNode.insertBefore(placeholder, e.target);



Here's the original source code.



https://codepen.io/adamaoc/pen/GoKZKE



Thank you.










share|improve this question



























    0


















    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>
    )
    }
    }
    class App extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
    colors: ['Red', 'Green', 'Blue', 'Yellow', 'Black', 'White', 'Orange']
    }
    }
    render() {
    return (
    <div>
    <List colors={this.state.colors} />
    </div>
    )
    }
    }

    ReactDOM.render(
    <App />,
    document.getElementById('app')
    );

    body {
    width: 100%;
    max-width: 380px;
    margin: 0 auto;
    }

    h1 {
    border-left: 5px solid #e1e1e1;
    padding-left: 5px;
    }

    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);
    }
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
    <h1>React Drag &amp; Drop</h1>
    <div id="app"></div>





    There Is a simple Drag And Drop Source With react.



    There are some errors here.



    1.If you drag an elemnt to the bottom, an error occurs.



    2.You cannot Swtich for the last item.



    There seems to be a problem with the code below.



    e.target.parentNode.insertBefore(placeholder, e.target);



    Here's the original source code.



    https://codepen.io/adamaoc/pen/GoKZKE



    Thank you.










    share|improve this question

























      0












      0








      0











      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>
      )
      }
      }
      class App extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      colors: ['Red', 'Green', 'Blue', 'Yellow', 'Black', 'White', 'Orange']
      }
      }
      render() {
      return (
      <div>
      <List colors={this.state.colors} />
      </div>
      )
      }
      }

      ReactDOM.render(
      <App />,
      document.getElementById('app')
      );

      body {
      width: 100%;
      max-width: 380px;
      margin: 0 auto;
      }

      h1 {
      border-left: 5px solid #e1e1e1;
      padding-left: 5px;
      }

      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);
      }
      }

      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
      <h1>React Drag &amp; Drop</h1>
      <div id="app"></div>





      There Is a simple Drag And Drop Source With react.



      There are some errors here.



      1.If you drag an elemnt to the bottom, an error occurs.



      2.You cannot Swtich for the last item.



      There seems to be a problem with the code below.



      e.target.parentNode.insertBefore(placeholder, e.target);



      Here's the original source code.



      https://codepen.io/adamaoc/pen/GoKZKE



      Thank you.










      share|improve this question

















      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>
      )
      }
      }
      class App extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      colors: ['Red', 'Green', 'Blue', 'Yellow', 'Black', 'White', 'Orange']
      }
      }
      render() {
      return (
      <div>
      <List colors={this.state.colors} />
      </div>
      )
      }
      }

      ReactDOM.render(
      <App />,
      document.getElementById('app')
      );

      body {
      width: 100%;
      max-width: 380px;
      margin: 0 auto;
      }

      h1 {
      border-left: 5px solid #e1e1e1;
      padding-left: 5px;
      }

      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);
      }
      }

      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
      <h1>React Drag &amp; Drop</h1>
      <div id="app"></div>





      There Is a simple Drag And Drop Source With react.



      There are some errors here.



      1.If you drag an elemnt to the bottom, an error occurs.



      2.You cannot Swtich for the last item.



      There seems to be a problem with the code below.



      e.target.parentNode.insertBefore(placeholder, e.target);



      Here's the original source code.



      https://codepen.io/adamaoc/pen/GoKZKE



      Thank you.






      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>
      )
      }
      }
      class App extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      colors: ['Red', 'Green', 'Blue', 'Yellow', 'Black', 'White', 'Orange']
      }
      }
      render() {
      return (
      <div>
      <List colors={this.state.colors} />
      </div>
      )
      }
      }

      ReactDOM.render(
      <App />,
      document.getElementById('app')
      );

      body {
      width: 100%;
      max-width: 380px;
      margin: 0 auto;
      }

      h1 {
      border-left: 5px solid #e1e1e1;
      padding-left: 5px;
      }

      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);
      }
      }

      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
      <h1>React Drag &amp; Drop</h1>
      <div id="app"></div>





      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>
      )
      }
      }
      class App extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      colors: ['Red', 'Green', 'Blue', 'Yellow', 'Black', 'White', 'Orange']
      }
      }
      render() {
      return (
      <div>
      <List colors={this.state.colors} />
      </div>
      )
      }
      }

      ReactDOM.render(
      <App />,
      document.getElementById('app')
      );

      body {
      width: 100%;
      max-width: 380px;
      margin: 0 auto;
      }

      h1 {
      border-left: 5px solid #e1e1e1;
      padding-left: 5px;
      }

      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);
      }
      }

      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
      <h1>React Drag &amp; Drop</h1>
      <div id="app"></div>






      css reactjs ecmascript-6 drag-and-drop






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 22:58









      EdgarHanEdgarHan

      83




      83
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271329%2fjavascript-drag-and-drop-but-cant-move-to-the-bottom-with-react%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271329%2fjavascript-drag-and-drop-but-cant-move-to-the-bottom-with-react%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation