-
Notifications
You must be signed in to change notification settings - Fork 24
/
addTableFilter.ts
170 lines (157 loc) · 4.72 KB
/
addTableFilter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import type { BodyRow } from '../bodyRows';
import type { TablePlugin, NewTablePropSet, DeriveRowsFn } from '../types/TablePlugin';
import { recordSetStore } from '../utils/store';
import { derived, writable, type Readable, type Writable } from 'svelte/store';
export interface TableFilterConfig {
fn?: TableFilterFn;
initialFilterValue?: string;
includeHiddenColumns?: boolean;
serverSide?: boolean;
}
export interface TableFilterState<Item> {
filterValue: Writable<string>;
preFilteredRows: Readable<BodyRow<Item>[]>;
}
// Item generic needed to infer type on `getFilteredRows`
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface TableFilterColumnOptions<Item> {
exclude?: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getFilterValue?: (value: any) => string;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type TableFilterFn = (props: TableFilterFnProps) => boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type TableFilterFnProps = {
filterValue: string;
value: string;
};
export type TableFilterPropSet = NewTablePropSet<{
'tbody.tr.td': {
matches: boolean;
};
}>;
interface GetFilteredRowsOptions {
tableCellMatches: Record<string, boolean>;
fn: TableFilterFn;
includeHiddenColumns: boolean;
}
const getFilteredRows = <Item, Row extends BodyRow<Item>>(
rows: Row[],
filterValue: string,
columnOptions: Record<string, TableFilterColumnOptions<Item>>,
{ tableCellMatches, fn, includeHiddenColumns }: GetFilteredRowsOptions
): Row[] => {
const $filteredRows = rows
// Filter `subRows`
.map((row) => {
const { subRows } = row;
if (subRows === undefined) {
return row;
}
const filteredSubRows = getFilteredRows(subRows, filterValue, columnOptions, {
tableCellMatches,
fn,
includeHiddenColumns,
});
const clonedRow = row.clone() as Row;
clonedRow.subRows = filteredSubRows;
return clonedRow;
})
.filter((row) => {
if ((row.subRows?.length ?? 0) !== 0) {
return true;
}
// An array of booleans, true if the cell matches the filter.
const rowCellMatches = Object.values(row.cellForId).map((cell) => {
const options = columnOptions[cell.id] as TableFilterColumnOptions<Item> | undefined;
if (options?.exclude === true) {
return false;
}
const isHidden = row.cells.find((c) => c.id === cell.id) === undefined;
if (isHidden && !includeHiddenColumns) {
return false;
}
if (!cell.isData()) {
return false;
}
let value = cell.value;
if (options?.getFilterValue !== undefined) {
value = options?.getFilterValue(value);
}
const matches = fn({ value: String(value), filterValue });
if (matches) {
const dataRowColId = cell.dataRowColId();
if (dataRowColId !== undefined) {
tableCellMatches[dataRowColId] = matches;
}
}
return matches;
});
// If any cell matches, include in the filtered results.
return rowCellMatches.includes(true);
});
return $filteredRows;
};
export const addTableFilter =
<Item>({
fn = textPrefixFilter,
initialFilterValue = '',
includeHiddenColumns = false,
serverSide = false,
}: TableFilterConfig = {}): TablePlugin<
Item,
TableFilterState<Item>,
TableFilterColumnOptions<Item>,
TableFilterPropSet
> =>
({ columnOptions }) => {
const filterValue = writable(initialFilterValue);
const preFilteredRows = writable<BodyRow<Item>[]>([]);
const tableCellMatches = recordSetStore();
const pluginState: TableFilterState<Item> = { filterValue, preFilteredRows };
const deriveRows: DeriveRowsFn<Item> = (rows) => {
return derived([rows, filterValue], ([$rows, $filterValue]) => {
preFilteredRows.set($rows);
tableCellMatches.clear();
const $tableCellMatches: Record<string, boolean> = {};
const $filteredRows = getFilteredRows($rows, $filterValue, columnOptions, {
tableCellMatches: $tableCellMatches,
fn,
includeHiddenColumns,
});
tableCellMatches.set($tableCellMatches);
if (serverSide) {
return $rows;
}
return $filteredRows;
});
};
return {
pluginState,
deriveRows,
hooks: {
'tbody.tr.td': (cell) => {
const props = derived(
[filterValue, tableCellMatches],
([$filterValue, $tableCellMatches]) => {
const dataRowColId = cell.dataRowColId();
return {
matches:
$filterValue !== '' &&
dataRowColId !== undefined &&
($tableCellMatches[dataRowColId] ?? false),
};
}
);
return { props };
},
},
};
};
export const textPrefixFilter: TableFilterFn = ({ filterValue, value }) => {
if (filterValue === '') {
return true;
}
return String(value).toLowerCase().startsWith(String(filterValue).toLowerCase());
};