Skip to main content

Filter from a value

A list already shows the values people want to filter by. Let them select one. Select shared in a row, and the field gets is:shared. Select it again, and the filter goes away. The field shows every filter that runs, so the person can read it and edit it like typed text.

How it works

toggleFacet from @ngrok/mantle/query-filter does the edit. It is a pure function of the grammar, the query, and one qualifier:

  • When the query doesn't have the qualifier, it adds it at the end, quoted when the value needs quotes.
  • When the query has it, it takes it out. Aliases count, so is:personal matches is:private.
  • The opposite qualifier gives way: -is:shared becomes is:shared.
  • An OR or AND beside a removed qualifier goes with it, so no operator dangles.
  • A comma list such as is:shared,private stays as written, because the person wrote it on purpose.

A few rules keep the table honest:

  • Each value is a real <button> with aria-pressed, so a screen reader hears whether the filter is on.
  • Pass a function to setQuery, so two quick selections never lose an edit.
  • When no row matches, queryUsesQualifiers picks the advice: a filtered query needs a filter removed, and a text query needs a spelling check.

To filter a list on another page, build the link with formatFacet. It writes the canonical spelling and quotes a value that has spaces:

import { formatFacet } from "@ngrok/mantle/query-filter";import { Link } from "react-router"; const q = formatFacet({ key: "name", value: project.name, negated: false }); <Link to={`/projects?${new URLSearchParams({ q })}`}>View in projects</Link>;

Read the param on the other page with the Query from the URL hook.