From ab17a5e1fa75ad1c2fdd8f1edd06623cf21b4124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Gla=CC=88ser?= Date: Thu, 4 Jun 2026 18:35:21 +0200 Subject: [PATCH] Simulate server-side type-ahead search --- components/data-display.html | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/components/data-display.html b/components/data-display.html index 77edaa2..963ac09 100644 --- a/components/data-display.html +++ b/components/data-display.html @@ -80,7 +80,7 @@
Large Table
-
+ - +
@@ -224,11 +224,12 @@ const searchInput = table.querySelector('[data-large-table-search]'); const searchFieldWrap = table.querySelector('[data-component="single-line-input"]'); const searchClearButton = table.querySelector('.sg-input-clear-button'); - const searchForm = table.querySelector('[data-large-table-search-form]'); + let searchTimer = null; const state = { columnIndex: 0, direction: 'ascending', query: '', + loading: false, }; function setHeaderState(activeIndex, direction) { @@ -280,6 +281,7 @@ } function renderRows() { + table.setAttribute('aria-busy', state.loading ? 'true' : 'false'); const sortedRows = rows.slice().sort((leftRow, rightRow) => { const leftCell = leftRow.querySelectorAll('[role="cell"]')[state.columnIndex]; const rightCell = rightRow.querySelectorAll('[role="cell"]')[state.columnIndex]; @@ -320,21 +322,30 @@ if (searchFieldWrap) { searchFieldWrap.setAttribute('data-has-value', searchInput.value ? 'true' : 'false'); } - }); - } - - if (searchForm && searchInput) { - searchForm.addEventListener('submit', (event) => { - event.preventDefault(); - state.query = searchInput.value.trim().toLowerCase(); + state.loading = true; renderRows(); + + if (searchTimer) { + window.clearTimeout(searchTimer); + } + + searchTimer = window.setTimeout(() => { + state.query = searchInput.value.trim().toLowerCase(); + state.loading = false; + renderRows(); + }, 250); }); } if (searchClearButton && searchInput) { searchClearButton.addEventListener('click', () => { + if (searchTimer) { + window.clearTimeout(searchTimer); + searchTimer = null; + } searchInput.value = ''; state.query = ''; + state.loading = false; if (searchFieldWrap) { searchFieldWrap.setAttribute('data-has-value', 'false'); }