Skip to content

Commit a682617

Browse files
authored
Merge pull request #176 from grid-js/custom-global-search
feat(serach): adding selector function
2 parents da98b9d + ab53861 commit a682617

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

src/operator/search.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import Tabular from '../tabular';
22
import { VNode } from 'preact';
33
import { HTMLContentProps } from '../view/htmlElement';
4+
import { TCell } from '../types';
45

5-
export default function (keyword: string, tabular: Tabular): Tabular {
6+
export default function (
7+
keyword: string,
8+
tabular: Tabular,
9+
selector?: (cell: TCell, rowIndex: number, cellIndex: number) => string,
10+
): Tabular {
611
// escape special regex chars
712
keyword = keyword.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
813

914
return new Tabular(
10-
tabular.rows.filter((row) =>
11-
row.cells.some((cell) => {
12-
if (!cell || !cell.data) {
15+
tabular.rows.filter((row, rowIndex) =>
16+
row.cells.some((cell, cellIndex) => {
17+
if (!cell) {
1318
return false;
1419
}
1520

1621
let data = '';
1722

18-
if (typeof cell.data === 'object') {
23+
if (typeof selector === 'function') {
24+
data = selector(cell.data, rowIndex, cellIndex);
25+
} else if (typeof cell.data === 'object') {
1926
// HTMLContent element
2027
const element = cell.data as VNode<HTMLContentProps>;
21-
if (element.props && element.props.content) {
28+
if (element && element.props && element.props.content) {
2229
// TODO: we should only search in the content of the element. props.content is the entire HTML element
2330
data = element.props.content;
2431
}

src/pipeline/filter/globalSearch.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import {
55
PipelineProcessorProps,
66
ProcessorType,
77
} from '../processor';
8+
import { TCell } from '../../types';
89

910
interface GlobalSearchFilterProps extends PipelineProcessorProps {
1011
keyword: string;
12+
selector?: (cell: TCell, rowIndex: number, cellIndex: number) => string;
1113
}
1214

1315
class GlobalSearchFilter extends PipelineProcessor<
@@ -20,7 +22,11 @@ class GlobalSearchFilter extends PipelineProcessor<
2022

2123
_process(data: Tabular): Tabular {
2224
if (this.props.keyword) {
23-
return search(String(this.props.keyword).trim(), data);
25+
return search(
26+
String(this.props.keyword).trim(),
27+
data,
28+
this.props.selector,
29+
);
2430
}
2531

2632
return data;

src/view/plugin/search/search.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { SearchStore, SearchStoreState } from './store';
66
import { SearchActions } from './actions';
77
import ServerGlobalSearchFilter from '../../../pipeline/filter/serverGlobalSearch';
88
import { debounce } from '../../../util/debounce';
9+
import { TCell } from '../../../types';
910

1011
export interface SearchConfig {
1112
keyword?: string;
1213
enabled?: boolean;
1314
debounceTimeout?: number;
15+
selector?: (cell: TCell, rowIndex: number, cellIndex: number) => string;
1416
server?: {
1517
url?: (prevUrl: string, keyword: string) => string;
1618
body?: (prevBody: BodyInit, keyword: string) => BodyInit;
@@ -53,6 +55,7 @@ export class Search extends BaseComponent<SearchConfig & BaseProps, {}> {
5355
} else {
5456
searchProcessor = new GlobalSearchFilter({
5557
keyword: props.keyword,
58+
selector: props.selector,
5659
});
5760
}
5861

tests/operator/search.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,20 @@ describe('search', () => {
4242
new Tabular([row3]).rows,
4343
);
4444
});
45+
46+
it('should use the selector with hardcoded string', () => {
47+
expect(search('test', tabular, () => 'custom keyword').rows).toStrictEqual(
48+
new Tabular([]).rows,
49+
);
50+
});
51+
52+
it('should use the selector with dynamic string', () => {
53+
expect(
54+
search(
55+
'00',
56+
tabular,
57+
(_, rowIndex, cellIndex) => `${rowIndex}${cellIndex}`,
58+
).rows,
59+
).toStrictEqual(new Tabular([row1]).rows);
60+
});
4561
});

0 commit comments

Comments
 (0)