feat: apply db table header filters as you type and widen filterable columns

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Guilhem Lemouel
2026-11-27 08:39:13 -08:00
co-authored by Claude Opus 5.5
parent 661eaa5baf
commit 9b935b99b7
+37 -22
View File
@@ -17,6 +17,9 @@
const MAX_AUTO_COL_WIDTH = 400
// Header padding plus its sort and key icons.
const HEADER_CHROME = 48
// The filter button in a filterable column's header, with its margin.
const HEADER_FILTER_WIDTH = 24
const HEADER_FILTER_DEBOUNCE = 300
const CELL_PADDING = 18
// A double click starts with a click: following a foreign key waits this long
// so the double click can edit the cell instead.
@@ -169,11 +172,20 @@
/** The column whose header filter is open, and the value typed there. A header filter
* writes the same entry the search bar does, so each shows the other's filter. */
let headerFilter: { column: string; draft: string } | undefined = $state(undefined)
function applyHeaderFilter(key: string, boolean: boolean, draft: string) {
const { [key]: _, ...rest } = filters
const value = draft.trim()
filters = value ? { ...rest, [key]: boolean ? value === 'true' : value } : rest
headerFilter = undefined
let headerFilterTimer: ReturnType<typeof setTimeout> | undefined
function setHeaderFilter(key: string, boolean: boolean, draft: string) {
clearTimeout(headerFilterTimer)
const { [key]: current, ...rest } = filters
const trimmed = draft.trim()
const value = trimmed ? (boolean ? trimmed === 'true' : trimmed) : undefined
if (value === current) return
filters = value === undefined ? rest : { ...rest, [key]: value }
}
/** Filters as the reader types; a half-typed comparison such as `>` matches no row. */
function typeHeaderFilter(key: string, draft: string) {
if (headerFilter) headerFilter.draft = draft
clearTimeout(headerFilterTimer)
headerFilterTimer = setTimeout(() => setHeaderFilter(key, false, draft), HEADER_FILTER_DEBOUNCE)
}
$effect(() => {
@@ -411,7 +423,10 @@
const next = { ...widths }
for (const col of columns) {
if (only ? !only.includes(col.field) : sizedColumns.has(col.field)) continue
let w = textWidth(col.field, `600 12px ${family}`) + HEADER_CHROME
let w =
textWidth(col.field, `600 12px ${family}`) +
HEADER_CHROME +
(filterSchema.keyOfColumn[col.field] ? HEADER_FILTER_WIDTH : 0)
for (const row of rows) {
const text = formatCell(row[col.field]).slice(0, 200) || 'NULL'
w = Math.max(w, textWidth(text, `400 12px ${family}`) + CELL_PADDING)
@@ -974,7 +989,10 @@
]}
bind:value={
() => headerFilter?.draft || undefined,
(v) => headerFilter && (headerFilter.draft = v ?? '')
(v) => {
if (headerFilter) headerFilter.draft = v ?? ''
setHeaderFilter(key, true, v ?? '')
}
}
placeholder="Value"
size="sm"
@@ -982,15 +1000,16 @@
{:else}
<TextInput
bind:value={
() => headerFilter?.draft ?? '',
(v) => headerFilter && (headerFilter.draft = String(v ?? ''))
() => headerFilter?.draft ?? '', (v) => typeHeaderFilter(key, String(v ?? ''))
}
size="sm"
inputProps={{
placeholder: number ? '10, >10, <=5' : 'Contains',
'aria-label': `Filter ${col.field}`,
onkeydown: (e: KeyboardEvent) => {
if (e.key === 'Enter') applyHeaderFilter(key, false, headerFilter?.draft ?? '')
if (e.key !== 'Enter') return
setHeaderFilter(key, false, headerFilter?.draft ?? '')
headerFilter = undefined
}
}}
/>
@@ -1000,24 +1019,20 @@
: 'Rows whose value contains this text. Start with = for an exact match.'}
</span>
{/if}
<div class="flex justify-end gap-2">
{#if active}
{#if active}
<div class="flex justify-end">
<Button
variant="subtle"
unifiedSize="sm"
onClick={() => applyHeaderFilter(key, boolean, '')}
onClick={() => {
setHeaderFilter(key, boolean, '')
headerFilter = undefined
}}
>
Clear
</Button>
{/if}
<Button
variant="accent"
unifiedSize="sm"
onClick={() => applyHeaderFilter(key, boolean, headerFilter?.draft ?? '')}
>
Apply
</Button>
</div>
</div>
{/if}
</div>
{/snippet}
</Popover>