0.0.28 • Published 9 months ago

@gavant/react-mui-nested-filter v0.0.28

Weekly downloads
-
License
-
Repository
github
Last release
9 months ago

MUI X Tree View Filters Component

Note: 🚧 This project is a work in progress.

The Filters component is a flexible, reusable filtering system for managing hierarchical and standalone filters via MUI X Tree View. It supports customizable parent-child mappings, sorting, and label overrides. This document provides an overview of the main Filters component, its child components (NestedFilter and StandaloneFilter), and the data structure used in its implementation.


Overview

Filters Component

The Filters component serves as the topmost provider for managing filter state and logic. It uses React context to share state with child components and handles features like expanded items, checked items, and parent-child interactions.

Features

  • Controlled and Uncontrolled Modes: Supports controlledCheckedItems for external state management or defaults to internal state while also allowing initialCheckedItems.
  • Parent-Child Relationships: Handles hierarchical relationships via NestedFilter.
  • Standalone Items: Allows independent filters via StandaloneFilter.
  • Sorting: Supports custom sorting for both parent and child items.
  • Label Overrides: Provides customizable labels for filters.
  • Dynamic Options: Offers behavior customization through the options prop.

Props

Filters Props

PropTypeDefaultDescription
checkboxSizeMuiCheckboxSizes'medium'Size of the checkboxes.
initialCheckedItemsCheckedItemsTypeundefinedInitial state for checked items (uncontrolled mode).
controlledCheckedItemsCheckedItemsTypeundefinedExternal state for checked items (controlled mode).
onFilterChange(updatedFilters: CheckedItemsType) => voidRequiredCallback for when the filter state changes.
optionsFilterOptions{ replaceChildrenWithParentOnAllChecked: true }Configuration options for filter behavior.
childrenReactNode \| ReactNode[]RequiredChild components (NestedFilter, StandaloneFilter, etc.).

NestedFilter Props

PropTypeDefaultDescription
filterKeystringRequiredUnique key to group this filter.
itemsParentTypeRequiredParent filter items.
childItemsChildTypeRequiredChild filter items.
mappingMappingType<ParentType, ChildType>RequiredMaps parent items to their corresponding child items.
parentSortRecord<Value<I>, number>undefinedSorting order for parent items.
childSortRecord<Value<I>, number>undefinedSorting order for child items.
labelOverridesPartial<Record<Value<I> \| Value<C>, string>>undefinedCustom labels for parent and child items.
optionsFilterOptionsundefinedAdditional options for nested filters.

StandaloneFilter Props

PropTypeDefaultDescription
filterKeystringRequiredUnique key to group this filter.
titlestringRequiredDisplay title for the filter.
valuestringRequiredValue of the filter item.
groupWithChildrenbooleanfalseWhether this standalone filter should be grouped with child filters.

Data Structure

Enums and Mappings

The following enums and mappings are used to represent the parent-child relationships, sorting order, and label overrides for filters:

PetType Enum

export enum PetType {
    Dog = 'DOG',
    Cat = 'CAT',
    Bird = 'BIRD',
    Other = 'OTHER',
}

PetBreed Enum

export enum PetBreed {
    Abyssinian = 'ABYSSINIAN',
    Bengal = 'BENGAL',
    Parrot = 'PARROT',
    Poodle = 'POODLE',
}

breedMapping

export const breedMapping: Record<Exclude<PetType, PetType.Other>, PetBreed[]> = {
    [PetType.Cat]: [PetBreed.Abyssinian, PetBreed.Bengal],
    [PetType.Dog]: [PetBreed.Poodle],
    [PetType.Bird]: [PetBreed.Parrot],
};

Sorting

export const sort = {
    [PetType.Cat]: 3,
    [PetType.Dog]: 1,
    [PetType.Bird]: 2,
};

Label Overrides

export const Overrides = {
    [PetType.Bird]: 'Feathered Friends',
};

Example Usage

Basic Implementation

<Filters
    checkboxSize="medium"
    initialCheckedItems={CheckedItems}
    onFilterChange={handleFilterChange}
>
    <NestedFilter
        filterKey="PetTypeBreed"
        items={PetType}
        childItems={PetBreed}
        mapping={breedMapping}
        labelOverrides={Overrides}
        parentSort={sort}
    />
    <StandaloneFilter filterKey="PetTypeBreed" id="other" title="Other" value="OTHER" />
</Filters>

Explanation

  1. Filters: Wraps the entire filter system and manages shared state and logic.
  2. NestedFilter: Displays parent-child relationships with sorting and label overrides.
  3. StandaloneFilter: Adds an independent filter option for items like "Other."

Example Output

When users interact with the filters, the resulting state is passed to onFilterChange as an object. Child filters exist in the filterKey's child property, and respectively parent filters exist in the parent property.

Checked Example

{
    PetTypeBreed: {
        parent: ["BIRD"],
        child: ["ABYSSINIAN"]
    }
}

Notes

  • Parent-Child Relationship: The NestedFilter allows seamless mapping between parent and child filters with options for sorting and label customization.
  • Controlled Mode: Use controlledCheckedItems to manage state externally. For internal state, use initialCheckedItems.
  • Extensibility: Additional filters or components can be nested under Filters as needed.
0.0.28

9 months ago

0.0.27

9 months ago

0.0.26

9 months ago

0.0.25

9 months ago

0.0.24

9 months ago

0.0.23

9 months ago

0.0.22

9 months ago

0.0.21

9 months ago

0.0.20

9 months ago

0.0.19

9 months ago

0.0.18

9 months ago

0.0.17

9 months ago

0.0.16

9 months ago

0.0.15

9 months ago

0.0.14

10 months ago

0.0.13

10 months ago

0.0.12

10 months ago

0.0.11

10 months ago

0.0.10

10 months ago

0.0.9

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago