Skip to content

Commit

Permalink
fix: don't trigger clicks when dropping array items
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Jan 30, 2025
1 parent 7a0c1af commit 24de1fc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export const ArrayField = ({
}
}, []);

const [isDragging, setIsDragging] = useState(false);

const forceReadOnly = getPermissions({ item: selectedItem }).edit === false;

if (field.type !== "array" || !field.arrayFields) {
Expand All @@ -119,6 +121,7 @@ export const ArrayField = ({
readOnly={readOnly}
>
<SortableProvider
onDragStart={() => setIsDragging(true)}
onMove={(move) => {
const newValue = reorder(value, move.source, move.target);
const newArrayStateItems: ItemWithId[] = reorder(
Expand All @@ -138,6 +141,7 @@ export const ArrayField = ({
value: newValue,
arrayState: { ...arrayState, items: newArrayStateItems },
});
setIsDragging(false);
}}
>
<div
Expand Down Expand Up @@ -172,6 +176,8 @@ export const ArrayField = ({
>
<div
onClick={(e) => {
if (isDragging) return;

e.preventDefault();
e.stopPropagation();

Expand Down Expand Up @@ -334,6 +340,8 @@ export const ArrayField = ({
type="button"
className={getClassName("addButton")}
onClick={() => {
if (isDragging) return;

const existingValue = value || [];

const newValue = [
Expand Down
3 changes: 3 additions & 0 deletions packages/core/components/Sortable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import { RestrictToElement } from "@dnd-kit/dom/modifiers";

export const SortableProvider = ({
children,
onDragStart,
onMove,
}: PropsWithChildren<{
onDragStart: () => void;
onMove: (moveData: { source: number; target: number }) => void;
}>) => {
const [move, setMove] = useState({ source: -1, target: -1 });
Expand All @@ -28,6 +30,7 @@ export const SortableProvider = ({
},
}),
]}
onDragStart={onDragStart}
onDragOver={(event, manager) => {
const { operation } = event;
const { source, target } = operation;
Expand Down

0 comments on commit 24de1fc

Please sign in to comment.