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

filters

FilterCollection[]

Array of filter groups, each with field name, label, and list of options.

value

FilterValue

Currently selected filter values, keyed by filter field.

onChange

(data: FilterValue) => void

Callback invoked when the filter values change (on submit or reset).

onOpenChange

(open: boolean) => void (optional)

Callback for popover open state changes (not used internally here).

setOpen

(open: boolean) => void

Function to control popover open/close state externally.

Internal Logic

Rendered UI

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

children

React.ReactNode

The trigger element which opens the popover on click.

value

FilterValue

Current filter selections.

onChange

(data: FilterValue) => void

Callback when filter changes occur.

onOpenChange

(open: boolean) => void (optional)

Optional callback on popover open state changes.

filters

FilterCollection[]

Filter definitions to render inside the popover.

Internal Logic

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.


Implementation Details and Algorithms


Interactions with Other System Parts


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

This file is central to implementing intuitive, multi-filter selection UIs, encapsulating form logic and popover behavior in clean, modular React components.