filter-popover.tsx
Overview
The filter-popover.tsx file provides a reusable React component for displaying and managing a multi-select filter UI within a popover. It is designed to integrate with form state management (using react-hook-form and zod for schema validation) and supports multiple filter groups, each containing multiple checkbox options. Users can select or clear filter values, which are then propagated back via callback handlers.
This component is typically used in applications where dynamic filtering of data lists or tables is required, allowing users to refine displayed content interactively.
Detailed Component and Function Documentation
1. CheckboxFormMultiple Component
Purpose
Manages the form UI and logic for multiple checkbox filters. It renders groups of checkbox fields based on provided filters, handles selection state, validation (via Zod schema), submission, and reset.
Props (CheckboxFormMultipleProps)
Prop | Type | Description |
|---|---|---|
|
| Array of filter groups, each with field name, label, and list of options. |
|
| Currently selected filter values, keyed by filter field. |
|
| Callback invoked when the filter values change (on submit or reset). |
|
| Callback for popover open state changes (not used internally here). |
|
| Function to control popover open/close state externally. |
Internal Logic
Form Schema Construction: Dynamically creates a Zod schema object where each filter field is validated as an array of strings.
Form Initialization: Uses
react-hook-formwith the generated schema and default values derived from the filters.Submit Handler: Calls
onChangewith updated filter data and closes the popover.Reset Handler: Resets the form to initial empty selections and closes the popover.
Effect: Syncs form values with external
valueprop whenever it changes.
Rendered UI
For each filter group:
A label is displayed.
A list of checkboxes representing filter options, each showing the label and count.
Two buttons at the bottom:
Clear: Resets the selections.
Submit: Submits the form.
Example Usage
<CheckboxFormMultiple
filters={[
{
field: "category",
label: "Category",
list: [
{ id: "books", label: "Books", count: 12 },
{ id: "electronics", label: "Electronics", count: 5 },
],
},
]}
value={{ category: ["books"] }}
onChange={(newFilters) => console.log(newFilters)}
setOpen={(open) => console.log("Popover open:", open)}
/>
2. FilterPopover Component
Purpose
A wrapper component that manages the popover's open/close state and renders the CheckboxFormMultiple inside a Popover UI. It abstracts the popover behavior from the form logic.
Props
Extends CheckboxFormMultipleProps except it omits setOpen (which is managed internally).
Prop | Type | Description |
|---|---|---|
|
| The trigger element which opens the popover on click. |
|
| Current filter selections. |
|
| Callback when filter changes occur. |
|
| Optional callback on popover open state changes. |
|
| Filter definitions to render inside the popover. |
Internal Logic
Maintains
openstate for popover visibility.Calls
onOpenChangecallback if provided when popover state changes.Passes
setOpenfunction toCheckboxFormMultipleto allow it to close the popover on submit/reset.
Example Usage
<FilterPopover
filters={filters}
value={selectedFilters}
onChange={(newFilters) => setSelectedFilters(newFilters)}
>
<button>Open Filters</button>
</FilterPopover>
Types and Interfaces
These types are imported from ./interface and define the structure of the filters and their values.
FilterCollection
Represents a filter group with fields:
field: string — unique field keylabel: string — display label for the filter grouplist: array of filter options (each withid,label,count)
FilterValue
An object mapping filter field keys to arrays of selected option IDs (strings).
FilterChange
Callback type invoked on filter value changes.
Implementation Details and Algorithms
Dynamic Zod Schema Creation:
The form validation schema is dynamically constructed based on the incoming filters array, supporting any number of filter groups without hardcoding. Each filter group's field expects an array of strings, representing the selected checkbox IDs.State Synchronization:
The form's internal state is synchronized with the externalvalueprop using auseEffectthat callsform.reset(value). This ensures that if the filter values change externally, the form UI reflects the updates immediately.Efficient Checkbox Handling:
Each checkbox updates the corresponding array for its group by either adding or removing the selected item ID. This is done immutably using array spread and filter operations.Popover Control Flow:
The popover open state is controlled both internally inFilterPopoverand externally via callbacks, allowing parent components to respond to open/close events and control the popover if necessary.
Interactions with Other System Parts
UI Components:
Relies heavily on shared UI components such asPopover,Button,Checkbox, and form components (Form,FormField,FormControl,FormItem,FormLabel,FormMessage) which are likely part of a design system or component library.Localization:
Uses thei18nexttfunction to translate button labels like "Clear" and "Submit".Form Validation:
Usesreact-hook-formfor form state and validation, integrated withzodviazodResolver.Filter Data:
Expects filter definitions and current filter values from parent components, enabling this component to be flexible and reusable for different filter configurations.
Mermaid Component Diagram
componentDiagram
FilterPopover <|-- CheckboxFormMultiple
component FilterPopover {
+children: ReactNode
+value: FilterValue
+onChange(data: FilterValue): void
+onOpenChange(open: boolean): void (optional)
+filters: FilterCollection[]
-open: boolean
-setOpen(open: boolean): void
}
component CheckboxFormMultiple {
+filters: FilterCollection[]
+value: FilterValue
+onChange(data: FilterValue): void
+setOpen(open: boolean): void
-form: react-hook-form instance
-onSubmit(data): void
-onReset(): void
}
FilterPopover o-- Popover
Popover "1" -- "*" CheckboxFormMultiple
CheckboxFormMultiple --> Form
Form --> FormField
FormField --> Checkbox
CheckboxFormMultiple --> Button
Summary
CheckboxFormMultiple: A form component rendering multiple checkbox filter groups with validation and controlled state.FilterPopover: A popover wrapper that toggles the visibility ofCheckboxFormMultipleand manages its open state.Dynamic form schema created based on passed filter definitions.
Uses shared UI components and integrates localization.
Designed for flexibility and reuse in filtering interfaces of applications.
This file is central to implementing intuitive, multi-filter selection UIs, encapsulating form logic and popover behavior in clean, modular React components.