@tanstack/react-table 0.0.1-alpha.8 → 8.0.0-alpha.11

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 (61) hide show
  1. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +34 -0
  2. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
  3. package/build/cjs/index.js +85 -0
  4. package/build/cjs/index.js.map +1 -0
  5. package/build/esm/index.js +73 -0
  6. package/build/esm/index.js.map +1 -0
  7. package/build/stats-html.html +2689 -0
  8. package/build/stats-react.json +99 -0
  9. package/build/types/index.d.ts +3 -0
  10. package/build/umd/index.development.js +105 -0
  11. package/build/umd/index.development.js.map +1 -0
  12. package/build/umd/index.production.js +12 -0
  13. package/build/umd/index.production.js.map +1 -0
  14. package/package.json +12 -94
  15. package/src/index.tsx +56 -7
  16. package/dist/react-table.development.js +0 -3406
  17. package/dist/react-table.development.js.map +0 -1
  18. package/dist/react-table.production.min.js +0 -2
  19. package/dist/react-table.production.min.js.map +0 -1
  20. package/lib/index.js +0 -65
  21. package/src/aggregationTypes.ts +0 -115
  22. package/src/core.tsx +0 -1194
  23. package/src/createTable.tsx +0 -181
  24. package/src/features/Expanding.ts +0 -388
  25. package/src/features/Filters.ts +0 -707
  26. package/src/features/Grouping.ts +0 -451
  27. package/src/features/Headers.ts +0 -907
  28. package/src/features/Ordering.ts +0 -134
  29. package/src/features/Pinning.ts +0 -213
  30. package/src/features/Sorting.ts +0 -487
  31. package/src/features/Visibility.ts +0 -281
  32. package/src/features/notest/useAbsoluteLayout.test.js +0 -152
  33. package/src/features/notest/useBlockLayout.test.js +0 -158
  34. package/src/features/notest/useColumnOrder.test.js +0 -186
  35. package/src/features/notest/useExpanded.test.js +0 -125
  36. package/src/features/notest/useFilters.test.js +0 -393
  37. package/src/features/notest/useFiltersAndRowSelect.test.js +0 -256
  38. package/src/features/notest/useFlexLayout.test.js +0 -152
  39. package/src/features/notest/useGroupBy.test.js +0 -259
  40. package/src/features/notest/usePagination.test.js +0 -231
  41. package/src/features/notest/useResizeColumns.test.js +0 -229
  42. package/src/features/notest/useRowSelect.test.js +0 -250
  43. package/src/features/notest/useRowState.test.js +0 -178
  44. package/src/features/tests/Visibility.test.tsx +0 -225
  45. package/src/features/tests/__snapshots__/Visibility.test.tsx.snap +0 -390
  46. package/src/features/tests/withSorting.notest.tsx +0 -341
  47. package/src/features/withColumnResizing.ts +0 -281
  48. package/src/features/withPagination.ts +0 -208
  49. package/src/features/withRowSelection.ts +0 -467
  50. package/src/filterTypes.ts +0 -251
  51. package/src/sortTypes.ts +0 -159
  52. package/src/test-utils/makeTestData.ts +0 -41
  53. package/src/tests/__snapshots__/core.test.tsx.snap +0 -148
  54. package/src/tests/core.test.tsx +0 -241
  55. package/src/types.ts +0 -285
  56. package/src/utils/columnFilterRowsFn.ts +0 -162
  57. package/src/utils/expandRowsFn.ts +0 -53
  58. package/src/utils/globalFilterRowsFn.ts +0 -129
  59. package/src/utils/groupRowsFn.ts +0 -196
  60. package/src/utils/sortRowsFn.ts +0 -115
  61. package/src/utils.tsx +0 -243
@@ -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
- }