-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathuseColumnResizing.ts
More file actions
300 lines (261 loc) · 10.5 KB
/
useColumnResizing.ts
File metadata and controls
300 lines (261 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import type { MouseEvent, TouchEvent } from 'react';
import { useCallback } from 'react';
import { actions, defaultColumn, makePropGetter, useGetLatest, useMountedLayoutEffect } from 'react-table';
import type { ColumnType, ReactTableHooks, TableInstance } from '../types/index.js';
// Default Column
defaultColumn.canResize = true;
// Actions
actions.columnStartResizing = 'columnStartResizing';
actions.columnResizing = 'columnResizing';
actions.columnDoneResizing = 'columnDoneResizing';
actions.resetResize = 'resetResize';
/**
* Computes the projected column width after a resize drag.
*
* Uses the same percentage-based formula as react-table's `columnResizing` reducer:
* the drag delta is expressed as a percentage of the original column width, then
* applied multiplicatively. RTL inverts the delta direction.
*/
export function getProjectedWidth(rawDeltaX: number, originalWidth: number, isRtl: boolean): number {
const deltaX = isRtl ? -rawDeltaX : rawDeltaX;
const percentageDeltaX = deltaX / originalWidth;
return Math.max(originalWidth + originalWidth * percentageDeltaX, 0);
}
/**
* UI5WCR custom column resizing plugin — a fork of react-table v7's useResizeColumns hook.
* Original source: https://github.com/TanStack/table/blob/v7/src/plugin-hooks/useResizeColumns.js
*
* This is a fork of react-table's `useResizeColumns` with the following changes:
* - Deferred resize: CSS transform feedback during drag (zero renders), single state dispatch on mouseup
* - RTL delta inversion inlined into the reducer (previously handled in `stateReducer.ts`)
* - Removed `columnWidth` from reducer state destructuring — uses per-header width via `getProjectedWidth`
* - Removed `ensurePluginOrder` check for `useAbsoluteLayout` (not used by AnalyticalTable)
* - Removed `getLeafHeaders` (AnalyticalTable does not support grouped column headers)
* - Moved `getHeaderProps` inline `position: relative` style to `.th` in `AnalyticalTable.module.css`
* - Added `e.preventDefault()` on mousedown to prevent text selection in Firefox during drag
* - Split mouse/touch into separate branches (touch needs `{ passive: false }` for `preventDefault()`)
* - Added 3px dead zone and `data-active-resizer` attribute for double-click compatibility
* - Removed `cursor: col-resize` style (AnalyticalTable provides its own resizer styling)
* - Clamped resize width to `minWidth`/`maxWidth` in both the visual drag and the reducer (original clamps to 0)
* - Fixed falsy `0` bug: replaced `||` with `??` in `useInstanceBeforeDimensions` width assignment
*/
export const useColumnResizing = (hooks: ReactTableHooks) => {
// UI5WCR: replaced default RAF-throttled getResizerProps with deferred version
hooks.getResizerProps = [deferredGetResizerProps];
// UI5WCR: removed getHeaderProps push for `position: relative` — moved to CSS
hooks.stateReducers.push(reducer);
hooks.useInstance.push(useInstance);
hooks.useInstanceBeforeDimensions.push(useInstanceBeforeDimensions);
};
useColumnResizing.pluginName = 'useColumnResizing';
const deferredGetResizerProps: ReactTableHooks['getResizerProps'][0] = (props, { instance, header }) => {
const { dispatch } = instance;
const startResize = (resizerElement: HTMLDivElement, startClientX: number, isTouchEvent: boolean) => {
// UI5WCR: use header directly instead of getLeafHeaders (no grouped columns)
const headerIdWidths = [[header.id, header.totalWidth, header.minWidth, header.maxWidth]];
const columnWidth = header.totalWidth;
const minWidth = header.minWidth;
const maxWidth = header.maxWidth;
const columnId = header.id;
const isRtl = instance.state.isRtl;
dispatch({
type: actions.columnStartResizing,
columnId,
columnWidth,
headerIdWidths,
clientX: startClientX,
});
// UI5WCR: capture original transform so it can be restored on mouseup
const originalTransform = resizerElement.style.transform;
// UI5WCR: data attribute replaces :active (which stops when cursor leaves the 5px resizer)
resizerElement.dataset.activeResizer = '';
let deltaX = 0;
let isDragging = false;
const finishResize = () => {
delete resizerElement.dataset.activeResizer;
// UI5WCR: restore transform to keep resizer centered (prevents double-click detection issues)
resizerElement.style.transform = originalTransform;
// UI5WCR: only dispatch columnResizing if user dragged past dead zone
if (isDragging) {
dispatch({ type: actions.columnResizing, clientX: startClientX + deltaX });
}
dispatch({ type: actions.columnDoneResizing });
};
// UI5WCR: 3px dead zone prevents transform shifts during double-click sequences
const applyDrag = (clientX: number) => {
deltaX = clientX - startClientX;
// UI5WCR: clamp so resizer can't be dragged past minWidth/maxWidth boundary
if (isRtl) {
deltaX = Math.max(deltaX, columnWidth - maxWidth);
deltaX = Math.min(deltaX, columnWidth - minWidth);
} else {
deltaX = Math.max(deltaX, minWidth - columnWidth);
deltaX = Math.min(deltaX, maxWidth - columnWidth);
}
if (!isDragging && Math.abs(deltaX) < 3) {
return;
}
isDragging = true;
resizerElement.style.transform = `${originalTransform} translateX(${deltaX}px)`;
};
// UI5WCR: separate mouse/touch branches (touch needs { passive: false } for preventDefault)
if (isTouchEvent) {
const handleTouchMove = (moveEvent: globalThis.TouchEvent) => {
if (moveEvent.cancelable) {
moveEvent.preventDefault();
moveEvent.stopPropagation();
}
applyDrag(moveEvent.touches[0].clientX);
};
const handleTouchEnd = () => {
document.removeEventListener('touchmove', handleTouchMove);
document.removeEventListener('touchend', handleTouchEnd);
finishResize();
};
document.addEventListener('touchmove', handleTouchMove, { passive: false });
document.addEventListener('touchend', handleTouchEnd, { passive: false });
} else {
const handleMouseMove = (moveEvent: globalThis.MouseEvent) => {
applyDrag(moveEvent.clientX);
};
const handleMouseUp = () => {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
finishResize();
};
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
}
};
return {
...props,
role: 'separator',
draggable: false,
'aria-hidden': 'true',
onMouseDown: (e: MouseEvent<HTMLDivElement>) => {
// UI5WCR: prevent text selection during drag in Firefox
e.preventDefault();
startResize(e.currentTarget, e.clientX, false);
},
onTouchStart: (e: TouchEvent<HTMLDivElement>) => {
// lets not respond to multiple touches (e.g. 2 or 3 fingers)
if (e.touches && e.touches.length > 1) {
return;
}
startResize(e.currentTarget, Math.round(e.touches[0].clientX), true);
},
};
};
const reducer: TableInstance['stateReducer'] = (state, action) => {
if (action.type === actions.init) {
return {
columnResizing: {
columnWidths: {},
},
...state,
};
}
if (action.type === actions.resetResize) {
return {
...state,
columnResizing: {
columnWidths: {},
},
};
}
if (action.type === actions.columnStartResizing) {
const { clientX, columnId, columnWidth, headerIdWidths } = action;
return {
...state,
columnResizing: {
...state.columnResizing,
startX: clientX,
headerIdWidths,
columnWidth,
isResizingColumn: columnId,
},
};
}
if (action.type === actions.columnResizing) {
const { clientX } = action;
// UI5WCR: removed `columnWidth` from destructuring — uses per-header width via getProjectedWidth
const { startX, headerIdWidths = [] } = state.columnResizing;
// UI5WCR: RTL delta inversion inlined (previously in stateReducer.ts)
const rawDeltaX = clientX - startX;
const newColumnWidths: Record<string, number> = {};
headerIdWidths.forEach(
([headerId, headerWidth, headerMinWidth, headerMaxWidth]: [string, number, number, number]) => {
// UI5WCR: clamp to minWidth/maxWidth (original only clamps to 0)
const projected = getProjectedWidth(rawDeltaX, headerWidth, state.isRtl);
newColumnWidths[headerId] = Math.min(Math.max(projected, headerMinWidth), headerMaxWidth);
},
);
return {
...state,
columnResizing: {
...state.columnResizing,
columnWidths: {
...state.columnResizing.columnWidths,
...newColumnWidths,
},
},
};
}
if (action.type === actions.columnDoneResizing) {
return {
...state,
columnResizing: {
...state.columnResizing,
startX: null,
isResizingColumn: null,
},
};
}
};
// Replaces react-table's internal `getFirstDefined` from `utils.js` (not publicly exported)
function getFirstDefined<T>(...args: (T | undefined)[]): T | undefined {
for (let i = 0; i < args.length; i += 1) {
if (typeof args[i] !== 'undefined') {
return args[i];
}
}
}
const useInstanceBeforeDimensions = (instance: TableInstance) => {
const {
flatHeaders,
disableResizing,
getHooks,
state: { columnResizing },
} = instance;
const getInstance = useGetLatest(instance);
flatHeaders.forEach((header: ColumnType) => {
const canResize = getFirstDefined(
header.disableResizing === true ? false : undefined,
disableResizing === true ? false : undefined,
true,
);
header.canResize = canResize;
// UI5WCR: use ?? instead of || (original uses ||, which treats 0 as falsy)
header.width = columnResizing.columnWidths[header.id] ?? header.originalWidth ?? header.width;
header.isResizing = columnResizing.isResizingColumn === header.id;
if (canResize) {
header.getResizerProps = makePropGetter(getHooks().getResizerProps, {
instance: getInstance(),
header,
});
}
});
};
function useInstance(instance: TableInstance) {
const { dispatch, autoResetResize = true, columns } = instance;
const getAutoResetResize = useGetLatest(autoResetResize);
useMountedLayoutEffect(() => {
if (getAutoResetResize()) {
dispatch({ type: actions.resetResize });
}
}, [columns]);
const resetResizing = useCallback(() => dispatch({ type: actions.resetResize }), [dispatch]);
Object.assign(instance, {
resetResizing,
});
}