Skip to content

feat: AND / OR filter builder with groups, live count and filter chip - #19

Merged
Schero94 merged 2 commits into
mainfrom
feat/filter-groups
Nov 20, 2026
Merged

Schero94 merged 2 commits into
mainfrom
feat/filter-groups

Conversation

@Schero94

Copy link
Copy Markdown
Owner

What

Advanced filters becomes a simple AND / OR filter builder with one level of groups, plus the UX fixes found while walking the current flows. Minor release (feat:). No server change, no migration: bookmarks store the URL query as before.

Current state (4.0.5, walked headless on Strapi 5.63)

All flows work (create, apply, edit, share, delete, widget, list integration), but:

  1. A list filtered with Any condition looks unfiltered: Strapi shows only $and filters as tags, so nothing on the list says a filter is active or what it is.
  2. Only a flat "all" or "any" list; no way to say "news and (popular or by Ben)".
  3. No feedback before Apply: you only see the result after closing the dialog.
  4. Empty dialog with one blank row and no guidance.
  5. Technical wording: true / false, "is null", "is greater than or equal to", one "is" for date-time fields that matches only one exact instant; the case-sensitive "contains" was the default.
  6. Removing a condition cannot be undone; focus is lost after removing; closing the dialog leaves focus on the page body instead of the toolbar button.
  7. The bookmark page says "2 filters" without saying which.
  8. At 320 px in German the dialog footer (Clear all / Cancel / Apply) runs off-screen.

Design

Progressive disclosure: one sentence-style list by default; groups only on request.

┌ Advanced filters ────────────────────────────────────────────── ✕ ┐
│ Show entries that match [all conditions ▾]                        │
│ ┌───────────────────────────────────────────────────────────────┐ │
│ │ [Category ▾]   [is ▾]               [news ▾]               🗑 │ │
│ └───────────────────────────────────────────────────────────────┘ │
│   AND                                                             │
│ ┌ Any of these ──────────────────────────────────────────── 🗑 ┐ │
│ │ At least one of these must match.                            │ │
│ │ [Views ▾]          [is greater than ▾] [100]              🗑 │ │
│ │   OR                                                         │ │
│ │ [Author (name) ▾]  [is ▾]  [Ben Muster ▾ suggestions]     🗑 │ │
│ │ + Add condition to this group                                │ │
│ └──────────────────────────────────────────────────────────────┘ │
│ + Add condition      + Add an "any of" group                      │
│ With "all", one "any of" group is possible. Choose "any" …        │
│ Condition removed. Undo                                           │
│ ┌───────────────────────────────────────────────────────────────┐ │
│ │ 4 entries match.                               (live region) │ │
│ └───────────────────────────────────────────────────────────────┘ │
│ [Clear all]                                    [Cancel] [Apply]   │
└───────────────────────────────────────────────────────────────────┘

Empty state:
│ Show only the entries you need                                    │
│ Start with an example or add your own condition. …                │
│ [Updated in the last 7 days] [Published entries] [Title contains …]│
│ [+ Add condition]                                                 │
│ No conditions yet: all 12 entries are shown.                      │

List toolbar when the filter has parts the native tags cannot show:
  [English (en) ▾] [⧩ category is guide or featured is Yes … ] [✕] [⚙] [★] [⚙]
                    └ opens the builder; full sentence in tooltip and accessible name
  • Match all / any at the top. A group always uses the other one, so there is never a "same as parent" group.
  • Inputs follow the type: Yes/No (boolean), list (enumeration, publication status), relation main field with suggestions from the related entries (_q search on the target's Content Manager list; free text stays possible), dates with "Today / 7 days ago / 30 days ago", two inputs for "is between", comma list for "is one of".
  • Plain operators per type: "is at least", "is on or after", "is empty"; text defaults to case-insensitive contains. Case-sensitive variants only appear when a saved filter uses them.
  • Live count: the Content Manager list request with pageSize=1 (same endpoint, same locale, date-only date-time values converted like the list's own toApiFilters), debounced 400 ms, aborted on change.
  • Inline validation (missing field/condition/value on Apply; structural errors such as status placement and duplicate pairs at once), undo, focus follows add/remove/undo and returns to the opener on close.
  • Chip in the toolbar for $or or groups, with "Remove all filters".
  • Bookmark page shows the filters as a sentence; the saved-view summary shows groups.

What the Content Manager accepts (verified in the installed 5.63 source and against the host)

Question Finding (source)
URL parse useQueryParams = qs.parse(search) with defaults: depth 5, arrayLimit 20 (@strapi/admin 5.63 hooks/useQueryParams). filters[$and][0][$or][0][title][$eq] arrives as { title: { '[$eq]': … } } and the API answers 400 Invalid key [$eq] (measured).
Server parse strapi::query: qs depth 20, arrayLimit 100; longer lists 400 since 5.63 (unchanged limits from 4.0.4).
Native tags Read query.filters.$and only. Adding a tag keeps other root keys ({ ...query.filters, $and }); removing a tag writes { $and } only and drops a root $or.
Status __status with $eq draft / published / published-modified / published-unmodified, read only from the root $and (normalizeStatusFromFilters, present 5.57–5.63). In $or → 400. When status is the only $and entry, the controller deletes the whole filters object, dropping a root $or (measured: group ignored).
Date-time toApiFilters turns date-only values into day ranges only for root $and entries.
Implicit AND Several keys in one object are ANDed (Strapi REST filters docs, verified on the host).

Resulting URL shapes (all ≤ 5 brackets below filters):

  • all, no group → filters[$and][i][field][$op] (native tags)
  • any → filters[$or][i][field][$op]
  • all of (A…, any of (B, C)) → filters[$and][…]=A…&filters[$or][0]=B&filters[$or][1]=C
  • any of (A, all of (B, C)) → filters[$or][0]=A&filters[$or][1][B…]&filters[$or][1][C…]
  • status alone next to a group → adds filters[$and][1][id][$notNull]=true (always true) so the group survives; the builder hides it when reading back.

Compatibility

Old bookmarks open exactly as saved (the opener does not go through the builder). The builder reads every earlier shape: native $and, 4.x $or, 3.x root fields and shorthand (filters[featured]=true), two operators on one field, $and next to $or. tests/fixtures/legacy-bookmark-queries.json + tests/admin-filter-groups.test.cjs check, for each shape and both parsers (bookmark parse, admin parse), that the rebuilt filter selects the same records of a synthetic data set with Strapi's filter semantics, fits depth 5, and is stable on save → reopen. Unsupported shapes ($not, two group levels, deeper relations, unknown operators) are flagged in the dialog and left untouched until Apply.

Tests and evidence

  • npm test 132/132 (new: 10 model tests for build/parse/validate, round trips, limits, legacy fixtures), test:admin 9/9 (new filter-builder.test.jsx: grouped URL → chip sentence → builder, one count request per change in StrictMode, remove + undo, example + Apply writes __status), lint, format, both type checks, build, verify, verify:runtime, npm pack --dry-run.
  • Browser acceptance, headless Chromium, isolated Strapi 5.63.0 host (127.0.0.1:1361, SQLite, random secrets, egress blocked, autoOpen: false, plugin installed from the packed tarball of this branch), 12 synthetic articles with known result sets. Built through the UI, result count line and real CM list both checked, light and dark:
Scenario Built in the UI Expected = got
AND category is news, views > 50 A01 A05 A08
OR any: category is guide, featured is Yes A01 A02 A03 A06 A08 A09 A10
all + any group news and any of (views > 100, author Ben via suggestion) A01 A05 A08 A12
any + all group opinion or all of (featured Yes, views ≥ 200) A03 A04 A06 A08 A09
status + group Draft and any of (news, review) A03 A07 A12
save → reopen bookmark of the last view opened from the bookmark page A03 A07 A12
legacy 3.x filters[featured]=true, 4.x $or, two ops in one entry, created via API opened + builder reads them, counts match
  • Keyboard: open with Enter, remove → focus moves to the next condition, Undo restores, 25 Tab stops stay inside the dialog, Escape returns focus to "Advanced filters".
  • measure.js 1440 / 1280 / 1080 / 1024 / 768 / 520 / 390 / 320, light and dark, builder (empty and grouped), list with chip, bookmark page, widget; German at 1440 and 320: documentOverflow 0, no clipped text, no low contrast, no off-scale gaps, no row mismatch in plugin UI. Remaining smallTargets are DS-internal (Combobox <input> 22 px inside its 40 px field, Combobox clear button 16 px) and core (CM checkboxes, homepage widget header links rendered by Strapi), same as on 4.0.5.
  • Console: 0 plugin errors or warnings; core only (useRBAC object warning, React Router future flag) and blocked egress.

Limits (what the Content Manager cannot express)

  • One level of groups; with "all" only one "any of" group (the root has one $or key); inside an "all of" group each field + condition once.
  • Status only as a main condition with "all".
  • Removing a native filter tag also removes the group (core rewrites the filter as $and only); the chip then shows what is left.
  • Relative dates are resolved when applied; a bookmark keeps the absolute date (Strapi filters have no relative date operator).
  • Locale is the Content Manager's locale switch (saved with the bookmark), not a filter condition.

🤖 Generated with Claude Code

Schero94 and others added 2 commits November 20, 2026 11:07
Advanced filters now build "all of (A, any of (B, C))" and "any of (A, all of (B, C))"
as plain sentences, with field-aware inputs, a live count of matching entries,
one-click examples, undo and a toolbar chip that shows the active filter.

Groups use Strapi's implicit AND (`$and` next to `$or`, or several fields in one
`$or` entry): the admin parses the list URL with qs depth 5, so a nested logical
operator does not reach the Content Manager intact. The publication status uses
the Content Manager's `__status` filter, which it reads only from the root `$and`.

Earlier bookmark shapes (native tags, 4.x `$or`, 3.x root fields, two operators on
one field) open unchanged and read back into the builder; a fixture test checks
that the rebuilt filter selects the same entries.

Docs checked: Strapi REST filters (docs.strapi.io/cms/api/rest/filters, context7
/strapi/documentation, 2026-11-20); installed @strapi/admin 5.63.0 useQueryParams,
Filters, dateTimeFilters, getFetchClient; @strapi/content-manager 5.63.0
ListViewPage, ListView Filters and server collection-types (normalizeStatusFromFilters),
5.57.0 for the peer floor; @strapi/design-system 2.3.1 Combobox, SingleSelect, Modal,
Box responsive props.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README section "Building filters (AND / OR)" with the limits the Content Manager
sets (qs depth 5, one group level, status only in the root $and, native tag
removal drops a group), new examples and FAQ, CHANGELOG [Unreleased], and the
Pages text and advanced-filters screenshot (synthetic data, Strapi 5.63 host).
site.css is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Schero94
Schero94 merged commit c2ed0a3 into main Nov 20, 2026
8 checks passed
@Schero94
Schero94 deleted the feat/filter-groups branch November 20, 2026 10:17
github-actions Bot pushed a commit that referenced this pull request Nov 20, 2026
# [4.1.0](v4.0.5...v4.1.0) (2026-11-20)

### Features

* AND / OR filter builder with groups, live count and filter chip ([#19](#19)) ([c2ed0a3](c2ed0a3))

### Added

* **Advanced filters build AND / OR filters with one level of groups.** The builder reads as a sentence ("Show entries that match all conditions"), one line per condition. "Add an “any of” group" (with "all") or "Add an “all of” group" (with "any") adds one level of nesting, for example "Category is news and any of (Views is greater than 100, Author is Ben)". Groups are written with Strapi's implicit AND (`filters[$and]` next to `filters[$or]`, or several fields in one `$or` entry), because the Content Manager parses the list URL with a qs depth of 5 and a nested `$or` inside `$and` does not survive it.
* Live count of matching entries while editing (the Content Manager's own list request with `pageSize=1`), an empty state with one-click examples, undo for removed conditions and groups, and focus that follows adding, removing and undoing and returns to the opening button when the dialog closes.
* Field-aware inputs: Yes/No for booleans, the publication status (Draft, Published, Published with or without unpublished changes; the Content Manager's `__status` filter), suggestions from the related entries for relation fields, "Today", "7 days ago" and "30 days ago" for dates. Operators use plain words per type ("is at least", "is on or after", "is empty"); the case-sensitive text variants appear only when a saved filter uses them.
* A summary chip in the list view toolbar shows the whole filter as one sentence when it has parts the native filter tags cannot show ("any" conditions or a group). Selecting it opens the builder; its cross removes every filter. Before, a list filtered with "any" looked unfiltered apart from the entry count.
* The bookmark page shows each bookmark's filters as a sentence instead of "2 filters", and the saved-view summary shows groups.

### Changed

* "Contains", "starts with" and "ends with" on text fields now default to the case-insensitive operators (`$containsi`, ...). Saved filters keep their operator.
* Date-time fields offer before / after operators instead of "is", which matched only one exact instant.

### Compatibility

* No migration. Bookmarks store the URL query as before, and every earlier shape opens unchanged: native `$and` tags, the 4.x "any condition" `$or` list, 3.x root fields (`filters[title][$eq]=…`, `filters[featured]=true`), entries with two operators on one field, and `$and` next to `$or`. The builder reads all of them; the rebuilt filter selects the same entries (`tests/admin-filter-groups.test.cjs`, fixtures in `tests/fixtures/legacy-bookmark-queries.json`). Shapes it cannot show (`$not`, two levels of groups) are flagged in the dialog and stay untouched until you apply.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant