Skip to content

Commit

Permalink
fix: remove dropping placeholder on drag leave (react-grid-layout#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
daynin authored and STRML committed Oct 11, 2019
1 parent 056c6f2 commit 1a1ab98
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions lib/ReactGridLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ export default class ReactGridLayout extends React.Component<Props, State> {
children: []
};

dragEnterCounter = 0;

constructor(props: Props, context: any): void {
super(props, context);
autoBindHandlers(this, [
Expand Down Expand Up @@ -743,11 +745,10 @@ export default class ReactGridLayout extends React.Component<Props, State> {
e.preventDefault();
};

onDrop = () => {
removeDroppingPlaceholder = () => {
const { droppingItem, cols } = this.props;
const { layout } = this.state;

const { x, y, w, h } = layout.find(l => l.i === droppingItem.i) || {};
const newLayout = compact(
layout.filter(l => l.i !== droppingItem.i),
compactType(this.props),
Expand All @@ -760,6 +761,34 @@ export default class ReactGridLayout extends React.Component<Props, State> {
activeDrag: null,
droppingPosition: undefined
});
};

onDragLeave = () => {
this.dragEnterCounter--;

// onDragLeave can be triggered on each layout's child.
// But we know that count of dragEnter and dragLeave events
// will be balanced after leaving the layout's container
// so we can increase and decrease count of dragEnter and
// when it'll be equal to 0 we'll remove the placeholder
if (this.dragEnterCounter === 0) {
this.removeDroppingPlaceholder();
}
};

onDragEnter = () => {
this.dragEnterCounter++;
};

onDrop = () => {
const { droppingItem } = this.props;
const { layout } = this.state;
const { x, y, w, h } = layout.find(l => l.i === droppingItem.i) || {};

// reset gragEnter counter on drop
this.dragEnterCounter = 0;

this.removeDroppingPlaceholder();

this.props.onDrop({ x, y, w, h });
};
Expand All @@ -778,6 +807,8 @@ export default class ReactGridLayout extends React.Component<Props, State> {
className={mergedClassName}
style={mergedStyle}
onDrop={isDroppable ? this.onDrop : noop}
onDragLeave={isDroppable ? this.onDragLeave : noop}
onDragEnter={isDroppable ? this.onDragEnter : noop}
onDragOver={isDroppable ? this.onDragOver : noop}
>
{React.Children.map(this.props.children, child =>
Expand Down

0 comments on commit 1a1ab98

Please sign in to comment.