Open in a new tab

Filter a list as the user types, with an accessible live count.

Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.

When to use it

Small interactive pages that need DOM helpers and clean listener lifetimes without a component framework.

Copy-paste steps

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html, main.js and style.css, then open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.

What it shows

  • dom.query / queryAll, show / hide, classAdd / classRemove, text.
  • events.scope(name).on(...): named listeners you can clear as a group.

Code

main.js

javascriptApache-2.029 lines

// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { dom } from '@awacloud/fw/dom/query/dom.js';
import { events } from '@awacloud/fw/dom/query/events.js';

fw.runtime.registerAllDeep([dom, events]);
const $ = fw.runtime.resolve('dom');
const ui = fw.runtime.resolve('events').scope('filter');

const list = /** @type {HTMLElement} */ ($.query(document.body, '#items'));
const count = /** @type {HTMLElement} */ ($.query(document.body, '#count'));

/** @param {string} term */
export function applyFilter(term) {
    const needle = term.trim().toLowerCase();
    let shown = 0;
    for (const item of $.queryAll(list, 'li')) {
        const match = (item.textContent ?? '').toLowerCase().includes(needle);
        if (match) { $.show(item); shown++; } else { $.hide(item); }
        if (match && needle) $.classAdd(item, 'match'); else $.classRemove(item, 'match');
    }
    $.text(count, `${shown} shown`);
    return shown;
}

ui.on('input', $.query(document.body, '#filter'), 'input', (event) => {
    applyFilter(/** @type {HTMLInputElement} */ (event.target).value);
});
applyFilter('');

index.html

htmlApache-2.030 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>fw · Filter a list with dom and events</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <link rel="stylesheet" href="./style.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>Filter a list</h1>
    <label for="filter">Filter</label>
    <input id="filter" type="search" autocomplete="off">
    <ul id="items">
      <li>Base64</li>
      <li>CBOR</li>
      <li>MessagePack</li>
      <li>UTF-8</li>
    </ul>
    <p id="count" aria-live="polite"></p>
  </main>
</body>
</html>

style.css

cssApache-2.03 lines

/* SPDX-License-Identifier: Apache-2.0 */
input { display: block; inline-size: 100%; margin-block: 0.5rem; }
li.match { font-weight: 600; }