Skip to content

Commit

Permalink
breaking(moveRows) - Decouple tree.moveRows from *reactabular-dnd*
Browse files Browse the repository at this point in the history
Now you have to pass the move operation instead.
  • Loading branch information
bebraw committed Dec 16, 2016
1 parent 6b56b8c commit c1b75fd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 78 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
==================

* Feature - Add `tree.wrap = ({ operations: [rows => rows], idField = 'id' }) => (rows) => [<operatedRow>]`.
* Breaking `tree.moveRows` does not depend on *reactabular-dnd* directly anymore. Instead you have to pass the move `operation` to it. The new signature is `tree.moveRows = ({ operation: (rows) => [<row>], retain = [], idField = 'id', parentField = 'parent' }) => (rows) => [<movedRow>]`.
* 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) => [<row>], idField = 'id', parentField = 'parent' }) => (rows) => [<searchedRow>]`.
* Breaking - `tree.sort` has been dropped. Use `tree.wrap` instead. Example:

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ Unpacks children from root level nodes. This is useful with sorting and filterin

### Drag and Drop

**`tree.moveRows = ({ sourceRowId, targetRowId, retain = [], idField = 'id', parentField = 'parent' }) => (rows) => [<movedRow>]`**
**`tree.moveRows = ({ operation: (rows) => [<row>], retain = [], idField = 'id', parentField = 'parent' }) => (rows) => [<movedRow>]`**

Allows moving tree rows while `retain`ing given fields at their original rows.
Allows moving tree rows while `retain`ing given fields at their original rows. You should pass an `operation` that performs actual moving here. [reactabular-dnd](https://www.npmjs.com/package/reactabular-dnd) `moveRows` is one option.

### UI

Expand Down
49 changes: 21 additions & 28 deletions __tests__/move-rows-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { reverse } from 'lodash';
import * as dnd from 'reactabular-dnd';
import { moveRows } from '../src';

describe('tree.moveRows', function () {
Expand All @@ -17,8 +18,7 @@ describe('tree.moveRows', function () {
const targetRowId = 0;

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(rows);
});

Expand All @@ -37,8 +37,7 @@ describe('tree.moveRows', function () {
const targetRowId = 1;

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(reverse(rows));
});

Expand All @@ -61,8 +60,7 @@ describe('tree.moveRows', function () {
const targetRowId = 2;

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual([rows[1], rows[2], rows[0]]);
});

Expand Down Expand Up @@ -94,8 +92,7 @@ describe('tree.moveRows', function () {
];

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(expectedRows);
});

Expand All @@ -115,8 +112,7 @@ describe('tree.moveRows', function () {
const targetRowId = 1;

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(rows);

expect(console.warn).toBeCalled(); // eslint-disable-line no-console
Expand Down Expand Up @@ -159,8 +155,7 @@ describe('tree.moveRows', function () {
];

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(expectedRows);
});

Expand Down Expand Up @@ -203,8 +198,7 @@ describe('tree.moveRows', function () {
];

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(expectedRows);
});

Expand Down Expand Up @@ -239,8 +233,7 @@ describe('tree.moveRows', function () {
];

expect(moveRows({
sourceRowId,
targetRowId,
operation: dnd.moveRows({ sourceRowId, targetRowId }),
retain: [retainedField]
})(rows)).toEqual(expectedRows);
});
Expand Down Expand Up @@ -280,8 +273,7 @@ describe('tree.moveRows', function () {
];

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(expectedRows);
});

Expand Down Expand Up @@ -322,8 +314,7 @@ describe('tree.moveRows', function () {
];

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(expectedRows);
});

Expand Down Expand Up @@ -374,8 +365,7 @@ describe('tree.moveRows', function () {
];

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(expectedRows);
});

Expand Down Expand Up @@ -427,8 +417,7 @@ describe('tree.moveRows', function () {
];

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(expectedRows);
});

Expand Down Expand Up @@ -480,8 +469,7 @@ describe('tree.moveRows', function () {
];

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(expectedRows);
});

Expand Down Expand Up @@ -543,8 +531,13 @@ describe('tree.moveRows', function () {
];

expect(moveRows({
sourceRowId,
targetRowId
operation: dnd.moveRows({ sourceRowId, targetRowId })
})(rows)).toEqual(expectedRows);
});

/* TODO: test idField/parentField */

it('throws an error if operation is not passed', function () {
expect(moveRows.bind(null, {})).toThrow(Error);
});
});
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"peerDependencies": {
"lodash": ">= 3.0.0 < 5.0.0",
"react": ">= 15.0.0 < 16.0.0",
"reactabular-dnd": ">= 8.0.0 < 9.0.0",
"redux": ">= 3.0.0 < 4.0.0"
},
"pre-push": [
Expand Down
94 changes: 47 additions & 47 deletions src/move-rows.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
import { findIndex, omit, pick } from 'lodash';
import { moveRows } from 'reactabular-dnd';

const moveTreeRows = ({
sourceRowId,
targetRowId,
operation,
retain = [],
idField = 'id',
parentField = 'parent'
} = {}) => (rows) => {
let movedRows = moveRows({
sourceRowId,
targetRowId,
idField
})(rows);
let cancelMoving = false;
} = {}) => {
if (!operation) {
throw new Error('tree.moveRows - Missing operation!');
}

if (movedRows) {
// Walk through the old row definition, patch parent relations and fields
// of the new one
movedRows = rows.map((row, i) => {
if (typeof row[parentField] === 'undefined' || row[parentField] === null) {
return {
...omit(movedRows[i], retain),
...pick(row, retain),
[idField]: movedRows[i][idField],
[parentField]: undefined
};
}
return (rows) => {
let movedRows = operation(rows);
let cancelMoving = false;

// Find the index of the old parent
const index = findIndex(rows, {
[idField]: row[parentField]
});
if (movedRows) {
// Walk through the old row definition, patch parent relations and fields
// of the new one
movedRows = rows.map((row, i) => {
if (typeof row[parentField] === 'undefined' || row[parentField] === null) {
return {
...omit(movedRows[i], retain),
...pick(row, retain),
[idField]: movedRows[i][idField],
[parentField]: undefined
};
}

if (index < 0) {
console.warn( // eslint-disable-line no-console
'tree.moveRows - Failed to find the old parent', rows, row, idField, parentField
);
// Find the index of the old parent
const index = findIndex(rows, {
[idField]: row[parentField]
});

cancelMoving = true;
if (index < 0) {
console.warn( // eslint-disable-line no-console
'tree.moveRows - Failed to find the old parent', rows, row, idField, parentField
);

return null;
}
cancelMoving = true;

// Figure out the new id based on that index
const id = movedRows[index][idField];
return null;
}

return {
...omit(movedRows[i], retain),
...pick(row, retain),
[idField]: movedRows[i][idField],
[parentField]: id
};
});
}
// Figure out the new id based on that index
const id = movedRows[index][idField];

if (cancelMoving) {
return rows;
}
return {
...omit(movedRows[i], retain),
...pick(row, retain),
[idField]: movedRows[i][idField],
[parentField]: id
};
});
}

if (cancelMoving) {
return rows;
}

return movedRows;
return movedRows;
};
};

export default moveTreeRows;

0 comments on commit c1b75fd

Please sign in to comment.