Names read from MARC personal-name fields are stored as "Steve Allen", and names already stored as "Allen, Steve" are shown and printed in natural order. Search matches both forms. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
38 lines
1.9 KiB
TypeScript
38 lines
1.9 KiB
TypeScript
import { displayAuthors } from '../../shared/names.ts';
|
|
import type { SearchResult } from '../../shared/types.ts';
|
|
|
|
/** Next highlighted index for an arrow key; -1 means nothing is highlighted. */
|
|
export function moveActive(key: string, active: number, count: number): number {
|
|
if (count === 0) return -1;
|
|
if (key === 'ArrowDown') return active < count - 1 ? active + 1 : 0;
|
|
if (key === 'ArrowUp') return active > 0 ? active - 1 : count - 1;
|
|
return active;
|
|
}
|
|
|
|
export interface Footer { label: string; onPick: () => void }
|
|
|
|
/** Search results as a listbox. The input that owns it keeps focus and points at the highlighted option. */
|
|
export function ResultList({ id, label, results, active, onPick, onHover, footer }: {
|
|
id: string; label: string; results: SearchResult[]; active: number;
|
|
onPick: (r: SearchResult) => void; onHover: (i: number) => void; footer?: Footer;
|
|
}) {
|
|
const keep = (e: Event) => e.preventDefault(); // a click must not take focus from the input
|
|
return (
|
|
<ul class="results" role="listbox" id={id} aria-label={label}>
|
|
{results.map((r, i) => (
|
|
<li role="option" id={`${id}-${i}`} class={'result' + (i === active ? ' active' : '')} aria-selected={i === active}
|
|
onMouseDown={keep} onMouseMove={() => onHover(i)} onClick={() => onPick(r)}>
|
|
<span class="result-title">{r.title}</span>
|
|
<span class="result-sub">{[displayAuthors(r.authors).join(', '), r.year, r.publisher].filter(Boolean).join(' · ')}</span>
|
|
</li>
|
|
))}
|
|
{footer ? (
|
|
<li role="option" id={`${id}-${results.length}`} class={'result result-footer' + (active === results.length ? ' active' : '')}
|
|
aria-selected={active === results.length} onMouseDown={keep} onMouseMove={() => onHover(results.length)} onClick={footer.onPick}>
|
|
<span class="result-title">{footer.label}</span>
|
|
</li>
|
|
) : null}
|
|
</ul>
|
|
);
|
|
}
|