Skip to content

Commit 44f6391

Browse files
committed
Avoids dupe headers & ensure empty if no children
1 parent ea12b91 commit 44f6391

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/views/viewBase.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -508,21 +508,23 @@ export abstract class ViewBase<
508508
private addHeaderNode(node: ViewNode, promise: ViewNode[] | Promise<ViewNode[]>): ViewNode[] | Promise<ViewNode[]> {
509509
if (!this.grouped || node !== this.root) return promise;
510510

511-
if (!isPromise(promise)) {
512-
if (promise.length && !(promise[0] instanceof GroupedHeaderNode)) {
513-
promise.unshift(new GroupedHeaderNode(this as unknown as View, node));
511+
const ensureGroupedHeaderNode = (children: ViewNode[]): ViewNode[] => {
512+
if (!children.length) return children;
513+
if (children[0] instanceof GroupedHeaderNode) return children.length === 1 ? [] : children;
514+
515+
const index = children.findIndex(n => n instanceof GroupedHeaderNode);
516+
if (index === -1) {
517+
children.unshift(new GroupedHeaderNode(this as unknown as View, node));
518+
} else if (index > 0) {
519+
children.unshift(children.splice(index, 1)[0]);
514520
}
515521

516-
return promise;
517-
}
522+
return children;
523+
};
518524

519-
return promise.then(c => {
520-
if (c.length && !(c[0] instanceof GroupedHeaderNode)) {
521-
c.unshift(new GroupedHeaderNode(this as unknown as View, node));
522-
}
525+
if (!isPromise(promise)) return ensureGroupedHeaderNode(promise);
523526

524-
return c;
525-
});
527+
return promise.then(c => ensureGroupedHeaderNode(c));
526528
}
527529

528530
getChildren(node?: ViewNode): ViewNode[] | Promise<ViewNode[]> {

0 commit comments

Comments
 (0)