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-filterable-content

v1.0.0

Published

React library to make any react component searchable and filterable

Downloads

135

Readme

react-filterable-content

Working Example

http://filterable.adnanhidayat.com

How It's Work

This library work by tapping the component's rendering, going through the component hierarchy to find the matching keyword, give highlight styling to the matching text and only render the one that has matching text.

Getting Started

Install

  • Using NPM

    npm install --save react-filterable-content
  • Using Yarn

    yarn add react-filterable-content

Basic Usage

  • FilterableContent

    This is main building block for this library. We should wrap the content that need to be filtered in this component.

    This component received two props as follows:

    • keyword - Receive string variable as keyword to filter the child's component.
    • config - Receive javascript object contain configuration for the Filter Engine. See Configuration section for more detail.

    JSX Example :

      <FilterableContent 
        keyword={keyword}
        config={config}
        >
        <p>First Content Here</p>
        <p>Second Content Here</p>
        <p>Third Content Here</p>
      </FilterableContent>
  • FilterableSection

    Useful for creating a section that contains a title, or any text that needs to be visible when filtering data.

    FilterableSection has the same function with filterable-sticky flag. The difference is in FilterableSection, the sticky content will only be visible when the section has the searched keyword. Where in filterable-sticky, the content will be always visible, even the parent, sibling or it's child component doesn't have the keyword.

    This component received one props below:

    • stickyContent - Put node or string that will be visible when FilterableSection has the keyword, for example is the title of the section.

    JSX Example :

      <FilterableContent 
        keyword={keyword}
        >
        <FilterableSection 
          stickyContent={<h3>Group Demo</h3>}
         >
          <p>This paragraph is not grouped with the third paragraph</p>
          <span filterable-group="true">
            <p><i>This paragraph</i> is grouped with the third paragraph</p>
            <p>A revenue loses! The owed break refreshes software. Programming weighs the forest. On top of the institute dresses software. Software negates programming into its isolate blessed. The aardvark bells programming over a portrayed biology.</p>
          </span>
        </FilterableSection>
      </FilterableContent>

Config and Customize Filter Behaviour

Configuration

There is some configuration that can affect the filter behaviour. Below is the default configuration for the FilterEngine.

  {
    maxCache: 30, 
    highlightResult: true,
    caseSensitive: false,
    highlightStyle: {
      background: '#fff542',
      display: 'inline',
      padding: '2px 0'
    }
  }
  • maxCache - Every search result will be cached in memory, this config is to configure how many search results kept in the memory.
  • highlightResult - Set whether the found keyword should be highlighted or not. You can set this as false to make the rendering faster. Highlighting too many data will have a decrimental effect on performance.
  • caseSensitive - Set true if you want the searching is case sensitive
  • highlightStyle - Styling for the highlight, you can change the color, opacity, and other detail in here. The style should be plain CSS-in-js object.

To override the configuration, you can pass the config object as props in FilterableContent component.

Customize Filter Behaviour

You can also add specific props to the any components inside to instruct the Filter Engine about what to do with those components. Props that available are:

  • filterable-sticky - It will make sure the component always be displayed, although they don't have the matched keyword. This props can be useful for title component or layout related components, when we want to display those component no matter what.
  • filterable-group - To group the components. It's useful for a component that consists of several child components, and we want to display all part of the components if there any matching keyword inside the child component. Without this flag, the Filter engine will only show the child that has the matching keyword.
  • filterable-ignore - If components have this props, then Filter engine will ignore this component. It will not count as a result although the component has the keyword.
  • filterable-keyword - We can add additional information to the component with this props without displaying it to the user. The Filter Engine will display the component if the content of keyword props matched with the search input.

You can see how to apply the props above in example folder.

Example

Basic Example

import React, { Component } from 'react'
import { FilterableContent } from 'react-filterable-content'

export default class Example extends Component {

  constructor(props){
    super(props);
    this.state = {
      keyword: '',
    }  
    this.onChange = this.onChange.bind(this);

  }

  onChange(event) {
    if (event && event.target) {
      const { value } = event.target;
      this.setState({keyword:value});
    }
  }

  render () {
    let { keyword } = this.state;

    return (
      <div>
        <input
          type={'text'}
          value={keyword}
          placeholder={'input keyword'}
          onChange={this.onChange}
        />

        <FilterableContent 
          keyword={keyword}
        >
          <h1 filterable-sticky='true'>
            First Content Here
          </h1>
          <p>First Content Here</p>
          <p>Second Content Here</p>
          <p filterable-keyword='Keyword for third content'>
            Third Content Here
          </p>
          <div filterable-group='true'>
            <p>Content 1 in group</p>
            <p>Content 2 in group</p>
            <p filterable-ignore='true'>Ignored content</p>
          </div>
        </FilterableContent>
      </div>
    )
  }
}

Advanced Example

You can get more advanced example in 'example' folder.

License

MIT © adnanhidayat