1.1.5 • Published 2 years ago

react-cool-search v1.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

REACT-COOL-SEARCH

This is a React hook that provides a simples way to implement search/filter functionality on a list of objects based on their properties in React Components.

GitHub Workflow Status Coverage Status npm version npm downloads npm all downloads npm bundle size MIT License All Contributors GitHub Repo stars

Installation

This package is distributed via npm.

$ yarn add react-cool-search
# or
$ npm install --save react-cool-search

Usage

Common use case.

import useSearch from 'react-cool-search';

interface User {
  id: number;
  name: string;
  lastName: string;
}

const users: User[] = [
  { id: 1, name: 'Lorem', lastName: 'Ipsum' },
  { id: 2, name: 'Foo', lastName: 'Bar' },
  { id: 3, name: 'Feijão', lastName: 'Arroz' },
  { id: 4, name: 'John', lastName: 'Doe' },
];

const Users = () => {
  const { data, query, handleChange } = useSearch(users);

  return (
    <div>
      <input
        type="text"
        placeholder="Search users"
        value={query}
        onChange={handleChange}
      />
      <ul>
        {data.map(user => (
          <li key={user.id + user.name}>{user.name}</li>
        ))}
      </ul>
    </div>
  );
};

Definig fields to search.

import useSearch from 'react-cool-search';

interface User {
  id: number;
  name: string;
  lastName: string;
}

const users: User[] = [
  { id: 1, name: 'Lorem', lastName: 'Ipsum' },
  { id: 2, name: 'Foo', lastName: 'Bar' },
  { id: 3, name: 'Feijão', lastName: 'Arroz' },
  { id: 4, name: 'John', lastName: 'Doe' },
];

const Users = () => {
  const { data, query, handleChange } = useSearch(users, { fields: ['name'] });

  return (
    <div>
      <input
        type="text"
        placeholder="Search users only by name"
        value={query}
        onChange={handleChange}
      />
      <ul>
        {data.map(user => (
          <li key={user.id + user.name}>{user.name}</li>
        ))}
      </ul>
    </div>
  );
};

API

useSearch<T>

const obj = useSearch<T>(collection: T[], options?: Options);

react-cool-search provides a hook as default export; it takes two parameters:

KeyTypeDefaultDescription
collectionArrayAn array of elements of type T.
optionsobjectConfiguration object. See Options.

Options

The options object contains the following properies:

KeyTypeDefaultDescription
initialQuerystring""The query used for the initial collection returned from useSearch
debouncenumber300Number of milliseconds to delay before triggering the function to filter the collection.
fieldsArrayObject.keys(collection[0])Properties that must be searched for each object in the collection.

Return object

The hook returns an object with the following properies:

KeyTypeDefaultDescription
dataArrayinitialQuery ? filterCollection(collection) : collectionA filtered version of collection passed to useSearch.
statusstringinitialQuery ? 'OK' : 'IDLE'Search status. It might be IDLE or OK or NOT_FOUND
querystringinitialQueryThe current query
handleChangefunction(event) => {}An event handler for an HTML input element. This is to be passes to the search input element as its onChange prop.
setQueryfunction(query) => {}A function to programmatically set the query value.

License

MIT © Arimário Jesus

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!