Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clicking one draggable selects them all #1404

Closed
joshbedo opened this issue May 15, 2024 · 1 comment
Closed

Clicking one draggable selects them all #1404

joshbedo opened this issue May 15, 2024 · 1 comment

Comments

@joshbedo
Copy link

joshbedo commented May 15, 2024

Super confused I have a pretty basic example below but for some reason when you select one draggable in a list they are all selected. Can't really figure out why its mapping the draggable wrong. Looking at dragStart and other events its not selecting anything besides the last item and the tabindex is 0 for everything in the list.

App.tsx

        {answerChoices.length ? (
          answerChoices.map((answer) => (
            <Draggable key={answer?.id} id={answer?.id} index={answer?.index}>
              <Button>{answer?.text}</Button>
            </Draggable>
          ))
        ) : (
          <div>loading</div>
        )}

Draggable.tsx

import React, { PropsWithChildren } from 'react';
import { useDraggable } from '@dnd-kit/core';

type DraggableProps = PropsWithChildren<{ id: string; }>;

function Draggable({ id, children }: DraggableProps) {
  const { attributes, listeners, setNodeRef, transform, isDragging } = useDraggable({
    id: 'draggable',
    data: {
      id,
    }
  });

  const style = transform ? {
    opacity: isDragging ? 0.5 : undefined,
    // transform: `translate3d(${transform.x}px, ${transform.y}px, 0)`,
  } : undefined;

  return (
    <div style={style} ref={setNodeRef} {...attributes} {...listeners}>
      {children}
    </div>
  );
}

export default Draggable;
@joshbedo
Copy link
Author

Figured it out, for useDraggable i had to update the id from a generic "draggable" id to incorporate the id

  const { attributes, listeners, setNodeRef, transform, isDragging } = useDraggable({
    id: `draggable-${id}`,
    data: {
      id,
    }
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant