Skip to content

Commit

Permalink
dragdrop-container: fix max-children logic issue
Browse files Browse the repository at this point in the history
* Actually check if the dragged is already children to be sure.
* Fix situation for canAccept test in the onDrop method when
  onDragOver already denied the element from entering.
  • Loading branch information
yookoala committed Nov 16, 2023
1 parent 98386dc commit cacb8a8
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/dragdrop-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default class DragDropContainer extends HTMLElement {
return;
}

if (!this.canAcceptChild(dragged, true)) {
if (!this.canAcceptChild(dragged, this.isAncestorOf(dragged))) {
if (dragged instanceof DragDropChild) {
dragged.bounce();
}
Expand Down Expand Up @@ -244,6 +244,24 @@ export default class DragDropContainer extends HTMLElement {
this.#maxChildren = Number.parseInt(newValue);
}
}

/**
* Check if this child is the ancestor of the given element.
*
* @param {HTMLElement} element
*
* @returns {boolean}
*/
isAncestorOf(element) {
let parent = element.parentElement;
while (parent) {
if (parent === this) {
return true;
}
parent = parent.parentElement;
}
return false;
}
}

customElements.define('dragdrop-container', DragDropContainer);

0 comments on commit cacb8a8

Please sign in to comment.