-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathColumnSizing.ts
594 lines (539 loc) · 21 KB
/
ColumnSizing.ts
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
import { _getVisibleLeafColumns } from '..'
import {
RowData,
Column,
Header,
OnChangeFn,
Table,
Updater,
TableFeature,
} from '../types'
import { getMemoOptions, makeStateUpdater, memo } from '../utils'
import { ColumnPinningPosition } from './ColumnPinning'
//
export interface ColumnSizingTableState {
columnSizing: ColumnSizingState
columnSizingInfo: ColumnSizingInfoState
}
export type ColumnSizingState = Record<string, number>
export interface ColumnSizingInfoState {
columnSizingStart: [string, number][]
deltaOffset: null | number
deltaPercentage: null | number
isResizingColumn: false | string
startOffset: null | number
startSize: null | number
}
export type ColumnResizeMode = 'onChange' | 'onEnd'
export type ColumnResizeDirection = 'ltr' | 'rtl'
export interface ColumnSizingOptions {
/**
* Determines when the columnSizing state is updated. `onChange` updates the state when the user is dragging the resize handle. `onEnd` updates the state when the user releases the resize handle.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#columnresizemode)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
columnResizeMode?: ColumnResizeMode
/**
* Enables or disables column resizing for the column.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#enablecolumnresizing)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
enableColumnResizing?: boolean
/**
* Enables or disables right-to-left support for resizing the column. defaults to 'ltr'.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#columnResizeDirection)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
columnResizeDirection?: ColumnResizeDirection
/**
* If provided, this function will be called with an `updaterFn` when `state.columnSizing` changes. This overrides the default internal state management, so you will also need to supply `state.columnSizing` from your own managed state.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#oncolumnsizingchange)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
onColumnSizingChange?: OnChangeFn<ColumnSizingState>
/**
* If provided, this function will be called with an `updaterFn` when `state.columnSizingInfo` changes. This overrides the default internal state management, so you will also need to supply `state.columnSizingInfo` from your own managed state.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#oncolumnsizinginfochange)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
onColumnSizingInfoChange?: OnChangeFn<ColumnSizingInfoState>
}
export type ColumnSizingDefaultOptions = Pick<
ColumnSizingOptions,
| 'columnResizeMode'
| 'onColumnSizingChange'
| 'onColumnSizingInfoChange'
| 'columnResizeDirection'
>
export interface ColumnSizingInstance {
/**
* If pinning, returns the total size of the center portion of the table by calculating the sum of the sizes of all unpinned/center leaf-columns.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getcentertotalsize)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getCenterTotalSize: () => number
/**
* Returns the total size of the left portion of the table by calculating the sum of the sizes of all left leaf-columns.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getlefttotalsize)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getLeftTotalSize: () => number
/**
* Returns the total size of the right portion of the table by calculating the sum of the sizes of all right leaf-columns.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getrighttotalsize)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getRightTotalSize: () => number
/**
* Returns the total size of the table by calculating the sum of the sizes of all leaf-columns.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#gettotalsize)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getTotalSize: () => number
/**
* Resets column sizing to its initial state. If `defaultState` is `true`, the default state for the table will be used instead of the initialValue provided to the table.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#resetcolumnsizing)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
resetColumnSizing: (defaultState?: boolean) => void
/**
* Resets column sizing info to its initial state. If `defaultState` is `true`, the default state for the table will be used instead of the initialValue provided to the table.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#resetheadersizeinfo)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
resetHeaderSizeInfo: (defaultState?: boolean) => void
/**
* Sets the column sizing state using an updater function or a value. This will trigger the underlying `onColumnSizingChange` function if one is passed to the table options, otherwise the state will be managed automatically by the table.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#setcolumnsizing)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
setColumnSizing: (updater: Updater<ColumnSizingState>) => void
/**
* Sets the column sizing info state using an updater function or a value. This will trigger the underlying `onColumnSizingInfoChange` function if one is passed to the table options, otherwise the state will be managed automatically by the table.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#setcolumnsizinginfo)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
setColumnSizingInfo: (updater: Updater<ColumnSizingInfoState>) => void
}
export interface ColumnSizingColumnDef {
/**
* Enables or disables column resizing for the column.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#enableresizing)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
enableResizing?: boolean
/**
* The maximum allowed size for the column
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#maxsize)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
maxSize?: number
/**
* The minimum allowed size for the column
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#minsize)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
minSize?: number
/**
* The desired size for the column
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#size)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
size?: number
}
export interface ColumnSizingColumn {
/**
* Returns `true` if the column can be resized.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getcanresize)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getCanResize: () => boolean
/**
* Returns `true` if the column is currently being resized.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getisresizing)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getIsResizing: () => boolean
/**
* Returns the current size of the column.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getsize)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getSize: () => number
/**
* Returns the offset measurement along the row-axis (usually the x-axis for standard tables) for the header. This is effectively a sum of the offset measurements of all preceding (left) headers in relation to the current column.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getstart)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getStart: (position?: ColumnPinningPosition | 'center') => number
/**
* Returns the offset measurement along the row-axis (usually the x-axis for standard tables) for the header. This is effectively a sum of the offset measurements of all succeeding (right) headers in relation to the current column.
*/
getAfter: (position?: ColumnPinningPosition | 'center') => number
/**
* Resets the column to its initial size.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#resetsize)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
resetSize: () => void
}
export interface ColumnSizingHeader {
/**
* Returns an event handler function that can be used to resize the header. It can be used as an:
* - `onMouseDown` handler
* - `onTouchStart` handler
*
* The dragging and release events are automatically handled for you.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getresizehandler)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getResizeHandler: (context?: Document) => (event: unknown) => void
/**
* Returns the current size of the header.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getsize)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getSize: () => number
/**
* Returns the offset measurement along the row-axis (usually the x-axis for standard tables) for the header. This is effectively a sum of the offset measurements of all preceding headers.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getstart-1)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getStart: (position?: ColumnPinningPosition) => number
/**
* Returns the offset measurement along the row-axis (usually the x-axis for standard tables) for the header. This is effectively a sum of the offset measurements of all preceding headers.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getafter-1)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
getAfter: (position?: ColumnPinningPosition) => number
}
//
export const defaultColumnSizing = {
size: 150,
minSize: 20,
maxSize: Number.MAX_SAFE_INTEGER,
}
const getDefaultColumnSizingInfoState = (): ColumnSizingInfoState => ({
startOffset: null,
startSize: null,
deltaOffset: null,
deltaPercentage: null,
isResizingColumn: false,
columnSizingStart: [],
})
export const ColumnSizing: TableFeature = {
getDefaultColumnDef: (): ColumnSizingColumnDef => {
return defaultColumnSizing
},
getInitialState: (state): ColumnSizingTableState => {
return {
columnSizing: {},
columnSizingInfo: getDefaultColumnSizingInfoState(),
...state,
}
},
getDefaultOptions: <TData extends RowData>(
table: Table<TData>
): ColumnSizingDefaultOptions => {
return {
columnResizeMode: 'onEnd',
columnResizeDirection: 'ltr',
onColumnSizingChange: makeStateUpdater('columnSizing', table),
onColumnSizingInfoChange: makeStateUpdater('columnSizingInfo', table),
}
},
createColumn: <TData extends RowData, TValue>(
column: Column<TData, TValue>,
table: Table<TData>
): void => {
column.getSize = () => {
const columnSize = table.getState().columnSizing[column.id]
return Math.min(
Math.max(
column.columnDef.minSize ?? defaultColumnSizing.minSize,
columnSize ?? column.columnDef.size ?? defaultColumnSizing.size
),
column.columnDef.maxSize ?? defaultColumnSizing.maxSize
)
}
column.getStart = memo(
position => [
position,
_getVisibleLeafColumns(table, position),
table.getState().columnSizing,
],
(position, columns) =>
columns
.slice(0, column.getIndex(position))
.reduce((sum, column) => sum + column.getSize(), 0),
getMemoOptions(table.options, 'debugColumns', 'getStart')
)
column.getAfter = memo(
position => [
position,
_getVisibleLeafColumns(table, position),
table.getState().columnSizing,
],
(position, columns) =>
columns
.slice(column.getIndex(position) + 1)
.reduce((sum, column) => sum + column.getSize(), 0),
getMemoOptions(table.options, 'debugColumns', 'getAfter')
)
column.resetSize = () => {
table.setColumnSizing(({ [column.id]: _, ...rest }) => {
return rest
})
}
column.getCanResize = () => {
return (
(column.columnDef.enableResizing ?? true) &&
(table.options.enableColumnResizing ?? true)
)
}
column.getIsResizing = () => {
return table.getState().columnSizingInfo.isResizingColumn === column.id
}
},
createHeader: <TData extends RowData, TValue>(
header: Header<TData, TValue>,
table: Table<TData>
): void => {
header.getSize = () => {
let sum = 0
const recurse = (header: Header<TData, TValue>) => {
if (header.subHeaders.length) {
header.subHeaders.forEach(recurse)
} else {
sum += header.column.getSize() ?? 0
}
}
recurse(header)
return sum
}
header.getStart = () => {
if (header.index > 0) {
const prevSiblingHeader = header.headerGroup.headers[header.index - 1]!
return prevSiblingHeader.getStart() + prevSiblingHeader.getSize()
}
return 0
}
header.getAfter = () => {
const rightIndex = header.headerGroup.headers.length - header.index - 1
if (rightIndex > 0) {
const nextSiblingHeader = header.headerGroup.headers[header.index + 1]!
return nextSiblingHeader.getAfter() + nextSiblingHeader.getSize()
}
return 0
}
header.getResizeHandler = _contextDocument => {
const column = table.getColumn(header.column.id)
const canResize = column?.getCanResize()
return (e: unknown) => {
if (!column || !canResize) {
return
}
;(e as any).persist?.()
if (isTouchStartEvent(e)) {
// lets not respond to multiple touches (e.g. 2 or 3 fingers)
if (e.touches && e.touches.length > 1) {
return
}
}
const startSize = header.getSize()
const columnSizingStart: [string, number][] = header
? header.getLeafHeaders().map(d => [d.column.id, d.column.getSize()])
: [[column.id, column.getSize()]]
const clientX = isTouchStartEvent(e)
? Math.round(e.touches[0]!.clientX)
: (e as MouseEvent).clientX
const newColumnSizing: ColumnSizingState = {}
const updateOffset = (
eventType: 'move' | 'end',
clientXPos?: number
) => {
if (typeof clientXPos !== 'number') {
return
}
table.setColumnSizingInfo(old => {
const deltaDirection =
table.options.columnResizeDirection === 'rtl' ? -1 : 1
const deltaOffset =
(clientXPos - (old?.startOffset ?? 0)) * deltaDirection
const deltaPercentage = Math.max(
deltaOffset / (old?.startSize ?? 0),
-0.999999
)
old.columnSizingStart.forEach(([columnId, headerSize]) => {
newColumnSizing[columnId] =
Math.round(
Math.max(headerSize + headerSize * deltaPercentage, 0) * 100
) / 100
})
return {
...old,
deltaOffset,
deltaPercentage,
}
})
if (
table.options.columnResizeMode === 'onChange' ||
eventType === 'end'
) {
table.setColumnSizing(old => ({
...old,
...newColumnSizing,
}))
}
}
const onMove = (clientXPos?: number) => updateOffset('move', clientXPos)
const onEnd = (clientXPos?: number) => {
updateOffset('end', clientXPos)
table.setColumnSizingInfo(old => ({
...old,
isResizingColumn: false,
startOffset: null,
startSize: null,
deltaOffset: null,
deltaPercentage: null,
columnSizingStart: [],
}))
}
const contextDocument =
_contextDocument || typeof document !== 'undefined' ? document : null
const mouseEvents = {
moveHandler: (e: MouseEvent) => onMove(e.clientX),
upHandler: (e: MouseEvent) => {
contextDocument?.removeEventListener(
'mousemove',
mouseEvents.moveHandler
)
contextDocument?.removeEventListener(
'mouseup',
mouseEvents.upHandler
)
onEnd(e.clientX)
},
}
const touchEvents = {
moveHandler: (e: TouchEvent) => {
if (e.cancelable) {
e.preventDefault()
e.stopPropagation()
}
onMove(e.touches[0]!.clientX)
return false
},
upHandler: (e: TouchEvent) => {
contextDocument?.removeEventListener(
'touchmove',
touchEvents.moveHandler
)
contextDocument?.removeEventListener(
'touchend',
touchEvents.upHandler
)
if (e.cancelable) {
e.preventDefault()
e.stopPropagation()
}
onEnd(e.touches[0]?.clientX)
},
}
const passiveIfSupported = passiveEventSupported()
? { passive: false }
: false
if (isTouchStartEvent(e)) {
contextDocument?.addEventListener(
'touchmove',
touchEvents.moveHandler,
passiveIfSupported
)
contextDocument?.addEventListener(
'touchend',
touchEvents.upHandler,
passiveIfSupported
)
} else {
contextDocument?.addEventListener(
'mousemove',
mouseEvents.moveHandler,
passiveIfSupported
)
contextDocument?.addEventListener(
'mouseup',
mouseEvents.upHandler,
passiveIfSupported
)
}
table.setColumnSizingInfo(old => ({
...old,
startOffset: clientX,
startSize,
deltaOffset: 0,
deltaPercentage: 0,
columnSizingStart,
isResizingColumn: column.id,
}))
}
}
},
createTable: <TData extends RowData>(table: Table<TData>): void => {
table.setColumnSizing = updater =>
table.options.onColumnSizingChange?.(updater)
table.setColumnSizingInfo = updater =>
table.options.onColumnSizingInfoChange?.(updater)
table.resetColumnSizing = defaultState => {
table.setColumnSizing(
defaultState ? {} : table.initialState.columnSizing ?? {}
)
}
table.resetHeaderSizeInfo = defaultState => {
table.setColumnSizingInfo(
defaultState
? getDefaultColumnSizingInfoState()
: table.initialState.columnSizingInfo ??
getDefaultColumnSizingInfoState()
)
}
table.getTotalSize = () =>
table.getHeaderGroups()[0]?.headers.reduce((sum, header) => {
return sum + header.getSize()
}, 0) ?? 0
table.getLeftTotalSize = () =>
table.getLeftHeaderGroups()[0]?.headers.reduce((sum, header) => {
return sum + header.getSize()
}, 0) ?? 0
table.getCenterTotalSize = () =>
table.getCenterHeaderGroups()[0]?.headers.reduce((sum, header) => {
return sum + header.getSize()
}, 0) ?? 0
table.getRightTotalSize = () =>
table.getRightHeaderGroups()[0]?.headers.reduce((sum, header) => {
return sum + header.getSize()
}, 0) ?? 0
},
}
let passiveSupported: boolean | null = null
export function passiveEventSupported() {
if (typeof passiveSupported === 'boolean') return passiveSupported
let supported = false
try {
const options = {
get passive() {
supported = true
return false
},
}
const noop = () => {}
window.addEventListener('test', noop, options)
window.removeEventListener('test', noop)
} catch (err) {
supported = false
}
passiveSupported = supported
return passiveSupported
}
function isTouchStartEvent(e: unknown): e is TouchEvent {
return (e as TouchEvent).type === 'touchstart'
}