2019-09-17 20:57:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
|
*/
|
|
|
|
|
2020-09-29 19:00:29 +02:00
|
|
|
import './drag_drop.scss';
|
2020-11-03 21:18:44 +01:00
|
|
|
import React, { useState, useContext, useEffect } from 'react';
|
2019-09-17 20:57:53 +02:00
|
|
|
import classNames from 'classnames';
|
2020-11-03 21:18:44 +01:00
|
|
|
import { keys, EuiScreenReaderOnly } from '@elastic/eui';
|
|
|
|
import { i18n } from '@kbn/i18n';
|
|
|
|
import { DragContext, DragContextState, ReorderContext, ReorderState } from './providers';
|
2019-10-16 05:01:43 +02:00
|
|
|
import { trackUiEvent } from '../lens_ui_telemetry';
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-11-03 21:18:44 +01:00
|
|
|
export type DroppableEvent = React.DragEvent<HTMLElement>;
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A function that handles a drop event.
|
|
|
|
*/
|
|
|
|
export type DropHandler = (item: unknown) => void;
|
|
|
|
|
2020-11-03 21:18:44 +01:00
|
|
|
/**
|
|
|
|
* A function that handles a dropTo event.
|
|
|
|
*/
|
|
|
|
export type DropToHandler = (dropTargetId: string) => void;
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
/**
|
2020-03-03 17:48:54 +01:00
|
|
|
* The base props to the DragDrop component.
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
2020-03-03 17:48:54 +01:00
|
|
|
interface BaseProps {
|
2019-09-17 20:57:53 +02:00
|
|
|
/**
|
|
|
|
* The CSS class(es) for the root element.
|
|
|
|
*/
|
|
|
|
className?: string;
|
|
|
|
|
2020-11-03 21:18:44 +01:00
|
|
|
/**
|
|
|
|
* The event handler that fires when this item
|
|
|
|
* is dropped to the one with passed id
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
dropTo?: DropToHandler;
|
2019-09-17 20:57:53 +02:00
|
|
|
/**
|
|
|
|
* The event handler that fires when an item
|
|
|
|
* is dropped onto this DragDrop component.
|
|
|
|
*/
|
|
|
|
onDrop?: DropHandler;
|
|
|
|
/**
|
|
|
|
* The value associated with this item, if it is draggable.
|
|
|
|
* If this component is dragged, this will be the value of
|
|
|
|
* "dragging" in the root drag/drop context.
|
|
|
|
*/
|
2020-11-03 21:18:44 +01:00
|
|
|
value?: DragContextState['dragging'];
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-01 17:58:23 +02:00
|
|
|
* Optional comparison function to check whether a value is the dragged one
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
2020-10-01 17:58:23 +02:00
|
|
|
isValueEqual?: (value1: unknown, value2: unknown) => boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The React element which will be passed the draggable handlers
|
|
|
|
*/
|
|
|
|
children: React.ReactElement;
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether or not the currently dragged item
|
|
|
|
* can be dropped onto this component.
|
|
|
|
*/
|
|
|
|
droppable?: boolean;
|
|
|
|
|
2020-09-04 16:11:41 +02:00
|
|
|
/**
|
|
|
|
* Additional class names to apply when another element is over the drop target
|
|
|
|
*/
|
|
|
|
getAdditionalClassesOnEnter?: () => string;
|
|
|
|
|
2020-03-03 17:48:54 +01:00
|
|
|
/**
|
|
|
|
* The optional test subject associated with this DOM element.
|
|
|
|
*/
|
|
|
|
'data-test-subj'?: string;
|
2020-10-01 17:58:23 +02:00
|
|
|
|
2020-11-03 21:18:44 +01:00
|
|
|
/**
|
|
|
|
* items belonging to the same group that can be reordered
|
|
|
|
*/
|
|
|
|
itemsInGroup?: string[];
|
|
|
|
|
2020-10-01 17:58:23 +02:00
|
|
|
/**
|
|
|
|
* Indicates to the user whether the currently dragged item
|
|
|
|
* will be moved or copied
|
|
|
|
*/
|
2020-11-03 21:18:44 +01:00
|
|
|
dragType?: 'copy' | 'move' | 'reorder';
|
2020-10-01 17:58:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates to the user whether the drop action will
|
|
|
|
* replace something that is existing or add a new one
|
|
|
|
*/
|
2020-11-03 21:18:44 +01:00
|
|
|
dropType?: 'add' | 'replace' | 'reorder';
|
2020-03-03 17:48:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The props for a draggable instance of that component.
|
|
|
|
*/
|
|
|
|
interface DraggableProps extends BaseProps {
|
2019-09-17 20:57:53 +02:00
|
|
|
/**
|
|
|
|
* Indicates whether or not this component is draggable.
|
|
|
|
*/
|
2020-03-03 17:48:54 +01:00
|
|
|
draggable: true;
|
|
|
|
/**
|
|
|
|
* The label, which should be attached to the drag event, and which will e.g.
|
|
|
|
* be used if the element will be dropped into a text field.
|
|
|
|
*/
|
|
|
|
label: string;
|
|
|
|
}
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-03-03 17:48:54 +01:00
|
|
|
/**
|
|
|
|
* The props for a non-draggable instance of that component.
|
|
|
|
*/
|
|
|
|
interface NonDraggableProps extends BaseProps {
|
2019-09-17 20:57:53 +02:00
|
|
|
/**
|
2020-03-03 17:48:54 +01:00
|
|
|
* Indicates whether or not this component is draggable.
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
2020-03-03 17:48:54 +01:00
|
|
|
draggable?: false;
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 17:48:54 +01:00
|
|
|
type Props = DraggableProps | NonDraggableProps;
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
/**
|
|
|
|
* A draggable / droppable item. Items can be both draggable and droppable at
|
|
|
|
* the same time.
|
|
|
|
*
|
|
|
|
* @param props
|
|
|
|
*/
|
2020-05-29 09:34:55 +02:00
|
|
|
|
|
|
|
export const DragDrop = (props: Props) => {
|
2019-09-17 20:57:53 +02:00
|
|
|
const { dragging, setDragging } = useContext(DragContext);
|
2020-10-01 17:58:23 +02:00
|
|
|
const { value, draggable, droppable, isValueEqual } = props;
|
2020-11-03 21:18:44 +01:00
|
|
|
|
2020-05-29 09:34:55 +02:00
|
|
|
return (
|
|
|
|
<DragDropInner
|
|
|
|
{...props}
|
|
|
|
dragging={droppable ? dragging : undefined}
|
2020-11-03 21:18:44 +01:00
|
|
|
setDragging={setDragging}
|
2020-10-01 17:58:23 +02:00
|
|
|
isDragging={
|
|
|
|
!!(draggable && ((isValueEqual && isValueEqual(value, dragging)) || value === dragging))
|
|
|
|
}
|
2020-09-04 16:11:41 +02:00
|
|
|
isNotDroppable={
|
|
|
|
// If the configuration has provided a droppable flag, but this particular item is not
|
|
|
|
// droppable, then it should be less prominent. Ignores items that are both
|
|
|
|
// draggable and drop targets
|
|
|
|
droppable === false && Boolean(dragging) && value !== dragging
|
|
|
|
}
|
2020-05-29 09:34:55 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const DragDropInner = React.memo(function DragDropInner(
|
2020-11-03 21:18:44 +01:00
|
|
|
props: Props &
|
|
|
|
DragContextState & {
|
|
|
|
isDragging: boolean;
|
|
|
|
isNotDroppable: boolean;
|
|
|
|
}
|
2020-05-29 09:34:55 +02:00
|
|
|
) {
|
2020-09-04 16:11:41 +02:00
|
|
|
const [state, setState] = useState({
|
|
|
|
isActive: false,
|
|
|
|
dragEnterClassNames: '',
|
|
|
|
});
|
2020-05-29 09:34:55 +02:00
|
|
|
const {
|
|
|
|
className,
|
|
|
|
onDrop,
|
|
|
|
value,
|
|
|
|
children,
|
|
|
|
droppable,
|
|
|
|
draggable,
|
|
|
|
dragging,
|
|
|
|
setDragging,
|
|
|
|
isDragging,
|
2020-09-04 16:11:41 +02:00
|
|
|
isNotDroppable,
|
2020-10-01 17:58:23 +02:00
|
|
|
dragType = 'copy',
|
|
|
|
dropType = 'add',
|
2020-11-03 21:18:44 +01:00
|
|
|
dropTo,
|
|
|
|
itemsInGroup,
|
2020-05-29 09:34:55 +02:00
|
|
|
} = props;
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-10-01 17:58:23 +02:00
|
|
|
const isMoveDragging = isDragging && dragType === 'move';
|
|
|
|
|
2020-09-04 16:11:41 +02:00
|
|
|
const classes = classNames(
|
|
|
|
'lnsDragDrop',
|
|
|
|
{
|
2020-10-01 17:58:23 +02:00
|
|
|
'lnsDragDrop-isDraggable': draggable,
|
|
|
|
'lnsDragDrop-isDragging': isDragging,
|
|
|
|
'lnsDragDrop-isHidden': isMoveDragging,
|
|
|
|
'lnsDragDrop-isDroppable': !draggable,
|
2020-11-03 21:18:44 +01:00
|
|
|
'lnsDragDrop-isDropTarget': droppable && dragType !== 'reorder',
|
|
|
|
'lnsDragDrop-isActiveDropTarget': droppable && state.isActive && dragType !== 'reorder',
|
2020-10-01 17:58:23 +02:00
|
|
|
'lnsDragDrop-isNotDroppable': !isMoveDragging && isNotDroppable,
|
|
|
|
'lnsDragDrop-isReplacing': droppable && state.isActive && dropType === 'replace',
|
2020-09-04 16:11:41 +02:00
|
|
|
},
|
|
|
|
state.dragEnterClassNames
|
|
|
|
);
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
const dragStart = (e: DroppableEvent) => {
|
|
|
|
// Setting stopPropgagation causes Chrome failures, so
|
|
|
|
// we are manually checking if we've already handled this
|
|
|
|
// in a nested child, and doing nothing if so...
|
|
|
|
if (e.dataTransfer.getData('text')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-03 17:48:54 +01:00
|
|
|
// We only can reach the dragStart method if the element is draggable,
|
|
|
|
// so we know we have DraggableProps if we reach this code.
|
|
|
|
e.dataTransfer.setData('text', (props as DraggableProps).label);
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
// Chrome causes issues if you try to render from within a
|
|
|
|
// dragStart event, so we drop a setTimeout to avoid that.
|
2020-11-03 21:18:44 +01:00
|
|
|
setState({ ...state });
|
2019-09-17 20:57:53 +02:00
|
|
|
setTimeout(() => setDragging(value));
|
|
|
|
};
|
|
|
|
|
|
|
|
const dragEnd = (e: DroppableEvent) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
setDragging(undefined);
|
|
|
|
};
|
|
|
|
|
|
|
|
const dragOver = (e: DroppableEvent) => {
|
|
|
|
if (!droppable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// An optimization to prevent a bunch of React churn.
|
|
|
|
if (!state.isActive) {
|
2020-09-04 16:11:41 +02:00
|
|
|
setState({
|
|
|
|
...state,
|
|
|
|
isActive: true,
|
|
|
|
dragEnterClassNames: props.getAdditionalClassesOnEnter
|
|
|
|
? props.getAdditionalClassesOnEnter()
|
|
|
|
: '',
|
|
|
|
});
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const dragLeave = () => {
|
2020-09-04 16:11:41 +02:00
|
|
|
setState({ ...state, isActive: false, dragEnterClassNames: '' });
|
2019-09-17 20:57:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const drop = (e: DroppableEvent) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2020-09-04 16:11:41 +02:00
|
|
|
setState({ ...state, isActive: false, dragEnterClassNames: '' });
|
2019-09-17 20:57:53 +02:00
|
|
|
setDragging(undefined);
|
|
|
|
|
2019-10-16 05:01:43 +02:00
|
|
|
if (onDrop && droppable) {
|
|
|
|
trackUiEvent('drop_total');
|
2019-09-17 20:57:53 +02:00
|
|
|
onDrop(dragging);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-03 21:18:44 +01:00
|
|
|
const isReorderDragging = !!(dragging && itemsInGroup?.includes(dragging.id));
|
|
|
|
|
|
|
|
if (
|
|
|
|
draggable &&
|
|
|
|
itemsInGroup?.length &&
|
|
|
|
itemsInGroup.length > 1 &&
|
|
|
|
value?.id &&
|
|
|
|
dropTo &&
|
|
|
|
(!dragging || isReorderDragging)
|
|
|
|
) {
|
|
|
|
const { label } = props as DraggableProps;
|
|
|
|
return (
|
|
|
|
<ReorderableDragDrop
|
|
|
|
dropTo={dropTo}
|
|
|
|
label={label}
|
|
|
|
className={className}
|
2020-11-11 12:36:04 +01:00
|
|
|
dataTestSubj={props['data-test-subj'] || 'lnsDragDrop'}
|
2020-11-03 21:18:44 +01:00
|
|
|
draggingProps={{
|
|
|
|
className: classNames(children.props.className, classes),
|
|
|
|
draggable,
|
|
|
|
onDragEnd: dragEnd,
|
|
|
|
onDragStart: dragStart,
|
|
|
|
isReorderDragging,
|
|
|
|
}}
|
|
|
|
dropProps={{
|
|
|
|
onDrop: drop,
|
|
|
|
onDragOver: dragOver,
|
|
|
|
onDragLeave: dragLeave,
|
|
|
|
dragging,
|
|
|
|
droppable,
|
|
|
|
itemsInGroup,
|
|
|
|
id: value.id,
|
|
|
|
isActive: state.isActive,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</ReorderableDragDrop>
|
|
|
|
);
|
|
|
|
}
|
2020-10-01 17:58:23 +02:00
|
|
|
return React.cloneElement(children, {
|
|
|
|
'data-test-subj': props['data-test-subj'] || 'lnsDragDrop',
|
2020-11-03 21:18:44 +01:00
|
|
|
className: classNames(children.props.className, classes, className),
|
2020-10-01 17:58:23 +02:00
|
|
|
onDragOver: dragOver,
|
|
|
|
onDragLeave: dragLeave,
|
|
|
|
onDrop: drop,
|
|
|
|
draggable,
|
|
|
|
onDragEnd: dragEnd,
|
|
|
|
onDragStart: dragStart,
|
|
|
|
});
|
2020-05-29 09:34:55 +02:00
|
|
|
});
|
2020-11-03 21:18:44 +01:00
|
|
|
|
|
|
|
const getKeyboardReorderMessageMoved = (
|
|
|
|
itemLabel: string,
|
|
|
|
position: number,
|
|
|
|
prevPosition: number
|
|
|
|
) =>
|
|
|
|
i18n.translate('xpack.lens.dragDrop.elementMoved', {
|
|
|
|
defaultMessage: `You have moved the item {itemLabel} from position {prevPosition} to position {position}`,
|
|
|
|
values: {
|
|
|
|
itemLabel,
|
|
|
|
position,
|
|
|
|
prevPosition,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const getKeyboardReorderMessageLifted = (itemLabel: string, position: number) =>
|
|
|
|
i18n.translate('xpack.lens.dragDrop.elementLifted', {
|
|
|
|
defaultMessage: `You have lifted an item {itemLabel} in position {position}`,
|
|
|
|
values: {
|
|
|
|
itemLabel,
|
|
|
|
position,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const lnsLayerPanelDimensionMargin = 8;
|
|
|
|
|
|
|
|
export const ReorderableDragDrop = ({
|
|
|
|
draggingProps,
|
|
|
|
dropProps,
|
|
|
|
children,
|
|
|
|
label,
|
|
|
|
dropTo,
|
|
|
|
className,
|
2020-11-11 12:36:04 +01:00
|
|
|
dataTestSubj,
|
2020-11-03 21:18:44 +01:00
|
|
|
}: {
|
|
|
|
draggingProps: {
|
|
|
|
className: string;
|
|
|
|
draggable: Props['draggable'];
|
|
|
|
onDragEnd: (e: DroppableEvent) => void;
|
|
|
|
onDragStart: (e: DroppableEvent) => void;
|
|
|
|
isReorderDragging: boolean;
|
|
|
|
};
|
|
|
|
dropProps: {
|
|
|
|
onDrop: (e: DroppableEvent) => void;
|
|
|
|
onDragOver: (e: DroppableEvent) => void;
|
|
|
|
onDragLeave: () => void;
|
|
|
|
dragging: DragContextState['dragging'];
|
|
|
|
droppable: DraggableProps['droppable'];
|
|
|
|
itemsInGroup: string[];
|
|
|
|
id: string;
|
|
|
|
isActive: boolean;
|
|
|
|
};
|
|
|
|
children: React.ReactElement;
|
|
|
|
label: string;
|
|
|
|
dropTo: DropToHandler;
|
|
|
|
className?: string;
|
2020-11-11 12:36:04 +01:00
|
|
|
dataTestSubj: string;
|
2020-11-03 21:18:44 +01:00
|
|
|
}) => {
|
|
|
|
const { itemsInGroup, dragging, id, droppable } = dropProps;
|
|
|
|
const { reorderState, setReorderState } = useContext(ReorderContext);
|
|
|
|
|
|
|
|
const { isReorderOn, reorderedItems, draggingHeight, direction, groupId } = reorderState;
|
|
|
|
const currentIndex = itemsInGroup.indexOf(id);
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() =>
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
isReorderOn: draggingProps.isReorderDragging,
|
|
|
|
})),
|
|
|
|
[draggingProps.isReorderDragging, setReorderState]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2020-11-11 12:36:04 +01:00
|
|
|
<div
|
|
|
|
className={classNames('lnsDragDrop__reorderableContainer', className)}
|
|
|
|
data-test-subj={dataTestSubj}
|
|
|
|
>
|
2020-11-03 21:18:44 +01:00
|
|
|
<EuiScreenReaderOnly showOnFocus>
|
|
|
|
<button
|
|
|
|
aria-label={label}
|
|
|
|
aria-describedby={`lnsDragDrop-reorderInstructions-${groupId}`}
|
|
|
|
className="lnsDragDrop__keyboardHandler"
|
|
|
|
data-test-subj="lnsDragDrop-keyboardHandler"
|
|
|
|
onBlur={() => {
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
isReorderOn: false,
|
|
|
|
keyboardReorderMessage: '',
|
|
|
|
}));
|
|
|
|
}}
|
|
|
|
onKeyDown={(e: React.KeyboardEvent<HTMLButtonElement>) => {
|
|
|
|
if (e.key === keys.ENTER || e.key === keys.SPACE) {
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
isReorderOn: !isReorderOn,
|
|
|
|
keyboardReorderMessage: isReorderOn
|
|
|
|
? ''
|
|
|
|
: getKeyboardReorderMessageLifted(label, currentIndex + 1),
|
|
|
|
}));
|
|
|
|
} else if (e.key === keys.ESCAPE) {
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
isReorderOn: false,
|
|
|
|
keyboardReorderMessage: '',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (isReorderOn) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (keys.ARROW_DOWN === e.key) {
|
|
|
|
if (currentIndex < itemsInGroup.length - 1) {
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
keyboardReorderMessage: getKeyboardReorderMessageMoved(
|
|
|
|
label,
|
|
|
|
currentIndex + 2,
|
|
|
|
currentIndex + 1
|
|
|
|
),
|
|
|
|
}));
|
|
|
|
dropTo(itemsInGroup[currentIndex + 1]);
|
|
|
|
}
|
|
|
|
} else if (keys.ARROW_UP === e.key) {
|
|
|
|
if (currentIndex > 0) {
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
keyboardReorderMessage: getKeyboardReorderMessageMoved(
|
|
|
|
label,
|
|
|
|
currentIndex,
|
|
|
|
currentIndex + 1
|
|
|
|
),
|
|
|
|
}));
|
|
|
|
dropTo(itemsInGroup[currentIndex - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</EuiScreenReaderOnly>
|
|
|
|
{React.cloneElement(children, {
|
2020-11-11 12:36:04 +01:00
|
|
|
['data-test-subj']: 'lnsDragDrop-reorderableDrag',
|
2020-11-03 21:18:44 +01:00
|
|
|
draggable: draggingProps.draggable,
|
|
|
|
onDragEnd: draggingProps.onDragEnd,
|
|
|
|
onDragStart: (e: DroppableEvent) => {
|
|
|
|
const height = e.currentTarget.offsetHeight + lnsLayerPanelDimensionMargin;
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
draggingHeight: height,
|
|
|
|
}));
|
|
|
|
draggingProps.onDragStart(e);
|
|
|
|
},
|
|
|
|
className: classNames(
|
|
|
|
draggingProps.className,
|
|
|
|
{
|
|
|
|
'lnsDragDrop-isKeyboardModeActive': isReorderOn,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'lnsDragDrop-isReorderable': draggingProps.isReorderDragging,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
style: reorderedItems.includes(id)
|
|
|
|
? {
|
|
|
|
transform: `translateY(${direction}${draggingHeight}px)`,
|
|
|
|
}
|
|
|
|
: {},
|
|
|
|
})}
|
|
|
|
<div
|
|
|
|
data-test-subj="lnsDragDrop-reorderableDrop"
|
|
|
|
className={classNames('lnsDragDrop', {
|
|
|
|
['lnsDragDrop__reorderableDrop']: dragging && droppable,
|
|
|
|
})}
|
|
|
|
onDrop={(e) => {
|
|
|
|
dropProps.onDrop(e);
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
reorderedItems: [],
|
|
|
|
}));
|
|
|
|
}}
|
|
|
|
onDragOver={(e: DroppableEvent) => {
|
|
|
|
if (!droppable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dropProps.onDragOver(e);
|
|
|
|
if (!dropProps.isActive) {
|
|
|
|
if (!dragging) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const draggingIndex = itemsInGroup.indexOf(dragging.id);
|
|
|
|
const droppingIndex = currentIndex;
|
|
|
|
if (draggingIndex === droppingIndex) {
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
reorderedItems: [],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
setReorderState((s: ReorderState) =>
|
|
|
|
draggingIndex < droppingIndex
|
|
|
|
? {
|
|
|
|
...s,
|
|
|
|
reorderedItems: itemsInGroup.slice(draggingIndex + 1, droppingIndex + 1),
|
|
|
|
direction: '-',
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
...s,
|
|
|
|
reorderedItems: itemsInGroup.slice(droppingIndex, draggingIndex),
|
|
|
|
direction: '+',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onDragLeave={() => {
|
|
|
|
dropProps.onDragLeave();
|
|
|
|
setReorderState((s: ReorderState) => ({
|
|
|
|
...s,
|
|
|
|
reorderedItems: [],
|
|
|
|
}));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|