Skip to content

Commit

Permalink
breaking(sort) - Replace sort with more generic wrap
Browse files Browse the repository at this point in the history
This is handy if you want to perform an operation against root level
nodes.
  • Loading branch information
bebraw committed Dec 16, 2016
1 parent 2de496b commit 0af87e6
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 44 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
## treetabular

2.0.0 / 2016-12-16
==================

* Feature - Add `tree.wrap = ({ operations: [rows => rows], idField = 'id' }) => (rows) => [<operatedRow>]`.
* Breaking - `tree.sort` has been dropped. Use `tree.wrap` instead. Example:

```javascript
wrap({
operations: [
sorter({
columns,
sortingColumns,
sort: orderBy
})
]
})(rows);
```

1.0.6 / 2016-11-30
==================

Expand Down
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,23 @@ Searches against a tree structure while matching against children too. If childr

> This depends on [resolve.index](https://www.npmjs.com/package/table-resolver#resolveindex)!
**`tree.sort = ({ columns, sortingColumns, strategy, idField = 'id' }) => (rows) => [<sortedRow>]`**
**`tree.wrap = ({ operations: [rows => rows], idField = 'id' }) => (rows) => [<operatedRow>]`**

Sorts a tree (packs/unpacks internally to maintain root level sorting).
If you want to perform an operation, such as sorting, against the root rows of a tree, use `tree.wrap`.

**Example:**

```javascript
wrap({
operations: [
sorter({
columns,
sortingColumns,
sort: orderBy
})
]
})(rows);
```

### Packing

Expand Down Expand Up @@ -239,9 +253,13 @@ class TreeTable extends React.Component {
const visibleColumns = columns.filter(column => column.visible);
const rows = compose(
tree.filter({ fieldName: 'showingChildren' }),
tree.sort({
columns,
sortingColumns
tree.wrap({
operations: [
sort.sorter({
columns,
sortingColumns
})
]
}),
tree.search({ columns, query })
)(this.state.rows);
Expand Down
43 changes: 35 additions & 8 deletions __tests__/sort-test.js → __tests__/wrap-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { sort } from '../src';
import { sorter } from 'sortabular';
import { orderBy } from 'lodash';
import { wrap } from '../src';

describe('tree.sort', function () {
describe('tree.wrap', function () {
it('returns empty rows if empty rows are passed', function () {
expect(sort({ columns: [], query: {} })([])).toEqual([]);
expect(wrap({
operations: [
sorter({
columns: [],
sortingColumns: {},
sort: orderBy
})
]
})([])).toEqual([]);
});

it('returns sorted rows', function () {
Expand Down Expand Up @@ -34,7 +44,15 @@ describe('tree.sort', function () {
}
];

expect(sort({ columns, sortingColumns })(given)).toEqual(expected);
expect(wrap({
operations: [
sorter({
columns,
sortingColumns,
sort: orderBy
})
]
})(given)).toEqual(expected);
});

it('accepts custom id', function () {
Expand Down Expand Up @@ -70,10 +88,19 @@ describe('tree.sort', function () {
}
];

expect(sort({
columns,
sortingColumns,
idField: 'Id'
expect(wrap({
idField: 'Id',
operations: [
sorter({
columns,
sortingColumns,
sort: orderBy
})
]
})(given)).toEqual(expected);
});

it('throws an error if operations are not passed', function () {
expect(wrap.bind(null, {})).toThrow(Error);
});
});
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"react": ">= 15.0.0 < 16.0.0",
"reactabular-dnd": ">= 8.0.0 < 9.0.0",
"searchtabular": ">= 1.0.0 < 2.0.0",
"sortabular": ">= 1.0.0 < 2.0.0",
"redux": ">= 3.0.0 < 4.0.0"
},
"pre-push": [
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { default as hasChildren } from './has-children';
export { default as moveRows } from './move-rows';
export { default as pack } from './pack';
export { default as unpack } from './unpack';
export { default as sort } from './sort';
export { default as wrap } from './wrap';
export { default as search } from './search';
export { default as toggleChildren } from './toggle-children';
export { default as collapseAll } from './collapse-all';
Expand Down
29 changes: 0 additions & 29 deletions src/sort.js

This file was deleted.

20 changes: 20 additions & 0 deletions src/wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { compose } from 'redux';
import pack from './pack';
import unpack from './unpack';

function wrap({
idField,
operations
} = {}) {
if (!operations) {
throw new Error('tree.wrap - Missing operations!');
}

return compose(...[
unpack({ idField }),
...operations,
pack({ idField })
]);
}

export default wrap;

0 comments on commit 0af87e6

Please sign in to comment.