Merge pull request #15192 from strapi/fix/relation-reordering-scrolling

This commit is contained in:
Josh 2022-12-16 14:17:02 +00:00 committed by GitHub
commit 1ad8af4237
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 14 deletions

View File

@ -132,7 +132,16 @@ import { ConnectDropTarget, ConnectDragSource, ConnectDragPreview } from 'react-
interface UseDragAndDropOptions {
index: number;
onMoveItem: (newIndex: number, currentIndex: number) => void;
/**
* @default "regular"
* Defines whether the change in index should be immediately over another
* dropzone or half way over it (regular).
*/
dropSensitivity?: 'immediate' | 'regular';
item?: object;
/**
* @default 'STRAPI_DND'
*/
type?: string;
onCancel?: (index: number) => void;
onDropItem?: (index: number) => void;

View File

@ -352,9 +352,7 @@ const RelationInput = ({
relations,
updatePositionOfRelation: handleUpdatePositionOfRelation,
}}
itemKey={(index, { relations: relationsItems }) =>
`${relationsItems[index].mainField}_${relationsItems[index].id}`
}
itemKey={(index) => `${relations[index].mainField}_${relations[index].id}`}
innerElementType="ol"
>
{ListItem}

View File

@ -50,11 +50,13 @@ export const RelationItem = ({
item: {
displayedValue: displayValue,
status,
id,
},
onGrabItem,
onDropItem,
onCancel,
onMoveItem: updatePositionOfRelation,
dropSensitivity: 'immediate',
});
const composedRefs = composeRefs(relationRef, dragRef);

View File

@ -12,6 +12,7 @@ import { useKeyboardDragAndDrop } from './useKeyboardDragAndDrop';
* item?: object,
* onStart?: () => void,
* onEnd?: () => void,
* dropSensitivity?: 'regular' | 'immediate'
* } & import('./useKeyboardDragAndDrop').UseKeyboardDragAndDropCallbacks}
*/
@ -39,6 +40,7 @@ export const useDragAndDrop = (
onDropItem,
onCancel,
onMoveItem,
dropSensitivity = 'regular',
}
) => {
const objectRef = useRef(null);
@ -62,19 +64,21 @@ export const useDragAndDrop = (
return;
}
const hoverBoundingRect = objectRef.current.getBoundingClientRect();
const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
const clientOffset = monitor.getClientOffset();
const hoverClientY = clientOffset.y - hoverBoundingRect.top;
if (dropSensitivity === 'regular') {
const hoverBoundingRect = objectRef.current.getBoundingClientRect();
const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
const clientOffset = monitor.getClientOffset();
const hoverClientY = clientOffset.y - hoverBoundingRect.top;
// Dragging downwards
if (dragIndex < newInd && hoverClientY < hoverMiddleY) {
return;
}
// Dragging downwards
if (dragIndex < newInd && hoverClientY < hoverMiddleY) {
return;
}
// Dragging upwards
if (dragIndex > newInd && hoverClientY > hoverMiddleY) {
return;
// Dragging upwards
if (dragIndex > newInd && hoverClientY > hoverMiddleY) {
return;
}
}
// Time to actually perform the action
@ -104,6 +108,16 @@ export const useDragAndDrop = (
}
},
canDrag: active,
/**
* This is for useful when the item is in a virtualized list.
* However, if we don't have an ID then we want the libraries
* defaults to take care of this.
*/
isDragging: item.id
? (monitor) => {
return item.id === monitor.getItem().id;
}
: undefined,
collect: (monitor) => ({
isDragging: monitor.isDragging(),
}),