Skip to content

Commit 6854aae

Browse files
authored
Fix double-click to auto-size columns (#3744)
* Fix double-click to auto-size columns * fix tests * fix types * add back comment
1 parent eacbe61 commit 6854aae

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/HeaderCell.tsx

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react';
1+
import { useRef, useState } from 'react';
22
import { css } from '@linaria/core';
33

44
import { useRovingTabIndex } from './hooks';
@@ -85,6 +85,7 @@ export default function HeaderCell<R, SR>({
8585
direction,
8686
dragDropKey
8787
}: HeaderCellProps<R, SR>) {
88+
const hasDoubleClickedRef = useRef(false);
8889
const [isDragging, setIsDragging] = useState(false);
8990
const [isOver, setIsOver] = useState(false);
9091
const isRtl = direction === 'rtl';
@@ -119,7 +120,7 @@ export default function HeaderCell<R, SR>({
119120
const headerCell = currentTarget.parentElement!;
120121
const { right, left } = headerCell.getBoundingClientRect();
121122
const offset = isRtl ? event.clientX - left : right - event.clientX;
122-
let hasDoubleClicked = false;
123+
hasDoubleClickedRef.current = false;
123124

124125
function onPointerMove(event: PointerEvent) {
125126
const { width, right, left } = headerCell.getBoundingClientRect();
@@ -130,29 +131,27 @@ export default function HeaderCell<R, SR>({
130131
}
131132
}
132133

133-
function onDoubleClick() {
134-
hasDoubleClicked = true;
135-
onColumnResize(column, 'max-content');
136-
}
137-
138134
function onLostPointerCapture(event: PointerEvent) {
139135
// Handle final pointer position that may have been skipped by coalesced pointer move events.
140136
// Skip move pointer handling if the user double-clicked.
141-
if (!hasDoubleClicked) {
137+
if (!hasDoubleClickedRef.current) {
142138
onPointerMove(event);
143139
}
144140

145141
currentTarget.removeEventListener('pointermove', onPointerMove);
146-
currentTarget.removeEventListener('dblclick', onDoubleClick);
147142
currentTarget.removeEventListener('lostpointercapture', onLostPointerCapture);
148143
}
149144

150145
currentTarget.setPointerCapture(pointerId);
151146
currentTarget.addEventListener('pointermove', onPointerMove);
152-
currentTarget.addEventListener('dblclick', onDoubleClick);
153147
currentTarget.addEventListener('lostpointercapture', onLostPointerCapture);
154148
}
155149

150+
function onDoubleClick() {
151+
hasDoubleClickedRef.current = true;
152+
onColumnResize(column, 'max-content');
153+
}
154+
156155
function onSort(ctrlClick: boolean) {
157156
if (onSortColumnsChange == null) return;
158157
const { sortDescendingFirst } = column;
@@ -304,6 +303,7 @@ export default function HeaderCell<R, SR>({
304303
className={resizeHandleClassname}
305304
onClick={stopPropagation}
306305
onPointerDown={onPointerDown}
306+
onDoubleClick={onDoubleClick}
307307
/>
308308
)}
309309
</div>

test/browser/column/resizable.test.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { commands, userEvent } from '@vitest/browser/context';
1+
import { commands, page } from '@vitest/browser/context';
22

33
import type { Column } from '../../../src';
44
import { resizeHandleClassname } from '../../../src/HeaderCell';
@@ -36,9 +36,9 @@ async function resize({ column, resizeBy }: ResizeArgs) {
3636
}
3737

3838
async function autoResize(column: Element) {
39-
const resizeHandle = getResizeHandle(column);
39+
const resizeHandle = page.elementLocator(getResizeHandle(column));
4040

41-
await userEvent.dblClick(resizeHandle);
41+
await resizeHandle.dblClick();
4242
}
4343

4444
const columns: readonly Column<Row>[] = [

tsconfig.website.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "./tsconfig.base.json",
33
"compilerOptions": {
4-
"lib": ["ESNext", "DOM", "DOM.Iterable", "DOM.AsyncIterable"]
4+
"lib": ["ESNext", "DOM", "DOM.Iterable", "DOM.AsyncIterable"],
5+
"skipLibCheck": true
56
},
67
"include": ["website/**/*"],
78
"references": [{ "path": "tsconfig.src.json" }]

0 commit comments

Comments
 (0)