diff --git a/src/app/app.component.css b/src/app/app.component.css
index 6fc5dac..ff63d4d 100644
--- a/src/app/app.component.css
+++ b/src/app/app.component.css
@@ -27,6 +27,11 @@ input.search {
inset 0 0 2px rgba(0, 0, 0, 0.19);
}
+input.search:disabled {
+ background: white;
+ cursor: not-allowed;
+}
+
.suggestions {
margin: 0;
padding: 0;
diff --git a/src/app/app.component.html b/src/app/app.component.html
index c5fbf03..9a0e114 100644
--- a/src/app/app.component.html
+++ b/src/app/app.component.html
@@ -11,12 +11,12 @@
@if (error) {
{{`HTTP error ${error.message}`}}
} @else {
-
+
@for (disease of filteredDiseases(); track $index) {
-
- {{ disease.name }},
- {{ disease.code }}
+ {{ disease.name }},
+ {{ disease.code }}
{{ disease.abbr }}
diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 4c4b986..6a963f0 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -1,9 +1,10 @@
import { Component, computed, inject, signal } from '@angular/core';
import { DiseaseService } from './shared/services/disease/disease.service';
import { disabled, form, FormField } from '@angular/forms/signals';
+import { HighlightDirective } from './shared/highlight/highlight.directive';
@Component({
- imports: [FormField],
+ imports: [FormField, HighlightDirective],
selector: 'app-root',
styleUrl: './app.component.css',
templateUrl: './app.component.html',
diff --git a/src/app/shared/highlight/highlight.directive.ts b/src/app/shared/highlight/highlight.directive.ts
new file mode 100644
index 0000000..aa727bd
--- /dev/null
+++ b/src/app/shared/highlight/highlight.directive.ts
@@ -0,0 +1,68 @@
+import {
+ afterRenderEffect,
+ contentChildren,
+ DestroyRef,
+ Directive,
+ ElementRef,
+ inject,
+ input,
+} from '@angular/core';
+
+@Directive({
+ selector: 'ul[appHighlight]',
+})
+export class HighlightDirective {
+ readonly query = input('', { alias: 'appHighlight' });
+
+ private readonly labels = contentChildren>('highlightTarget', {
+ descendants: true,
+ });
+
+ private readonly destroyRef = inject(DestroyRef);
+
+ constructor() {
+ afterRenderEffect({
+ earlyRead: () => {
+ const query = this.query();
+ const labels = this.labels();
+ const ranges: Range[] = [];
+
+ if (!query) return ranges;
+
+ for (const label of labels) {
+ const textNode = label.nativeElement.firstChild;
+
+ if (!textNode || textNode.nodeType !== Node.TEXT_NODE) continue;
+
+ const text = textNode.textContent?.toLowerCase() ?? '';
+ let matchIndex = text.indexOf(query);
+
+ while (matchIndex !== -1) {
+ const range = new Range();
+
+ range.setStart(textNode, matchIndex);
+ range.setEnd(textNode, matchIndex + query.length);
+
+ ranges.push(range);
+
+ matchIndex = text.indexOf(query, matchIndex + query.length);
+ }
+ }
+
+ return ranges;
+ },
+
+ write: (ranges) => {
+ if (!('highlights' in CSS)) return;
+
+ CSS.highlights.set('search-match', new Highlight(...ranges()));
+ },
+ });
+
+ this.destroyRef.onDestroy(() => {
+ if (typeof CSS !== 'undefined' && 'highlights' in CSS) {
+ CSS.highlights.delete('search-match');
+ }
+ });
+ }
+}