|
1 | | -import { RowModel } from '..' |
2 | | -import { BuiltInAggregationFn, aggregationFns } from '../aggregationFns' |
| 1 | +import { BuiltInAggregationFn } from '../../aggregationFns' |
3 | 2 | import { |
4 | 3 | AggregationFns, |
5 | 4 | Cell, |
6 | | - Column, |
7 | 5 | ColumnDefTemplate, |
8 | 6 | OnChangeFn, |
9 | 7 | Row, |
10 | 8 | RowData, |
| 9 | + RowModel, |
11 | 10 | Table, |
12 | | - TableFeature, |
13 | 11 | Updater, |
14 | | -} from '../types' |
15 | | -import { isFunction, makeStateUpdater } from '../utils' |
| 12 | +} from '../../types' |
16 | 13 |
|
17 | 14 | export type GroupingState = string[] |
18 | 15 |
|
@@ -236,187 +233,3 @@ export interface GroupingInstance<TData extends RowData> { |
236 | 233 | */ |
237 | 234 | setGrouping: (updater: Updater<GroupingState>) => void |
238 | 235 | } |
239 | | - |
240 | | -// |
241 | | - |
242 | | -export const ColumnGrouping: TableFeature = { |
243 | | - _getDefaultColumnDef: <TData extends RowData>(): GroupingColumnDef< |
244 | | - TData, |
245 | | - unknown |
246 | | - > => { |
247 | | - return { |
248 | | - aggregatedCell: props => (props.getValue() as any)?.toString?.() ?? null, |
249 | | - aggregationFn: 'auto', |
250 | | - } |
251 | | - }, |
252 | | - |
253 | | - _getInitialState: (state): GroupingTableState => { |
254 | | - return { |
255 | | - grouping: [], |
256 | | - ...state, |
257 | | - } |
258 | | - }, |
259 | | - |
260 | | - _getDefaultOptions: <TData extends RowData>( |
261 | | - table: Table<TData> |
262 | | - ): GroupingOptions => { |
263 | | - return { |
264 | | - onGroupingChange: makeStateUpdater('grouping', table), |
265 | | - groupedColumnMode: 'reorder', |
266 | | - } |
267 | | - }, |
268 | | - |
269 | | - _createColumn: <TData extends RowData, TValue>( |
270 | | - column: Column<TData, TValue>, |
271 | | - table: Table<TData> |
272 | | - ): void => { |
273 | | - column.toggleGrouping = () => { |
274 | | - table.setGrouping(old => { |
275 | | - // Find any existing grouping for this column |
276 | | - if (old?.includes(column.id)) { |
277 | | - return old.filter(d => d !== column.id) |
278 | | - } |
279 | | - |
280 | | - return [...(old ?? []), column.id] |
281 | | - }) |
282 | | - } |
283 | | - |
284 | | - column.getCanGroup = () => { |
285 | | - return ( |
286 | | - (column.columnDef.enableGrouping ?? true) && |
287 | | - (table.options.enableGrouping ?? true) && |
288 | | - (!!column.accessorFn || !!column.columnDef.getGroupingValue) |
289 | | - ) |
290 | | - } |
291 | | - |
292 | | - column.getIsGrouped = () => { |
293 | | - return table.getState().grouping?.includes(column.id) |
294 | | - } |
295 | | - |
296 | | - column.getGroupedIndex = () => table.getState().grouping?.indexOf(column.id) |
297 | | - |
298 | | - column.getToggleGroupingHandler = () => { |
299 | | - const canGroup = column.getCanGroup() |
300 | | - |
301 | | - return () => { |
302 | | - if (!canGroup) return |
303 | | - column.toggleGrouping() |
304 | | - } |
305 | | - } |
306 | | - column.getAutoAggregationFn = () => { |
307 | | - const firstRow = table.getCoreRowModel().flatRows[0] |
308 | | - |
309 | | - const value = firstRow?.getValue(column.id) |
310 | | - |
311 | | - if (typeof value === 'number') { |
312 | | - return aggregationFns.sum |
313 | | - } |
314 | | - |
315 | | - if (Object.prototype.toString.call(value) === '[object Date]') { |
316 | | - return aggregationFns.extent |
317 | | - } |
318 | | - } |
319 | | - column.getAggregationFn = () => { |
320 | | - if (!column) { |
321 | | - throw new Error() |
322 | | - } |
323 | | - |
324 | | - return isFunction(column.columnDef.aggregationFn) |
325 | | - ? column.columnDef.aggregationFn |
326 | | - : column.columnDef.aggregationFn === 'auto' |
327 | | - ? column.getAutoAggregationFn() |
328 | | - : table.options.aggregationFns?.[ |
329 | | - column.columnDef.aggregationFn as string |
330 | | - ] ?? |
331 | | - aggregationFns[ |
332 | | - column.columnDef.aggregationFn as BuiltInAggregationFn |
333 | | - ] |
334 | | - } |
335 | | - }, |
336 | | - |
337 | | - _createTable: <TData extends RowData>(table: Table<TData>): void => { |
338 | | - table.setGrouping = updater => table.options.onGroupingChange?.(updater) |
339 | | - |
340 | | - table.resetGrouping = defaultState => { |
341 | | - table.setGrouping(defaultState ? [] : table.initialState?.grouping ?? []) |
342 | | - } |
343 | | - |
344 | | - table.getPreGroupedRowModel = () => table.getFilteredRowModel() |
345 | | - table.getGroupedRowModel = () => { |
346 | | - if (!table._getGroupedRowModel && table.options.getGroupedRowModel) { |
347 | | - table._getGroupedRowModel = table.options.getGroupedRowModel(table) |
348 | | - } |
349 | | - |
350 | | - if (table.options.manualGrouping || !table._getGroupedRowModel) { |
351 | | - return table.getPreGroupedRowModel() |
352 | | - } |
353 | | - |
354 | | - return table._getGroupedRowModel() |
355 | | - } |
356 | | - }, |
357 | | - |
358 | | - _createRow: <TData extends RowData>( |
359 | | - row: Row<TData>, |
360 | | - table: Table<TData> |
361 | | - ): void => { |
362 | | - row.getIsGrouped = () => !!row.groupingColumnId |
363 | | - row.getGroupingValue = columnId => { |
364 | | - if (row._groupingValuesCache.hasOwnProperty(columnId)) { |
365 | | - return row._groupingValuesCache[columnId] |
366 | | - } |
367 | | - |
368 | | - const column = table.getColumn(columnId) |
369 | | - |
370 | | - if (!column?.columnDef.getGroupingValue) { |
371 | | - return row.getValue(columnId) |
372 | | - } |
373 | | - |
374 | | - row._groupingValuesCache[columnId] = column.columnDef.getGroupingValue( |
375 | | - row.original |
376 | | - ) |
377 | | - |
378 | | - return row._groupingValuesCache[columnId] |
379 | | - } |
380 | | - row._groupingValuesCache = {} |
381 | | - }, |
382 | | - |
383 | | - _createCell: <TData extends RowData, TValue>( |
384 | | - cell: Cell<TData, TValue>, |
385 | | - column: Column<TData, TValue>, |
386 | | - row: Row<TData>, |
387 | | - table: Table<TData> |
388 | | - ): void => { |
389 | | - const getRenderValue = () => |
390 | | - cell.getValue() ?? table.options.renderFallbackValue |
391 | | - |
392 | | - cell.getIsGrouped = () => |
393 | | - column.getIsGrouped() && column.id === row.groupingColumnId |
394 | | - cell.getIsPlaceholder = () => !cell.getIsGrouped() && column.getIsGrouped() |
395 | | - cell.getIsAggregated = () => |
396 | | - !cell.getIsGrouped() && !cell.getIsPlaceholder() && !!row.subRows?.length |
397 | | - }, |
398 | | -} |
399 | | - |
400 | | -export function orderColumns<TData extends RowData>( |
401 | | - leafColumns: Column<TData, unknown>[], |
402 | | - grouping: string[], |
403 | | - groupedColumnMode?: GroupingColumnMode |
404 | | -) { |
405 | | - if (!grouping?.length || !groupedColumnMode) { |
406 | | - return leafColumns |
407 | | - } |
408 | | - |
409 | | - const nonGroupingColumns = leafColumns.filter( |
410 | | - col => !grouping.includes(col.id) |
411 | | - ) |
412 | | - |
413 | | - if (groupedColumnMode === 'remove') { |
414 | | - return nonGroupingColumns |
415 | | - } |
416 | | - |
417 | | - const groupingColumns = grouping |
418 | | - .map(g => leafColumns.find(col => col.id === g)!) |
419 | | - .filter(Boolean) |
420 | | - |
421 | | - return [...groupingColumns, ...nonGroupingColumns] |
422 | | -} |
0 commit comments