From c54826de6aaed78a8616ea8133941cc506b7d6cb Mon Sep 17 00:00:00 2001 From: Shubham Zanwar Date: Sun, 28 Jun 2020 17:31:05 +0530 Subject: [PATCH] feat: adding ordering and next sibling method to flex layout (#615) * feat: adding methods to get the child index and next child * feat: ordering children properly in insert before --- src/lib/core/FlexLayout.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib/core/FlexLayout.ts b/src/lib/core/FlexLayout.ts index 0749caf28..6ed19989b 100644 --- a/src/lib/core/FlexLayout.ts +++ b/src/lib/core/FlexLayout.ts @@ -72,7 +72,13 @@ export class FlexLayout extends NodeLayout { this.removeWidget(childWidget, childYogaNode); } const beforeChildYogaNode = beforeChildFlexNode || beforeChildWidget.getFlexNode(); - this.nodeChildren.add(childWidget); // No orderer required yet, so just inserting at the end. + + const widgetArr = Array.from(this.nodeChildren); + const beforeChildIndex = this.getChildIndex(beforeChildWidget); + if (beforeChildIndex !== -1) { + widgetArr.splice(beforeChildIndex, 0, childWidget); + } + this.nodeChildren = new Set(widgetArr); this.native.insertChildBefore(childWidget.native, beforeChildYogaNode, childYogaNode); } @@ -89,4 +95,18 @@ export class FlexLayout extends NodeLayout { this.flexNode = flexNode; this.native.setFlexNode(flexNode); } + + getChildIndex(childWidget: NodeWidget): number { + const widgetArr = Array.from(this.nodeChildren); + return widgetArr.indexOf(childWidget); + } + + getNextSibling(childWidget: NodeWidget): NodeWidget | null { + const childIndex = this.getChildIndex(childWidget); + const widgetArr = Array.from(this.nodeChildren); + if (childIndex !== -1) { + return (widgetArr[childIndex + 1] as NodeWidget) || null; + } + return null; + } }