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

fix(explore): cannot reorder dnd of Metrics #28269

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { OptionControlLabel } from 'src/explore/components/controls/OptionContro

import ExploreContainer, { DraggingContext, DropzoneContext } from '.';
import OptionWrapper from '../controls/DndColumnSelectControl/OptionWrapper';
import DatasourcePanelDragOption from '../DatasourcePanel/DatasourcePanelDragOption';
import { DndItemType } from '../DndItemType';

const MockChildren = () => {
const dragging = React.useContext(DraggingContext);
Expand Down Expand Up @@ -61,7 +63,7 @@ test('should render children', () => {
expect(getByText('not dragging')).toBeInTheDocument();
});

test('should propagate dragging state', () => {
test('should only propagate dragging state when dragging the panel option', () => {
const defaultProps = {
label: <span>Test label</span>,
tooltipTitle: 'This is a tooltip title',
Expand All @@ -73,15 +75,19 @@ test('should propagate dragging state', () => {
};
const { container, getByText } = render(
<ExploreContainer>
<DatasourcePanelDragOption
value={{ metric_name: 'panel option' }}
type={DndItemType.Metric}
/>
<OptionControlLabel
{...defaultProps}
index={1}
label={<span>Label 1</span>}
label={<span>Metric item</span>}
/>
<OptionWrapper
{...defaultProps}
index={2}
label="Label 2"
label="Column item"
clickClose={() => {}}
onShiftOptions={() => {}}
/>
Expand All @@ -93,12 +99,15 @@ test('should propagate dragging state', () => {
},
);
expect(container.getElementsByClassName('dragging')).toHaveLength(0);
fireEvent.dragStart(getByText('Label 1'));
fireEvent.dragStart(getByText('panel option'));
expect(container.getElementsByClassName('dragging')).toHaveLength(1);
fireEvent.dragEnd(getByText('Label 1'));
fireEvent.dragEnd(getByText('panel option'));
fireEvent.dragStart(getByText('Metric item'));
expect(container.getElementsByClassName('dragging')).toHaveLength(0);
fireEvent.dragEnd(getByText('Metric item'));
expect(container.getElementsByClassName('dragging')).toHaveLength(0);
// don't show dragging state for the sorting item
fireEvent.dragStart(getByText('Label 2'));
fireEvent.dragStart(getByText('Column item'));
expect(container.getElementsByClassName('dragging')).toHaveLength(0);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export const AddIconButton = styled.button`
`;

interface DragItem {
index: number;
dragIndex: number;
type: string;
}

Expand Down Expand Up @@ -287,7 +287,7 @@ export const OptionControlLabel = ({
if (!ref.current) {
return;
}
const dragIndex = item.index;
const { dragIndex } = item;
const hoverIndex = index;
// Don't replace items with themselves
if (dragIndex === hoverIndex) {
Expand Down Expand Up @@ -322,13 +322,13 @@ export const OptionControlLabel = ({
// but it's good here for the sake of performance
// to avoid expensive index searches.
// eslint-disable-next-line no-param-reassign
item.index = hoverIndex;
item.dragIndex = hoverIndex;
},
});
const [{ isDragging }, drag] = useDrag({
item: {
type,
index,
dragIndex: index,
value: savedMetric?.metric_name ? savedMetric : adhocMetric,
},
collect: monitor => ({
Expand Down
Loading