@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,196 +0,0 @@
1
- import { ReactTable, Row, RowModel } from '../types'
2
- import { Options } from '../types'
3
- import { GroupingState } from '../features/Grouping'
4
- import { flattenBy } from '../utils'
5
-
6
- export const groupRowsFn: Options<any, any, {}, {}, {}>['groupRowsFn'] = <
7
- TData,
8
- TValue,
9
- TFilterFns,
10
- TSortingFns,
11
- TAggregationFns
12
- >(
13
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
14
- groupingState: GroupingState,
15
- sortedRowModel: RowModel<
16
- TData,
17
- TValue,
18
- TFilterFns,
19
- TSortingFns,
20
- TAggregationFns
21
- >
22
- ): RowModel<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> => {
23
- // Filter the grouping list down to columns that exist
24
- const existingGrouping = groupingState.filter(columnId =>
25
- instance.getColumn(columnId)
26
- )
27
-
28
- // Find the columns that can or are aggregating
29
- // Uses each column to aggregate rows into a single value
30
- const aggregateRowsToValues = (
31
- leafRows: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[],
32
- groupedRows: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[],
33
- depth: number
34
- ) => {
35
- const values: Record<string, unknown> = {}
36
-
37
- instance.getAllLeafColumns().forEach(column => {
38
- // Don't aggregate columns that are in the grouping
39
- if (existingGrouping.includes(column.id)) {
40
- values[column.id] = groupedRows[0]
41
- ? groupedRows[0].values[column.id]
42
- : null
43
- return
44
- }
45
-
46
- // Aggregate the values
47
- const aggregateFn = instance.getColumnAggregationFn(column.id)
48
-
49
- if (aggregateFn) {
50
- // Get the columnValues to aggregate
51
- const groupedValues = groupedRows.map(row => row.values[column.id])
52
-
53
- // Get the columnValues to aggregate
54
- const leafValues = leafRows.map(row => {
55
- let columnValue = row.values[column.id]
56
-
57
- if (!depth && column.aggregateValue) {
58
- columnValue = column.aggregateValue(columnValue)
59
- }
60
-
61
- return columnValue
62
- })
63
-
64
- values[column.id] = aggregateFn(leafValues, groupedValues)
65
- } else if (column.aggregationType) {
66
- console.info({ column })
67
- throw new Error(
68
- process.env.NODE_ENV !== 'production'
69
- ? `React Table: Invalid column.aggregateType option for column listed above`
70
- : ''
71
- )
72
- } else {
73
- values[column.id] = null
74
- }
75
- })
76
-
77
- return values
78
- }
79
-
80
- const groupedFlatRows: Row<
81
- TData,
82
- TValue,
83
- TFilterFns,
84
- TSortingFns,
85
- TAggregationFns
86
- >[] = []
87
- const groupedRowsById: Record<
88
- string,
89
- Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
90
- > = {}
91
- // const onlyGroupedFlatRows: Row[] = [];
92
- // const onlyGroupedRowsById: Record<RowId, Row> = {};
93
- // const nonGroupedFlatRows: Row[] = [];
94
- // const nonGroupedRowsById: Record<RowId, Row> = {};
95
-
96
- // Recursively group the data
97
- const groupUpRecursively = (
98
- rows: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[],
99
- depth = 0,
100
- parentId: string
101
- ) => {
102
- // This is the last level, just return the rows
103
- if (depth === existingGrouping.length) {
104
- return rows
105
- }
106
-
107
- const columnId = existingGrouping[depth]!
108
-
109
- // Group the rows together for this level
110
- const rowGroupsMap = groupBy(rows, columnId)
111
-
112
- // Peform aggregations for each group
113
- const aggregatedGroupedRows = Array.from(rowGroupsMap.entries()).map(
114
- ([groupingValue, groupedRows], index) => {
115
- let id = `${columnId}:${groupingValue}`
116
- id = parentId ? `${parentId}>${id}` : id
117
-
118
- // First, Recurse to group sub rows before aggregation
119
- const subRows = groupUpRecursively(groupedRows, depth + 1, id)
120
-
121
- // Flatten the leaf rows of the rows in this group
122
- const leafRows = depth
123
- ? flattenBy(groupedRows, row => row.leafRows)
124
- : groupedRows
125
-
126
- const values = aggregateRowsToValues(leafRows, groupedRows, depth)
127
-
128
- const row = instance.createRow(id, undefined, index, depth, values)
129
-
130
- Object.assign(row, {
131
- groupingColumnId: columnId,
132
- groupingValue,
133
- subRows,
134
- leafRows,
135
- })
136
-
137
- subRows.forEach(subRow => {
138
- groupedFlatRows.push(subRow)
139
- groupedRowsById[subRow.id] = subRow
140
- // if (subRow.getIsGrouped?.()) {
141
- // onlyGroupedFlatRows.push(subRow);
142
- // onlyGroupedRowsById[subRow.id] = subRow;
143
- // } else {
144
- // nonGroupedFlatRows.push(subRow);
145
- // nonGroupedRowsById[subRow.id] = subRow;
146
- // }
147
- })
148
-
149
- return row
150
- }
151
- )
152
-
153
- return aggregatedGroupedRows
154
- }
155
-
156
- const groupedRows = groupUpRecursively(sortedRowModel.rows, 0, '')
157
-
158
- groupedRows.forEach(subRow => {
159
- groupedFlatRows.push(subRow)
160
- groupedRowsById[subRow.id] = subRow
161
- // if (subRow.getIsGrouped?.()) {
162
- // onlyGroupedFlatRows.push(subRow);
163
- // onlyGroupedRowsById[subRow.id] = subRow;
164
- // } else {
165
- // nonGroupedFlatRows.push(subRow);
166
- // nonGroupedRowsById[subRow.id] = subRow;
167
- // }
168
- })
169
-
170
- return {
171
- rows: groupedRows,
172
- flatRows: groupedFlatRows,
173
- rowsById: groupedRowsById,
174
- }
175
- }
176
-
177
- function groupBy<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>(
178
- rows: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[],
179
- columnId: string
180
- ) {
181
- const groupMap = new Map<
182
- any,
183
- Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[]
184
- >()
185
-
186
- return rows.reduce((map, row) => {
187
- const resKey = `${row.values[columnId]}`
188
- const previous = map.get(resKey)
189
- if (!previous) {
190
- map.set(resKey, [row])
191
- } else {
192
- map.set(resKey, [...previous, row])
193
- }
194
- return map
195
- }, groupMap)
196
- }
@@ -1,115 +0,0 @@
1
- import { ReactTable, Row, RowModel } from '../types'
2
- import { SortingFn, SortingState } from '../features/Sorting'
3
- import { Options } from '../types'
4
-
5
- export const sortRowsFn: Options<any, any, {}, {}, {}>['sortRowsFn'] = <
6
- TData,
7
- TValue,
8
- TFilterFns,
9
- TSortingFns,
10
- TAggregationFns
11
- >(
12
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
13
- sortingState: SortingState,
14
- rowModel: RowModel<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
15
- ): RowModel<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> => {
16
- const sortedFlatRows: Row<
17
- TData,
18
- TValue,
19
- TFilterFns,
20
- TSortingFns,
21
- TAggregationFns
22
- >[] = []
23
-
24
- // Filter out sortings that correspond to non existing columns
25
- const availableSorting = sortingState.filter(sort =>
26
- instance.getColumnCanSort(sort.id)
27
- )
28
-
29
- const columnInfoById: Record<
30
- string,
31
- {
32
- sortUndefined?: false | -1 | 1
33
- invertSorting?: boolean
34
- sortingFn: SortingFn<
35
- TData,
36
- TValue,
37
- TFilterFns,
38
- TSortingFns,
39
- TAggregationFns
40
- >
41
- }
42
- > = {}
43
-
44
- availableSorting.forEach(sortEntry => {
45
- const column = instance.getColumn(sortEntry.id)!
46
-
47
- columnInfoById[sortEntry.id] = {
48
- sortUndefined: column.sortUndefined,
49
- invertSorting: column.invertSorting,
50
- sortingFn: instance.getColumnSortingFn(sortEntry.id)!,
51
- }
52
- })
53
-
54
- const sortData = (
55
- rows: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[]
56
- ) => {
57
- // This will also perform a stable sorting using the row index
58
- // if needed.
59
- const sortedData = rows.slice()
60
-
61
- sortedData.sort((rowA, rowB) => {
62
- for (let i = 0; i < availableSorting.length; i += 1) {
63
- const sortEntry = availableSorting[i]!
64
- const columnInfo = columnInfoById[sortEntry.id]!
65
- const isDesc = sortEntry?.desc ?? false
66
-
67
- if (columnInfo.sortUndefined) {
68
- const aValue = rowA.values[sortEntry.id]
69
- const bValue = rowB.values[sortEntry.id]
70
-
71
- const aUndefined = typeof aValue === 'undefined'
72
- const bUndefined = typeof bValue === 'undefined'
73
-
74
- if (aUndefined || bUndefined) {
75
- return aUndefined && bUndefined ? 0 : aUndefined ? 1 : -1
76
- }
77
- }
78
-
79
- // This function should always return in ascending order
80
- let sortInt = columnInfo.sortingFn(rowA, rowB, sortEntry.id)
81
-
82
- if (sortInt !== 0) {
83
- if (isDesc) {
84
- sortInt *= -1
85
- }
86
-
87
- if (columnInfo.invertSorting) {
88
- sortInt *= -1
89
- }
90
-
91
- return sortInt
92
- }
93
- }
94
-
95
- return rowA.index - rowB.index
96
- })
97
-
98
- // If there are sub-rows, sort them
99
- sortedData.forEach(row => {
100
- sortedFlatRows.push(row)
101
- if (!row.subRows || row.subRows.length <= 1) {
102
- return
103
- }
104
- row.subRows = sortData(row.subRows)
105
- })
106
-
107
- return sortedData
108
- }
109
-
110
- return {
111
- rows: sortData(rowModel.rows),
112
- flatRows: sortedFlatRows,
113
- rowsById: rowModel.rowsById,
114
- }
115
- }
package/src/utils.tsx DELETED
@@ -1,243 +0,0 @@
1
- import React from 'react'
2
- import { Getter, NoInfer, PropGetterValue, Renderable } from './types'
3
-
4
- export type IsAny<T> = 0 extends 1 & T ? true : false
5
- export type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
6
- export type RequiredKeys<T, K extends keyof T> = Omit<T, K> &
7
- Required<Pick<T, K>>
8
- export type Overwrite<T, U> = Omit<T, keyof U> & U
9
-
10
- export type DataUpdateFunction<TInput, TOutput> = (input: TInput) => TOutput
11
-
12
- export type Updater<TInput, TOutput> =
13
- | TOutput
14
- | DataUpdateFunction<TInput, TOutput>
15
-
16
- export function functionalUpdate<TInput, TOutput = TInput>(
17
- updater: Updater<TInput, TOutput>,
18
- input: TInput
19
- ): TOutput {
20
- return typeof updater === 'function'
21
- ? (updater as DataUpdateFunction<TInput, TOutput>)(input)
22
- : updater
23
- }
24
-
25
- export function noop() {
26
- //
27
- }
28
-
29
- export function makeStateUpdater(key: string, instance: unknown) {
30
- return (updater: Updater<any, any>) => {
31
- ;(instance as any).setState(<TTableState,>(old: TTableState) => {
32
- return {
33
- ...old,
34
- [key]: functionalUpdate(updater, (old as any)[key]),
35
- }
36
- })
37
- }
38
- }
39
-
40
- // SSR has issues with useLayoutEffect still, so use useEffect during SSR
41
- export const safeUseLayoutEffect =
42
- typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect
43
-
44
- export function useMountedLayoutEffect(fn: any, deps: any[]) {
45
- const mountedRef = React.useRef(false)
46
-
47
- safeUseLayoutEffect(() => {
48
- if (mountedRef.current) {
49
- fn()
50
- }
51
- mountedRef.current = true
52
- // eslint-disable-next-line
53
- }, deps)
54
- }
55
-
56
- export function useGetLatest<T>(obj: T): () => T {
57
- const ref = React.useRef<T>()
58
- ref.current = obj
59
-
60
- return React.useCallback(() => ref.current!, [])
61
- }
62
-
63
- type AnyFunction = (...args: any) => any
64
-
65
- export function isFunction<T extends AnyFunction>(d: any): d is T {
66
- return d instanceof Function
67
- }
68
-
69
- export function flattenBy<TNode>(
70
- arr: TNode[],
71
- getChildren: (item: TNode) => TNode[]
72
- ) {
73
- const flat: TNode[] = []
74
-
75
- const recurse = (subArr: TNode[]) => {
76
- subArr.forEach(item => {
77
- flat.push(item)
78
- const children = getChildren(item)
79
- if (children?.length) {
80
- recurse(children)
81
- }
82
- })
83
- }
84
-
85
- recurse(arr)
86
-
87
- return flat
88
- }
89
-
90
- type PropGetterImpl = <TBaseProps, TGetter extends Getter<TBaseProps>>(
91
- initial: TBaseProps,
92
- userProps?: TGetter
93
- ) => PropGetterValue<TBaseProps, TGetter>
94
-
95
- // @ts-ignore // Just rely on the type, not the implementation
96
- export const propGetter: PropGetterImpl = (initial, getter) => {
97
- if (isFunction(getter)) {
98
- return getter(initial)
99
- }
100
-
101
- return {
102
- ...initial,
103
- ...(getter ?? {}),
104
- }
105
- }
106
-
107
- export function memo<TDeps extends readonly any[], TResult>(
108
- getDeps: () => [...TDeps],
109
- fn: (...args: NoInfer<[...TDeps]>) => TResult,
110
- key?: string,
111
- debug?: boolean
112
- ): () => TResult {
113
- let deps: any[] = []
114
- let result: TResult | undefined
115
-
116
- return () => {
117
- const newDeps = getDeps()
118
- const newSerializedDeps = newDeps
119
- const oldSerializedDeps = deps
120
-
121
- const depsChanged =
122
- newSerializedDeps.length !== oldSerializedDeps.length ||
123
- newSerializedDeps.some(
124
- (dep: any, index: number) => oldSerializedDeps[index] !== dep
125
- )
126
-
127
- if (depsChanged) {
128
- if (debug) {
129
- console.info(key, {
130
- length: `${oldSerializedDeps.length} -> ${newSerializedDeps.length}`,
131
- ...newSerializedDeps
132
- .map((_, index) => {
133
- if (oldSerializedDeps[index] !== newSerializedDeps[index]) {
134
- return [
135
- index,
136
- oldSerializedDeps[index],
137
- newSerializedDeps[index],
138
- ]
139
- }
140
-
141
- return false
142
- })
143
- .filter(Boolean)
144
- .reduce(
145
- (accu, curr: any) => ({
146
- ...accu,
147
- [curr[0]]: curr.slice(1),
148
- }),
149
- {}
150
- ),
151
- parent,
152
- })
153
- }
154
- result = fn(...newDeps)
155
- deps = newSerializedDeps
156
- }
157
-
158
- return result!
159
- }
160
- }
161
-
162
- // Copied from: https://github.com/jonschlinkert/is-plain-object
163
- export function isPlainObject(o: any): o is Object {
164
- if (!hasObjectPrototype(o)) {
165
- return false
166
- }
167
-
168
- // If has modified constructor
169
- const ctor = o.constructor
170
- if (typeof ctor === 'undefined') {
171
- return true
172
- }
173
-
174
- // If has modified prototype
175
- const prot = ctor.prototype
176
- if (!hasObjectPrototype(prot)) {
177
- return false
178
- }
179
-
180
- // If constructor does not have an Object-specific method
181
- if (!prot.hasOwnProperty('isPrototypeOf')) {
182
- return false
183
- }
184
-
185
- // Most likely a plain Object
186
- return true
187
- }
188
-
189
- function hasObjectPrototype(o: any): boolean {
190
- return Object.prototype.toString.call(o) === '[object Object]'
191
- }
192
-
193
- export type Render = typeof flexRender
194
-
195
- export function flexRender<TProps extends {}>(
196
- Comp: Renderable<TProps>,
197
- props: TProps
198
- ): React.ReactNode {
199
- return !Comp ? null : isReactComponent(Comp) ? <Comp {...props} /> : Comp
200
- }
201
-
202
- function isReactComponent(component: unknown): component is React.FC {
203
- return (
204
- isClassComponent(component) ||
205
- typeof component === 'function' ||
206
- isExoticComponent(component)
207
- )
208
- }
209
-
210
- function isClassComponent(component: any) {
211
- return (
212
- typeof component === 'function' &&
213
- (() => {
214
- const proto = Object.getPrototypeOf(component)
215
- return proto.prototype && proto.prototype.isReactComponent
216
- })()
217
- )
218
- }
219
-
220
- function isExoticComponent(component: any) {
221
- return (
222
- typeof component === 'object' &&
223
- typeof component.$$typeof === 'symbol' &&
224
- ['react.memo', 'react.forward_ref'].includes(component.$$typeof.description)
225
- )
226
- }
227
-
228
- // export function hashString(str: string, seed = 0): string {
229
- // let h1 = 0xdeadbeef ^ seed,
230
- // h2 = 0x41c6ce57 ^ seed
231
- // for (let i = 0, ch; i < str.length; i++) {
232
- // ch = str.charCodeAt(i)
233
- // h1 = Math.imul(h1 ^ ch, 2654435761)
234
- // h2 = Math.imul(h2 ^ ch, 1597334677)
235
- // }
236
- // h1 =
237
- // Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^
238
- // Math.imul(h2 ^ (h2 >>> 13), 3266489909)
239
- // h2 =
240
- // Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^
241
- // Math.imul(h1 ^ (h1 >>> 13), 3266489909)
242
- // return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString()
243
- // }