npm.io
0.1.0 • Published 8 years ago

react-refine

Licence
MIT
Version
0.1.0
Deps
1
Size
142 kB
Vulns
0
Weekly
0

react-refine

Travis npm package Coveralls

Composable components for flexible filtering and sorting!

Installation

yarn add react-refine

DOCS ARE WIP!

Usage

Consuming refine results

Within a scope filter and sorting can be applied using (withSorter and withFilter) HoCs. The result can then be consumed using Refine (filtered & sorted), Filter and Sort components.

import { RefineScope, Refine } from 'react-refine';

<RefineScope>
  <FreeTextSearch />

  <Refine items={['abc', '123', 'def']}>{items => items.map(item => <div>{item}</div>)}</Refine>
</RefineScope>;

withFilter HOC

To create components which provides filters in the scope you can use the filterController HOC, which will provide removeFilter() and setFilter(filterFunc) functions to your component.

Example usage of withFilter
import { withFilter } from 'react-refine';

class FreeTextSearch extends Component {
  onChange = evt => {
    const query = evt.target.value;

    if (query.length === 0) {
      this.props.unsetFilter();
    } else {
      this.props.setFilter(items => items.filter(item => item.includes(query)));
    }
  };

  render() {
    return <input onChange={this.onChange} />;
  }
}

export default withFilter(FreeTextSearch);
Example SortControl usage
import { SortControl, ASC, DESC, OFF } from 'react-refine';

const Column = ({ column, comparator }) => (
  <SortControl comparator={comparator}>
    {({ toggleSorter, removeSorter, sortDirection }) => (
      <th>
        {column}{' '}
        <Arrow
          direction={sortDirection === DESC ? 'down' : 'up'}
          disabled={sortDirection === OFF}
        />
      </th>
    )}
  </SortControl>
);
const alphabeticSort = (field) => (a, b) => a[field] < b[field] ? -1 : a[field] > b[field] ? 1 : 0

<Column column={'Name'} comparator={alphabeticSort('name')} />
<Column column={'Adress'} comparator={alphabeticSort('street')} />

Keywords