FilterBar
The filters of a list: one field for each, a chip for every filter that is on, and «Clear filters». On a phone the fields sit two by two.
import { FilterBar } from '@sypnia/ui/react'
Fields and chips
Type or choose something. The line below shows what the bar reports.
{"status":"Paid"}
Code
import { useState } from 'react';
import { FilterBar, type DataFilters } from '@sypnia/ui/react';
const fields = [
{ id: 'customer', label: 'Customer', placeholder: 'Name or email' },
{ id: 'status', label: 'Status', type: 'select' as const, options: ['Paid', 'Pending', 'Refunded'] },
{ id: 'country', label: 'Country', type: 'select' as const, options: [{ value: 'co', label: 'Colombia' }, { value: 'mx', label: 'Mexico' }, { value: 'es', label: 'Spain' }] },
{ id: 'id', label: 'Order', placeholder: 'ord_…' },
];
// Type or choose something: a chip appears for every filter that is on, with an x to take it away, and «Clear filters».
export default function Basic() {
const [values, setValues] = useState<DataFilters>({ status: 'Paid' });
return (
<div className="sy-stack">
<FilterBar label="Filter orders" fields={fields} values={values} onChange={setValues} />
<p className="sy-machine">{JSON.stringify(values)}</p>
</div>
);
} Only the chips
With collapsed the fields stay hidden and the chips still say what is filtered.
Code
import { useState } from 'react';
import { Button, FilterBar, type DataFilters } from '@sypnia/ui/react';
const fields = [
{ id: 'status', label: 'Status', type: 'select' as const, options: ['Paid', 'Pending', 'Refunded'] },
{ id: 'country', label: 'Country', type: 'select' as const, options: ['Colombia', 'Mexico', 'Spain'] },
];
// With collapsed, only the chips of the filters that are on show: the fields stay hidden until the person asks for them.
export default function Collapsed() {
const [values, setValues] = useState<DataFilters>({ status: 'Pending', country: 'Mexico' });
const [open, setOpen] = useState(false);
return (
<div className="sy-stack">
<div><Button size="sm" icon="filter" onClick={() => setOpen((o) => !o)}>{open ? 'Hide filters' : 'Filters'}</Button></div>
<FilterBar label="Filter orders" fields={fields} values={values} onChange={setValues} collapsed={!open} />
</div>
);
} When to use it
Use it when
- People narrow a long list by a few facts: status, country, customer.
- A text filter for what people type (a name, an id), a choice for what has a few known values.
- It reports every change in
onChange: the values by field id, and a filter that is off is not there.
Do not use it when
- There is a single search: SearchInput is enough.
- The list is short enough to read whole.
- You are inside a DataTable: give columns a
filterand it draws this bar by itself.
For everyone
- It is a search region with a name (
label), and every field has its visible label. - Each chip names its filter and its button says what it removes: «Remove Status».
- On phones the fields sit two by two, at least 44 px tall.
Props
| FilterBar | Type | Default |
|---|---|---|
fieldsrequired | FilterField[] | |
onChangerequired | (values: DataFilters) => void | |
valuesrequiredThe value of each filter by field id. An empty or missing value means the filter is off. | DataFilters | |
collapsedOnly the chips of the filters that are on, without the fields. | boolean | |
labelA name for screen readers. | string |