index.tsx
Overview
This file defines React components that provide a flexible and reusable filter bar user interface element typically used in list or table views. The filter bar includes a search input, optional filtering controls via a popover, and a customizable left panel or title area.
The main exported component, ListFilterBar, combines several UI elements:
A title or customizable left panel area with optional icon support.
A search input field with change handling.
A filter button that shows the number of active filters and triggers a filter popover.
Additional children components for extending UI functionality.
Additionally, the file exports a FilterButton component, which is a styled button with a filter icon and an optional badge showing the count of active filters.
This file organizes the filter-related UI controls in a compact, accessible, and customizable way to be reused across different list or data display pages.
Components and Interfaces
Interface: IProps
Defines the props for the main ListFilterBar component (partial, extended later):
Prop | Type | Description |
|---|---|---|
|
| Title text or node displayed on the left side. |
|
| Current value of the search input field. |
|
| Callback fired on search input value changes. |
|
| Whether to display the filter popover and button. Defaults to |
|
| Custom React node to replace the title on left side. |
Component: FilterButton
A forward-ref React component rendering a button with a funnel icon (filter icon) and an optional badge showing the count of active filters.
Props
Extends
ButtonProps(from../ui/button).Adds an optional
count?: numberprop showing the number of active filters.
Behavior
If
count> 0, shows a small badge with the count number.Always shows the funnel icon from
lucide-react.Uses a secondary button variant styles.
Usage Example
<FilterButton count={3} onClick={() => console.log('Filter clicked')} />
This renders a filter button with a badge showing "3" and triggers a click handler.
Component: ListFilterBar
The primary export; a composite filter bar UI component with search and filter controls.
Props
Extends IProps and all props from CheckboxFormMultipleProps (except setOpen), plus:
Prop | Type | Description |
|---|---|---|
| object or array | Current filter values, used to calculate active filters count. |
| function | Callback when filters change. |
| function | Callback when filter popover open state changes. |
| array | Filter definitions passed to |
| string (optional) | Additional CSS classes applied to the container div. |
|
| Optional icon to display alongside the title. If a string, rendered using |
Functional Details
Uses
useMemoto calculatefilterCountby summing the lengths of values in thevalueobject (assuming arrays of selections).Conditionally renders a
FilterPopoverifshowFilteris true, passing the relevant filter props and rendering theFilterButtonwith the count.Renders a
SearchInputwith the currentsearchStringandonSearchChangehandler.Shows either the
leftPanelnode or thetitleprop on the left side, optionally prefixed by theicon.Supports additional child components rendered after the search input.
Usage Example
<ListFilterBar
title="Users"
icon="user"
searchString={searchValue}
onSearchChange={handleSearchChange}
showFilter={true}
value={filtersValue}
onChange={handleFilterChange}
onOpenChange={handlePopoverOpen}
filters={filterDefinitions}
className="my-custom-class"
>
<SomeOtherComponent />
</ListFilterBar>
This example renders a filter bar titled "Users" with a user icon, search and filter controls, and additional child components.
Important Implementation Details
Filter Count Calculation:
The number displayed on the filter button badge is computed by iterating over thevalueobject, summing the lengths of the arrays representing selected filter options:Object.values(value).reduce((pre, cur) => pre + cur.length, 0)This assumes
valueis an object where each key corresponds to a filter category and each value is an array of selected options.Conditional Icon Rendering:
If theiconprop is a string, it uses theIconFontcomponent to render it; otherwise, it renders the React node directly. This allows flexible icon inputs.CSS Utility Usage:
Thecnutility function is used to combine class names conditionally. For example, it merges a fixed set of classes (flex,justify-between, etc.) with any custom classes passed viaclassName.ForwardRef for
FilterButton:FilterButtonusesReact.forwardRefto expose the button's ref for parent components, allowing focus management or other direct DOM interactions.Component Composition:
The filter UI is split into smaller reusable components (FilterButton,FilterPopover,SearchInput) to maintain separation of concerns and improve modularity.
Interaction with Other Modules
Uses UI primitives from the local UI library:
ButtonandButtonPropsfrom../ui/buttonSearchInputfrom../ui/inputFilterPopoverandCheckboxFormMultiplePropsfrom the sibling./filter-popovermodule.IconFontfrom../icon-fontfor icon rendering.
Uses the
Funnelicon from the externallucide-reacticon library.Uses a utility function
cnfrom@/lib/utilsfor conditional className merging.
These dependencies indicate integration with a design system or component framework within the project.
Visual Diagram
componentDiagram
component ListFilterBar {
+title: ReactNode
+searchString: string
+onSearchChange: ChangeEventHandler
+showFilter: boolean
+leftPanel: ReactNode
+value: object
+onChange: (val) => void
+onOpenChange: (open) => void
+filters: array
+className?: string
+icon?: ReactNode | string
+children: ReactNode
}
component FilterButton {
+count?: number
+props: ButtonProps
}
component FilterPopover {
+value
+onChange
+filters
+onOpenChange
+children
}
component SearchInput {
+value: string
+onChange: ChangeEventHandler
+className?: string
}
ListFilterBar --> FilterPopover : wraps
ListFilterBar --> FilterButton : renders inside FilterPopover
ListFilterBar --> SearchInput : renders
ListFilterBar --> IconFont : used if icon is string
FilterButton --> Button : extends
FilterButton --> Funnel : icon inside button
Summary
The index.tsx file provides a composable React filter bar UI component (ListFilterBar) that integrates search input, filter popover controls, and flexible title/icon support. It is designed to be easily customized and embedded in list or table views where search and filtering are needed. The FilterButton component shows the number of active filters and triggers the filter popover. The file leverages local UI components and icons, making it a key part of the app's filtering user experience.