Comments (9)
Commenting here rather than creating a new issue as the fix is likely related.
I'm also wondering how to get Sortable to work with objects - in particular, a store containing an array of objects. Replacing the items signal with a store throws the attached errors (even after implementing your codepen changes).
Below is a basic example of the code, which throws the same errors:
import {
DragDropProvider,
DragDropSensors,
DragOverlay,
SortableProvider,
createSortable,
closestCenter
} from "@thisbeyond/solid-dnd";
import { createSignal, For } from "solid-js";
import { createStore } from "solid-js/store";
const Sortable = (props) => {
const sortable = createSortable(props.item);
return (
<div
use:sortable
class="sortable"
classList={{ "opacity-25": sortable.isActiveDraggable }}
>
{props.item}
</div>
);
};
export const SortableTest = () => {
const [items, setItems] = createStore([
{
toDoId: 4,
description: "Lorem Ipsum4"
},
{
toDoId: 12,
description: "Lorem Ipsum12"
},
{
toDoId: 1,
description: "Lorem Ipsum1"
},
{
toDoId: 20,
description: "Lorem Ipsum20"
}
]);
const [activeItem, setActiveItem] = createSignal(null);
const ids = () => items.map((item) => item.toDoId);
const onDragStart = ({ draggable }) => setActiveItem(draggable.id);
const onDragEnd = ({ draggable, droppable }) => {
if (draggable && droppable) {
const currentItems = ids();
const fromIndex = currentItems.indexOf(draggable.id);
const toIndex = currentItems.indexOf(droppable.id);
if (fromIndex !== toIndex) {
const updatedItems = currentItems.slice();
updatedItems.splice(toIndex, 0, ...updatedItems.splice(fromIndex, 1));
setItems(updatedItems);
}
}
setActiveItem(null);
};
return (
<DragDropProvider
onDragStart={onDragStart}
onDragEnd={onDragEnd}
collisionDetector={closestCenter}
>
<DragDropSensors />
<div class="column self-stretch">
<SortableProvider ids={ids()}>
<For each={items}>{(item) => <Sortable item={item} />}</For>
</SortableProvider>
</div>
<DragOverlay>
<div class="sortable">{activeItem()}</div>
</DragOverlay>
</DragDropProvider>
);
};
export default SortableTest;
from solid-dnd.
No problem! And, yeah, better docs is on the list for future #17
from solid-dnd.
Yeah, I created the examples to be as simple as possible - just numbers - but I appreciate how they might actually complicate things when applied to real world usage. I'll think about updating them in future.
from solid-dnd.
SortableProvider
coordinates the ordering of items so that sortables can tranform themselves to the correct position as the order changes. The ids
property should be an array of the initial ordering of items as per their unique id
- e.g. [1, 3, 5, 7]
. Those ids should correspond to the ids you use to register each Sortable
-> e.g. sortable = createSortable(id)
.
In your example, I imagine the error is coming from your item
- what does that look like? If you have a full example to share that demonstrates the error then I can look into this more.
from solid-dnd.
An item
looks roughly like this:
{
"title": "Hello, world",
"body": {
"content": "Lorem ipsum dolor sit amet"
}
}
If an item
ISN'T an object (e.g. a string), it does work.
Before I made this issue I already tried to set ids
to unique ID strings (I also tried numbers), but they gave the same error.
from solid-dnd.
Without a full example it will be hard to debug this further.
One thing I note is that you are not creating sortable's anywhere so even without the error it won't actually do anything.
Another thing to check is that you have a <DragDropProvider>
wrapping everything.
I made a go at recreating your setup from the limited info shared that might help you -> https://codesandbox.io/s/solid-dnd-github-issue-41-bxc17o
from solid-dnd.
The error is correct - you need to pass the unique identifier (not an object) to createSortable
.
So in your code the fix would be:
const sortable = createSortable(props.item.toDoId)
You can also pass arbitrary data (your item
) as an optional argument if you want to access that in the onX
handlers. Note that solid-dnd doesn't care about the data at all, it just passes it through.
const sortable = createSortable(props.item.toDoId, props.item)
from solid-dnd.
That makes sense to me - thanks for taking the time to provide some explanation. Just starting to wrap my head around this - perhaps the docs could do with a little more hand-holding for the newcomers like myself?
from solid-dnd.
After changing sortable to:
const sortable = createSortable(props.item.toDoId, props.item)
And then further changing the return from props.item
to props.item.description
(since, obviously, you can't return an object in jsx), I was still getting some bug where the items would drag correctly but never drop into a new order.
After racking my brain for far too long, I realised .indexOf only works for primitive data types. As such, a better solution is to replace:
const fromIndex = currentItems.indexOf(draggable.id);
with:
const fromIndex = currentItems.findIndex(
(item) => item.toDoId === draggable.id
);
Felt a little ridiculous once I spotted it - figured it was worth posting both to help others and also potentially to update the example in the docs, as findIndex will work with all data types.
from solid-dnd.
Related Issues (20)
- Bento Draggable Layout HOT 2
- attach is used before it is defined HOT 1
- Non deterministic order of events onDragStart, onDragOver, onDragMove
- solid-dnd.com is down! HOT 2
- Get `useDragDropContext is not a function or its return value is not iterable` HOT 1
- How to get pointer coordinate in CollisionDetector HOT 1
- Scrolling when nearing top/bottom of droppable container / sortable container. HOT 1
- Sortable with Draghandle HOT 4
- Placeholder at the beginning of a drag HOT 1
- createDraggable and createDroppable are not reactive HOT 2
- Conditionally Prevent Drag HOT 1
- Draggable SVG shape moves faster than pointer HOT 4
- Unable to load data dynamically. Please help HOT 2
- Recompute layouts on remove
- Access state and actions from callback handlers
- Get current droppable on onDragMove HOT 1
- Only the text is visible on the dragged element HOT 1
- Add support for sortables of irregular sizes. HOT 4
- Beginners Documentation HOT 10
- Trying to render a custom component as sortable HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from solid-dnd.