42
loading...
This website collects cookies to deliver better user experience
import React, { useState, useEffect, useRef } from 'react';
import Sortable from 'sortablejs';
import './style.css';
const initData = Array.from({ length: 15 }, (_, i) => ({
_id: (i + 1).toString(),
content: (i + 1).toString(),
}));
const SortableGrid = () => {
const gridRef = useRef(null);
const sortableJsRef = useRef(null);
const [data, setData] = useState(JSON.parse(sessionStorage.getItem('my-grid')) || initData);
const onListChange = () => {
const newData = [...gridRef.current.children]
.map(i => i.dataset.id)
.map(id => data.find(item => item._id === id));
sessionStorage.setItem('my-grid', JSON.stringify(newData));
setData(data);
};
useEffect(() => {
sortableJsRef.current = new Sortable(gridRef.current, {
animation: 150,
onEnd: onListChange,
});
}, []);
return (
<div ref={gridRef} id="gridDemo">
{data.map(({ _id, content }) => (
<div key={_id} data-id={_id} className="grid-square">
{content}
</div>
))}
</div>
);
};
export default SortableGrid;
This library continues to be relied upon heavily by Atlassian products, but we are focused on other priorities right now and have no current plans for further feature development or improvements.
Please note that this is not considered ready for production, as there are still a number of bugs being sent through.
div#gridDemo
and some div.square-items
inside. I checkd for gridDemo
in the source code, and found out the code for that example:// Grid demo
new Sortable(gridDemo, {
animation: 150,
ghostClass: 'blue-background-class',
});
onEnd
function seems doing the job I needed.import React, { useState, useEffect, useRef } from 'react';
import Sortable from 'sortablejs';
import './style.css';
const initData = Array.from({ length: 15 }, (_, i) => ({
_id: (i + 1).toString(),
content: (i + 1).toString(),
}));
const SortableGrid = () => {
const [data, setData] = useState(initData);
return (
<div id="gridDemo">
{data.map(({ _id, content }) => (
<div key={_id} data-id={_id} className="grid-square">
{content}
</div>
))}
</div>
);
};
export default SortableGrid;
data-id
to all of the items in the grid layout