swarm-ui: with an agent open, the list's filters sit behind one filter button
An open agent narrows the list to a column, where the filter boxes and menus took three rows. They now stack in a popover under one button that shows how many filters are active; the full-width list keeps them in the bar.
This commit is contained in:
parent
8d8fcb59cf
commit
e701be870a
8 changed files with 186 additions and 9 deletions
|
|
@ -154,7 +154,12 @@ above the details shuts them and the list takes the width again, and focus
|
||||||
returns to the agent's card. A name the roster doesn't have, from a stale link,
|
returns to the agent's card. A name the roster doesn't have, from a stale link,
|
||||||
shows "no agent named …" and the same button. The filter card, which also holds
|
shows "no agent named …" and the same button. The filter card, which also holds
|
||||||
the **+ agent** button and the refresh picker, filters by name, hive, status,
|
the **+ agent** button and the refresh picker, filters by name, hive, status,
|
||||||
message and wanted state. While the agent list loads for the first time, the
|
message and wanted state.
|
||||||
|
While an agent is open the list is a column with no room for them side by side,
|
||||||
|
so the filters sit behind one funnel button.
|
||||||
|
It shows how many filters are active beside the icon, and its name reads **filter (2)** to a screen reader.
|
||||||
|
The button opens a popover with **reset filters** at its foot.
|
||||||
|
While the agent list loads for the first time, the
|
||||||
page shows grey placeholder cards with the same layout as the real ones, so the
|
page shows grey placeholder cards with the same layout as the real ones, so the
|
||||||
cards don't jump when the data arrives. The placeholders stand still where
|
cards don't jump when the data arrives. The placeholders stand still where
|
||||||
motion is off.
|
motion is off.
|
||||||
|
|
|
||||||
|
|
@ -200,6 +200,9 @@ export function AgentsPage() {
|
||||||
<FilterableView
|
<FilterableView
|
||||||
view="cards"
|
view="cards"
|
||||||
standalone
|
standalone
|
||||||
|
// An open agent narrows the list to a column: one button holds
|
||||||
|
// the filters.
|
||||||
|
compactFilters={selected !== null}
|
||||||
barActions={barActions}
|
barActions={barActions}
|
||||||
columns={FIELDS}
|
columns={FIELDS}
|
||||||
rows={rows}
|
rows={rows}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,112 @@
|
||||||
|
// <FilterPopover> — every filter of a list behind one `filter` button. A list
|
||||||
|
// squeezed into a column has no room for a row of text boxes and menus, so they
|
||||||
|
// stack in a popover instead, with the same state as the bar that would show
|
||||||
|
// them inline. The button is the funnel icon, with the number of active filters
|
||||||
|
// after it when there are any; its accessible name is `filter (2)`.
|
||||||
|
import { useRef, useState } from "preact/hooks";
|
||||||
|
import { FilterIcon } from "@hive/shared/icons.js";
|
||||||
|
import { Button } from "../button/Button.js";
|
||||||
|
import {
|
||||||
|
MultiselectFilterOptions,
|
||||||
|
NegateToggle,
|
||||||
|
type MultiselectFilterState,
|
||||||
|
} from "../multiselect-filter/MultiselectFilter.js";
|
||||||
|
import { Panel } from "../panel/Panel.js";
|
||||||
|
import { Popover } from "../popover/Popover.js";
|
||||||
|
import type { FilterColumn } from "../table/Table.js";
|
||||||
|
import { filterButtonLabel } from "./filter-label.js";
|
||||||
|
|
||||||
|
export function FilterPopover<T>({
|
||||||
|
columns,
|
||||||
|
getFilter,
|
||||||
|
updateFilter,
|
||||||
|
toggleFilterValue,
|
||||||
|
resetFilters,
|
||||||
|
activeCount,
|
||||||
|
multiselectOptionsFor,
|
||||||
|
label,
|
||||||
|
}: {
|
||||||
|
columns: FilterColumn<T>[];
|
||||||
|
getFilter: (key: string) => MultiselectFilterState & { value: string };
|
||||||
|
updateFilter: (
|
||||||
|
key: string,
|
||||||
|
patch: Partial<{ value: string; values: string[]; negate: boolean }>,
|
||||||
|
) => void;
|
||||||
|
toggleFilterValue: (key: string, value: string) => void;
|
||||||
|
resetFilters: () => void;
|
||||||
|
activeCount: number;
|
||||||
|
multiselectOptionsFor: (c: FilterColumn<T>) => string[];
|
||||||
|
label: (c: FilterColumn<T>) => string;
|
||||||
|
}) {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const anchorRef = useRef<HTMLSpanElement>(null);
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<span ref={anchorRef}>
|
||||||
|
<Button
|
||||||
|
ariaLabel={filterButtonLabel(activeCount)}
|
||||||
|
onClick={() => setOpen((o) => !o)}
|
||||||
|
>
|
||||||
|
<FilterIcon />
|
||||||
|
{activeCount > 0 ? ` ${activeCount}` : null}
|
||||||
|
</Button>
|
||||||
|
</span>
|
||||||
|
<Popover
|
||||||
|
open={open}
|
||||||
|
onClose={() => setOpen(false)}
|
||||||
|
anchorRef={anchorRef}
|
||||||
|
label="filters"
|
||||||
|
>
|
||||||
|
<Panel title="filters" onClose={() => setOpen(false)}>
|
||||||
|
<div class="ui-filter-popover">
|
||||||
|
{columns.map((c) => {
|
||||||
|
const name = label(c);
|
||||||
|
if (c.filterMode === "multiselect") {
|
||||||
|
const filter = getFilter(c.key);
|
||||||
|
return (
|
||||||
|
<fieldset key={c.key} class="ui-filter-popover-group">
|
||||||
|
<legend>{name}</legend>
|
||||||
|
<MultiselectFilterOptions
|
||||||
|
options={multiselectOptionsFor(c)}
|
||||||
|
filter={filter}
|
||||||
|
onToggle={(v) => toggleFilterValue(c.key, v)}
|
||||||
|
scrollable={false}
|
||||||
|
/>
|
||||||
|
<NegateToggle
|
||||||
|
negate={filter.negate}
|
||||||
|
onChange={(negate) => updateFilter(c.key, { negate })}
|
||||||
|
/>
|
||||||
|
</fieldset>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<input
|
||||||
|
key={c.key}
|
||||||
|
type="text"
|
||||||
|
class="ui-filterable-view-search"
|
||||||
|
placeholder={`filter by ${name}…`}
|
||||||
|
aria-label={`filter by ${name}`}
|
||||||
|
value={getFilter(c.key).value}
|
||||||
|
onInput={(e) =>
|
||||||
|
updateFilter(c.key, {
|
||||||
|
value: (e.target as HTMLInputElement).value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
{activeCount > 0 ? (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="ui-table-reset-filters"
|
||||||
|
onClick={resetFilters}
|
||||||
|
>
|
||||||
|
reset filters
|
||||||
|
</button>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
</Panel>
|
||||||
|
</Popover>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -40,3 +40,21 @@
|
||||||
gap: 0.5em;
|
gap: 0.5em;
|
||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
}
|
}
|
||||||
|
/* The filters stacked in a popover (`FilterPopover`). */
|
||||||
|
.ui-filter-popover {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.6em;
|
||||||
|
}
|
||||||
|
.ui-filter-popover-group {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0.4em 0.6em 0.5em;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
.ui-filter-popover-group legend {
|
||||||
|
padding: 0 0.3em;
|
||||||
|
color: var(--muted);
|
||||||
|
font-size: 0.85em;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ import {
|
||||||
type FilterColumn,
|
type FilterColumn,
|
||||||
type TableColumn,
|
type TableColumn,
|
||||||
} from "../table/Table.js";
|
} from "../table/Table.js";
|
||||||
|
import { FilterPopover } from "./FilterPopover.js";
|
||||||
import "./FilterableView.css";
|
import "./FilterableView.css";
|
||||||
|
|
||||||
function isFilterable<T>(c: FilterColumn<T>): boolean {
|
function isFilterable<T>(c: FilterColumn<T>): boolean {
|
||||||
|
|
@ -52,9 +53,11 @@ function FilterBar<T>({
|
||||||
multiselectOptionsFor,
|
multiselectOptionsFor,
|
||||||
actions,
|
actions,
|
||||||
standalone,
|
standalone,
|
||||||
|
compact,
|
||||||
}: {
|
}: {
|
||||||
actions?: ComponentChildren;
|
actions?: ComponentChildren;
|
||||||
standalone?: boolean;
|
standalone?: boolean;
|
||||||
|
compact?: boolean;
|
||||||
columns: FilterColumn<T>[];
|
columns: FilterColumn<T>[];
|
||||||
getFilter: (key: string) => MultiselectFilterState & { value: string };
|
getFilter: (key: string) => MultiselectFilterState & { value: string };
|
||||||
updateFilter: (
|
updateFilter: (
|
||||||
|
|
@ -76,7 +79,19 @@ function FilterBar<T>({
|
||||||
: "ui-filterable-view-filter-bar"
|
: "ui-filterable-view-filter-bar"
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{filterable.map((c) => {
|
{compact ? (
|
||||||
|
<FilterPopover
|
||||||
|
columns={filterable}
|
||||||
|
getFilter={getFilter}
|
||||||
|
updateFilter={updateFilter}
|
||||||
|
toggleFilterValue={toggleFilterValue}
|
||||||
|
resetFilters={resetFilters}
|
||||||
|
activeCount={activeFilters.length}
|
||||||
|
multiselectOptionsFor={multiselectOptionsFor}
|
||||||
|
label={columnLabel}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
{(compact ? [] : filterable).map((c) => {
|
||||||
const label = columnLabel(c);
|
const label = columnLabel(c);
|
||||||
if (c.filterMode === "multiselect") {
|
if (c.filterMode === "multiselect") {
|
||||||
return (
|
return (
|
||||||
|
|
@ -106,7 +121,7 @@ function FilterBar<T>({
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
{activeFilters.length > 0 ? (
|
{activeFilters.length > 0 && !compact ? (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
class="ui-table-reset-filters"
|
class="ui-table-reset-filters"
|
||||||
|
|
@ -146,13 +161,16 @@ export function FilterableView<T>(
|
||||||
// `barActions` (buttons, a refresh picker) sit at its trailing edge.
|
// `barActions` (buttons, a refresh picker) sit at its trailing edge.
|
||||||
standalone?: boolean;
|
standalone?: boolean;
|
||||||
barActions?: ComponentChildren;
|
barActions?: ComponentChildren;
|
||||||
|
// Cards view only. The filters sit behind one `filter` button and its
|
||||||
|
// popover instead of in the bar, for a list squeezed into a column.
|
||||||
|
compactFilters?: boolean;
|
||||||
// Cards view only. Drawn after the cards with the rows the filter leaves, so
|
// Cards view only. Drawn after the cards with the rows the filter leaves, so
|
||||||
// a bar that acts on what is shown (select all) needs no copy of the filter.
|
// a bar that acts on what is shown (select all) needs no copy of the filter.
|
||||||
footer?: (visibleRows: T[]) => ComponentChildren;
|
footer?: (visibleRows: T[]) => ComponentChildren;
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const { columns, rows, rowKey, storageKey, renderCard, emptyMessage } = props;
|
const { columns, rows, rowKey, storageKey, renderCard, emptyMessage } = props;
|
||||||
const { standalone, barActions, footer } = props;
|
const { standalone, barActions, footer, compactFilters } = props;
|
||||||
const {
|
const {
|
||||||
visibleRows,
|
visibleRows,
|
||||||
activeFilters,
|
activeFilters,
|
||||||
|
|
@ -187,6 +205,7 @@ export function FilterableView<T>(
|
||||||
multiselectOptionsFor={multiselectOptionsFor}
|
multiselectOptionsFor={multiselectOptionsFor}
|
||||||
actions={barActions}
|
actions={barActions}
|
||||||
standalone={standalone}
|
standalone={standalone}
|
||||||
|
compact={compactFilters}
|
||||||
/>
|
/>
|
||||||
{rows.length === 0 && emptyMessage ? (
|
{rows.length === 0 && emptyMessage ? (
|
||||||
<p class="ui-filterable-view-empty">{emptyMessage}</p>
|
<p class="ui-filterable-view-empty">{emptyMessage}</p>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
// The accessible name of the button that holds a list's filters: its name, and
|
||||||
|
// how many filters are narrowing the list when any are. Kept apart from the component so
|
||||||
|
// a test reaches it without a DOM.
|
||||||
|
|
||||||
|
/** `filter`, or `filter (2)` while two filters are active. */
|
||||||
|
export function filterButtonLabel(active: number): string {
|
||||||
|
return active > 0 ? `filter (${active})` : "filter";
|
||||||
|
}
|
||||||
|
|
@ -66,11 +66,11 @@ export function MultiselectFilterOptions({
|
||||||
|
|
||||||
// The negate toggle — mara: "you should be able to change if the filter
|
// The negate toggle — mara: "you should be able to change if the filter
|
||||||
// is negated or not (eg search for this text vs exclude it)", same
|
// is negated or not (eg search for this text vs exclude it)", same
|
||||||
// reasoning as `Table`'s own copy. Small enough (one checkbox+label)
|
// reasoning as `Table`'s own copy. One checkbox and its label, exported so the
|
||||||
// that keeping it inline here rather than a third exported piece isn't
|
// stacked filters in `FilterPopover` show the same toggle under each option
|
||||||
// worth the indirection; `Table` keeps rendering its own copy for text-
|
// list; `Table` keeps rendering its own copy for text-mode filters, which
|
||||||
// mode filters, which don't otherwise touch this file at all.
|
// don't otherwise touch this file at all.
|
||||||
function NegateToggle({
|
export function NegateToggle({
|
||||||
negate,
|
negate,
|
||||||
onChange,
|
onChange,
|
||||||
}: {
|
}: {
|
||||||
|
|
|
||||||
12
frontend/packages/swarm-ui/test/filter-label.test.mjs
Normal file
12
frontend/packages/swarm-ui/test/filter-label.test.mjs
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { test } from "node:test";
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { filterButtonLabel } from "../src/ui/filterable-view/filter-label.ts";
|
||||||
|
|
||||||
|
test("the filter button shows no count while no filter is active", () => {
|
||||||
|
assert.equal(filterButtonLabel(0), "filter");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("the filter button counts the active filters", () => {
|
||||||
|
assert.equal(filterButtonLabel(1), "filter (1)");
|
||||||
|
assert.equal(filterButtonLabel(3), "filter (3)");
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue