diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b653c6..87f6a81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ================== * Feature - Add `tree.wrap = ({ operations: [rows => rows], idField = 'id' }) => (rows) => []`. + * Breaking - `tree.search` does not depend on *selectabular* directly anymore. Instead you have to pass the search `operation` to it. The new signature is `tree.search = ({ operation: (rows) => [], idField = 'id', parentField = 'parent' }) => (rows) => []`. * Breaking - `tree.sort` has been dropped. Use `tree.wrap` instead. Example: ```javascript diff --git a/README.md b/README.md index 28669aa..608251d 100644 --- a/README.md +++ b/README.md @@ -77,9 +77,9 @@ Returns parents based on given `rows` and `index`. Returns a boolean based on whether or not the row at the given `index` has children. -**`tree.search = ({ columns, query, idField = 'id', parentField = 'parent' }) => (rows) => []`** +**`tree.search = ({ operation: (rows) => [], idField = 'id', parentField = 'parent' }) => (rows) => []`** -Searches against a tree structure while matching against children too. If children are found, associated parents are returned as well. +Searches against a tree structure using `operation` while matching against children too. If children are found, associated parents are returned as well. This has been designed to [searchtabular](https://www.npmjs.com/package/searchtabular) `multipleColumns` and `singleColumn`, but as long as the passed operation follows the interface, it should fit in. > This depends on [resolve.index](https://www.npmjs.com/package/table-resolver#resolveindex)! @@ -261,7 +261,9 @@ class TreeTable extends React.Component { }) ] }), - tree.search({ columns, query }) + tree.search({ + operation: search.multipleColumns({ columns, query }) + }) )(this.state.rows); return ( diff --git a/__tests__/search-test.js b/__tests__/search-test.js index 7cb1fd5..c95e5d5 100644 --- a/__tests__/search-test.js +++ b/__tests__/search-test.js @@ -1,8 +1,14 @@ +import { multipleColumns } from 'searchtabular'; import { search } from '../src'; describe('tree.search', function () { it('returns empty rows if empty rows are passed', function () { - expect(search({ columns: [], query: {} })([])).toEqual([]); + expect(search({ + operation: multipleColumns({ + columns: [], + query: {} + }) + })([])).toEqual([]); }); it('returns matching rows', function () { @@ -20,7 +26,11 @@ describe('tree.search', function () { foo: 'bar' }; - expect(search({ columns, query })(given)).toEqual(given); + expect( + search({ + operation: multipleColumns({ columns, query }) + })(given) + ).toEqual(given); }); it('matches children', function () { @@ -46,7 +56,9 @@ describe('tree.search', function () { foo: 'zoo' }; - expect(search({ columns, query })(given)).toEqual(given); + expect(search({ + operation: multipleColumns({ columns, query }) + })(given)).toEqual(given); }); it('matches multiple children', function () { @@ -78,7 +90,9 @@ describe('tree.search', function () { foo: 'zoo' }; - expect(search({ columns, query })(given)).toEqual(given); + expect(search({ + operation: multipleColumns({ columns, query }) + })(given)).toEqual(given); }); it('returns the same structure with an empty query', function () { @@ -108,7 +122,9 @@ describe('tree.search', function () { ]; const query = {}; - expect(search({ columns, query })(given)).toEqual(given); + expect(search({ + operation: multipleColumns({ columns, query }) + })(given)).toEqual(given); }); it('returns the same structure with an empty all query', function () { @@ -140,7 +156,9 @@ describe('tree.search', function () { all: '' }; - expect(search({ columns, query })(given)).toEqual(given); + expect(search({ + operation: multipleColumns({ columns, query }) + })(given)).toEqual(given); }); it('retains children on match', function () { @@ -191,6 +209,14 @@ describe('tree.search', function () { } ]; - expect(search({ columns, query })(given)).toEqual(expected); + expect(search({ + operation: multipleColumns({ columns, query }) + })(given)).toEqual(expected); + }); + + /* TODO: test idField/parentField */ + + it('throws an error if operation is not passed', function () { + expect(search.bind(null, {})).toThrow(Error); }); }); diff --git a/package.json b/package.json index be6abd8..130c3be 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,6 @@ "lodash": ">= 3.0.0 < 5.0.0", "react": ">= 15.0.0 < 16.0.0", "reactabular-dnd": ">= 8.0.0 < 9.0.0", - "searchtabular": ">= 1.0.0 < 2.0.0", "redux": ">= 3.0.0 < 4.0.0" }, "pre-push": [ diff --git a/src/search.js b/src/search.js index fe6e189..a519a6c 100644 --- a/src/search.js +++ b/src/search.js @@ -1,27 +1,22 @@ import { uniq } from 'lodash'; -import { multipleColumns } from 'searchtabular'; import getChildren from './get-children'; import getParents from './get-parents'; function searchTree({ - columns, - query, idField = 'id', - parentField = 'parent' + parentField = 'parent', + operation } = {}) { + if (!operation) { + throw new Error('tree.search - Missing operation!'); + } + return (rows) => { // Track fetched parents to get them into the results only once const fetchedParents = {}; - if (!Object.keys(query).length) { - return rows; - } - return uniq([].concat( - ...multipleColumns({ - columns, - query - })(rows).map((row) => { + ...operation(rows).map((row) => { const rowParent = row[parentField]; if (fetchedParents[rowParent]) {