[Lens] fix selection when dragging (#93034)

This commit is contained in:
Marta Bondyra 2021-03-02 11:08:18 +01:00 committed by GitHub
parent d7cac22ba2
commit 657c273866
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View file

@ -76,6 +76,20 @@ describe('DragDrop', () => {
expect(preventDefault).not.toBeCalled();
});
test('removes selection on mouse down before dragging', async () => {
const removeAllRanges = jest.fn();
global.getSelection = jest.fn(() => (({ removeAllRanges } as unknown) as Selection));
const component = mount(
<DragDrop value={value} draggable={true} order={[2, 0, 1, 0]}>
<button>Hi!</button>
</DragDrop>
);
component.find('[data-test-subj="lnsDragDrop"]').simulate('mousedown');
expect(global.getSelection).toBeCalled();
expect(removeAllRanges).toBeCalled();
});
test('dragstart sets dragging in the context and calls it with proper params', async () => {
const setDragging = jest.fn();

View file

@ -202,6 +202,13 @@ export const DragDrop = (props: BaseProps) => {
return <DropInner {...dropProps} />;
};
const removeSelectionBeforeDragging = () => {
const selection = window.getSelection();
if (selection) {
selection.removeAllRanges();
}
};
const DragInner = memo(function DragInner({
dataTestSubj,
className,
@ -366,6 +373,7 @@ const DragInner = memo(function DragInner({
draggable: true,
onDragEnd: dragEnd,
onDragStart: dragStart,
onMouseDown: removeSelectionBeforeDragging,
})}
</div>
);