Skip to content

Commit

Permalink
Add onSortOver prop to SortableContainer (clauderic#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse authored and kiejo committed Jul 15, 2021
1 parent 590e3fe commit 06b8d9f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ There are already a number of great Drag & Drop libraries out there (for instanc
| shouldCancelStart | Function | [Function](https://github.com/clauderic/react-sortable-hoc/blob/master/src/SortableContainer/index.js#L48) | This function is invoked before sorting begins, and can be used to programatically cancel sorting before it begins. By default, it will cancel sorting if the event target is either an `input`, `textarea`, `select` or `option`. |
| onSortStart | Function | | Callback that is invoked when sorting begins. `function({node, index, collection}, event)` |
| onSortMove | Function | | Callback that is invoked during sorting as the cursor moves. `function(event)` |
| onSortOver | Function | | Callback that get's invoked when moving over an item. `function({index, oldIndex, newIndex, collection}, e)` |
| onSortEnd | Function | | Callback that is invoked when sorting ends. `function({oldIndex, newIndex, collection}, e)` |
| useDragHandle | Boolean | `false` | If you're using the `SortableHandle` HOC, set this to `true` |
| useWindowAsScrollContainer | Boolean | `false` | If you want, you can set the `window` as the scrolling container |
Expand Down Expand Up @@ -156,11 +157,11 @@ const SortableList = SortableContainer(({items}) => {
return (
<ul>
{items.map((value, index) => (
<SortableItem
<SortableItem
key={`item-${index}`}
index={index}
sortIndex={index}
value={value}
value={value}
/>
))}
</ul>
Expand Down
13 changes: 12 additions & 1 deletion src/SortableContainer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
contentWindow: PropTypes.any,
onSortStart: PropTypes.func,
onSortMove: PropTypes.func,
onSortOver: PropTypes.func,
onSortEnd: PropTypes.func,
shouldCancelStart: PropTypes.func,
pressDelay: PropTypes.number,
Expand Down Expand Up @@ -565,7 +566,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
}

animateNodes() {
const {transitionDuration, hideSortableGhost} = this.props;
const {transitionDuration, hideSortableGhost, onSortOver} = this.props;
const nodes = this.manager.getOrderedRefs();
const deltaScroll = {
left: this.scrollContainer.scrollLeft - this.initialScroll.left,
Expand All @@ -579,6 +580,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
top: (window.pageYOffset - this.initialWindowScroll.top),
left: (window.pageXOffset - this.initialWindowScroll.left),
};
const prevIndex = this.newIndex;
this.newIndex = null;

for (let i = 0, len = nodes.length; i < len; i++) {
Expand Down Expand Up @@ -723,6 +725,15 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
if (this.newIndex == null) {
this.newIndex = this.index;
}

if (onSortOver && this.newIndex !== prevIndex) {
onSortOver({
newIndex: this.newIndex,
oldIndex: prevIndex,
index: this.index,
collection: this.manager.active.collection,
});
}
}

autoscroll = () => {
Expand Down

0 comments on commit 06b8d9f

Please sign in to comment.