|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { RowModel, Table, createRow, Row, RowData } from "@tanstack/react-table"; |
| 3 | + |
| 4 | +import { SyncCatalogUIModel } from "./SyncCatalogTable"; |
| 5 | + |
| 6 | +/** |
| 7 | + * This method is unchanged from the original implementation, except for removing the generic typings: |
| 8 | + * https://github.com/TanStack/table/blob/v8.7.0/packages/table-core/src/utils/filterRowsUtils.ts#L4C1-L14C2 |
| 9 | + */ |
| 10 | +export function filterRows( |
| 11 | + rows: Array<Row<SyncCatalogUIModel>>, |
| 12 | + filterRowImpl: (row: Row<SyncCatalogUIModel>) => any, |
| 13 | + table: Table<SyncCatalogUIModel> |
| 14 | +) { |
| 15 | + if (table.options.filterFromLeafRows) { |
| 16 | + return filterRowModelFromLeafs(rows, filterRowImpl, table); |
| 17 | + } |
| 18 | + |
| 19 | + return filterRowModelFromRoot(rows, filterRowImpl, table); |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * This method has been customized to always include sibling fields of a matching row. Original implementation: |
| 24 | + * https://github.com/TanStack/table/blob/v8.7.0/packages/table-core/src/utils/filterRowsUtils.ts#L16-L76 |
| 25 | + */ |
| 26 | +function filterRowModelFromLeafs( |
| 27 | + rowsToFilter: Array<Row<SyncCatalogUIModel>>, |
| 28 | + filterRow: (row: Row<SyncCatalogUIModel>) => Array<Row<SyncCatalogUIModel>>, |
| 29 | + table: Table<SyncCatalogUIModel> |
| 30 | +): RowModel<SyncCatalogUIModel> { |
| 31 | + const maxDepth = table.options.maxLeafRowFilterDepth ?? 100; |
| 32 | + |
| 33 | + const recurseFilterRows = (rowsToFilter: Array<Row<SyncCatalogUIModel>>, depth = 0) => { |
| 34 | + const rows: Array<Row<SyncCatalogUIModel>> = []; |
| 35 | + |
| 36 | + for (let i = 0; i < rowsToFilter.length; i++) { |
| 37 | + const row = rowsToFilter[i]; |
| 38 | + |
| 39 | + const newRow = createRow(table, row.id, row.original, row.index, row.depth); |
| 40 | + newRow.columnFilters = row.columnFilters; |
| 41 | + |
| 42 | + if (row.subRows?.length && depth < maxDepth) { |
| 43 | + // Recursing again here to do a depth first search (deep leaf nodes first) |
| 44 | + newRow.subRows = recurseFilterRows(row.subRows, depth + 1); |
| 45 | + |
| 46 | + // If a stream is a match, include all its subrows |
| 47 | + if (newRow.original.rowType === "stream" && filterRow(newRow)) { |
| 48 | + newRow.subRows = rowsToFilter[i].subRows; |
| 49 | + rows.push(newRow); |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + // If a stream contains at least one match, include all its subrows |
| 54 | + if (newRow.original.rowType === "stream" && newRow.subRows.length) { |
| 55 | + newRow.subRows = rowsToFilter[i].subRows; |
| 56 | + rows.push(newRow); |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + if (filterRow(newRow) && !newRow.subRows.length) { |
| 61 | + rows.push(newRow); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if (filterRow(newRow) || newRow.subRows.length) { |
| 66 | + rows.push(newRow); |
| 67 | + continue; |
| 68 | + } |
| 69 | + } else if (filterRow(newRow)) { |
| 70 | + rows.push(newRow); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return rows; |
| 75 | + }; |
| 76 | + |
| 77 | + const filteredRows: Array<Row<SyncCatalogUIModel>> = recurseFilterRows(rowsToFilter); |
| 78 | + const filteredRowsById: Record<string, Row<SyncCatalogUIModel>> = {}; |
| 79 | + |
| 80 | + function flattenRows(rows: Array<Row<SyncCatalogUIModel>>) { |
| 81 | + const flattenedRows: Array<Row<SyncCatalogUIModel>> = []; |
| 82 | + for (let i = 0; i < rows.length; i++) { |
| 83 | + const row = rows[i]; |
| 84 | + flattenedRows.push(row); |
| 85 | + filteredRowsById[row.id] = row; |
| 86 | + if (row.subRows) { |
| 87 | + flattenedRows.push(...flattenRows(row.subRows)); |
| 88 | + } |
| 89 | + } |
| 90 | + return flattenedRows; |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + rows: filteredRows, |
| 95 | + rowsById: filteredRowsById, |
| 96 | + flatRows: flattenRows(filteredRows), |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * This method is unchanged from the original implementation: |
| 102 | + * https://github.com/TanStack/table/blob/v8.7.0/packages/table-core/src/utils/filterRowsUtils.ts#L78-L126 |
| 103 | + */ |
| 104 | +export function filterRowModelFromRoot<TData extends RowData>( |
| 105 | + rowsToFilter: Array<Row<TData>>, |
| 106 | + filterRow: (row: Row<TData>) => any, |
| 107 | + table: Table<TData> |
| 108 | +): RowModel<TData> { |
| 109 | + const newFilteredFlatRows: Array<Row<TData>> = []; |
| 110 | + const newFilteredRowsById: Record<string, Row<TData>> = {}; |
| 111 | + const maxDepth = table.options.maxLeafRowFilterDepth ?? 100; |
| 112 | + |
| 113 | + // Filters top level and nested rows |
| 114 | + const recurseFilterRows = (rowsToFilter: Array<Row<TData>>, depth = 0) => { |
| 115 | + // Filter from parents downward first |
| 116 | + |
| 117 | + const rows = []; |
| 118 | + |
| 119 | + // Apply the filter to any subRows |
| 120 | + for (let i = 0; i < rowsToFilter.length; i++) { |
| 121 | + let row = rowsToFilter[i]; |
| 122 | + |
| 123 | + const pass = filterRow(row); |
| 124 | + |
| 125 | + if (pass) { |
| 126 | + if (row.subRows?.length && depth < maxDepth) { |
| 127 | + const newRow = createRow(table, row.id, row.original, row.index, row.depth); |
| 128 | + newRow.subRows = recurseFilterRows(row.subRows, depth + 1); |
| 129 | + row = newRow; |
| 130 | + } |
| 131 | + |
| 132 | + rows.push(row); |
| 133 | + newFilteredFlatRows.push(row); |
| 134 | + newFilteredRowsById[row.id] = row; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return rows; |
| 139 | + }; |
| 140 | + |
| 141 | + return { |
| 142 | + rows: recurseFilterRows(rowsToFilter), |
| 143 | + flatRows: newFilteredFlatRows, |
| 144 | + rowsById: newFilteredRowsById, |
| 145 | + }; |
| 146 | +} |
0 commit comments