Skip to content

Commit

Permalink
Merge pull request #1542 from clauderic/fix-zero-id
Browse files Browse the repository at this point in the history
Fix for when id is equal to 0
  • Loading branch information
clauderic authored Nov 23, 2024
2 parents b7f46bb + f629ec6 commit baad391
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 22 deletions.
6 changes: 6 additions & 0 deletions .changeset/fix-zero-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@dnd-kit/core': patch
'@dnd-ki/sortable': patch
---

Fix bug with draggable and sortable elements with an `id` equal to `0`.
4 changes: 2 additions & 2 deletions packages/core/src/components/DndContext/DndContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const DndContext = memo(function DndContext({
draggable: {active: activeId, nodes: draggableNodes, translate},
droppable: {containers: droppableContainers},
} = state;
const node = activeId ? draggableNodes.get(activeId) : null;
const node = activeId != null ? draggableNodes.get(activeId) : null;
const activeRects = useRef<Active['rect']['current']>({
initial: null,
translated: null,
Expand Down Expand Up @@ -197,7 +197,7 @@ export const DndContext = memo(function DndContext({
);

useLayoutShiftScrollCompensation({
activeNode: activeId ? draggableNodes.get(activeId) : null,
activeNode: activeId != null ? draggableNodes.get(activeId) : null,
config: autoScrollOptions.layoutShiftCompensation,
initialRect: initialActiveNodeRect,
measure: measuringConfiguration.draggable.measure,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/hooks/useDraggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const NullContext = createContext<any>(null);

const defaultRole = 'button';

const ID_PREFIX = 'Droppable';
const ID_PREFIX = 'Draggable';

export function useDraggable({
id,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/hooks/utilities/useCachedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export function useCachedNode(
draggableNodes: DraggableNodes,
id: UniqueIdentifier | null
): DraggableNode['node']['current'] {
const draggableNode = id !== null ? draggableNodes.get(id) : undefined;
const draggableNode = id != null ? draggableNodes.get(id) : undefined;
const node = draggableNode ? draggableNode.node.current : null;

return useLazyMemo(
(cachedNode) => {
if (id === null) {
if (id == null) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function reducer(state: State, action: Actions): State {
},
};
case Action.DragMove:
if (!state.draggable.active) {
if (state.draggable.active == null) {
return state;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sortable/src/hooks/useSortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function useSortable({
return;
}

if (activeId && !previous.current.activeId) {
if (activeId != null && previous.current.activeId == null) {
previous.current.activeId = activeId;
return;
}
Expand Down
8 changes: 5 additions & 3 deletions stories/2 - Presets/Sortable/5-Virtualized.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function Sortable({
})
);
const getIndex = (id: UniqueIdentifier) => items.indexOf(id);
const activeIndex = activeId ? getIndex(activeId) : -1;
const activeIndex = activeId != null ? getIndex(activeId) : -1;

return (
<DndContext
Expand Down Expand Up @@ -80,7 +80,9 @@ function Sortable({
className={styles.VirtualList}
itemCount={items.length}
itemSize={64}
stickyIndices={activeId ? [items.indexOf(activeId)] : undefined}
stickyIndices={
activeId != null ? [items.indexOf(activeId)] : undefined
}
renderItem={({index, style}) => {
const id = items[index];

Expand All @@ -104,7 +106,7 @@ function Sortable({
</Wrapper>
{createPortal(
<DragOverlay adjustScale={adjustScale}>
{activeId ? (
{activeId != null ? (
<Item
value={items[activeIndex]}
handle={handle}
Expand Down
3 changes: 2 additions & 1 deletion stories/2 - Presets/Sortable/MultipleContainers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ export function MultipleContainers({
const [activeId, setActiveId] = useState<UniqueIdentifier | null>(null);
const lastOverId = useRef<UniqueIdentifier | null>(null);
const recentlyMovedToNewContainer = useRef(false);
const isSortingContainer = activeId ? containers.includes(activeId) : false;
const isSortingContainer =
activeId != null ? containers.includes(activeId) : false;

/**
* Custom collision detection strategy optimized for multiple containers
Expand Down
9 changes: 4 additions & 5 deletions stories/2 - Presets/Sortable/Sortable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ export function Sortable({
}: Props) {
const [items, setItems] = useState<UniqueIdentifier[]>(
() =>
initialItems ??
createRange<UniqueIdentifier>(itemCount, (index) => index + 1)
initialItems ?? createRange<UniqueIdentifier>(itemCount, (index) => index)
);
const [activeId, setActiveId] = useState<UniqueIdentifier | null>(null);
const sensors = useSensors(
Expand All @@ -137,7 +136,7 @@ export function Sortable({
const isFirstAnnouncement = useRef(true);
const getIndex = (id: UniqueIdentifier) => items.indexOf(id);
const getPosition = (id: UniqueIdentifier) => getIndex(id) + 1;
const activeIndex = activeId ? getIndex(activeId) : -1;
const activeIndex = activeId != null ? getIndex(activeId) : -1;
const handleRemove = removable
? (id: UniqueIdentifier) =>
setItems((items) => items.filter((item) => item !== id))
Expand Down Expand Up @@ -184,7 +183,7 @@ export function Sortable({
};

useEffect(() => {
if (!activeId) {
if (activeId == null) {
isFirstAnnouncement.current = true;
}
}, [activeId]);
Expand Down Expand Up @@ -246,7 +245,7 @@ export function Sortable({
adjustScale={adjustScale}
dropAnimation={dropAnimation}
>
{activeId ? (
{activeId != null ? (
<Item
value={items[activeIndex]}
handle={handle}
Expand Down
4 changes: 2 additions & 2 deletions stories/3 - Examples/Advanced/Pages/Pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function Pages({layout}: Props) {
const [items, setItems] = useState(() =>
createRange<UniqueIdentifier>(20, (index) => `${index + 1}`)
);
const activeIndex = activeId ? items.indexOf(activeId) : -1;
const activeIndex = activeId != null ? items.indexOf(activeId) : -1;
const sensors = useSensors(
useSensor(PointerSensor),
useSensor(KeyboardSensor, {coordinateGetter: sortableKeyboardCoordinates})
Expand Down Expand Up @@ -102,7 +102,7 @@ export function Pages({layout}: Props) {
</ul>
</SortableContext>
<DragOverlay dropAnimation={dropAnimation}>
{activeId ? (
{activeId != null ? (
<PageOverlay id={activeId} layout={layout} items={items} />
) : null}
</DragOverlay>
Expand Down
9 changes: 5 additions & 4 deletions stories/3 - Examples/Tree/SortableTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function SortableTree({

return removeChildrenOf(
flattenedTree,
activeId ? [activeId, ...collapsedItems] : collapsedItems
activeId != null ? [activeId, ...collapsedItems] : collapsedItems
);
}, [activeId, items]);
const projected =
Expand All @@ -156,9 +156,10 @@ export function SortableTree({
})
);

const sortedIds = useMemo(() => flattenedItems.map(({id}) => id), [
flattenedItems,
]);
const sortedIds = useMemo(
() => flattenedItems.map(({id}) => id),
[flattenedItems]
);
const activeItem = activeId
? flattenedItems.find(({id}) => id === activeId)
: null;
Expand Down

0 comments on commit baad391

Please sign in to comment.