@tanstack/react-table 8.0.0-alpha.1 → 8.0.0-alpha.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -78
  2. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
  3. package/build/cjs/index.js +96 -12
  4. package/build/cjs/index.js.map +1 -1
  5. package/build/esm/index.js +40 -3330
  6. package/build/esm/index.js.map +1 -1
  7. package/build/stats-html.html +1 -1
  8. package/build/stats-react.json +28 -632
  9. package/build/types/index.d.ts +13 -7
  10. package/build/umd/index.development.js +50 -3332
  11. package/build/umd/index.development.js.map +1 -1
  12. package/build/umd/index.production.js +1 -1
  13. package/build/umd/index.production.js.map +1 -1
  14. package/package.json +4 -1
  15. package/src/index.tsx +105 -7
  16. package/build/cjs/aggregationTypes.js +0 -130
  17. package/build/cjs/aggregationTypes.js.map +0 -1
  18. package/build/cjs/core.js +0 -493
  19. package/build/cjs/core.js.map +0 -1
  20. package/build/cjs/createTable.js +0 -103
  21. package/build/cjs/createTable.js.map +0 -1
  22. package/build/cjs/features/Expanding.js +0 -234
  23. package/build/cjs/features/Expanding.js.map +0 -1
  24. package/build/cjs/features/Filters.js +0 -396
  25. package/build/cjs/features/Filters.js.map +0 -1
  26. package/build/cjs/features/Grouping.js +0 -228
  27. package/build/cjs/features/Grouping.js.map +0 -1
  28. package/build/cjs/features/Headers.js +0 -483
  29. package/build/cjs/features/Headers.js.map +0 -1
  30. package/build/cjs/features/Ordering.js +0 -83
  31. package/build/cjs/features/Ordering.js.map +0 -1
  32. package/build/cjs/features/Pinning.js +0 -163
  33. package/build/cjs/features/Pinning.js.map +0 -1
  34. package/build/cjs/features/Sorting.js +0 -269
  35. package/build/cjs/features/Sorting.js.map +0 -1
  36. package/build/cjs/features/Visibility.js +0 -160
  37. package/build/cjs/features/Visibility.js.map +0 -1
  38. package/build/cjs/filterTypes.js +0 -172
  39. package/build/cjs/filterTypes.js.map +0 -1
  40. package/build/cjs/sortTypes.js +0 -121
  41. package/build/cjs/sortTypes.js.map +0 -1
  42. package/build/cjs/utils/columnFilterRowsFn.js +0 -130
  43. package/build/cjs/utils/columnFilterRowsFn.js.map +0 -1
  44. package/build/cjs/utils/expandRowsFn.js +0 -38
  45. package/build/cjs/utils/expandRowsFn.js.map +0 -1
  46. package/build/cjs/utils/globalFilterRowsFn.js +0 -100
  47. package/build/cjs/utils/globalFilterRowsFn.js.map +0 -1
  48. package/build/cjs/utils/groupRowsFn.js +0 -154
  49. package/build/cjs/utils/groupRowsFn.js.map +0 -1
  50. package/build/cjs/utils/sortRowsFn.js +0 -93
  51. package/build/cjs/utils/sortRowsFn.js.map +0 -1
  52. package/build/cjs/utils.js +0 -143
  53. package/build/cjs/utils.js.map +0 -1
  54. package/build/types/aggregationTypes.d.ts +0 -22
  55. package/build/types/core.d.ts +0 -122
  56. package/build/types/createTable.d.ts +0 -35
  57. package/build/types/features/Expanding.d.ts +0 -52
  58. package/build/types/features/Filters.d.ts +0 -93
  59. package/build/types/features/Grouping.d.ts +0 -82
  60. package/build/types/features/Headers.d.ts +0 -41
  61. package/build/types/features/Ordering.d.ts +0 -19
  62. package/build/types/features/Pinning.d.ts +0 -39
  63. package/build/types/features/Sorting.d.ts +0 -75
  64. package/build/types/features/Visibility.d.ts +0 -47
  65. package/build/types/filterTypes.d.ts +0 -50
  66. package/build/types/sortTypes.d.ts +0 -17
  67. package/build/types/types.d.ts +0 -122
  68. package/build/types/utils/columnFilterRowsFn.d.ts +0 -2
  69. package/build/types/utils/expandRowsFn.d.ts +0 -2
  70. package/build/types/utils/globalFilterRowsFn.d.ts +0 -2
  71. package/build/types/utils/groupRowsFn.d.ts +0 -2
  72. package/build/types/utils/sortRowsFn.d.ts +0 -2
  73. package/build/types/utils.d.ts +0 -24
  74. package/src/aggregationTypes.ts +0 -115
  75. package/src/core.tsx +0 -1194
  76. package/src/createTable.tsx +0 -181
  77. package/src/features/Expanding.ts +0 -388
  78. package/src/features/Filters.ts +0 -707
  79. package/src/features/Grouping.ts +0 -451
  80. package/src/features/Headers.ts +0 -907
  81. package/src/features/Ordering.ts +0 -134
  82. package/src/features/Pinning.ts +0 -213
  83. package/src/features/Sorting.ts +0 -487
  84. package/src/features/Visibility.ts +0 -281
  85. package/src/features/withColumnResizing.oldts +0 -281
  86. package/src/features/withPagination.oldts +0 -208
  87. package/src/features/withRowSelection.oldts +0 -467
  88. package/src/filterTypes.ts +0 -251
  89. package/src/sortTypes.ts +0 -159
  90. package/src/types.ts +0 -285
  91. package/src/utils/columnFilterRowsFn.ts +0 -162
  92. package/src/utils/expandRowsFn.ts +0 -53
  93. package/src/utils/globalFilterRowsFn.ts +0 -129
  94. package/src/utils/groupRowsFn.ts +0 -196
  95. package/src/utils/sortRowsFn.ts +0 -115
  96. package/src/utils.tsx +0 -243
@@ -1,281 +0,0 @@
1
- import {
2
- Cell,
3
- Column,
4
- Getter,
5
- OnChangeFn,
6
- PropGetterValue,
7
- ReactTable,
8
- Updater,
9
- } from '../types'
10
- import { functionalUpdate, makeStateUpdater, memo, propGetter } from '../utils'
11
-
12
- export type VisibilityOptions = {
13
- onColumnVisibilityChange?: OnChangeFn<VisibilityState>
14
- enableHiding?: boolean
15
- }
16
-
17
- export type VisibilityDefaultOptions = {
18
- onColumnVisibilityChange: OnChangeFn<VisibilityState>
19
- }
20
-
21
- export type VisibilityState = Record<string, boolean>
22
-
23
- export type VisibilityTableState = {
24
- columnVisibility: VisibilityState
25
- }
26
-
27
- export type VisibilityInstance<
28
- TData,
29
- TValue,
30
- TFilterFns,
31
- TSortingFns,
32
- TAggregationFns
33
- > = {
34
- getVisibleFlatColumns: () => Column<
35
- TData,
36
- TValue,
37
- TFilterFns,
38
- TSortingFns,
39
- TAggregationFns
40
- >[]
41
- getVisibleLeafColumns: () => Column<
42
- TData,
43
- TValue,
44
- TFilterFns,
45
- TSortingFns,
46
- TAggregationFns
47
- >[]
48
- setColumnVisibility: (updater: Updater<VisibilityState>) => void
49
- toggleColumnVisibility: (columnId: string, value?: boolean) => void
50
- toggleAllColumnsVisible: (value?: boolean) => void
51
- getColumnIsVisible: (columId: string) => boolean
52
- getColumnCanHide: (columnId: string) => boolean
53
- getIsAllColumnsVisible: () => boolean
54
- getIsSomeColumnsVisible: () => boolean
55
- getToggleAllColumnsVisibilityProps: <
56
- TGetter extends Getter<ToggleAllColumnsVisibilityProps>
57
- >(
58
- userProps?: TGetter
59
- ) => undefined | PropGetterValue<ToggleAllColumnsVisibilityProps, TGetter>
60
- }
61
-
62
- type ToggleVisibilityProps = {}
63
- type ToggleAllColumnsVisibilityProps = {}
64
-
65
- export type VisibilityColumnDef = {
66
- enableHiding?: boolean
67
- defaultCanHide?: boolean
68
- }
69
-
70
- export type VisibilityRow<
71
- TData,
72
- TValue,
73
- TFilterFns,
74
- TSortingFns,
75
- TAggregationFns
76
- > = {
77
- getVisibleCells: () => Cell<
78
- TData,
79
- TValue,
80
- TFilterFns,
81
- TSortingFns,
82
- TAggregationFns
83
- >[]
84
- }
85
-
86
- export type VisibilityColumn = {
87
- getCanHide: () => boolean
88
- getIsVisible: () => boolean
89
- toggleVisibility: (value?: boolean) => void
90
- getToggleVisibilityProps: <TGetter extends Getter<ToggleVisibilityProps>>(
91
- userProps?: TGetter
92
- ) => PropGetterValue<ToggleVisibilityProps, TGetter>
93
- }
94
-
95
- //
96
-
97
- export function getInitialState(): VisibilityTableState {
98
- return {
99
- columnVisibility: {},
100
- }
101
- }
102
-
103
- export function getDefaultOptions<
104
- TData,
105
- TValue,
106
- TFilterFns,
107
- TSortingFns,
108
- TAggregationFns
109
- >(
110
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
111
- ): VisibilityDefaultOptions {
112
- return {
113
- onColumnVisibilityChange: makeStateUpdater('columnVisibility', instance),
114
- }
115
- }
116
-
117
- export function getDefaultColumn() {
118
- return {
119
- defaultIsVisible: true,
120
- }
121
- }
122
-
123
- export function createColumn<
124
- TData,
125
- TValue,
126
- TFilterFns,
127
- TSortingFns,
128
- TAggregationFns
129
- >(
130
- column: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
131
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
132
- ): VisibilityColumn {
133
- return {
134
- getCanHide: () => instance.getColumnCanHide(column.id),
135
- getIsVisible: () => instance.getColumnIsVisible(column.id),
136
- toggleVisibility: value =>
137
- instance.toggleColumnVisibility(column.id, value),
138
- getToggleVisibilityProps: userProps => {
139
- const props: ToggleVisibilityProps = {
140
- type: 'checkbox',
141
- checked: column.getIsVisible?.(),
142
- title: 'Toggle Column Visibility',
143
- onChange: (e: MouseEvent | TouchEvent) => {
144
- column.toggleVisibility?.((e.target as HTMLInputElement).checked)
145
- },
146
- }
147
-
148
- return propGetter(props, userProps)
149
- },
150
- }
151
- }
152
-
153
- export function getInstance<
154
- TData,
155
- TValue,
156
- TFilterFns,
157
- TSortingFns,
158
- TAggregationFns
159
- >(
160
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
161
- ): VisibilityInstance<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
162
- return {
163
- getVisibleFlatColumns: memo(
164
- () => [
165
- instance.getAllFlatColumns(),
166
- instance
167
- .getAllFlatColumns()
168
- .filter(d => d.getIsVisible?.())
169
- .map(d => d.id)
170
- .join('_'),
171
- ],
172
- allFlatColumns => {
173
- return allFlatColumns.filter(d => d.getIsVisible?.())
174
- },
175
- 'getVisibleFlatColumns',
176
- instance.options.debug
177
- ),
178
-
179
- getVisibleLeafColumns: memo(
180
- () => [
181
- instance.getAllLeafColumns(),
182
- instance
183
- .getAllLeafColumns()
184
- .filter(d => d.getIsVisible?.())
185
- .map(d => d.id)
186
- .join('_'),
187
- ],
188
- allFlatColumns => {
189
- return allFlatColumns.filter(d => d.getIsVisible?.())
190
- },
191
- 'getVisibleLeafColumns',
192
- instance.options.debug
193
- ),
194
-
195
- setColumnVisibility: updater =>
196
- instance.options.onColumnVisibilityChange?.(
197
- updater,
198
- functionalUpdate(updater, instance.getState().columnVisibility)
199
- ),
200
-
201
- toggleColumnVisibility: (columnId, value) => {
202
- if (!columnId) return
203
-
204
- if (instance.getColumnCanHide(columnId)) {
205
- instance.setColumnVisibility(old => ({
206
- ...old,
207
- [columnId]: value ?? !instance.getColumnIsVisible(columnId),
208
- }))
209
- }
210
- },
211
-
212
- toggleAllColumnsVisible: value => {
213
- value = value ?? !instance.getIsAllColumnsVisible()
214
-
215
- instance.setColumnVisibility(
216
- instance.getAllLeafColumns().reduce(
217
- (obj, column) => ({
218
- ...obj,
219
- [column.id]: !value ? !column.getCanHide?.() : value,
220
- }),
221
- {}
222
- )
223
- )
224
- },
225
-
226
- getColumnIsVisible: columnId => {
227
- const column = instance.getColumn(columnId)
228
-
229
- if (!column) {
230
- throw new Error()
231
- }
232
-
233
- return (
234
- instance.getState().columnVisibility?.[columnId] ??
235
- column.defaultIsVisible ??
236
- true
237
- )
238
- },
239
-
240
- getColumnCanHide: columnId => {
241
- const column = instance.getColumn(columnId)
242
-
243
- if (!column) {
244
- throw new Error()
245
- }
246
-
247
- return (
248
- instance.options.enableHiding ??
249
- column.enableHiding ??
250
- column.defaultCanHide ??
251
- true
252
- )
253
- },
254
-
255
- getIsAllColumnsVisible: () =>
256
- !instance.getAllLeafColumns().some(column => !column.getIsVisible?.()),
257
-
258
- getIsSomeColumnsVisible: () =>
259
- instance.getAllLeafColumns().some(column => column.getIsVisible?.()),
260
-
261
- getToggleAllColumnsVisibilityProps: userProps => {
262
- const props: ToggleAllColumnsVisibilityProps = {
263
- onChange: (e: MouseEvent) => {
264
- instance.toggleAllColumnsVisible(
265
- (e.target as HTMLInputElement)?.checked
266
- )
267
- },
268
- type: 'checkbox',
269
- title: 'Toggle visibility for all columns',
270
- checked: instance.getIsAllColumnsVisible(),
271
- indeterminate:
272
- !instance.getIsAllColumnsVisible() &&
273
- instance.getIsSomeColumnsVisible()
274
- ? 'indeterminate'
275
- : undefined,
276
- }
277
-
278
- return propGetter(props, userProps)
279
- },
280
- }
281
- }
@@ -1,281 +0,0 @@
1
- import React, {
2
- ComponentProps,
3
- MouseEvent as ReactMouseEvent,
4
- PropsWithoutRef,
5
- PropsWithRef,
6
- TouchEvent as ReactTouchEvent,
7
- } from 'react'
8
-
9
- import { getLeafHeaders, makeStateUpdater } from '../utils'
10
-
11
- import { withColumnResizing as name, withColumnVisibility } from '../Constants'
12
- import {
13
- ColumnId,
14
- ColumnResizing,
15
- ReduceColumn,
16
- ReduceHeader,
17
- Header,
18
- MakeInstance,
19
- GetDefaultOptions,
20
- } from '../types'
21
-
22
- let passiveSupported: boolean | null = null
23
-
24
- export function passiveEventSupported() {
25
- // memoize support to avoid adding multiple test events
26
- if (typeof passiveSupported === 'boolean') return passiveSupported
27
-
28
- let supported = false
29
- try {
30
- const options = {
31
- get passive() {
32
- supported = true
33
- return false
34
- },
35
- }
36
-
37
- window.addEventListener('test', noop, options)
38
- window.removeEventListener('test', noop)
39
- } catch (err) {
40
- supported = false
41
- }
42
- passiveSupported = supported
43
- return passiveSupported
44
- }
45
-
46
- const getDefaultOptions: GetDefaultOptions = options => {
47
- return {
48
- onColumnResizingChange: React.useCallback(
49
- makeStateUpdater('columnResizing'),
50
- []
51
- ),
52
- ...options,
53
- initialState: {
54
- columnResizing: {
55
- columnWidths: {},
56
- },
57
- ...options.initialState,
58
- },
59
- }
60
- }
61
-
62
- const extendInstance: MakeInstance = instance => {
63
- instance.setColumnResizing = React.useCallback(
64
- updater => instance.options.onColumnResizingChange?.(updater, instance),
65
- [instance]
66
- )
67
-
68
- instance.getColumnCanResize = React.useCallback(
69
- columnId => {
70
- const column = instance.allColumns.find(d => d.id === columnId)
71
-
72
- if (!column) return false
73
-
74
- return (
75
- (column.disableResizing ? false : undefined) ??
76
- (instance.options?.disableResizing ? false : undefined) ??
77
- column.defaultCanResize ??
78
- true
79
- )
80
- },
81
- [instance.allColumns, instance.options.disableResizing]
82
- )
83
-
84
- instance.getColumnWidth = React.useCallback(
85
- columnId => {
86
- if (!columnId) return 0
87
- const { allColumns } = instance
88
- const column = allColumns.find(d => d.id === columnId)
89
- const columnWidths = instance.state.columnResizing?.columnWidths
90
-
91
- if (!column) return 0
92
-
93
- return Math.min(
94
- Math.max(
95
- column?.minWidth ?? 0,
96
- (columnWidths?.[columnId] ?? 0) || (column.width ?? 0)
97
- ),
98
- column.maxWidth ?? 0
99
- )
100
- },
101
- [instance]
102
- )
103
-
104
- const isResizingColumn = instance.state.columnResizing?.isResizingColumn
105
- instance.getColumnIsResizing = React.useCallback(
106
- columnId => {
107
- return isResizingColumn === columnId
108
- },
109
- [isResizingColumn]
110
- )
111
-
112
- return instance
113
- }
114
-
115
- const reduceColumn: ReduceColumn = (column, { instance }) => {
116
- column.getIsResizing = () => instance.getColumnIsResizing(column.id)
117
- column.getCanResize = () => instance.getColumnCanResize(column.id)
118
-
119
- return column
120
- }
121
-
122
- const reduceHeader: ReduceHeader = (header, { instance }) => {
123
- header.getIsResizing = () =>
124
- header?.column?.getIsResizing ? header.column.getIsResizing() : true
125
- header.getCanResize = () =>
126
- header?.column?.getCanResize ? header.column.getCanResize() : true
127
-
128
- function isTouchStartEvent(
129
- e: ReactTouchEvent | ReactMouseEvent
130
- ): e is ReactTouchEvent {
131
- return e.type === 'touchstart'
132
- }
133
-
134
- header.getResizerProps = (props = {}) => {
135
- const onResizeStart = (
136
- e: ReactMouseEvent | ReactTouchEvent,
137
- header: Header
138
- ) => {
139
- if (isTouchStartEvent(e)) {
140
- // lets not respond to multiple touches (e.g. 2 or 3 fingers)
141
- if (e.touches && e.touches.length > 1) {
142
- return
143
- }
144
- }
145
- const headersToResize = getLeafHeaders(header)
146
- const headerIdWidths: [ColumnId, number][] = headersToResize.map(d => [
147
- d.id,
148
- d.getWidth(),
149
- ])
150
-
151
- const clientX = isTouchStartEvent(e)
152
- ? Math.round(e.touches[0].clientX)
153
- : e.clientX
154
-
155
- const onMove = (clientXPos?: number) => {
156
- if (typeof clientXPos !== 'number') {
157
- return
158
- }
159
-
160
- return instance.setColumnResizing(old => {
161
- const deltaX = clientXPos - (old?.startX ?? 0)
162
- const percentageDeltaX = Math.max(
163
- deltaX / (old?.columnWidth ?? 0),
164
- -0.999999
165
- )
166
-
167
- const newColumnWidths: ColumnResizing['columnWidths'] = {}
168
- ;(old?.headerIdWidths ?? []).forEach(([headerId, headerWidth]) => {
169
- newColumnWidths[headerId] = Math.max(
170
- headerWidth + headerWidth * percentageDeltaX,
171
- 0
172
- )
173
- })
174
-
175
- return {
176
- ...old,
177
- columnWidths: {
178
- ...(old?.columnWidths ?? {}),
179
- ...newColumnWidths,
180
- },
181
- }
182
- })
183
- }
184
-
185
- const onEnd = () =>
186
- instance.setColumnResizing(old => ({
187
- ...old,
188
- startX: null,
189
- isResizingColumn: false,
190
- }))
191
-
192
- const mouseEvents = {
193
- moveHandler: (e: MouseEvent) => onMove(e.clientX),
194
- upHandler: () => {
195
- document.removeEventListener('mousemove', mouseEvents.moveHandler)
196
- document.removeEventListener('mouseup', mouseEvents.upHandler)
197
- onEnd()
198
- },
199
- }
200
-
201
- const touchEvents = {
202
- moveHandler: (e: TouchEvent) => {
203
- if (e.cancelable) {
204
- e.preventDefault()
205
- e.stopPropagation()
206
- }
207
- onMove(e.touches[0].clientX)
208
- return false
209
- },
210
- upHandler: () => {
211
- document.removeEventListener('touchmove', touchEvents.moveHandler)
212
- document.removeEventListener('touchend', touchEvents.upHandler)
213
- onEnd()
214
- },
215
- }
216
-
217
- const passiveIfSupported = passiveEventSupported()
218
- ? { passive: false }
219
- : false
220
-
221
- if (isTouchStartEvent(e)) {
222
- document.addEventListener(
223
- 'touchmove',
224
- touchEvents.moveHandler,
225
- passiveIfSupported
226
- )
227
- document.addEventListener(
228
- 'touchend',
229
- touchEvents.upHandler,
230
- passiveIfSupported
231
- )
232
- } else {
233
- document.addEventListener(
234
- 'mousemove',
235
- mouseEvents.moveHandler,
236
- passiveIfSupported
237
- )
238
- document.addEventListener(
239
- 'mouseup',
240
- mouseEvents.upHandler,
241
- passiveIfSupported
242
- )
243
- }
244
-
245
- instance.setColumnResizing(old => ({
246
- ...old,
247
- startX: clientX,
248
- headerIdWidths,
249
- columnWidth: header.getWidth(),
250
- isResizingColumn: header.id,
251
- }))
252
- }
253
-
254
- return {
255
- onMouseDown: (e: ReactMouseEvent) => {
256
- e.persist()
257
- onResizeStart(e, header)
258
- },
259
- onTouchStart: (e: ReactTouchEvent) => {
260
- e.persist()
261
- onResizeStart(e, header)
262
- },
263
- draggable: false,
264
- role: 'separator',
265
- ...props,
266
- }
267
- }
268
-
269
- return header
270
- }
271
-
272
- export const withColumnResizing = {
273
- name,
274
- after: [withColumnVisibility],
275
- plugs: {
276
- getDefaultOptions,
277
- extendInstance,
278
- reduceColumn,
279
- reduceHeader,
280
- },
281
- }