-
-
Notifications
You must be signed in to change notification settings - Fork 902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: highlight search term in labels in dropdown list #233
feat: highlight search term in labels in dropdown list #233
Conversation
3f0aebe
to
57ad5f0
Compare
57ad5f0
to
59af579
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BenjaminLefevreGithub Thanks for contributing. I tested it locally. I like how it looks, but there are some issues. It would be great if you create custom directive which would be responsible for highlighting.
@@ -53,7 +53,7 @@ | |||
[class.marked]="item === itemsList.markedItem"> | |||
|
|||
<ng-template #defaultOptionTemplate> | |||
<span class="ng-option-label" [innerHTML]="item.label"></span> | |||
<span class="ng-option-label" [innerHTML]="item.highlightedLabel"></span> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach is limited to default filter only and will not work if using custom [typeahead]. It would be better to create directive [ngOptionHighlight]="filterValue" which would be responsible for highlighting label.
<span [ngOptionHighlight]="filterValue" class="ng-option-label" [innerHTML]="item.label"></span>
Also users will be able to apply this directive even if they are using custom option templates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep good idea, done.
src/ng-select/items-list.ts
Outdated
}; | ||
} | ||
|
||
private _highlightLabelWithSearchTerm(option: NgOption, term: string, indexOfTerm: number) { | ||
if (indexOfTerm > -1 && !option.selected && !option.disabled) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed the end index was wrong on the third substring
0d05e54
to
fde53f7
Compare
@anjmao thanks for the review. I fixed the bug (mistake on substring ;) the end index is not included :p) And tried to make what you said: make a directive. I fixed also a bug discovered during my tests (https://ng-select.github.io/ng-select#/data-sources : console error log when trying to filter on the object array (true, 'Two', 3). |
fde53f7
to
f7f5ace
Compare
… it also (demos have been updated)
f7f5ace
to
ac23f6d
Compare
@Directive({ | ||
selector: '[ngOptionHighlight]' | ||
}) | ||
export class NgOptionHighlightDirective implements OnChanges { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks nice, only add some unit tests for this directive and I can merge it. You can create new test file called ng-option-highlight.directive.spec.ts
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem I will add some unit tests tonight
Just add unit test for the directive |
#152
highlight search term in the items with a default style 'hightlight' (bold and underline text)