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

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 (100) hide show
  1. package/build/cjs/core.js +118 -59
  2. package/build/cjs/core.js.map +1 -1
  3. package/build/cjs/createTable.js +11 -6
  4. package/build/cjs/createTable.js.map +1 -1
  5. package/build/cjs/features/ColumnSizing.js +317 -0
  6. package/build/cjs/features/ColumnSizing.js.map +1 -0
  7. package/build/cjs/features/Expanding.js +23 -2
  8. package/build/cjs/features/Expanding.js.map +1 -1
  9. package/build/cjs/features/Filters.js +54 -5
  10. package/build/cjs/features/Filters.js.map +1 -1
  11. package/build/cjs/features/Grouping.js +23 -2
  12. package/build/cjs/features/Grouping.js.map +1 -1
  13. package/build/cjs/features/Headers.js +88 -22
  14. package/build/cjs/features/Headers.js.map +1 -1
  15. package/build/cjs/features/Ordering.js +4 -1
  16. package/build/cjs/features/Ordering.js.map +1 -1
  17. package/build/cjs/features/Pagination.js +198 -0
  18. package/build/cjs/features/Pagination.js.map +1 -0
  19. package/build/cjs/features/Pinning.js +0 -14
  20. package/build/cjs/features/Pinning.js.map +1 -1
  21. package/build/cjs/features/RowSelection.js +541 -0
  22. package/build/cjs/features/RowSelection.js.map +1 -0
  23. package/build/cjs/features/Sorting.js +76 -18
  24. package/build/cjs/features/Sorting.js.map +1 -1
  25. package/build/cjs/features/Visibility.js +8 -2
  26. package/build/cjs/features/Visibility.js.map +1 -1
  27. package/build/cjs/index.js +2 -0
  28. package/build/cjs/index.js.map +1 -1
  29. package/build/cjs/sortTypes.js +1 -0
  30. package/build/cjs/sortTypes.js.map +1 -1
  31. package/build/cjs/utils/columnFilterRowsFn.js +3 -2
  32. package/build/cjs/utils/columnFilterRowsFn.js.map +1 -1
  33. package/build/cjs/utils/expandRowsFn.js +2 -2
  34. package/build/cjs/utils/expandRowsFn.js.map +1 -1
  35. package/build/cjs/utils/globalFilterRowsFn.js +3 -2
  36. package/build/cjs/utils/globalFilterRowsFn.js.map +1 -1
  37. package/build/cjs/utils/groupRowsFn.js +4 -3
  38. package/build/cjs/utils/groupRowsFn.js.map +1 -1
  39. package/build/cjs/utils/paginateRowsFn.js +44 -0
  40. package/build/cjs/utils/paginateRowsFn.js.map +1 -0
  41. package/build/cjs/utils/sortRowsFn.js +3 -2
  42. package/build/cjs/utils/sortRowsFn.js.map +1 -1
  43. package/build/cjs/utils.js +6 -3
  44. package/build/cjs/utils.js.map +1 -1
  45. package/build/esm/index.js +1613 -225
  46. package/build/esm/index.js.map +1 -1
  47. package/build/stats-html.html +1 -1
  48. package/build/stats-react.json +363 -217
  49. package/build/types/core.d.ts +14 -25
  50. package/build/types/createTable.d.ts +21 -3
  51. package/build/types/features/ColumnSizing.d.ts +67 -0
  52. package/build/types/features/Expanding.d.ts +2 -1
  53. package/build/types/features/Filters.d.ts +7 -2
  54. package/build/types/features/Grouping.d.ts +2 -2
  55. package/build/types/features/Ordering.d.ts +1 -1
  56. package/build/types/features/Pagination.d.ts +44 -0
  57. package/build/types/features/Pinning.d.ts +3 -3
  58. package/build/types/features/RowSelection.d.ts +66 -0
  59. package/build/types/features/Sorting.d.ts +5 -2
  60. package/build/types/index.d.ts +1 -0
  61. package/build/types/sortTypes.d.ts +5 -4
  62. package/build/types/types.d.ts +13 -8
  63. package/build/types/utils/columnFilterRowsFn.d.ts +2 -2
  64. package/build/types/utils/expandRowsFn.d.ts +2 -2
  65. package/build/types/utils/globalFilterRowsFn.d.ts +2 -2
  66. package/build/types/utils/groupRowsFn.d.ts +2 -2
  67. package/build/types/utils/paginateRowsFn.d.ts +2 -0
  68. package/build/types/utils/sortRowsFn.d.ts +2 -2
  69. package/build/types/utils.d.ts +7 -3
  70. package/build/umd/index.development.js +1613 -224
  71. package/build/umd/index.development.js.map +1 -1
  72. package/build/umd/index.production.js +1 -1
  73. package/build/umd/index.production.js.map +1 -1
  74. package/package.json +1 -1
  75. package/src/core.tsx +252 -265
  76. package/src/createTable.tsx +69 -9
  77. package/src/features/ColumnSizing.ts +453 -0
  78. package/src/features/Expanding.ts +27 -11
  79. package/src/features/Filters.ts +75 -20
  80. package/src/features/Grouping.ts +27 -12
  81. package/src/features/Headers.ts +55 -50
  82. package/src/features/Ordering.ts +2 -3
  83. package/src/features/Pagination.ts +327 -0
  84. package/src/features/Pinning.ts +3 -16
  85. package/src/features/RowSelection.ts +831 -0
  86. package/src/features/Sorting.ts +82 -22
  87. package/src/features/Visibility.ts +2 -4
  88. package/src/index.tsx +1 -0
  89. package/src/sortTypes.ts +1 -1
  90. package/src/types.ts +55 -9
  91. package/src/utils/columnFilterRowsFn.ts +5 -12
  92. package/src/utils/expandRowsFn.ts +2 -5
  93. package/src/utils/globalFilterRowsFn.ts +3 -10
  94. package/src/utils/groupRowsFn.ts +3 -5
  95. package/src/utils/paginateRowsFn.ts +34 -0
  96. package/src/utils/sortRowsFn.ts +5 -5
  97. package/src/utils.tsx +20 -6
  98. package/src/features/withColumnResizing.oldts +0 -281
  99. package/src/features/withPagination.oldts +0 -208
  100. package/src/features/withRowSelection.oldts +0 -467
@@ -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
- }
@@ -1,208 +0,0 @@
1
- import React from 'react'
2
-
3
- import {
4
- functionalUpdate,
5
- expandRows,
6
- useLazyMemo,
7
- useMountedLayoutEffect,
8
- makeStateUpdater,
9
- } from '../utils'
10
-
11
- import {
12
- withPagination as name,
13
- withColumnVisibility,
14
- withColumnFilters,
15
- withGlobalFilter,
16
- withGrouping,
17
- withSorting,
18
- withExpanding,
19
- } from '../Constants'
20
- import { MakeInstance, GetDefaultOptions } from '../types'
21
-
22
- const getDefaultOptions: GetDefaultOptions = options => {
23
- return {
24
- onPageIndexChange: React.useCallback(makeStateUpdater('pageIndex'), []),
25
- onPageSizeChange: React.useCallback(makeStateUpdater('pageSize'), []),
26
- onPageCountChange: React.useCallback(makeStateUpdater('pageCount'), []),
27
- autoResetPageIndex: true,
28
- paginateExpandedRows: true,
29
- ...options,
30
- initialState: {
31
- pageIndex: 0,
32
- pageSize: 10,
33
- pageCount: 0,
34
- ...options.initialState,
35
- },
36
- }
37
- }
38
-
39
- const extendInstance: MakeInstance = instance => {
40
- const pageResetDeps = [
41
- instance.options.manualPagination ? null : instance.options.data,
42
- instance.state.globalFilter,
43
- instance.state.columnFilters,
44
- instance.state.grouping,
45
- instance.state.sorting,
46
- ]
47
- React.useMemo(() => {
48
- if (instance.options.autoResetPageIndex) {
49
- instance.state.pageIndex = instance.options.initialState?.pageIndex ?? 0
50
- }
51
- // eslint-disable-next-line react-hooks/exhaustive-deps
52
- }, pageResetDeps)
53
-
54
- useMountedLayoutEffect(() => {
55
- if (instance.options.autoResetPageIndex) {
56
- instance.resetPageIndex?.()
57
- }
58
- }, pageResetDeps)
59
-
60
- instance.setPageIndex = React.useCallback(
61
- updater =>
62
- instance.options.onPageIndexChange?.(old => {
63
- const newPageIndex = functionalUpdate(updater, old)
64
- const pageCount = instance.getPageCount?.() ?? 0
65
- const maxPageIndex =
66
- pageCount > 0 ? pageCount - 1 : Number.MAX_SAFE_INTEGER
67
-
68
- return Math.min(Math.max(0, newPageIndex), maxPageIndex)
69
- }, instance),
70
- [instance]
71
- )
72
-
73
- instance.setPageSize = React.useCallback(
74
- updater =>
75
- instance.options.onPageSizeChange?.(old => {
76
- const newPageSize = Math.max(1, functionalUpdate(updater, old))
77
- const topRowIndex = old! * instance.state.pageIndex!
78
- const pageIndex = Math.floor(topRowIndex / newPageSize)
79
-
80
- if (instance.state.pageIndex !== pageIndex) {
81
- instance.setPageIndex?.(pageIndex)
82
- }
83
-
84
- return newPageSize
85
- }, instance),
86
- [instance]
87
- )
88
-
89
- instance.setPageCount = React.useCallback(
90
- updater =>
91
- instance.options.onPageCountChange?.(
92
- old => Math.max(0, functionalUpdate(updater, old)),
93
- instance
94
- ),
95
- [instance]
96
- )
97
-
98
- instance.resetPageIndex = React.useCallback(
99
- () =>
100
- instance.setPageIndex?.(instance.options.initialState?.pageIndex ?? 0),
101
- [instance]
102
- )
103
-
104
- instance.resetPageSize = React.useCallback(
105
- () => instance.setPageSize?.(instance.options.initialState?.pageSize ?? 10),
106
- [instance]
107
- )
108
-
109
- instance.resetPageCount = React.useCallback(
110
- () =>
111
- instance.setPageCount?.(instance.options.initialState?.pageCount ?? 0),
112
- [instance]
113
- )
114
-
115
- instance.getPageCount = React.useCallback(
116
- () =>
117
- instance.options.manualPagination
118
- ? instance.state.pageCount!
119
- : instance.rows
120
- ? Math.ceil(instance.rows.length / instance.state.pageSize!)
121
- : -1,
122
- [
123
- instance.options.manualPagination,
124
- instance.rows,
125
- instance.state.pageCount,
126
- instance.state.pageSize,
127
- ]
128
- )
129
-
130
- const pageCount = instance.getPageCount?.()
131
-
132
- instance.getPageOptions = useLazyMemo(() => {
133
- let pageOptions: number[] = []
134
- if (pageCount > 0) {
135
- pageOptions = [...new Array(pageCount)].fill(null).map((_, i) => i)
136
- }
137
- return pageOptions
138
- }, [pageCount])
139
-
140
- instance.getPageRows = React.useCallback(() => {
141
- const {
142
- rows,
143
- state: { pageIndex, pageSize },
144
- options: { manualPagination },
145
- } = instance
146
- let page
147
-
148
- if (manualPagination) {
149
- page = rows
150
- } else {
151
- const pageStart = pageSize! * pageIndex!
152
- const pageEnd = pageStart + pageSize!
153
-
154
- page = rows.slice(pageStart, pageEnd)
155
- }
156
-
157
- if (instance.options.paginateExpandedRows) {
158
- return page
159
- }
160
-
161
- return expandRows(page, instance)
162
- }, [instance])
163
-
164
- instance.getCanPreviousPage = React.useCallback(
165
- () => instance.state.pageIndex! > 0,
166
- [instance.state.pageIndex]
167
- )
168
-
169
- instance.getCanNextPage = React.useCallback(() => {
170
- const {
171
- state: { pageSize, pageIndex },
172
- getPageRows,
173
- getPageCount,
174
- } = instance
175
-
176
- const pageCount = getPageCount?.() ?? 0
177
-
178
- return pageCount === 0
179
- ? (getPageRows?.().length ?? 0) >= pageSize!
180
- : pageIndex! < (pageCount ?? 0) - 1
181
- }, [instance])
182
-
183
- instance.gotoPreviousPage = React.useCallback(() => {
184
- return instance.setPageIndex?.(old => old! - 1)
185
- }, [instance])
186
-
187
- instance.gotoNextPage = React.useCallback(() => {
188
- return instance.setPageIndex?.(old => old! + 1)
189
- }, [instance])
190
-
191
- return instance
192
- }
193
-
194
- export const withPagination = {
195
- name,
196
- after: [
197
- withColumnVisibility,
198
- withColumnFilters,
199
- withGlobalFilter,
200
- withGrouping,
201
- withSorting,
202
- withExpanding,
203
- ],
204
- plugs: {
205
- getDefaultOptions,
206
- extendInstance,
207
- },
208
- }