npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-data-multi-filter

v0.5.10

Published

A react component for efficient data filtering.

Downloads

40

Readme

React Data Multi Filter

A react component for efficient data filtering.

Multi-level function caching (using memoization)enables efficient filtering for large data sets, large filter sets, and expensive filters.

Render prop and Context API makes the component extremely flexible.

Usage

This is a standalone component with render props...

import { DataFilter } from 'react-data-multi-filter';

const MyComponent = () => {
  const data = [0, 1, 2, 3, 4, 5];
  const greaterThanTwo = (datum: number) => datum > 2;
  const lessThanFive = (datum: number) => datum < 5;
  return (
    <DataFilter data={data} filters={{ greaterThanTwo, lessThanFive }}>
      {({ filteredInData, addFilter }) => (
        <>
          <button onClick={() => addFilter({ equalToThree: datum => datum === 3 })}>AddFilter</button>
          <span>
            {filteredInData}
          </span>
        </>
      )}
    </DataFilter>,
  );
}

...and a context provider...

import { DataFilterContext } from 'react-data-multi-filter';

const MyComponent = () => {
  const data = [{ a: 0 }, { a: 1 }, { a: 2 }];
  const greaterThanOne = (datum: { a: number }) => datum.a > 1;
  return (
    <DataFilterContext.Provider data={data}>
      <DataFilterContext.Consumer filters={{ greaterThanOne }}>
        {({ filteredInData }) => (
          <span>
            {filteredInData/* [{ a: 2 }] */}
          </span>
        )}
      </DataFilterContext.Consumer>
    </DataFilterContext.Provider>,
  );
}

Advanced Usage

Create multiple independent data filter contexts

import { createDataFilterContext } from 'react-data-multi-filter';

const MyComponent = () => {
  const data = [0, 1, 2, 3, 4, 5];
  const greaterThanTwo = (datum: number) => datum > 2;
  const lessThanFive = (datum: number) => datum < 5;

  const DataFilter1Context = createDataFilterContext();
  const DataFilter2Context = createDataFilterContext();
  return (
    <DataFilter1Context.Provider data={data} filters={{ greaterThanTwo }}>
      <DataFilter2Context.Provider data={data} filters={{ lessThanFive }}>
        <DataFilter1Context.Consumer>
          {({ filteredInData }) => (
            <span>
              {filteredInData}
            </span>
          )}
        </DataFilter1Context.Consumer>
        <DataFilter2Context.Consumer>
          {({ filteredInData }) => (
            <span>
              {filteredInData}
            </span>
          )}
        </DataFilter2Context.Consumer>
      </DataFilter2Context.Provider>,
    </DataFilter1Context.Provider>,
  );
}

Selectively apply filters

import { DataFilterContext } from 'react-data-multi-filter';

const MyComponent = () => {
  const data = [0, 1, 2, 3, 4, 5];
  const greaterThanTwo = (datum: number) => datum > 2;
  const lessThanFive = (datum: number) => datum < 5;
  const myFilters = { greaterThanTwo, lessThanFive };
  return (
    <DataFilterContext.Provider data={data} filters={myFilters}>
      <DataFilterContext.Consumer exclude={['lessThanFive']}>
        {({ filteredInData }) => (
          <span>
            {filteredInData}
          </span>
        )}
      </DataFilterContext.Consumer>
    </DataFilterContext.Provider>,
  );
}

API

Named Exports:

  • DataFilter
  • DataFilterContext
  • createDataFilterContext

DataFilter

export interface DataFilterProps<T> {
  data: T[]; // Data to filter
  filters?: { // Associative array of initial filters
    [key: string]: (datum: T, idx?: number, data?: T[]): boolean;
  };
  children: (props: { // Render props
    filteredInData: T[]; // Data included by the filters
    filteredOutData: T[]; // Data excluded by the filters
    allData: T[]; // Full data set
    filters: { // Associative array of all filters
      [key: string]: (datum: T, idx?: number, data?: T[]) => boolean;
    };
    activeFilters: { // Associative array of all active filters
      [key: string]: (datum: T, idx?: number, data?: T[]) => boolean;
    };
    addFilters: (filters: { // Add filters dynamically
      [key: string]: (datum: T, idx?: number, data?: T[]) => boolean;
    }) => void;
    removeFilters: (removeKeys: string[]) => void; // Remove filters by key dynamically
    filterData: (opts?: { // Function to apply a custom filter set
      exclude?: string[]; // Keys of filters to exclude
      include?: string[]; // Keys of filters to include
    }) => {
      filteredInData: T[]; // Data included by the custom filter set
      filteredOutData: T[];// Data excluded by the custom filter set
      activeFilters: { // Associative array of all active filters
        [key: string]: (datum: T, idx?: number, data?: T[]) => boolean;
      };
    };
  }) => JSX.Element;
}

DataFilterContext.Provider

export interface DataFilterContext<T>.Provider {
  data: T[]; // Data to filter
  filters?: { // Associative array of initial filters
    [key: string]: (datum: T, idx?: number, data?: T[]) => boolean;
  };
}

DataFilterContext.Consumer

export interface DataFilterContext<T>.Consumer {
  exclude?: string[]; // Keys of filters to exclude
  include?: string[]; // Keys of filters to include
  filters?: { [key: string]: (datum: T, idx?: number, data?: T[]) => boolean }; // Filters added by this component
  children: (props: { // Render Props
    filteredInData: T[]; // Data included by the filters
    filteredOutData: T[]; // Data excluded by the filters
    allData: T[]; // Full data set
    filters: { // All data filters
      [key: string]: (datum: T, idx?: number, data?: T[]) => boolean;
    };
    activeFilters: { // Active data filters
      [key: string]: (datum: T, idx?: number, data?: T[]) => boolean;
    };
    addFilters: (filters: { // Add new filters dynamically
      [key: string]: (datum: T, idx?: number, data?: T[]) => boolean;
    }) => void;
    removeFilters: (removeKeys: string[]) => void; // Remove filters by keys dynamically
    filterData: (opts?: { // Function to apply a custom filter set
      exclude?: string[]; // Keys of filters to exclude
      include?: string[]; // Keys of filters to include
    }) => {
      filteredInData: T[]; // Data included by the custom filter set
      filteredOutData: T[]; // Data excluded by the custom filter set
      activeFilters: { // Associative array of all active filters
        [key: string]: (datum: T, idx?: number, data?: T[]) => boolean;
      };
    };
  }) => JSX.Element;
}

createDataFilterContext

function createDataFilterContext<T>(): DataFilterContext<T>