Skip to content

Commit

Permalink
Merge pull request #146 from bindable-ui/dd/search3
Browse files Browse the repository at this point in the history
Highlight Search Phrases
  • Loading branch information
djedi authored Sep 20, 2022
2 parents eb40982 + 5e12e8e commit 5907448
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dev-app/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<c-nav-horizontal-item
position="right"
href="https://github.com/bindable-ui/bindable"
title="v1.11.6"
title="v1.11.7"
></c-nav-horizontal-item>
</c-nav-horizontal>
</l-box>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@bindable-ui/bindable",
"description": "An Aurelia component library",
"version": "1.11.6",
"version": "1.11.7",
"repository": {
"type": "git",
"url": "https://github.com/bindable-ui/bindable"
Expand Down
5 changes: 5 additions & 0 deletions src/components/tables/c-table/c-table-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export interface CTableCol {
* If you need to highlight a search string in the table (currently only supported on c-td-truncate)
*/
getSearchVal?(): string;

/**
* For custom search queries, pass in the words and phrases in an array that you would like to highlight
*/
getSearchPhrases?(): string[];
}

export interface CTableActions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Licensed under the terms of the MIT license. See the LICENSE file in the project root for license terms.
*/

import {highlightSearchPhrases} from '../../../../helpers/highlight-phrases';
import {highlightSearchText} from '../../../../helpers/highlight-text';

import * as styles from './c-td-truncate.css.json';
Expand All @@ -23,5 +24,12 @@ export class CTdTruncate {
this.searchHighlight = highlightSearchText(searchVal, this.value);
}
}

if (model.col && _.isFunction(model.col.getSearchPhrases)) {
const searchPhrases = model.col.getSearchPhrases();
if (searchPhrases.length > 0) {
this.searchHighlight = highlightSearchPhrases(searchPhrases, this.value);
}
}
}
}
13 changes: 13 additions & 0 deletions src/helpers/highlight-phrases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const highlightSearchPhrases = (searchPhrases: string[], matchAgainst?: string): string => {
let title = matchAgainst || '';
title = title
.replace('&', '&amp;')
.replace('<', '&lt;')
.replace('>', '&gt;');
if (searchPhrases && searchPhrases.length > 0) {
searchPhrases.forEach(orig => {
title = title.replace(orig, `<span style="background-color: #226684;">${orig}</span>`);
});
}
return title;
};

0 comments on commit 5907448

Please sign in to comment.