diff --git a/playwright/tests/cell.spec.js b/playwright/tests/cell.spec.js index 2be9a2c..3dd357a 100644 --- a/playwright/tests/cell.spec.js +++ b/playwright/tests/cell.spec.js @@ -108,3 +108,30 @@ test.describe('Cell focus in a sorted table', () => { expect(await page.locator('.dt-scrollable').evaluate(el => el.scrollTop)).toBe(0); }); }); + +test.describe('Cell mouse selection', () => { + test('stops selecting when the mouse is released outside the table', async ({ page }) => { + const center = async (col, row) => { + const box = await getCell(page, col, row).boundingBox(); + return [box.x + box.width / 2, box.y + box.height / 2]; + }; + const highlighted = page.locator('.dt-cell--highlight'); + + await page.mouse.move(...await center(2, 1)); + await page.mouse.down(); + await page.mouse.move(...await center(3, 2), { steps: 5 }); + await expect(highlighted).toHaveCount(4 + 2); + + const table = await page.locator('.datatable').first().boundingBox(); + const [, y] = await center(3, 2); + await page.mouse.move(table.x + table.width + 40, y, { steps: 5 }); + await page.mouse.up(); + const selected = await highlighted.count(); + + await page.mouse.move(...await center(5, 4), { steps: 5 }); + await page.waitForTimeout(200); + await page.mouse.move(...await center(6, 5), { steps: 5 }); + await page.waitForTimeout(200); + await expect(highlighted).toHaveCount(selected); + }); +}); diff --git a/src/cellmanager.js b/src/cellmanager.js index 77bec1b..9d4f613 100644 --- a/src/cellmanager.js +++ b/src/cellmanager.js @@ -171,8 +171,12 @@ export default class CellManager { this.focusCell($(e.delegatedTarget)); }); - $.on(this.bodyScrollable, 'mouseup', () => { + const onMouseup = () => { mouseDown = false; + }; + $.on(document, 'mouseup', onMouseup); + this.instance.on('onDestroy', () => { + $.off(document, 'mouseup', onMouseup); }); if (this.options.showTotalRow) {