@tanstack/table-core 8.0.0-alpha.9 → 8.0.0-beta.0

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 (186) hide show
  1. package/build/cjs/aggregationFns.js +120 -0
  2. package/build/cjs/aggregationFns.js.map +1 -0
  3. package/build/cjs/core/cell.js +40 -0
  4. package/build/cjs/core/cell.js.map +1 -0
  5. package/build/cjs/core/column.js +88 -0
  6. package/build/cjs/core/column.js.map +1 -0
  7. package/build/cjs/core/headers.js +403 -0
  8. package/build/cjs/core/headers.js.map +1 -0
  9. package/build/cjs/core/instance.js +255 -0
  10. package/build/cjs/core/instance.js.map +1 -0
  11. package/build/cjs/core/row.js +77 -0
  12. package/build/cjs/core/row.js.map +1 -0
  13. package/build/cjs/createTable.js +36 -29
  14. package/build/cjs/createTable.js.map +1 -1
  15. package/build/cjs/features/ColumnSizing.js +157 -189
  16. package/build/cjs/features/ColumnSizing.js.map +1 -1
  17. package/build/cjs/features/Expanding.js +103 -155
  18. package/build/cjs/features/Expanding.js.map +1 -1
  19. package/build/cjs/features/Filters.js +155 -304
  20. package/build/cjs/features/Filters.js.map +1 -1
  21. package/build/cjs/features/Grouping.js +97 -157
  22. package/build/cjs/features/Grouping.js.map +1 -1
  23. package/build/cjs/features/Ordering.js +30 -41
  24. package/build/cjs/features/Ordering.js.map +1 -1
  25. package/build/cjs/features/Pagination.js +102 -105
  26. package/build/cjs/features/Pagination.js.map +1 -1
  27. package/build/cjs/features/Pinning.js +131 -89
  28. package/build/cjs/features/Pinning.js.map +1 -1
  29. package/build/cjs/features/RowSelection.js +138 -267
  30. package/build/cjs/features/RowSelection.js.map +1 -1
  31. package/build/cjs/features/Sorting.js +82 -167
  32. package/build/cjs/features/Sorting.js.map +1 -1
  33. package/build/cjs/features/Visibility.js +69 -115
  34. package/build/cjs/features/Visibility.js.map +1 -1
  35. package/build/cjs/filterFns.js +107 -0
  36. package/build/cjs/filterFns.js.map +1 -0
  37. package/build/cjs/index.js +36 -23
  38. package/build/cjs/index.js.map +1 -1
  39. package/build/cjs/sortingFns.js +124 -0
  40. package/build/cjs/sortingFns.js.map +1 -0
  41. package/build/cjs/utils/filterRowsUtils.js +117 -0
  42. package/build/cjs/utils/filterRowsUtils.js.map +1 -0
  43. package/build/cjs/utils/getCoreRowModel.js +79 -0
  44. package/build/cjs/utils/getCoreRowModel.js.map +1 -0
  45. package/build/cjs/utils/getExpandedRowModel.js +57 -0
  46. package/build/cjs/utils/getExpandedRowModel.js.map +1 -0
  47. package/build/cjs/utils/getFacetedMinMaxValues.js +54 -0
  48. package/build/cjs/utils/getFacetedMinMaxValues.js.map +1 -0
  49. package/build/cjs/utils/getFacetedRowModel.js +50 -0
  50. package/build/cjs/utils/getFacetedRowModel.js.map +1 -0
  51. package/build/cjs/utils/getFacetedUniqueValues.js +48 -0
  52. package/build/cjs/utils/getFacetedUniqueValues.js.map +1 -0
  53. package/build/cjs/utils/getFilteredRowModel.js +139 -0
  54. package/build/cjs/utils/getFilteredRowModel.js.map +1 -0
  55. package/build/cjs/utils/getGroupedRowModel.js +161 -0
  56. package/build/cjs/utils/getGroupedRowModel.js.map +1 -0
  57. package/build/cjs/utils/getPaginationRowModel.js +61 -0
  58. package/build/cjs/utils/getPaginationRowModel.js.map +1 -0
  59. package/build/cjs/utils/getSortedRowModel.js +110 -0
  60. package/build/cjs/utils/getSortedRowModel.js.map +1 -0
  61. package/build/cjs/utils.js +38 -107
  62. package/build/cjs/utils.js.map +1 -1
  63. package/build/esm/index.js +2976 -3853
  64. package/build/esm/index.js.map +1 -1
  65. package/build/stats-html.html +1 -1
  66. package/build/stats-react.json +586 -408
  67. package/build/types/aggregationFns.d.ts +13 -0
  68. package/build/types/core/cell.d.ts +9 -0
  69. package/build/types/core/column.d.ts +40 -0
  70. package/build/types/core/headers.d.ts +46 -0
  71. package/build/types/core/instance.d.ts +57 -0
  72. package/build/types/core/row.d.ts +15 -0
  73. package/build/types/createTable.d.ts +47 -26
  74. package/build/types/features/ColumnSizing.d.ts +32 -44
  75. package/build/types/features/Expanding.d.ts +15 -26
  76. package/build/types/features/Filters.d.ts +64 -63
  77. package/build/types/features/Grouping.d.ts +32 -46
  78. package/build/types/features/Ordering.d.ts +7 -10
  79. package/build/types/features/Pagination.d.ts +16 -15
  80. package/build/types/features/Pinning.d.ts +16 -17
  81. package/build/types/features/RowSelection.d.ts +13 -33
  82. package/build/types/features/Sorting.d.ts +25 -44
  83. package/build/types/features/Visibility.d.ts +16 -25
  84. package/build/types/filterFns.d.ts +13 -0
  85. package/build/types/index.d.ts +16 -10
  86. package/build/types/sortingFns.d.ts +11 -0
  87. package/build/types/types.d.ts +37 -122
  88. package/build/types/utils/filterRowsUtils.d.ts +4 -0
  89. package/build/types/utils/getCoreRowModel.d.ts +2 -0
  90. package/build/types/utils/getExpandedRowModel.d.ts +7 -0
  91. package/build/types/utils/getFacetedMinMaxValues.d.ts +2 -0
  92. package/build/types/utils/getFacetedRowModel.d.ts +2 -0
  93. package/build/types/utils/getFacetedUniqueValues.d.ts +2 -0
  94. package/build/types/utils/getFilteredRowModel.d.ts +2 -0
  95. package/build/types/utils/getGroupedRowModel.d.ts +2 -0
  96. package/build/types/utils/getPaginationRowModel.d.ts +4 -0
  97. package/build/types/utils/getSortedRowModel.d.ts +2 -0
  98. package/build/types/utils.d.ts +7 -11
  99. package/build/umd/index.development.js +3024 -3893
  100. package/build/umd/index.development.js.map +1 -1
  101. package/build/umd/index.production.js +1 -1
  102. package/build/umd/index.production.js.map +1 -1
  103. package/package.json +9 -10
  104. package/src/.DS_Store +0 -0
  105. package/src/aggregationFns.ts +123 -0
  106. package/src/core/cell.ts +48 -0
  107. package/src/core/column.ts +147 -0
  108. package/src/core/headers.ts +575 -0
  109. package/src/core/instance.ts +361 -0
  110. package/src/core/row.ts +85 -0
  111. package/src/createTable.ts +188 -0
  112. package/src/features/ColumnSizing.ts +170 -185
  113. package/src/features/Expanding.ts +122 -194
  114. package/src/features/Filters.ts +301 -381
  115. package/src/features/Grouping.ts +160 -220
  116. package/src/features/Ordering.ts +24 -25
  117. package/src/features/Pagination.ts +90 -73
  118. package/src/features/Pinning.ts +186 -63
  119. package/src/features/RowSelection.ts +107 -245
  120. package/src/features/Sorting.ts +127 -225
  121. package/src/features/Visibility.ts +116 -132
  122. package/src/filterFns.ts +135 -0
  123. package/src/index.ts +29 -0
  124. package/src/{sortTypes.ts → sortingFns.ts} +63 -85
  125. package/src/types.ts +62 -168
  126. package/src/utils/filterRowsUtils.ts +112 -0
  127. package/src/utils/getCoreRowModel.ts +84 -0
  128. package/src/utils/getExpandedRowModel.ts +54 -0
  129. package/src/utils/getFacetedMinMaxValues.ts +40 -0
  130. package/src/utils/getFacetedRowModel.ts +50 -0
  131. package/src/utils/getFacetedUniqueValues.ts +37 -0
  132. package/src/utils/getFilteredRowModel.ts +159 -0
  133. package/src/utils/getGroupedRowModel.ts +175 -0
  134. package/src/utils/getPaginationRowModel.ts +48 -0
  135. package/src/utils/getSortedRowModel.ts +116 -0
  136. package/src/utils.ts +126 -0
  137. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -112
  138. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  139. package/build/cjs/aggregationTypes.js +0 -130
  140. package/build/cjs/aggregationTypes.js.map +0 -1
  141. package/build/cjs/core.js +0 -545
  142. package/build/cjs/core.js.map +0 -1
  143. package/build/cjs/features/Headers.js +0 -630
  144. package/build/cjs/features/Headers.js.map +0 -1
  145. package/build/cjs/filterTypes.js +0 -172
  146. package/build/cjs/filterTypes.js.map +0 -1
  147. package/build/cjs/sortTypes.js +0 -122
  148. package/build/cjs/sortTypes.js.map +0 -1
  149. package/build/cjs/types.js +0 -22
  150. package/build/cjs/types.js.map +0 -1
  151. package/build/cjs/utils/columnFilterRowsFn.js +0 -131
  152. package/build/cjs/utils/columnFilterRowsFn.js.map +0 -1
  153. package/build/cjs/utils/expandRowsFn.js +0 -38
  154. package/build/cjs/utils/expandRowsFn.js.map +0 -1
  155. package/build/cjs/utils/globalFilterRowsFn.js +0 -101
  156. package/build/cjs/utils/globalFilterRowsFn.js.map +0 -1
  157. package/build/cjs/utils/groupRowsFn.js +0 -155
  158. package/build/cjs/utils/groupRowsFn.js.map +0 -1
  159. package/build/cjs/utils/paginateRowsFn.js +0 -44
  160. package/build/cjs/utils/paginateRowsFn.js.map +0 -1
  161. package/build/cjs/utils/sortRowsFn.js +0 -94
  162. package/build/cjs/utils/sortRowsFn.js.map +0 -1
  163. package/build/types/aggregationTypes.d.ts +0 -22
  164. package/build/types/core.d.ts +0 -105
  165. package/build/types/features/Headers.d.ts +0 -43
  166. package/build/types/filterTypes.d.ts +0 -50
  167. package/build/types/sortTypes.d.ts +0 -18
  168. package/build/types/utils/columnFilterRowsFn.d.ts +0 -2
  169. package/build/types/utils/expandRowsFn.d.ts +0 -2
  170. package/build/types/utils/globalFilterRowsFn.d.ts +0 -2
  171. package/build/types/utils/groupRowsFn.d.ts +0 -2
  172. package/build/types/utils/paginateRowsFn.d.ts +0 -2
  173. package/build/types/utils/sortRowsFn.d.ts +0 -2
  174. package/src/aggregationTypes.ts +0 -115
  175. package/src/core.tsx +0 -763
  176. package/src/createTable.tsx +0 -137
  177. package/src/features/Headers.ts +0 -747
  178. package/src/filterTypes.ts +0 -188
  179. package/src/index.tsx +0 -23
  180. package/src/utils/columnFilterRowsFn.ts +0 -113
  181. package/src/utils/expandRowsFn.ts +0 -30
  182. package/src/utils/globalFilterRowsFn.ts +0 -89
  183. package/src/utils/groupRowsFn.ts +0 -170
  184. package/src/utils/paginateRowsFn.ts +0 -28
  185. package/src/utils/sortRowsFn.ts +0 -95
  186. package/src/utils.tsx +0 -221
@@ -1,6 +1,7 @@
1
+ import { TableFeature } from '../core/instance'
1
2
  import {
2
3
  OnChangeFn,
3
- PartialGenerics,
4
+ TableGenerics,
4
5
  TableInstance,
5
6
  RowModel,
6
7
  Updater,
@@ -10,35 +11,38 @@ import { functionalUpdate, makeStateUpdater, memo } from '../utils'
10
11
  export type PaginationState = {
11
12
  pageIndex: number
12
13
  pageSize: number
13
- pageCount: number
14
+ pageCount?: number
14
15
  }
15
16
 
16
17
  export type PaginationTableState = {
17
18
  pagination: PaginationState
18
19
  }
19
20
 
20
- export type PaginationOptions<TGenerics extends PartialGenerics> = {
21
+ export type PaginationInitialTableState = {
22
+ pagination?: Partial<PaginationState>
23
+ }
24
+
25
+ export type PaginationOptions<TGenerics extends TableGenerics> = {
26
+ manualPagination?: boolean
21
27
  onPaginationChange?: OnChangeFn<PaginationState>
22
28
  autoResetPageIndex?: boolean
23
- paginateRowsFn?: (
24
- instance: TableInstance<TGenerics>,
25
- rowModel: RowModel<TGenerics>
26
- ) => RowModel<TGenerics>
29
+ getPaginationRowModel?: (
30
+ instance: TableInstance<TGenerics>
31
+ ) => () => RowModel<TGenerics>
27
32
  }
28
33
 
29
34
  export type PaginationDefaultOptions = {
30
35
  onPaginationChange: OnChangeFn<PaginationState>
31
- autoResetPageIndex: boolean
32
36
  }
33
37
 
34
- export type PaginationInstance<TGenerics extends PartialGenerics> = {
35
- _notifyPageIndexReset: () => void
38
+ export type PaginationInstance<TGenerics extends TableGenerics> = {
39
+ _autoResetPageIndex: () => void
36
40
  setPagination: (updater: Updater<PaginationState>) => void
37
- resetPagination: () => void
41
+ resetPagination: (defaultState?: boolean) => void
38
42
  setPageIndex: (updater: Updater<number>) => void
39
- resetPageIndex: () => void
43
+ resetPageIndex: (defaultState?: boolean) => void
40
44
  setPageSize: (updater: Updater<number>) => void
41
- resetPageSize: () => void
45
+ resetPageSize: (defaultState?: boolean) => void
42
46
  setPageCount: (updater: Updater<number>) => void
43
47
  getPageOptions: () => number[]
44
48
  getCanPreviousPage: () => boolean
@@ -47,51 +51,65 @@ export type PaginationInstance<TGenerics extends PartialGenerics> = {
47
51
  nextPage: () => void
48
52
  getPrePaginationRowModel: () => RowModel<TGenerics>
49
53
  getPaginationRowModel: () => RowModel<TGenerics>
54
+ _getPaginationRowModel?: () => RowModel<TGenerics>
50
55
  getPageCount: () => number
51
56
  }
52
57
 
53
58
  //
54
59
 
55
- export const Pagination = {
56
- getInitialState: (): PaginationTableState => {
60
+ const defaultPageIndex = 0
61
+ const defaultPageSize = 10
62
+
63
+ const getDefaultPaginationState = (): PaginationState => ({
64
+ pageIndex: defaultPageIndex,
65
+ pageSize: defaultPageSize,
66
+ })
67
+
68
+ export const Pagination: TableFeature = {
69
+ getInitialState: (state): PaginationTableState => {
57
70
  return {
71
+ ...state,
58
72
  pagination: {
59
- pageCount: -1,
60
- pageIndex: 0,
61
- pageSize: 10,
73
+ ...getDefaultPaginationState(),
74
+ ...state?.pagination,
62
75
  },
63
76
  }
64
77
  },
65
78
 
66
- getDefaultOptions: <TGenerics extends PartialGenerics>(
79
+ getDefaultOptions: <TGenerics extends TableGenerics>(
67
80
  instance: TableInstance<TGenerics>
68
81
  ): PaginationDefaultOptions => {
69
82
  return {
70
83
  onPaginationChange: makeStateUpdater('pagination', instance),
71
- autoResetPageIndex: true,
72
84
  }
73
85
  },
74
86
 
75
- getInstance: <TGenerics extends PartialGenerics>(
87
+ createInstance: <TGenerics extends TableGenerics>(
76
88
  instance: TableInstance<TGenerics>
77
89
  ): PaginationInstance<TGenerics> => {
78
90
  let registered = false
91
+ let queued = false
92
+
79
93
  return {
80
- _notifyPageIndexReset: () => {
94
+ _autoResetPageIndex: () => {
81
95
  if (!registered) {
82
- registered = true
83
- return
84
- }
85
-
86
- if (instance.options.autoResetAll === false) {
96
+ instance._queue(() => {
97
+ registered = true
98
+ })
87
99
  return
88
100
  }
89
101
 
90
102
  if (
91
- instance.options.autoResetAll === true ||
92
- instance.options.autoResetPageIndex
103
+ instance.options.autoResetAll ??
104
+ instance.options.autoResetPageIndex ??
105
+ !instance.options.manualPagination
93
106
  ) {
94
- instance.resetPageIndex()
107
+ if (queued) return
108
+ queued = true
109
+ instance._queue(() => {
110
+ instance.resetPageIndex()
111
+ queued = false
112
+ })
95
113
  }
96
114
  },
97
115
  setPagination: updater => {
@@ -101,18 +119,13 @@ export const Pagination = {
101
119
  return newState
102
120
  }
103
121
 
104
- return instance.options.onPaginationChange?.(
105
- safeUpdater,
106
- functionalUpdate(safeUpdater, instance.getState().pagination)
107
- )
122
+ return instance.options.onPaginationChange?.(safeUpdater)
108
123
  },
109
- resetPagination: () => {
124
+ resetPagination: defaultState => {
110
125
  instance.setPagination(
111
- instance.initialState.pagination ?? {
112
- pageIndex: 0,
113
- pageSize: 10,
114
- pageCount: -1,
115
- }
126
+ defaultState
127
+ ? getDefaultPaginationState()
128
+ : instance.initialState.pagination ?? getDefaultPaginationState()
116
129
  )
117
130
  },
118
131
  setPageIndex: updater => {
@@ -120,7 +133,7 @@ export const Pagination = {
120
133
  let pageIndex = functionalUpdate(updater, old.pageIndex)
121
134
 
122
135
  const maxPageIndex =
123
- old.pageCount && old.pageCount > 0
136
+ typeof old.pageCount !== 'undefined'
124
137
  ? old.pageCount - 1
125
138
  : Number.MAX_SAFE_INTEGER
126
139
 
@@ -132,11 +145,19 @@ export const Pagination = {
132
145
  }
133
146
  })
134
147
  },
135
- resetPageIndex: () => {
136
- instance.setPageIndex(0)
148
+ resetPageIndex: defaultState => {
149
+ instance.setPageIndex(
150
+ defaultState
151
+ ? defaultPageIndex
152
+ : instance.initialState?.pagination?.pageIndex ?? defaultPageIndex
153
+ )
137
154
  },
138
- resetPageSize: () => {
139
- instance.setPageSize(instance.initialState?.pagination?.pageSize ?? 10)
155
+ resetPageSize: defaultState => {
156
+ instance.setPageSize(
157
+ defaultState
158
+ ? defaultPageSize
159
+ : instance.initialState?.pagination?.pageSize ?? defaultPageSize
160
+ )
140
161
  },
141
162
  setPageSize: updater => {
142
163
  instance.setPagination(old => {
@@ -153,7 +174,7 @@ export const Pagination = {
153
174
  },
154
175
  setPageCount: updater =>
155
176
  instance.setPagination(old => {
156
- let newPageCount = functionalUpdate(updater, old.pageCount)
177
+ let newPageCount = functionalUpdate(updater, old.pageCount ?? -1)
157
178
 
158
179
  if (typeof newPageCount === 'number') {
159
180
  newPageCount = Math.max(-1, newPageCount)
@@ -178,7 +199,7 @@ export const Pagination = {
178
199
  return pageOptions
179
200
  },
180
201
  {
181
- key: 'getPageOptions',
202
+ key: process.env.NODE_ENV === 'development' && 'getPageOptions',
182
203
  debug: () => instance.options.debugAll ?? instance.options.debugTable,
183
204
  }
184
205
  ),
@@ -186,8 +207,9 @@ export const Pagination = {
186
207
  getCanPreviousPage: () => instance.getState().pagination.pageIndex > 0,
187
208
 
188
209
  getCanNextPage: () => {
189
- const { pageIndex, pageCount, pageSize } =
190
- instance.getState().pagination
210
+ const { pageIndex } = instance.getState().pagination
211
+
212
+ const pageCount = instance.getPageCount()
191
213
 
192
214
  if (pageCount === -1) {
193
215
  return true
@@ -197,13 +219,7 @@ export const Pagination = {
197
219
  return false
198
220
  }
199
221
 
200
- return (
201
- pageIndex <
202
- Math.ceil(
203
- instance.getPrePaginationRowModel().rows.length / pageSize
204
- ) -
205
- 1
206
- )
222
+ return pageIndex < pageCount - 1
207
223
  },
208
224
 
209
225
  previousPage: () => {
@@ -217,28 +233,29 @@ export const Pagination = {
217
233
  },
218
234
 
219
235
  getPrePaginationRowModel: () => instance.getExpandedRowModel(),
220
- getPaginationRowModel: memo(
221
- () => [
222
- instance.getState().pagination,
223
- instance.getExpandedRowModel(),
224
- instance.options.paginateRowsFn,
225
- ],
226
- (_pagination, rowModel, paginateRowsFn) => {
227
- if (!paginateRowsFn || !rowModel.rows.length) {
228
- return rowModel
229
- }
236
+ getPaginationRowModel: () => {
237
+ if (
238
+ !instance._getPaginationRowModel &&
239
+ instance.options.getPaginationRowModel
240
+ ) {
241
+ instance._getPaginationRowModel =
242
+ instance.options.getPaginationRowModel(instance)
243
+ }
230
244
 
231
- return paginateRowsFn(instance, rowModel)
232
- },
233
- {
234
- key: 'getPaginationRowModel',
235
- debug: () => instance.options.debugAll ?? instance.options.debugTable,
245
+ if (
246
+ instance.options.manualPagination ||
247
+ !instance._getPaginationRowModel
248
+ ) {
249
+ return instance.getPrePaginationRowModel()
236
250
  }
237
- ),
251
+
252
+ return instance._getPaginationRowModel()
253
+ },
238
254
 
239
255
  getPageCount: () => {
240
256
  const { pageCount } = instance.getState().pagination
241
- if (pageCount > 0) {
257
+
258
+ if (typeof pageCount !== 'undefined') {
242
259
  return pageCount
243
260
  }
244
261
 
@@ -1,13 +1,16 @@
1
+ import { TableFeature } from '../core/instance'
1
2
  import {
2
3
  OnChangeFn,
3
4
  Updater,
4
5
  TableInstance,
5
6
  Column,
6
- PartialGenerics,
7
+ TableGenerics,
8
+ Row,
9
+ Cell,
7
10
  } from '../types'
8
- import { functionalUpdate, makeStateUpdater } from '../utils'
11
+ import { makeStateUpdater, memo } from '../utils'
9
12
 
10
- type ColumnPinningPosition = false | 'left' | 'right'
13
+ export type ColumnPinningPosition = false | 'left' | 'right'
11
14
 
12
15
  export type ColumnPinningState = {
13
16
  left?: string[]
@@ -29,7 +32,6 @@ export type ColumnPinningDefaultOptions = {
29
32
 
30
33
  export type ColumnPinningColumnDef = {
31
34
  enablePinning?: boolean
32
- defaultCanPin?: boolean
33
35
  }
34
36
 
35
37
  export type ColumnPinningColumn = {
@@ -39,28 +41,37 @@ export type ColumnPinningColumn = {
39
41
  pin: (position: ColumnPinningPosition) => void
40
42
  }
41
43
 
42
- export type ColumnPinningInstance<TGenerics extends PartialGenerics> = {
44
+ export type ColumnPinningRow<TGenerics extends TableGenerics> = {
45
+ getLeftVisibleCells: () => Cell<TGenerics>[]
46
+ getCenterVisibleCells: () => Cell<TGenerics>[]
47
+ getRightVisibleCells: () => Cell<TGenerics>[]
48
+ }
49
+
50
+ export type ColumnPinningInstance<TGenerics extends TableGenerics> = {
43
51
  setColumnPinning: (updater: Updater<ColumnPinningState>) => void
44
- resetColumnPinning: () => void
45
- pinColumn: (columnId: string, position: ColumnPinningPosition) => void
46
- getColumnCanPin: (columnId: string) => boolean
47
- getColumnIsPinned: (columnId: string) => ColumnPinningPosition
48
- getColumnPinnedIndex: (columnId: string) => number
52
+ resetColumnPinning: (defaultState?: boolean) => void
53
+ getIsSomeColumnsPinned: (position?: ColumnPinningPosition) => boolean
54
+ getLeftLeafColumns: () => Column<TGenerics>[]
55
+ getRightLeafColumns: () => Column<TGenerics>[]
56
+ getCenterLeafColumns: () => Column<TGenerics>[]
49
57
  }
50
58
 
51
59
  //
52
60
 
53
- export const Pinning = {
54
- getInitialState: (): ColumnPinningTableState => {
61
+ const getDefaultPinningState = (): ColumnPinningState => ({
62
+ left: [],
63
+ right: [],
64
+ })
65
+
66
+ export const Pinning: TableFeature = {
67
+ getInitialState: (state): ColumnPinningTableState => {
55
68
  return {
56
- columnPinning: {
57
- left: [],
58
- right: [],
59
- },
69
+ columnPinning: getDefaultPinningState(),
70
+ ...state,
60
71
  }
61
72
  },
62
73
 
63
- getDefaultOptions: <TGenerics extends PartialGenerics>(
74
+ getDefaultOptions: <TGenerics extends TableGenerics>(
64
75
  instance: TableInstance<TGenerics>
65
76
  ): ColumnPinningDefaultOptions => {
66
77
  return {
@@ -68,36 +79,14 @@ export const Pinning = {
68
79
  }
69
80
  },
70
81
 
71
- createColumn: <TGenerics extends PartialGenerics>(
82
+ createColumn: <TGenerics extends TableGenerics>(
72
83
  column: Column<TGenerics>,
73
84
  instance: TableInstance<TGenerics>
74
85
  ): ColumnPinningColumn => {
75
86
  return {
76
- getCanPin: () => instance.getColumnCanPin(column.id),
77
- getPinnedIndex: () => instance.getColumnPinnedIndex(column.id),
78
- getIsPinned: () => instance.getColumnIsPinned(column.id),
79
- pin: position => instance.pinColumn(column.id, position),
80
- }
81
- },
82
-
83
- getInstance: <TGenerics extends PartialGenerics>(
84
- instance: TableInstance<TGenerics>
85
- ): ColumnPinningInstance<TGenerics> => {
86
- return {
87
- setColumnPinning: updater =>
88
- instance.options.onColumnPinningChange?.(
89
- updater,
90
- functionalUpdate(updater, instance.getState().columnPinning)
91
- ),
92
-
93
- resetColumnPinning: () =>
94
- instance.setColumnPinning(instance.initialState?.columnPinning ?? {}),
95
-
96
- pinColumn: (columnId, position) => {
97
- const column = instance.getColumn(columnId)
98
-
87
+ pin: position => {
99
88
  const columnIds = column
100
- ?.getLeafColumns()
89
+ .getLeafColumns()
101
90
  .map(d => d.id)
102
91
  .filter(Boolean) as string[]
103
92
 
@@ -129,31 +118,17 @@ export const Pinning = {
129
118
  })
130
119
  },
131
120
 
132
- getColumnCanPin: columnId => {
133
- const column = instance.getColumn(columnId)
134
-
135
- if (!column) {
136
- throw new Error()
137
- }
138
-
121
+ getCanPin: () => {
139
122
  const leafColumns = column.getLeafColumns()
140
123
 
141
124
  return leafColumns.some(
142
125
  d =>
143
- d.enablePinning ??
144
- instance.options.enablePinning ??
145
- d.defaultCanPin ??
146
- !!d.accessorFn
126
+ (d.columnDef.enablePinning ?? true) &&
127
+ (instance.options.enablePinning ?? true)
147
128
  )
148
129
  },
149
130
 
150
- getColumnIsPinned: columnId => {
151
- const column = instance.getColumn(columnId)
152
-
153
- if (!column) {
154
- throw new Error()
155
- }
156
-
131
+ getIsPinned: () => {
157
132
  const leafColumnIds = column.getLeafColumns().map(d => d.id)
158
133
 
159
134
  const { left, right } = instance.getState().columnPinning
@@ -164,14 +139,162 @@ export const Pinning = {
164
139
  return isLeft ? 'left' : isRight ? 'right' : false
165
140
  },
166
141
 
167
- getColumnPinnedIndex: columnId => {
168
- const position = instance.getColumnIsPinned(columnId)
142
+ getPinnedIndex: () => {
143
+ const position = column.getIsPinned()
169
144
 
170
145
  return position
171
- ? instance.getState().columnPinning?.[position]?.indexOf(columnId) ??
146
+ ? instance.getState().columnPinning?.[position]?.indexOf(column.id) ??
172
147
  -1
173
148
  : 0
174
149
  },
175
150
  }
176
151
  },
152
+
153
+ createRow: <TGenerics extends TableGenerics>(
154
+ row: Row<TGenerics>,
155
+ instance: TableInstance<TGenerics>
156
+ ): ColumnPinningRow<TGenerics> => {
157
+ return {
158
+ getCenterVisibleCells: memo(
159
+ () => [
160
+ row._getAllVisibleCells(),
161
+ instance.getState().columnPinning.left,
162
+ instance.getState().columnPinning.right,
163
+ ],
164
+ (allCells, left, right) => {
165
+ const leftAndRight: string[] = [...(left ?? []), ...(right ?? [])]
166
+
167
+ return allCells.filter(d => !leftAndRight.includes(d.column.id))
168
+ },
169
+ {
170
+ key:
171
+ process.env.NODE_ENV === 'production' &&
172
+ 'row.getCenterVisibleCells',
173
+ debug: () => instance.options.debugAll ?? instance.options.debugRows,
174
+ }
175
+ ),
176
+ getLeftVisibleCells: memo(
177
+ () => [
178
+ row._getAllVisibleCells(),
179
+ instance.getState().columnPinning.left,
180
+ ,
181
+ ],
182
+ (allCells, left) => {
183
+ const cells = (left ?? [])
184
+ .map(
185
+ columnId => allCells.find(cell => cell.column.id === columnId)!
186
+ )
187
+ .filter(Boolean)
188
+ .map(d => ({ ...d, position: 'left' } as Cell<TGenerics>))
189
+
190
+ return cells
191
+ },
192
+ {
193
+ key:
194
+ process.env.NODE_ENV === 'production' && 'row.getLeftVisibleCells',
195
+ debug: () => instance.options.debugAll ?? instance.options.debugRows,
196
+ }
197
+ ),
198
+ getRightVisibleCells: memo(
199
+ () => [
200
+ row._getAllVisibleCells(),
201
+ instance.getState().columnPinning.right,
202
+ ],
203
+ (allCells, right) => {
204
+ const cells = (right ?? [])
205
+ .map(
206
+ columnId => allCells.find(cell => cell.column.id === columnId)!
207
+ )
208
+ .filter(Boolean)
209
+ .map(d => ({ ...d, position: 'left' } as Cell<TGenerics>))
210
+
211
+ return cells
212
+ },
213
+ {
214
+ key:
215
+ process.env.NODE_ENV === 'production' && 'row.getRightVisibleCells',
216
+ debug: () => instance.options.debugAll ?? instance.options.debugRows,
217
+ }
218
+ ),
219
+ }
220
+ },
221
+
222
+ createInstance: <TGenerics extends TableGenerics>(
223
+ instance: TableInstance<TGenerics>
224
+ ): ColumnPinningInstance<TGenerics> => {
225
+ return {
226
+ setColumnPinning: updater =>
227
+ instance.options.onColumnPinningChange?.(updater),
228
+
229
+ resetColumnPinning: defaultState =>
230
+ instance.setColumnPinning(
231
+ defaultState
232
+ ? getDefaultPinningState()
233
+ : instance.initialState?.columnPinning ?? getDefaultPinningState()
234
+ ),
235
+
236
+ getIsSomeColumnsPinned: position => {
237
+ const pinningState = instance.getState().columnPinning
238
+
239
+ if (!position) {
240
+ return Boolean(
241
+ pinningState.left?.length || pinningState.right?.length
242
+ )
243
+ }
244
+ return Boolean(pinningState[position]?.length)
245
+ },
246
+
247
+ getLeftLeafColumns: memo(
248
+ () => [
249
+ instance.getAllLeafColumns(),
250
+ instance.getState().columnPinning.left,
251
+ ],
252
+ (allColumns, left) => {
253
+ return (left ?? [])
254
+ .map(columnId => allColumns.find(column => column.id === columnId)!)
255
+ .filter(Boolean)
256
+ },
257
+ {
258
+ key: process.env.NODE_ENV === 'development' && 'getLeftLeafColumns',
259
+ debug: () =>
260
+ instance.options.debugAll ?? instance.options.debugColumns,
261
+ }
262
+ ),
263
+
264
+ getRightLeafColumns: memo(
265
+ () => [
266
+ instance.getAllLeafColumns(),
267
+ instance.getState().columnPinning.right,
268
+ ],
269
+ (allColumns, right) => {
270
+ return (right ?? [])
271
+ .map(columnId => allColumns.find(column => column.id === columnId)!)
272
+ .filter(Boolean)
273
+ },
274
+ {
275
+ key: process.env.NODE_ENV === 'development' && 'getRightLeafColumns',
276
+ debug: () =>
277
+ instance.options.debugAll ?? instance.options.debugColumns,
278
+ }
279
+ ),
280
+
281
+ getCenterLeafColumns: memo(
282
+ () => [
283
+ instance.getAllLeafColumns(),
284
+ instance.getState().columnPinning.left,
285
+ instance.getState().columnPinning.right,
286
+ ],
287
+ (allColumns, left, right) => {
288
+ const leftAndRight: string[] = [...(left ?? []), ...(right ?? [])]
289
+
290
+ return allColumns.filter(d => !leftAndRight.includes(d.id))
291
+ },
292
+ {
293
+ key: process.env.NODE_ENV === 'development' && 'getCenterLeafColumns',
294
+ debug: () =>
295
+ instance.options.debugAll ?? instance.options.debugColumns,
296
+ }
297
+ ),
298
+ }
299
+ },
177
300
  }