Skip to content

Commit 4822641

Browse files
committed
chore: split column sizing and resizing features
1 parent 4725396 commit 4822641

File tree

11 files changed

+435
-354
lines changed

11 files changed

+435
-354
lines changed

packages/table-core/src/core/table.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { ColumnFiltering } from '../features/column-filtering/ColumnFiltering'
2828
import { ColumnGrouping } from '../features/column-grouping/ColumnGrouping'
2929
import { ColumnOrdering } from '../features/column-ordering/ColumnOrdering'
3030
import { ColumnPinning } from '../features/column-pinning/ColumnPinning'
31+
import { ColumnResizing } from '../features/column-resizing/ColumnResizing'
3132
import { ColumnSizing } from '../features/column-sizing/ColumnSizing'
3233
import { ColumnVisibility } from '../features/column-visibility/ColumnVisibility'
3334
import { GlobalFaceting } from '../features/global-faceting/GlobalFaceting'
@@ -54,6 +55,7 @@ const builtInFeatures = [
5455
RowPinning,
5556
RowSelection,
5657
ColumnSizing,
58+
ColumnResizing,
5759
] as const
5860

5961
//
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { RowData, Column, Header, Table, TableFeature } from '../../types'
2+
import { makeStateUpdater } from '../../utils'
3+
import {
4+
ColumnResizingDefaultOptions,
5+
ColumnResizingTableState,
6+
} from './ColumnResizing.types'
7+
import {
8+
column_getCanResize,
9+
column_getIsResizing,
10+
getDefaultColumnSizingInfoState,
11+
header_getResizeHandler,
12+
table_resetHeaderSizeInfo,
13+
table_setColumnSizingInfo,
14+
} from './ColumnResizing.utils'
15+
16+
export const ColumnResizing: TableFeature = {
17+
_getInitialState: (state): ColumnResizingTableState => {
18+
return {
19+
columnSizing: {},
20+
columnSizingInfo: getDefaultColumnSizingInfoState(),
21+
...state,
22+
}
23+
},
24+
25+
_getDefaultOptions: <TData extends RowData>(
26+
table: Table<TData>
27+
): ColumnResizingDefaultOptions => {
28+
return {
29+
columnResizeMode: 'onEnd',
30+
columnResizeDirection: 'ltr',
31+
onColumnSizingInfoChange: makeStateUpdater('columnSizingInfo', table),
32+
}
33+
},
34+
35+
_createColumn: <TData extends RowData, TValue>(
36+
column: Column<TData, TValue>,
37+
table: Table<TData>
38+
): void => {
39+
column.getCanResize = () => column_getCanResize(table, column)
40+
41+
column.getIsResizing = () => column_getIsResizing(table, column)
42+
},
43+
44+
_createHeader: <TData extends RowData, TValue>(
45+
header: Header<TData, TValue>,
46+
table: Table<TData>
47+
): void => {
48+
header.getResizeHandler = _contextDocument =>
49+
header_getResizeHandler(header, table, _contextDocument)
50+
},
51+
52+
_createTable: <TData extends RowData>(table: Table<TData>): void => {
53+
table.setColumnSizingInfo = updater =>
54+
table_setColumnSizingInfo(table, updater)
55+
56+
table.resetHeaderSizeInfo = defaultState =>
57+
table_resetHeaderSizeInfo(table, defaultState)
58+
},
59+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { OnChangeFn, Updater } from '../../types'
2+
3+
export interface ColumnResizingTableState {
4+
columnSizingInfo: ColumnResizingInfoState
5+
}
6+
7+
export interface ColumnResizingInfoState {
8+
columnSizingStart: [string, number][]
9+
deltaOffset: null | number
10+
deltaPercentage: null | number
11+
isResizingColumn: false | string
12+
startOffset: null | number
13+
startSize: null | number
14+
}
15+
16+
export type ColumnResizeMode = 'onChange' | 'onEnd'
17+
18+
export type ColumnResizeDirection = 'ltr' | 'rtl'
19+
20+
export interface ColumnResizingOptions {
21+
/**
22+
* 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.
23+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#columnresizemode)
24+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
25+
*/
26+
columnResizeMode?: ColumnResizeMode
27+
/**
28+
* Enables or disables column resizing for the column.
29+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#enablecolumnresizing)
30+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
31+
*/
32+
enableColumnResizing?: boolean
33+
/**
34+
* Enables or disables right-to-left support for resizing the column. defaults to 'ltr'.
35+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#columnResizeDirection)
36+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
37+
*/
38+
columnResizeDirection?: ColumnResizeDirection
39+
/**
40+
* 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.
41+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#oncolumnsizinginfochange)
42+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
43+
*/
44+
onColumnSizingInfoChange?: OnChangeFn<ColumnResizingInfoState>
45+
}
46+
47+
export type ColumnResizingDefaultOptions = Pick<
48+
ColumnResizingOptions,
49+
'columnResizeMode' | 'onColumnSizingInfoChange' | 'columnResizeDirection'
50+
>
51+
52+
export interface ColumnResizingInstance {
53+
/**
54+
* 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.
55+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#resetheadersizeinfo)
56+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
57+
*/
58+
resetHeaderSizeInfo: (defaultState?: boolean) => void
59+
/**
60+
* 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.
61+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#setcolumnsizinginfo)
62+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
63+
*/
64+
setColumnSizingInfo: (updater: Updater<ColumnResizingInfoState>) => void
65+
}
66+
67+
export interface ColumnResizingColumnDef {
68+
/**
69+
* Enables or disables column resizing for the column.
70+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#enableresizing)
71+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
72+
*/
73+
enableResizing?: boolean
74+
}
75+
76+
export interface ColumnResizingColumn {
77+
/**
78+
* Returns `true` if the column can be resized.
79+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getcanresize)
80+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
81+
*/
82+
getCanResize: () => boolean
83+
/**
84+
* Returns `true` if the column is currently being resized.
85+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getisresizing)
86+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
87+
*/
88+
getIsResizing: () => boolean
89+
}
90+
91+
export interface ColumnResizingHeader {
92+
/**
93+
* Returns an event handler function that can be used to resize the header. It can be used as an:
94+
* - `onMouseDown` handler
95+
* - `onTouchStart` handler
96+
*
97+
* The dragging and release events are automatically handled for you.
98+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#getresizehandler)
99+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
100+
*/
101+
getResizeHandler: (context?: Document) => (event: unknown) => void
102+
}

0 commit comments

Comments
 (0)