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

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/aggregationFns.ts +123 -0
  105. package/src/core/cell.ts +48 -0
  106. package/src/core/column.ts +147 -0
  107. package/src/core/headers.ts +575 -0
  108. package/src/core/instance.ts +359 -0
  109. package/src/core/row.ts +85 -0
  110. package/src/createTable.ts +188 -0
  111. package/src/features/ColumnSizing.ts +170 -185
  112. package/src/features/Expanding.ts +120 -194
  113. package/src/features/Filters.ts +299 -381
  114. package/src/features/Grouping.ts +158 -220
  115. package/src/features/Ordering.ts +24 -25
  116. package/src/features/Pagination.ts +88 -73
  117. package/src/features/Pinning.ts +186 -63
  118. package/src/features/RowSelection.ts +107 -245
  119. package/src/features/Sorting.ts +125 -225
  120. package/src/features/Visibility.ts +116 -132
  121. package/src/filterFns.ts +135 -0
  122. package/src/index.ts +29 -0
  123. package/src/{sortTypes.ts → sortingFns.ts} +63 -85
  124. package/src/types.ts +62 -168
  125. package/src/utils/filterRowsUtils.ts +112 -0
  126. package/src/utils/getCoreRowModel.ts +84 -0
  127. package/src/utils/getExpandedRowModel.ts +54 -0
  128. package/src/utils/getFacetedMinMaxValues.ts +40 -0
  129. package/src/utils/getFacetedRowModel.ts +50 -0
  130. package/src/utils/getFacetedUniqueValues.ts +37 -0
  131. package/src/utils/getFilteredRowModel.ts +159 -0
  132. package/src/utils/getGroupedRowModel.ts +175 -0
  133. package/src/utils/getPaginationRowModel.ts +48 -0
  134. package/src/utils/getSortedRowModel.ts +116 -0
  135. package/src/utils.ts +126 -0
  136. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -112
  137. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  138. package/build/cjs/aggregationTypes.js +0 -130
  139. package/build/cjs/aggregationTypes.js.map +0 -1
  140. package/build/cjs/core.js +0 -545
  141. package/build/cjs/core.js.map +0 -1
  142. package/build/cjs/features/Headers.js +0 -630
  143. package/build/cjs/features/Headers.js.map +0 -1
  144. package/build/cjs/filterTypes.js +0 -172
  145. package/build/cjs/filterTypes.js.map +0 -1
  146. package/build/cjs/sortTypes.js +0 -122
  147. package/build/cjs/sortTypes.js.map +0 -1
  148. package/build/cjs/types.js +0 -22
  149. package/build/cjs/types.js.map +0 -1
  150. package/build/cjs/utils/columnFilterRowsFn.js +0 -131
  151. package/build/cjs/utils/columnFilterRowsFn.js.map +0 -1
  152. package/build/cjs/utils/expandRowsFn.js +0 -38
  153. package/build/cjs/utils/expandRowsFn.js.map +0 -1
  154. package/build/cjs/utils/globalFilterRowsFn.js +0 -101
  155. package/build/cjs/utils/globalFilterRowsFn.js.map +0 -1
  156. package/build/cjs/utils/groupRowsFn.js +0 -155
  157. package/build/cjs/utils/groupRowsFn.js.map +0 -1
  158. package/build/cjs/utils/paginateRowsFn.js +0 -44
  159. package/build/cjs/utils/paginateRowsFn.js.map +0 -1
  160. package/build/cjs/utils/sortRowsFn.js +0 -94
  161. package/build/cjs/utils/sortRowsFn.js.map +0 -1
  162. package/build/types/aggregationTypes.d.ts +0 -22
  163. package/build/types/core.d.ts +0 -105
  164. package/build/types/features/Headers.d.ts +0 -43
  165. package/build/types/filterTypes.d.ts +0 -50
  166. package/build/types/sortTypes.d.ts +0 -18
  167. package/build/types/utils/columnFilterRowsFn.d.ts +0 -2
  168. package/build/types/utils/expandRowsFn.d.ts +0 -2
  169. package/build/types/utils/globalFilterRowsFn.d.ts +0 -2
  170. package/build/types/utils/groupRowsFn.d.ts +0 -2
  171. package/build/types/utils/paginateRowsFn.d.ts +0 -2
  172. package/build/types/utils/sortRowsFn.d.ts +0 -2
  173. package/src/.DS_Store +0 -0
  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,36 @@ 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?: (instance: TableInstance<any>) => () => RowModel<any>
27
30
  }
28
31
 
29
32
  export type PaginationDefaultOptions = {
30
33
  onPaginationChange: OnChangeFn<PaginationState>
31
- autoResetPageIndex: boolean
32
34
  }
33
35
 
34
- export type PaginationInstance<TGenerics extends PartialGenerics> = {
35
- _notifyPageIndexReset: () => void
36
+ export type PaginationInstance<TGenerics extends TableGenerics> = {
37
+ _autoResetPageIndex: () => void
36
38
  setPagination: (updater: Updater<PaginationState>) => void
37
- resetPagination: () => void
39
+ resetPagination: (defaultState?: boolean) => void
38
40
  setPageIndex: (updater: Updater<number>) => void
39
- resetPageIndex: () => void
41
+ resetPageIndex: (defaultState?: boolean) => void
40
42
  setPageSize: (updater: Updater<number>) => void
41
- resetPageSize: () => void
43
+ resetPageSize: (defaultState?: boolean) => void
42
44
  setPageCount: (updater: Updater<number>) => void
43
45
  getPageOptions: () => number[]
44
46
  getCanPreviousPage: () => boolean
@@ -47,51 +49,65 @@ export type PaginationInstance<TGenerics extends PartialGenerics> = {
47
49
  nextPage: () => void
48
50
  getPrePaginationRowModel: () => RowModel<TGenerics>
49
51
  getPaginationRowModel: () => RowModel<TGenerics>
52
+ _getPaginationRowModel?: () => RowModel<TGenerics>
50
53
  getPageCount: () => number
51
54
  }
52
55
 
53
56
  //
54
57
 
55
- export const Pagination = {
56
- getInitialState: (): PaginationTableState => {
58
+ const defaultPageIndex = 0
59
+ const defaultPageSize = 10
60
+
61
+ const getDefaultPaginationState = (): PaginationState => ({
62
+ pageIndex: defaultPageIndex,
63
+ pageSize: defaultPageSize,
64
+ })
65
+
66
+ export const Pagination: TableFeature = {
67
+ getInitialState: (state): PaginationTableState => {
57
68
  return {
69
+ ...state,
58
70
  pagination: {
59
- pageCount: -1,
60
- pageIndex: 0,
61
- pageSize: 10,
71
+ ...getDefaultPaginationState(),
72
+ ...state?.pagination,
62
73
  },
63
74
  }
64
75
  },
65
76
 
66
- getDefaultOptions: <TGenerics extends PartialGenerics>(
77
+ getDefaultOptions: <TGenerics extends TableGenerics>(
67
78
  instance: TableInstance<TGenerics>
68
79
  ): PaginationDefaultOptions => {
69
80
  return {
70
81
  onPaginationChange: makeStateUpdater('pagination', instance),
71
- autoResetPageIndex: true,
72
82
  }
73
83
  },
74
84
 
75
- getInstance: <TGenerics extends PartialGenerics>(
85
+ createInstance: <TGenerics extends TableGenerics>(
76
86
  instance: TableInstance<TGenerics>
77
87
  ): PaginationInstance<TGenerics> => {
78
88
  let registered = false
89
+ let queued = false
90
+
79
91
  return {
80
- _notifyPageIndexReset: () => {
92
+ _autoResetPageIndex: () => {
81
93
  if (!registered) {
82
- registered = true
83
- return
84
- }
85
-
86
- if (instance.options.autoResetAll === false) {
94
+ instance._queue(() => {
95
+ registered = true
96
+ })
87
97
  return
88
98
  }
89
99
 
90
100
  if (
91
- instance.options.autoResetAll === true ||
92
- instance.options.autoResetPageIndex
101
+ instance.options.autoResetAll ??
102
+ instance.options.autoResetPageIndex ??
103
+ !instance.options.manualPagination
93
104
  ) {
94
- instance.resetPageIndex()
105
+ if (queued) return
106
+ queued = true
107
+ instance._queue(() => {
108
+ instance.resetPageIndex()
109
+ queued = false
110
+ })
95
111
  }
96
112
  },
97
113
  setPagination: updater => {
@@ -101,18 +117,13 @@ export const Pagination = {
101
117
  return newState
102
118
  }
103
119
 
104
- return instance.options.onPaginationChange?.(
105
- safeUpdater,
106
- functionalUpdate(safeUpdater, instance.getState().pagination)
107
- )
120
+ return instance.options.onPaginationChange?.(safeUpdater)
108
121
  },
109
- resetPagination: () => {
122
+ resetPagination: defaultState => {
110
123
  instance.setPagination(
111
- instance.initialState.pagination ?? {
112
- pageIndex: 0,
113
- pageSize: 10,
114
- pageCount: -1,
115
- }
124
+ defaultState
125
+ ? getDefaultPaginationState()
126
+ : instance.initialState.pagination ?? getDefaultPaginationState()
116
127
  )
117
128
  },
118
129
  setPageIndex: updater => {
@@ -120,7 +131,7 @@ export const Pagination = {
120
131
  let pageIndex = functionalUpdate(updater, old.pageIndex)
121
132
 
122
133
  const maxPageIndex =
123
- old.pageCount && old.pageCount > 0
134
+ typeof old.pageCount !== 'undefined'
124
135
  ? old.pageCount - 1
125
136
  : Number.MAX_SAFE_INTEGER
126
137
 
@@ -132,11 +143,19 @@ export const Pagination = {
132
143
  }
133
144
  })
134
145
  },
135
- resetPageIndex: () => {
136
- instance.setPageIndex(0)
146
+ resetPageIndex: defaultState => {
147
+ instance.setPageIndex(
148
+ defaultState
149
+ ? defaultPageIndex
150
+ : instance.initialState?.pagination?.pageIndex ?? defaultPageIndex
151
+ )
137
152
  },
138
- resetPageSize: () => {
139
- instance.setPageSize(instance.initialState?.pagination?.pageSize ?? 10)
153
+ resetPageSize: defaultState => {
154
+ instance.setPageSize(
155
+ defaultState
156
+ ? defaultPageSize
157
+ : instance.initialState?.pagination?.pageSize ?? defaultPageSize
158
+ )
140
159
  },
141
160
  setPageSize: updater => {
142
161
  instance.setPagination(old => {
@@ -153,7 +172,7 @@ export const Pagination = {
153
172
  },
154
173
  setPageCount: updater =>
155
174
  instance.setPagination(old => {
156
- let newPageCount = functionalUpdate(updater, old.pageCount)
175
+ let newPageCount = functionalUpdate(updater, old.pageCount ?? -1)
157
176
 
158
177
  if (typeof newPageCount === 'number') {
159
178
  newPageCount = Math.max(-1, newPageCount)
@@ -178,7 +197,7 @@ export const Pagination = {
178
197
  return pageOptions
179
198
  },
180
199
  {
181
- key: 'getPageOptions',
200
+ key: process.env.NODE_ENV === 'development' && 'getPageOptions',
182
201
  debug: () => instance.options.debugAll ?? instance.options.debugTable,
183
202
  }
184
203
  ),
@@ -186,8 +205,9 @@ export const Pagination = {
186
205
  getCanPreviousPage: () => instance.getState().pagination.pageIndex > 0,
187
206
 
188
207
  getCanNextPage: () => {
189
- const { pageIndex, pageCount, pageSize } =
190
- instance.getState().pagination
208
+ const { pageIndex } = instance.getState().pagination
209
+
210
+ const pageCount = instance.getPageCount()
191
211
 
192
212
  if (pageCount === -1) {
193
213
  return true
@@ -197,13 +217,7 @@ export const Pagination = {
197
217
  return false
198
218
  }
199
219
 
200
- return (
201
- pageIndex <
202
- Math.ceil(
203
- instance.getPrePaginationRowModel().rows.length / pageSize
204
- ) -
205
- 1
206
- )
220
+ return pageIndex < pageCount - 1
207
221
  },
208
222
 
209
223
  previousPage: () => {
@@ -217,28 +231,29 @@ export const Pagination = {
217
231
  },
218
232
 
219
233
  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
- }
234
+ getPaginationRowModel: () => {
235
+ if (
236
+ !instance._getPaginationRowModel &&
237
+ instance.options.getPaginationRowModel
238
+ ) {
239
+ instance._getPaginationRowModel =
240
+ instance.options.getPaginationRowModel(instance)
241
+ }
230
242
 
231
- return paginateRowsFn(instance, rowModel)
232
- },
233
- {
234
- key: 'getPaginationRowModel',
235
- debug: () => instance.options.debugAll ?? instance.options.debugTable,
243
+ if (
244
+ instance.options.manualPagination ||
245
+ !instance._getPaginationRowModel
246
+ ) {
247
+ return instance.getPrePaginationRowModel()
236
248
  }
237
- ),
249
+
250
+ return instance._getPaginationRowModel()
251
+ },
238
252
 
239
253
  getPageCount: () => {
240
254
  const { pageCount } = instance.getState().pagination
241
- if (pageCount > 0) {
255
+
256
+ if (typeof pageCount !== 'undefined') {
242
257
  return pageCount
243
258
  }
244
259
 
@@ -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
  }