Skip to content
العربية
App patterns

FilterBar

Search and filter menus above a list or table. Chosen options show as removable tags, and “Clear filters” removes them all and also turns off pressed toggle chips. actions puts the list's other controls (date range, columns, refresh, “⋯”) at the end of the first row, so one bar is the list toolbar. collapseOnPhone folds the filter menus below 768 px into one “Filters” button with the count of chosen values; it opens a bottom sheet with one group per filter (checkboxes, or radios when not multiple) and “Clear filters”. A change applies at once. Search, actions and the tags stay on the bar; with no filters there is no button. A filter with kind "toggle" is one chip (aria-pressed) with a check mark when on; its value is ["on"] or [], it has no tag, and it stays on the bar on a phone. In search, onSubmit runs on Enter, suggestions shows a list of words while typing (the text stays free), and maxLength limits the length. Keep suggestions defined for the life of the bar (pass [] for no words): a change between undefined and a list mounts a new field, and the focus goes away.

import { FilterBar, type FilterDef, type FilterValue } from "@percentsa/ui";

Examples

Status: Active
Code
export function FilterBarExample() {
  const t = useT();
  const [query, setQuery] = useState("");
  const [value, setValue] = useState<FilterValue>({ status: ["active"] });
  return (
    <FilterBar
      className="w-full"
      search={{ value: query, onValueChange: setQuery, placeholder: t("Search projects", "ابحث في المشاريع") }}
      value={value}
      onValueChange={setValue}
      filters={[
        {
          key: "status",
          label: t("Status", "الحالة"),
          multiple: true,
          options: [
            { value: "active", label: t("Active", "نشط") },
            { value: "paused", label: t("Paused", "متوقف") },
            { value: "done", label: t("Done", "مكتمل") },
          ],
        },
        {
          key: "owner",
          label: t("Owner", "المسؤول"),
          options: [
            { value: "sara", label: t("Sara", "سارة") },
            { value: "omar", label: t("Omar", "عمر") },
            { value: "lina", label: t("Lina", "لينا") },
          ],
        },
      ]}
    />
  );
}

/** The list toolbar: search and filters at the start; the date range, columns, refresh and "⋯" at the end. */
Code
export function FilterBarToolbarExample() {
  const t = useT();
  const [query, setQuery] = useState("");
  const [value, setValue] = useState<FilterValue>({});
  const [range, setRange] = useState<DateRange>({ from: null, to: null });
  const [hidden, setHidden] = useState<string[]>([]);
  const done = (title: string) => () => toast({ title });
  return (
    <FilterBar
      className="w-full"
      search={{ value: query, onValueChange: setQuery, placeholder: t("Search orders", "ابحث في الطلبات") }}
      value={value}
      onValueChange={setValue}
      filters={[
        {
          key: "status",
          label: t("Status", "الحالة"),
          multiple: true,
          options: [
            { value: "open", label: t("Open", "مفتوح") },
            { value: "paid", label: t("Paid", "مدفوع") },
          ],
        },
      ]}
      actions={
        <>
          <DateRangePicker size="sm" className="w-60" value={range} onValueChange={setRange} />
          <ColumnsMenu
            columns={[
              { key: "number", header: t("Number", "الرقم"), hideable: false },
              { key: "customer", header: t("Customer", "العميل") },
              { key: "date", header: t("Date", "التاريخ") },
              { key: "total", header: t("Total", "الإجمالي") },
            ]}
            hidden={hidden}
            onHiddenChange={setHidden}
          />
          <Button variant="secondary" iconOnly aria-label={t("Refresh", "تحديث")} onClick={done(t("Refreshed", "تم التحديث"))}>
            <RefreshCw aria-hidden />
          </Button>
          <DropdownMenu
            align="end"
            items={[
              { label: t("Export", "تصدير"), icon: <Download />, onSelect: done(t("Export started", "بدأ التصدير")) },
              { label: t("Print", "طباعة"), icon: <Printer />, onSelect: done(t("Print", "طباعة")) },
            ]}
            trigger={
              <Button variant="secondary" iconOnly aria-label={t("More actions", "إجراءات أخرى")}>
                <Ellipsis aria-hidden />
              </Button>
            }
          />
        </>
      }
    />
  );
}
Status: Paid
Code
export function FilterBarCollapseExample() {
  const t = useT();
  const [value, setValue] = useState<FilterValue>({ status: ["paid"] });
  return (
    <FilterBar
      className="w-full"
      collapseOnPhone
      value={value}
      onValueChange={setValue}
      filters={[
        {
          key: "status",
          label: t("Status", "الحالة"),
          multiple: true,
          options: [
            { value: "paid", label: t("Paid", "مدفوع") },
            { value: "open", label: t("Open", "مفتوح") },
            { value: "void", label: t("Void", "ملغى") },
          ],
        },
        {
          key: "branch",
          label: t("Branch", "الفرع"),
          options: [
            { value: "riyadh", label: t("Riyadh", "الرياض") },
            { value: "jeddah", label: t("Jeddah", "جدة") },
          ],
        },
      ]}
      actions={
        <Button variant="secondary" iconOnly aria-label={t("Refresh", "تحديث")}>
          <RefreshCw aria-hidden />
        </Button>
      }
    />
  );
}

/** A form page: the fields in a card and the bar at the end. The box scrolls, so the bar sticks to its bottom as it would to the window. */

Searched for: nothing yet

Code
export function FilterBarLogExample() {
  const t = useT();
  const [query, setQuery] = useState("");
  const [submitted, setSubmitted] = useState("");
  const [value, setValue] = useState<FilterValue>({});
  return (
    <div className="grid w-full gap-2">
      <FilterBar
        search={{ value: query, onValueChange: setQuery, onSubmit: setSubmitted, suggestions: ["nginx", "postgres", "worker"], maxLength: 100, placeholder: t("Search logs", "ابحث في السجلات") }}
        value={value}
        onValueChange={setValue}
        filters={[
          { key: "errors", label: t("Errors only", "الأخطاء فقط"), kind: "toggle", options: [] },
          {
            key: "level",
            label: t("Level", "المستوى"),
            options: [
              { value: "info", label: t("Info", "معلومات") },
              { value: "warn", label: t("Warning", "تحذير") },
            ],
          },
        ]}
      />
      <p className="text-sm text-fg-3">{t(`Searched for: ${submitted || "nothing yet"}`, `البحث عن: ${submitted || "لا شيء بعد"}`)}</p>
    </div>
  );
}

/** `collapseOnPhone`: below 768 px the two filter menus become one "Filters" button with a count, which opens a bottom sheet. Narrow the window to see it. */