Skip to content

Commit 5907448

Browse files
authored
Merge pull request #146 from bindable-ui/dd/search3
Highlight Search Phrases
2 parents eb40982 + 5e12e8e commit 5907448

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

dev-app/app.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<c-nav-horizontal-item
5353
position="right"
5454
href="https://github.com/bindable-ui/bindable"
55-
title="v1.11.6"
55+
title="v1.11.7"
5656
></c-nav-horizontal-item>
5757
</c-nav-horizontal>
5858
</l-box>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@bindable-ui/bindable",
33
"description": "An Aurelia component library",
4-
"version": "1.11.6",
4+
"version": "1.11.7",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/bindable-ui/bindable"

src/components/tables/c-table/c-table-interfaces.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ export interface CTableCol {
7171
* If you need to highlight a search string in the table (currently only supported on c-td-truncate)
7272
*/
7373
getSearchVal?(): string;
74+
75+
/**
76+
* For custom search queries, pass in the words and phrases in an array that you would like to highlight
77+
*/
78+
getSearchPhrases?(): string[];
7479
}
7580

7681
export interface CTableActions {

src/components/tables/td-contents/c-td-truncate/c-td-truncate.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Licensed under the terms of the MIT license. See the LICENSE file in the project root for license terms.
44
*/
55

6+
import {highlightSearchPhrases} from '../../../../helpers/highlight-phrases';
67
import {highlightSearchText} from '../../../../helpers/highlight-text';
78

89
import * as styles from './c-td-truncate.css.json';
@@ -23,5 +24,12 @@ export class CTdTruncate {
2324
this.searchHighlight = highlightSearchText(searchVal, this.value);
2425
}
2526
}
27+
28+
if (model.col && _.isFunction(model.col.getSearchPhrases)) {
29+
const searchPhrases = model.col.getSearchPhrases();
30+
if (searchPhrases.length > 0) {
31+
this.searchHighlight = highlightSearchPhrases(searchPhrases, this.value);
32+
}
33+
}
2634
}
2735
}

src/helpers/highlight-phrases.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const highlightSearchPhrases = (searchPhrases: string[], matchAgainst?: string): string => {
2+
let title = matchAgainst || '';
3+
title = title
4+
.replace('&', '&amp;')
5+
.replace('<', '&lt;')
6+
.replace('>', '&gt;');
7+
if (searchPhrases && searchPhrases.length > 0) {
8+
searchPhrases.forEach(orig => {
9+
title = title.replace(orig, `<span style="background-color: #226684;">${orig}</span>`);
10+
});
11+
}
12+
return title;
13+
};

0 commit comments

Comments
 (0)