Files
WebApp/src/app/indicadores/patient-search.ts
T

84 lines
2.9 KiB
TypeScript

import { Component, model, output, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
/** A search box's text as FHIR Patient search parameters: one word with a digit is an
* identifier (a CURP or a record number); anything else is her name. */
export function patientSearch(text: string): { name?: string; identifier?: string } {
const t = text.trim();
if (!t) return {};
return /\d/.test(t) && !/\s/.test(t) ? { identifier: t } : { name: t };
}
const fold = (s: string) => s.normalize('NFKD').replace(/\p{M}/gu, '').toLowerCase();
/** Whether a row matches the search in the browser (lists loaded whole): each word starts a
* word of one of its texts (a name, an identifier, the pseudonymous id). */
export function matchesSearch(text: string, texts: (string | undefined)[]): boolean {
const words = fold(text).split(/\s+/).filter(Boolean);
if (!words.length) return true;
const known = texts.filter((t): t is string => !!t).map((t) => fold(t).split(/\s+/));
return known.some((parts) => words.every((w) => parts.some((p) => p.startsWith(w))));
}
/** "Buscar paciente": applied on Enter or the search button, cleared with the cross. */
@Component({
selector: 'app-patient-search',
imports: [FormsModule, MatButtonModule, MatFormFieldModule, MatIconModule, MatInputModule],
template: `
<mat-form-field appearance="outline" subscriptSizing="dynamic" class="patient-search">
<mat-label>Buscar paciente</mat-label>
<input
matInput
type="search"
name="patient-search"
[placeholder]="hint"
[ngModel]="draft()"
[ngModelOptions]="{ standalone: true }"
(ngModelChange)="draft.set($event)"
(keydown.enter)="$event.preventDefault(); apply()"
/>
@if (value()) {
<button
mat-icon-button
matSuffix
type="button"
aria-label="Limpiar búsqueda"
(click)="clear()"
>
<mat-icon>close</mat-icon>
</button>
} @else {
<button mat-icon-button matSuffix type="button" aria-label="Buscar" (click)="apply()">
<mat-icon>search</mat-icon>
</button>
}
</mat-form-field>
`,
styles: `
.patient-search {
min-width: 18rem;
}
`,
})
export class PatientSearch {
/** The search as applied. */
readonly value = model('');
readonly searched = output<string>();
protected readonly draft = signal('');
protected readonly hint = 'Nombre, CURP o expediente';
protected apply(): void {
const text = this.draft().trim();
this.value.set(text);
this.searched.emit(text);
}
protected clear(): void {
this.draft.set('');
this.apply();
}
}