1

I am using react-sortable-hoc to dragging and dropping grid of elements. For me it is working fine if it a single column. but if it is a multiple column(grid), it is not working as expected. not sure what i am doing wrong. The live code example is in below link https://stackblitz.com/edit/react-qfqmnp?file=src/App.js

import React, { useState, useEffect } from 'react';
import './style.css';
import { SortableContainer, SortableElement } from 'react-sortable-hoc';
import arrayMove from 'array-move';

const list = [
  'Item 1',
  'Item 2',
  'Item 3',
  'Item 4',
  'Item 5',
  'Item 6',
  'Item 7',
  'Item 8',
  'Item 9',
  'Item 10',
  'Item 11',
  'Item 12',
  'Item 13',
  'Item 14',
  'Item 15',
  'Item 16',
  'Item 17',
  'Item 18'
];

const SortableListItem = SortableElement(({ item }) => {
  return <li className="list-group-item"> {item} </li>;
});

const SortableList = SortableContainer(({ items }) => {
  return (
    <ul className="list-group">
      {items.map((item, index) => {
        return (
          <SortableListItem axis="xy" key={index} index={index} item={item} />
        );
      })}
    </ul>
  );
});

export default function App() {
  const [items, setItems] = useState(list);

  const onSortEnd = ({ oldIndex, newIndex }) => {
    setItems(arrayMove(items, oldIndex, newIndex));
  };

  return (
    <div>
      <h1>Hello StackBlitz!</h1>
      <p>Start editing to see some magic happen :)</p>
      <SortableList items={items} onSortEnd={onSortEnd} />
    </div>
  );
}

1 Answer 1

1

For dragging in grid, you have to write some CSS to make UI as grid look like and then in SortableList you have to pass axis={'xy'} like

<SortableList axis={'xy'} items={items} onSortEnd={onSortEnd} />

You can check working code over here Running Sample

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.