forked from reactabular/treetabular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
breaking(moveRows) - Decouple
tree.moveRows
from *reactabular-dnd*
Now you have to pass the move operation instead.
- Loading branch information
Showing
5 changed files
with
71 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |