@tanstack/table-core 8.0.0-alpha.10

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 (112) hide show
  1. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +112 -0
  2. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
  3. package/build/cjs/aggregationTypes.js +130 -0
  4. package/build/cjs/aggregationTypes.js.map +1 -0
  5. package/build/cjs/core.js +545 -0
  6. package/build/cjs/core.js.map +1 -0
  7. package/build/cjs/createTable.js +65 -0
  8. package/build/cjs/createTable.js.map +1 -0
  9. package/build/cjs/features/ColumnSizing.js +316 -0
  10. package/build/cjs/features/ColumnSizing.js.map +1 -0
  11. package/build/cjs/features/Expanding.js +238 -0
  12. package/build/cjs/features/Expanding.js.map +1 -0
  13. package/build/cjs/features/Filters.js +413 -0
  14. package/build/cjs/features/Filters.js.map +1 -0
  15. package/build/cjs/features/Grouping.js +227 -0
  16. package/build/cjs/features/Grouping.js.map +1 -0
  17. package/build/cjs/features/Headers.js +630 -0
  18. package/build/cjs/features/Headers.js.map +1 -0
  19. package/build/cjs/features/Ordering.js +86 -0
  20. package/build/cjs/features/Ordering.js.map +1 -0
  21. package/build/cjs/features/Pagination.js +193 -0
  22. package/build/cjs/features/Pagination.js.map +1 -0
  23. package/build/cjs/features/Pinning.js +148 -0
  24. package/build/cjs/features/Pinning.js.map +1 -0
  25. package/build/cjs/features/RowSelection.js +525 -0
  26. package/build/cjs/features/RowSelection.js.map +1 -0
  27. package/build/cjs/features/Sorting.js +313 -0
  28. package/build/cjs/features/Sorting.js.map +1 -0
  29. package/build/cjs/features/Visibility.js +172 -0
  30. package/build/cjs/features/Visibility.js.map +1 -0
  31. package/build/cjs/filterTypes.js +172 -0
  32. package/build/cjs/filterTypes.js.map +1 -0
  33. package/build/cjs/index.js +75 -0
  34. package/build/cjs/index.js.map +1 -0
  35. package/build/cjs/sortTypes.js +122 -0
  36. package/build/cjs/sortTypes.js.map +1 -0
  37. package/build/cjs/types.js +22 -0
  38. package/build/cjs/types.js.map +1 -0
  39. package/build/cjs/utils/columnFilterRowsFn.js +131 -0
  40. package/build/cjs/utils/columnFilterRowsFn.js.map +1 -0
  41. package/build/cjs/utils/expandRowsFn.js +38 -0
  42. package/build/cjs/utils/expandRowsFn.js.map +1 -0
  43. package/build/cjs/utils/globalFilterRowsFn.js +101 -0
  44. package/build/cjs/utils/globalFilterRowsFn.js.map +1 -0
  45. package/build/cjs/utils/groupRowsFn.js +155 -0
  46. package/build/cjs/utils/groupRowsFn.js.map +1 -0
  47. package/build/cjs/utils/paginateRowsFn.js +44 -0
  48. package/build/cjs/utils/paginateRowsFn.js.map +1 -0
  49. package/build/cjs/utils/sortRowsFn.js +94 -0
  50. package/build/cjs/utils/sortRowsFn.js.map +1 -0
  51. package/build/cjs/utils.js +167 -0
  52. package/build/cjs/utils.js.map +1 -0
  53. package/build/esm/index.js +4689 -0
  54. package/build/esm/index.js.map +1 -0
  55. package/build/stats-html.html +2689 -0
  56. package/build/stats-react.json +939 -0
  57. package/build/types/aggregationTypes.d.ts +22 -0
  58. package/build/types/core.d.ts +105 -0
  59. package/build/types/createTable.d.ts +42 -0
  60. package/build/types/features/ColumnSizing.d.ts +75 -0
  61. package/build/types/features/Expanding.d.ts +50 -0
  62. package/build/types/features/Filters.d.ts +92 -0
  63. package/build/types/features/Grouping.d.ts +85 -0
  64. package/build/types/features/Headers.d.ts +43 -0
  65. package/build/types/features/Ordering.d.ts +21 -0
  66. package/build/types/features/Pagination.d.ts +41 -0
  67. package/build/types/features/Pinning.d.ts +41 -0
  68. package/build/types/features/RowSelection.d.ts +60 -0
  69. package/build/types/features/Sorting.d.ts +81 -0
  70. package/build/types/features/Visibility.d.ts +49 -0
  71. package/build/types/filterTypes.d.ts +50 -0
  72. package/build/types/index.d.ts +23 -0
  73. package/build/types/sortTypes.d.ts +18 -0
  74. package/build/types/types.d.ts +138 -0
  75. package/build/types/utils/columnFilterRowsFn.d.ts +2 -0
  76. package/build/types/utils/expandRowsFn.d.ts +2 -0
  77. package/build/types/utils/globalFilterRowsFn.d.ts +2 -0
  78. package/build/types/utils/groupRowsFn.d.ts +2 -0
  79. package/build/types/utils/paginateRowsFn.d.ts +2 -0
  80. package/build/types/utils/sortRowsFn.d.ts +2 -0
  81. package/build/types/utils.d.ts +23 -0
  82. package/build/umd/index.development.js +4728 -0
  83. package/build/umd/index.development.js.map +1 -0
  84. package/build/umd/index.production.js +12 -0
  85. package/build/umd/index.production.js.map +1 -0
  86. package/package.json +40 -0
  87. package/src/.DS_Store +0 -0
  88. package/src/aggregationTypes.ts +115 -0
  89. package/src/core.tsx +763 -0
  90. package/src/createTable.tsx +137 -0
  91. package/src/features/ColumnSizing.ts +432 -0
  92. package/src/features/Expanding.ts +320 -0
  93. package/src/features/Filters.ts +567 -0
  94. package/src/features/Grouping.ts +363 -0
  95. package/src/features/Headers.ts +747 -0
  96. package/src/features/Ordering.ts +112 -0
  97. package/src/features/Pagination.ts +252 -0
  98. package/src/features/Pinning.ts +177 -0
  99. package/src/features/RowSelection.ts +655 -0
  100. package/src/features/Sorting.ts +460 -0
  101. package/src/features/Visibility.ts +242 -0
  102. package/src/filterTypes.ts +188 -0
  103. package/src/index.tsx +23 -0
  104. package/src/sortTypes.ts +147 -0
  105. package/src/types.ts +311 -0
  106. package/src/utils/columnFilterRowsFn.ts +113 -0
  107. package/src/utils/expandRowsFn.ts +30 -0
  108. package/src/utils/globalFilterRowsFn.ts +89 -0
  109. package/src/utils/groupRowsFn.ts +170 -0
  110. package/src/utils/paginateRowsFn.ts +28 -0
  111. package/src/utils/sortRowsFn.ts +95 -0
  112. package/src/utils.tsx +221 -0
package/src/utils.tsx ADDED
@@ -0,0 +1,221 @@
1
+ import React from 'react'
2
+ import {
3
+ Getter,
4
+ NoInfer,
5
+ PropGetterValue,
6
+ Renderable,
7
+ TableState,
8
+ Updater,
9
+ } from './types'
10
+
11
+ export type IsAny<T> = 0 extends 1 & T ? true : false
12
+ export type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
13
+ export type RequiredKeys<T, K extends keyof T> = Omit<T, K> &
14
+ Required<Pick<T, K>>
15
+ export type Overwrite<T, U> = Omit<T, keyof U> & U
16
+
17
+ export type DataUpdateFunction<T> = (input: T) => T
18
+
19
+ export function functionalUpdate<T>(updater: Updater<T>, input: T): T {
20
+ return typeof updater === 'function'
21
+ ? (updater as DataUpdateFunction<T>)(input)
22
+ : updater
23
+ }
24
+
25
+ export function noop() {
26
+ //
27
+ }
28
+
29
+ export function makeStateUpdater(key: keyof TableState, instance: unknown) {
30
+ return (updater: Updater<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
+ type AnyFunction = (...args: any) => any
41
+
42
+ export function isFunction<T extends AnyFunction>(d: any): d is T {
43
+ return d instanceof Function
44
+ }
45
+
46
+ export function flattenBy<TNode>(
47
+ arr: TNode[],
48
+ getChildren: (item: TNode) => TNode[]
49
+ ) {
50
+ const flat: TNode[] = []
51
+
52
+ const recurse = (subArr: TNode[]) => {
53
+ subArr.forEach(item => {
54
+ flat.push(item)
55
+ const children = getChildren(item)
56
+ if (children?.length) {
57
+ recurse(children)
58
+ }
59
+ })
60
+ }
61
+
62
+ recurse(arr)
63
+
64
+ return flat
65
+ }
66
+
67
+ type PropGetterImpl = <TBaseProps, TGetter extends Getter<TBaseProps>>(
68
+ initial: TBaseProps,
69
+ userProps?: TGetter
70
+ ) => PropGetterValue<TBaseProps, TGetter>
71
+
72
+ // @ts-ignore // Just rely on the type, not the implementation
73
+ export const propGetter: PropGetterImpl = (initial, getter) => {
74
+ if (isFunction(getter)) {
75
+ return getter(initial)
76
+ }
77
+
78
+ return {
79
+ ...initial,
80
+ ...(getter ?? {}),
81
+ }
82
+ }
83
+
84
+ export function memo<TDeps extends readonly any[], TResult>(
85
+ getDeps: () => [...TDeps],
86
+ fn: (...args: NoInfer<[...TDeps]>) => TResult,
87
+ opts: {
88
+ key: string
89
+ debug?: () => any
90
+ onChange?: (result: TResult, previousResult?: TResult) => void
91
+ }
92
+ ): () => TResult {
93
+ let deps: any[] = []
94
+ let result: TResult | undefined
95
+
96
+ return () => {
97
+ let depTime: number
98
+ if (opts.key && opts.debug) depTime = performance.now()
99
+
100
+ const newDeps = getDeps()
101
+
102
+ const depsChanged =
103
+ newDeps.length !== deps.length ||
104
+ newDeps.some((dep: any, index: number) => deps[index] !== dep)
105
+
106
+ if (depsChanged) {
107
+ let oldResult = result
108
+ let resultTime: number
109
+ if (opts.key && opts.debug) resultTime = performance.now()
110
+ result = fn(...newDeps)
111
+ deps = newDeps
112
+ opts?.onChange?.(result, oldResult)
113
+
114
+ if (opts.key && opts.debug) {
115
+ if (opts?.debug()) {
116
+ const depEndTime =
117
+ Math.round((performance.now() - depTime!) * 100) / 100
118
+ const resultEndTime =
119
+ Math.round((performance.now() - resultTime!) * 100) / 100
120
+ const resultFpsPercentage = resultEndTime / 16
121
+
122
+ const pad = (str: number | string, num: number) => {
123
+ str = String(str)
124
+ while (str.length < num) {
125
+ str = ' ' + str
126
+ }
127
+ return str
128
+ }
129
+
130
+ console.info(
131
+ `%c⏱ ${pad(resultEndTime, 5)} /${pad(depEndTime, 5)} ms`,
132
+ `
133
+ font-size: .6rem;
134
+ font-weight: bold;
135
+ color: hsl(${Math.max(
136
+ 0,
137
+ Math.min(120 - 120 * resultFpsPercentage, 120)
138
+ )}deg 100% 31%);`,
139
+ opts?.key,
140
+ {
141
+ length: `${deps.length} -> ${newDeps.length}`,
142
+ ...newDeps
143
+ .map((_, index) => {
144
+ if (deps[index] !== newDeps[index]) {
145
+ return [index, deps[index], newDeps[index]]
146
+ }
147
+
148
+ return false
149
+ })
150
+ .filter(Boolean)
151
+ .reduce(
152
+ (accu, [a, b]: any) => ({
153
+ ...accu,
154
+ [a]: b,
155
+ }),
156
+ {}
157
+ ),
158
+ parent,
159
+ }
160
+ )
161
+ }
162
+ }
163
+
164
+ oldResult = undefined
165
+ }
166
+
167
+ return result!
168
+ }
169
+ }
170
+
171
+ export type Render = typeof flexRender
172
+
173
+ export function flexRender<TProps extends {}>(
174
+ Comp: Renderable<TProps>,
175
+ props: TProps
176
+ ): React.ReactNode {
177
+ return !Comp ? null : isReactComponent(Comp) ? <Comp {...props} /> : Comp
178
+ }
179
+
180
+ function isReactComponent(component: unknown): component is React.FC {
181
+ return (
182
+ isClassComponent(component) ||
183
+ typeof component === 'function' ||
184
+ isExoticComponent(component)
185
+ )
186
+ }
187
+
188
+ function isClassComponent(component: any) {
189
+ return (
190
+ typeof component === 'function' &&
191
+ (() => {
192
+ const proto = Object.getPrototypeOf(component)
193
+ return proto.prototype && proto.prototype.isReactComponent
194
+ })()
195
+ )
196
+ }
197
+
198
+ function isExoticComponent(component: any) {
199
+ return (
200
+ typeof component === 'object' &&
201
+ typeof component.$$typeof === 'symbol' &&
202
+ ['react.memo', 'react.forward_ref'].includes(component.$$typeof.description)
203
+ )
204
+ }
205
+
206
+ // export function hashString(str: string, seed = 0): string {
207
+ // let h1 = 0xdeadbeef ^ seed,
208
+ // h2 = 0x41c6ce57 ^ seed
209
+ // for (let i = 0, ch; i < str.length; i++) {
210
+ // ch = str.charCodeAt(i)
211
+ // h1 = Math.imul(h1 ^ ch, 2654435761)
212
+ // h2 = Math.imul(h2 ^ ch, 1597334677)
213
+ // }
214
+ // h1 =
215
+ // Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^
216
+ // Math.imul(h2 ^ (h2 >>> 13), 3266489909)
217
+ // h2 =
218
+ // Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^
219
+ // Math.imul(h1 ^ (h1 >>> 13), 3266489909)
220
+ // return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString()
221
+ // }