# Incremental Search

Utilities for incremental (type-ahead) search over data collections or DOM-backed lists.

- **`ListSearchController`** — the shared brain behind the built-in search
  popover in `ixfx-grid-list` / `ixfx-vertical-list` / `ixfx-detail-list` /
  `ixfx-data-grid` / `ixfx-tree-list` / `ixfx-miller-list`. Three modes
  (navigate / select / filter), driven by `list.search.*` commands. See
  [`docs/infra-incremental.md`](./infra-incremental.md) — start there
  for anything component-facing.
- **`createIncrSearch`** — the low-level fuzzy engine (fzf) both the controller
  and the helpers below are built on.
- **`createElementIncrSearch`** / **`IncrSearchTreeController`** — *deprecated*
  filter-only controllers driven by an external `<input>`. Kept for standalone
  use; components now use `ListSearchController`.

## Plain UL list

Matches the approach in `demo/incr-search.ts` using `createElementIncrSearch`.

```typescript
import { createElementIncrSearch } from '../src/incr-search/index.js';

const ITEMS = [
  'Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola',
  'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan',
];

const listEl = document.querySelector<HTMLUListElement>('#list')!;
const searchEl = document.querySelector<HTMLInputElement>('#search')!;

for (const name of ITEMS) {
  const li = document.createElement('li');
  li.textContent = name;
  listEl.append(li);
}

const search = createElementIncrSearch({
  container: listEl,
  itemSelector: 'li',
  highlightKey: 'demo-incr-highlight',
  onChange: state => {
    const count = state.query.trim() ? state.results.length : ITEMS.length;
    console.log('query', `"${state.query}" → ${count} items`);
  },
});

searchEl.addEventListener('input', () => {
  void search.setQuery(searchEl.value);
});

searchEl.addEventListener('keydown', (e: KeyboardEvent) => {
  if (e.key === 'Escape') {
    searchEl.value = '';
    void search.clearQuery();
  }
});
```

## Tree list component

Use `IncrSearchTreeController` with `ixfx-tree-list` (or any `TreeSearchHost`).

```typescript
import { IncrSearchTreeController } from '../src/incr-search/index.js';
import type { TreeListElement } from '../src/tree/tree-list.ts';

const treeEl = document.querySelector<TreeListElement>('ixfx-tree-list')!;
const searchEl = document.querySelector<HTMLInputElement>('[data-search]')!;

const searchCtrl = new IncrSearchTreeController(treeEl, {
  selector: ({ node }) => node.item.label,
  input: searchEl,
});

searchCtrl.connect();
```

## Miller tree component

`IncrSearchMillerController` is a compatibility export. Prefer `IncrSearchTreeController`
and pass the Miller label selector.

```typescript
import { IncrSearchTreeController } from '../src/incr-search/index.js';
import type { MillerListElement } from '../src/miller/miller-list.ts';

const millerEl = document.querySelector<MillerListElement>('ixfx-miller-list')!;
const searchEl = document.querySelector<HTMLInputElement>('[data-search]')!;

const searchCtrl = new IncrSearchTreeController(millerEl, {
  selector: ({ node }) => node.item.label,
  labelSelector: '.label',
  input: searchEl,
});

searchCtrl.connect();
```

## Notes

- `createElementIncrSearch` observes child mutations and refreshes automatically.
- `IncrSearchTreeController` sets `filterPredicate` and highlights matching labels.
- Call `disconnect()` when tearing down controllers to clear highlights and handlers.
