Skip to content

Commit

Permalink
Fix: ParentNode.appendChild can now move the child from a different p…
Browse files Browse the repository at this point in the history
…arent
  • Loading branch information
raquo committed Mar 14, 2020
1 parent 23db4aa commit f18a129
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/scala/com/raquo/laminar/nodes/ParentNode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ trait ParentNode[+Ref <: dom.Element] extends ReactiveNode[Ref] {

@inline def maybeChildren: Option[mutable.Buffer[ChildNode.Base]] = _maybeChildren

/** @return Whether child was successfully appended */
/** Note: can also be used to move children, even within the same parent
*
* @return Whether child was successfully appended
*/
def appendChild(child: ChildNode.Base): Boolean = {
val nextParent = Some(this)
child.willSetParent(nextParent)
Expand All @@ -20,7 +23,12 @@ trait ParentNode[+Ref <: dom.Element] extends ReactiveNode[Ref] {
val appended = DomApi.appendChild(parent = this, child = child)
if (appended) {

// 2. Update this node
// 2A. Update child's current parent node
child.maybeParent.foreach { childParent =>
childParent._maybeChildren.foreach(childParentChildren => childParentChildren -= child)
}

// 2B. Update this node
if (_maybeChildren.isEmpty) {
_maybeChildren = Some(mutable.Buffer(child))
} else {
Expand Down Expand Up @@ -60,7 +68,8 @@ trait ParentNode[+Ref <: dom.Element] extends ReactiveNode[Ref] {

/** Note: can also be used to move children, even within the same parent
*
* @return Whether child was successfully inserted */
* @return Whether child was successfully inserted
*/
def insertChild(
child: ChildNode.Base,
atIndex: Int
Expand Down

0 comments on commit f18a129

Please sign in to comment.