package.build.lib.features.Grouping.js.map Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of table-core Show documentation
Show all versions of table-core Show documentation
Headless UI for building powerful tables & datagrids for TS/JS.
The newest version!
{"version":3,"file":"Grouping.js","sources":["../../../src/features/Grouping.ts"],"sourcesContent":["import { RowModel } from '..'\nimport { BuiltInAggregationFn, aggregationFns } from '../aggregationFns'\nimport { TableFeature } from '../core/table'\nimport {\n Cell,\n Column,\n OnChangeFn,\n Table,\n Row,\n Updater,\n ColumnDefTemplate,\n RowData,\n AggregationFns,\n} from '../types'\nimport { isFunction, makeStateUpdater } from '../utils'\n\nexport type GroupingState = string[]\n\nexport interface GroupingTableState {\n grouping: GroupingState\n}\n\nexport type AggregationFn = (\n columnId: string,\n leafRows: Row[],\n childRows: Row[]\n) => any\n\nexport type CustomAggregationFns = Record>\n\nexport type AggregationFnOption =\n | 'auto'\n | keyof AggregationFns\n | BuiltInAggregationFn\n | AggregationFn\n\nexport interface GroupingColumnDef {\n aggregationFn?: AggregationFnOption\n aggregatedCell?: ColumnDefTemplate<\n ReturnType['getContext']>\n >\n enableGrouping?: boolean\n getGroupingValue?: (row: TData) => any\n}\n\nexport interface GroupingColumn {\n getCanGroup: () => boolean\n getIsGrouped: () => boolean\n getGroupedIndex: () => number\n toggleGrouping: () => void\n getToggleGroupingHandler: () => () => void\n getAutoAggregationFn: () => AggregationFn | undefined\n getAggregationFn: () => AggregationFn | undefined\n}\n\nexport interface GroupingRow {\n groupingColumnId?: string\n groupingValue?: unknown\n getIsGrouped: () => boolean\n getGroupingValue: (columnId: string) => unknown\n _groupingValuesCache: Record\n}\n\nexport interface GroupingCell {\n getIsGrouped: () => boolean\n getIsPlaceholder: () => boolean\n getIsAggregated: () => boolean\n}\n\nexport interface ColumnDefaultOptions {\n // Column\n onGroupingChange: OnChangeFn\n enableGrouping: boolean\n}\n\ninterface GroupingOptionsBase {\n manualGrouping?: boolean\n onGroupingChange?: OnChangeFn\n enableGrouping?: boolean\n getGroupedRowModel?: (table: Table) => () => RowModel\n groupedColumnMode?: false | 'reorder' | 'remove'\n}\n\ntype ResolvedAggregationFns = keyof AggregationFns extends never\n ? {\n aggregationFns?: Record>\n }\n : {\n aggregationFns: Record>\n }\n\nexport interface GroupingOptions\n extends GroupingOptionsBase,\n ResolvedAggregationFns {}\n\nexport type GroupingColumnMode = false | 'reorder' | 'remove'\n\nexport interface GroupingInstance {\n setGrouping: (updater: Updater) => void\n resetGrouping: (defaultState?: boolean) => void\n getPreGroupedRowModel: () => RowModel\n getGroupedRowModel: () => RowModel\n _getGroupedRowModel?: () => RowModel\n}\n\n//\n\nexport const Grouping: TableFeature = {\n getDefaultColumnDef: (): GroupingColumnDef<\n TData,\n unknown\n > => {\n return {\n aggregatedCell: props => (props.getValue() as any)?.toString?.() ?? null,\n aggregationFn: 'auto',\n }\n },\n\n getInitialState: (state): GroupingTableState => {\n return {\n grouping: [],\n ...state,\n }\n },\n\n getDefaultOptions: (\n table: Table\n ): GroupingOptions => {\n return {\n onGroupingChange: makeStateUpdater('grouping', table),\n groupedColumnMode: 'reorder',\n }\n },\n\n createColumn: (\n column: Column,\n table: Table\n ): GroupingColumn => {\n return {\n toggleGrouping: () => {\n table.setGrouping(old => {\n // Find any existing grouping for this column\n if (old?.includes(column.id)) {\n return old.filter(d => d !== column.id)\n }\n\n return [...(old ?? []), column.id]\n })\n },\n\n getCanGroup: () => {\n return (\n column.columnDef.enableGrouping ??\n true ??\n table.options.enableGrouping ??\n true ??\n !!column.accessorFn\n )\n },\n\n getIsGrouped: () => {\n return table.getState().grouping?.includes(column.id)\n },\n\n getGroupedIndex: () => table.getState().grouping?.indexOf(column.id),\n\n getToggleGroupingHandler: () => {\n const canGroup = column.getCanGroup()\n\n return () => {\n if (!canGroup) return\n column.toggleGrouping()\n }\n },\n getAutoAggregationFn: () => {\n const firstRow = table.getCoreRowModel().flatRows[0]\n\n const value = firstRow?.getValue(column.id)\n\n if (typeof value === 'number') {\n return aggregationFns.sum\n }\n\n if (Object.prototype.toString.call(value) === '[object Date]') {\n return aggregationFns.extent\n }\n },\n getAggregationFn: () => {\n if (!column) {\n throw new Error()\n }\n\n return isFunction(column.columnDef.aggregationFn)\n ? column.columnDef.aggregationFn\n : column.columnDef.aggregationFn === 'auto'\n ? column.getAutoAggregationFn()\n : table.options.aggregationFns?.[\n column.columnDef.aggregationFn as string\n ] ??\n aggregationFns[\n column.columnDef.aggregationFn as BuiltInAggregationFn\n ]\n },\n }\n },\n\n createTable: (\n table: Table\n ): GroupingInstance => {\n return {\n setGrouping: updater => table.options.onGroupingChange?.(updater),\n\n resetGrouping: defaultState => {\n table.setGrouping(\n defaultState ? [] : table.initialState?.grouping ?? []\n )\n },\n\n getPreGroupedRowModel: () => table.getFilteredRowModel(),\n getGroupedRowModel: () => {\n if (!table._getGroupedRowModel && table.options.getGroupedRowModel) {\n table._getGroupedRowModel = table.options.getGroupedRowModel(table)\n }\n\n if (table.options.manualGrouping || !table._getGroupedRowModel) {\n return table.getPreGroupedRowModel()\n }\n\n return table._getGroupedRowModel()\n },\n }\n },\n\n createRow: (\n row: Row,\n table: Table\n ): GroupingRow => {\n return {\n getIsGrouped: () => !!row.groupingColumnId,\n getGroupingValue: columnId => {\n if (row._groupingValuesCache.hasOwnProperty(columnId)) {\n return row._groupingValuesCache[columnId]\n }\n\n const column = table.getColumn(columnId)\n\n if (!column?.columnDef.getGroupingValue) {\n return row.getValue(columnId)\n }\n\n row._groupingValuesCache[columnId] = column.columnDef.getGroupingValue(\n row.original\n )\n\n return row._groupingValuesCache[columnId]\n },\n _groupingValuesCache: {},\n }\n },\n\n createCell: (\n cell: Cell,\n column: Column,\n row: Row,\n table: Table\n ): GroupingCell => {\n const getRenderValue = () =>\n cell.getValue() ?? table.options.renderFallbackValue\n\n return {\n getIsGrouped: () =>\n column.getIsGrouped() && column.id === row.groupingColumnId,\n getIsPlaceholder: () => !cell.getIsGrouped() && column.getIsGrouped(),\n getIsAggregated: () =>\n !cell.getIsGrouped() &&\n !cell.getIsPlaceholder() &&\n !!row.subRows?.length,\n }\n },\n}\n\nexport function orderColumns(\n leafColumns: Column[],\n grouping: string[],\n groupedColumnMode?: GroupingColumnMode\n) {\n if (!grouping?.length || !groupedColumnMode) {\n return leafColumns\n }\n\n const nonGroupingColumns = leafColumns.filter(\n col => !grouping.includes(col.id)\n )\n\n if (groupedColumnMode === 'remove') {\n return nonGroupingColumns\n }\n\n const groupingColumns = grouping\n .map(g => leafColumns.find(col => col.id === g)!)\n .filter(Boolean)\n\n return [...groupingColumns, ...nonGroupingColumns]\n}\n"],"names":["Grouping","getDefaultColumnDef","aggregatedCell","props","_toString","_props$getValue","getValue","toString","aggregationFn","getInitialState","state","grouping","getDefaultOptions","table","onGroupingChange","makeStateUpdater","groupedColumnMode","createColumn","column","toggleGrouping","setGrouping","old","includes","id","filter","d","getCanGroup","_ref","_ref2","_ref3","_column$columnDef$ena","columnDef","enableGrouping","options","accessorFn","getIsGrouped","_table$getState$group","getState","getGroupedIndex","_table$getState$group2","indexOf","getToggleGroupingHandler","canGroup","getAutoAggregationFn","firstRow","getCoreRowModel","flatRows","value","aggregationFns","sum","Object","prototype","call","extent","getAggregationFn","_table$options$aggreg","_table$options$aggreg2","Error","isFunction","createTable","updater","resetGrouping","defaultState","_table$initialState$g","_table$initialState","initialState","getPreGroupedRowModel","getFilteredRowModel","getGroupedRowModel","_getGroupedRowModel","manualGrouping","createRow","row","groupingColumnId","getGroupingValue","columnId","_groupingValuesCache","hasOwnProperty","getColumn","original","createCell","cell","getIsPlaceholder","getIsAggregated","_row$subRows","subRows","length","orderColumns","leafColumns","nonGroupingColumns","col","groupingColumns","map","g","find","Boolean"],"mappings":";;;;;;;;;;;;;;;;;AAyGA;;AAEO,MAAMA,QAAsB,GAAG;EACpCC,mBAAmB,EAAEA,MAGhB;IACH,OAAO;AACLC,MAAAA,cAAc,EAAEC,KAAK,IAAA;QAAA,IAAAC,SAAA,EAAAC,eAAA,CAAA;QAAA,OAAAD,CA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