Skip to content

Commit

Permalink
fix: return TreeNode for first and last child (#894)
Browse files Browse the repository at this point in the history
closes #893
  • Loading branch information
tobiasengelhardt authored Feb 18, 2021
1 parent 2c8120b commit c0792c3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ export class TreeNode implements ITreeNode {
getFirstChild(skipHidden = false) {
let children = skipHidden ? this.visibleChildren : this.children;

return [].concat(children || []).shift();
return children != null && children.length ? children[0] : null;
}

getLastChild(skipHidden = false) {
let children = skipHidden ? this.visibleChildren : this.children;

return [].concat(children || []).pop();
return children != null && children.length ? children[children.length - 1] : null;
}

findNextNode(goInside = true, skipHidden = false) {
Expand Down
6 changes: 4 additions & 2 deletions projects/angular-tree-component/src/lib/models/tree.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ export class TreeModel implements ITreeModel, OnDestroy {
}

getFirstRoot(skipHidden = false) {
return [].concat(skipHidden ? this.getVisibleRoots() : this.roots).shift();
const root = skipHidden ? this.getVisibleRoots() : this.roots;
return root != null && root.length ? root[0] : null;
}

getLastRoot(skipHidden = false) {
return [].concat(skipHidden ? this.getVisibleRoots() : this.roots).pop();
const root = skipHidden ? this.getVisibleRoots() : this.roots;
return root != null && root.length ? root[root.length - 1] : null;
}

get isFocused() {
Expand Down

0 comments on commit c0792c3

Please sign in to comment.