ReUIReUI
2.5kX

Filters

A comprehensive filtering system with multiple filter types, operators, and visual indicators for data organization.

"use client"

import { useCallback, useState } from "react"

Installation

pnpm dlx shadcn@latest add @reui/filters

Usage

import {
  createFilter,
  Filters,
  type Filter,
  type FilterFieldConfig,
} from "@/components/reui/filters"
const [filters, setFilters] = useState<Filter[]>([
  createFilter("priority", "is_any_of", ["low", "medium"]),
])
 
const fields: FilterFieldConfig[] = [
  {
    key: "priority",
    label: "Priority",
    type: "multiselect",
    options: [
      { value: "low", label: "Low" },
      { value: "medium", label: "Medium" },
      { value: "high", label: "High" },
    ],
  },
]
 
return <Filters filters={filters} fields={fields} onChange={setFilters} />

Examples

Validation

"use client"

import { useCallback, useState } from "react"

Trigger Button

"use client"

import { useCallback, useState } from "react"

Small Size

"use client"

import { useCallback, useState } from "react"

Large Size

"use client"

import { useCallback, useState } from "react"

Custom Controls

"use client"

import { useCallback, useEffect, useMemo, useState } from "react"

Data Grid

"use client"

import { useCallback, useEffect, useMemo, useRef, useState } from "react"

Nuqs

"use client"

import { useCallback, useEffect, useMemo, useRef, useState } from "react"

With i18n Support

"use client"

import { useCallback, useMemo, useState } from "react"

API Reference

Filters

The main component for displaying and managing a collection of active filters.

PropTypeDefaultDescription
filtersFilter<T>[]-Required. The array of active filters.
fieldsFilterFieldsConfig<T>-Required. The configuration for available filter fields.
onChange(filters: Filter<T>[]) => void-Required. Callback fired when filters are added, updated, or removed.
size"sm" | "default" | "lg""default"The size of the filter items and controls.
triggerReactNode-Custom element to trigger the "Add Filter" menu.
showSearchInputbooleantrueWhether to show the search input in the "Add Filter" menu.
allowMultiplebooleantrueWhether to allow multiple filters for the same field.
enableShortcutbooleanfalseWhether to enable the keyboard shortcut to open the filter menu.
shortcutKeystring"f"The key used for the shortcut (e.g., "f").
shortcutLabelstring"F"The label to display in the shortcut indicator.
i18nPartial<FilterI18nConfig>-Custom labels and operators for internationalization.
classNamestring-Additional CSS classes for the container.
menuPopupClassNamestring-Additional CSS classes for the filter dropdown menu.

FilterFieldConfig

Configuration for an individual filterable field.

PropertyTypeDescription
keystringRequired. Unique identifier for the field.
labelstringRequired. Human-readable label for the field.
type"text" | "select" | "multiselect" | "custom" | "separator"The type of filter input to use.
iconReactNodeOptional icon to display next to the field label.
optionsFilterOption<T>[]List of options for select and multiselect fields.
operatorsFilterOperator[]Custom operators for this specific field.
defaultOperatorstringThe operator to select by default when adding this field.
placeholderstringPlaceholder text for text inputs.
searchablebooleanWhether the options list should be searchable.
maxSelectionsnumberMaximum number of items allowed in multiselect.
prefixReactNode | stringPrefix element for the input field.
suffixReactNode | stringSuffix element for the input field.
patternstringRegex pattern for text input validation.
validation(value) => boolean | objectCustom validation function.
customRenderer(props) => ReactNodeCustom renderer for the filter value selector.
customValueRenderer(values, options) => ReactNodeCustom renderer for the active filter value display.
fieldsFilterFieldConfig<T>[]Nested fields if this is a group configuration.
groupstringGroup label if this is a group configuration.
classNamestringAdditional CSS classes for the field components.

FilterOption

Structure for options in select and multiselect fields.

PropertyTypeDescription
valueTRequired. The internal value of the option.
labelstringRequired. Human-readable label for the option.
iconReactNodeOptional icon to display next to the option.
classNamestringAdditional CSS classes for the option item.

Filter

The structure of an active filter object.

PropertyTypeDescription
idstringUnique identifier for the filter instance.
fieldstringThe key of the associated field configuration.
operatorstringThe selected operator (e.g., "is", "contains").
valuesT[]The current value(s) of the filter.

FilterI18nConfig

Configuration for internationalization and custom labels.

PropertyTypeDescription
addFilterstringLabel for the "Add Filter" button.
searchFieldsstringPlaceholder for the field search input.
operatorsobjectMap of operator keys to localized strings.
validationobjectMap of validation error messages.
placeholdersobjectMap of dynamic placeholder templates.

Helpers

FunctionSignatureDescription
createFilter(field, operator?, values?) => FilterCreates a new filter object with a unique ID.
createFilterGroup(id, label, fields, initialFilters?) => FilterGroupCreates a filter group configuration.