Skip to content

Commit 25b3f96

Browse files
committed
feat: improve tree-node typings on initialization;
fix missing normalization for addNode api Signed-off-by: Christian Kotzbauer <christian.kotzbauer@gmail.com>
1 parent 88b4b04 commit 25b3f96

File tree

19 files changed

+147
-94
lines changed

19 files changed

+147
-94
lines changed

demo/src/tree/multi-select/multi-select.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import simpleTree, { TreeNode, SimpleTree } from "simple-tree-component";
1+
import simpleTree, { InitTreeNode, SimpleTree } from "simple-tree-component";
22
import { autoinject } from "aurelia-framework";
33

44
@autoinject()
@@ -9,7 +9,7 @@ export class MultiSelect {
99

1010
public attached(): void {
1111
// eslint-disable-next-line @typescript-eslint/no-var-requires
12-
const nodes: TreeNode[] = require("./data.json");
12+
const nodes: InitTreeNode[] = require("./data.json");
1313

1414
this.tree = simpleTree(this.element.querySelector("#multi-select"), "multiSelectDropdown", {
1515
searchBar: true,

demo/src/tree/single-select/single-select.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import simpleTree, { TreeNode, SimpleTree } from "simple-tree-component";
1+
import simpleTree, { InitTreeNode, SimpleTree } from "simple-tree-component";
22
import { autoinject } from "aurelia-framework";
33

44
@autoinject()
@@ -9,7 +9,7 @@ export class SingleSelect {
99

1010
public attached(): void {
1111
// eslint-disable-next-line @typescript-eslint/no-var-requires
12-
const nodes: TreeNode[] = require("./data.json");
12+
const nodes: InitTreeNode[] = require("./data.json");
1313

1414
this.tree = simpleTree(this.element.querySelector("#single-select"), "singleSelectDropdown", {
1515
searchBar: true,

demo/src/tree/tree-view-checkbox/tree-view-checkbox.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import simpleTree, { TreeNode, SimpleTree } from "simple-tree-component";
1+
import simpleTree, { InitTreeNode, SimpleTree } from "simple-tree-component";
22
import { autoinject } from "aurelia-framework";
33

44
@autoinject()
@@ -9,7 +9,7 @@ export class TreeViewCheckbox {
99

1010
public attached(): void {
1111
// eslint-disable-next-line @typescript-eslint/no-var-requires
12-
const nodes: TreeNode[] = require("./data.json");
12+
const nodes: InitTreeNode[] = require("./data.json");
1313

1414
this.tree = simpleTree(this.element.querySelector("#tree-view-checkbox"), "tree", {
1515
searchBar: true,

demo/src/tree/tree-view/tree-view.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import simpleTree, { TreeNode, SimpleTree } from "simple-tree-component";
1+
import simpleTree, { InitTreeNode, SimpleTree } from "simple-tree-component";
22
import { autoinject } from "aurelia-framework";
33

44
@autoinject()
@@ -9,7 +9,7 @@ export class TreeView {
99

1010
public attached(): void {
1111
// eslint-disable-next-line @typescript-eslint/no-var-requires
12-
const nodes: TreeNode[] = require("./data.json");
12+
const nodes: InitTreeNode[] = require("./data.json");
1313

1414
this.tree = simpleTree(this.element.querySelector("#tree-view"), "tree", {
1515
searchBar: true,

dist/simple-tree-component.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,14 @@
470470
normalizeNodes(nodes) {
471471
return nodes
472472
.filter((node) => !!node)
473-
.map((node) => {
474-
const n = this.copyNode(node);
475-
n.uid = this.generateUid(node.value);
476-
this.mutateNode(n);
477-
n.children = this.normalizeNodes(n.children || []);
478-
return n;
479-
});
473+
.map((node) => this.normalizeNode(node));
474+
}
475+
normalizeNode(node) {
476+
const n = this.copyNode(node);
477+
n.uid = this.generateUid(node.value);
478+
this.mutateNode(n);
479+
n.children = this.normalizeNodes(n.children || []);
480+
return n;
480481
}
481482
mutateNode(node) {
482483
if (!node.selectable && node.selected) {
@@ -518,18 +519,18 @@
518519
if (!isTreeNodeValid(node) || isDuplicateNodeValue(this.allNodes, node.value)) {
519520
throw new Error("node value is invalid or node with value already exists!");
520521
}
521-
this.mutateNode(node);
522+
const n = this.normalizeNode(node);
522523
if (parent && this.isTreeNode(parent)) {
523-
parent.children.push(node);
524+
parent.children.push(n);
524525
}
525526
else if (typeof parent === "string") {
526527
const parentNode = this.getNodeInternal(this.allNodes, parent);
527528
if (this.isTreeNode(parentNode)) {
528-
parentNode.children.push(node);
529+
parentNode.children.push(n);
529530
}
530531
}
531532
else {
532-
this.allNodes.push(node);
533+
this.allNodes.push(n);
533534
}
534535
}
535536
moveNode(node, direction) {

dist/simple-tree-component.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/types/instance.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Options, BaseOptions } from "./options";
2-
import { TreeNode } from "./tree-node";
2+
import { InitTreeNode, TreeNode } from "./tree-node";
33
import { Subscription } from "../types/subscription";
44
export interface TreeModeNameMap {
55
singleSelectDropdown: TreeNode | null;
@@ -31,7 +31,7 @@ export interface TreeInstance<K extends keyof TreeModeNameMap> {
3131
* @param node to add.
3232
* @param parent of the new tree-node or null
3333
*/
34-
addNode(node: TreeNode, parent: TreeNode | string | null): void;
34+
addNode(node: InitTreeNode, parent: TreeNode | string | null): void;
3535
/**
3636
* Deletes the given tree-node from the tree.
3737
*

dist/types/options.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TreeNode } from "./tree-node";
1+
import { InitTreeNode, TreeNode } from "./tree-node";
22
/**
33
* @ignore
44
*/
@@ -102,7 +102,7 @@ export interface BaseOptions extends TreeConfiguration {
102102
* All tree-node data-objects to start with. Do not change this array afterwards.
103103
* (Default: `[]`)
104104
*/
105-
nodes: Partial<TreeNode>[];
105+
nodes: InitTreeNode[];
106106
}
107107
/**
108108
* @ignore

dist/types/rects.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ export interface Rect {
3636
* @param maxOverlayHeight The maximum height of the overlay. Defaults to `300`.
3737
* @returns The calculated rectangle of the overlay position.
3838
*/
39-
export declare function calculate(elementRect: Rect, availableHeight: number, overlayHeight: number, borderWith?: number, maxOverlayHeight?: number): Rect;
39+
export declare function calculate(
40+
elementRect: Rect,
41+
availableHeight: number,
42+
overlayHeight: number,
43+
borderWith?: number,
44+
maxOverlayHeight?: number
45+
): Rect;
4046
/**
4147
* Calculates the position of the `overlay` relative to the `element` and sets the values accordingly.
4248
* See the docs of the `calculate` function for more details.

dist/types/tree-node.d.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* The data representation of each node-object in the tree.
2+
* The data representation of each node-object in the tree at initialization-time.
33
*/
4-
export interface TreeNode {
4+
export interface InitTreeNode {
55
/**
66
* The displayed text of this tree-node.
77
*/
@@ -10,6 +10,29 @@ export interface TreeNode {
1010
* The data-value of this tree-node. It has to be unique.
1111
*/
1212
value: string;
13+
/**
14+
* Indicates if this node is currently selected and included in the component-value ({@link Instance.getSelected()}).
15+
*/
16+
selected?: boolean;
17+
/**
18+
* Indicates if this node is selectable. Mouse-clicks on the node are avoided if `false`.
19+
* Nodes that are not selectable are also ignored when setting via API.
20+
* In Recursive Checkbox Mode this flag has no effect since all nodes are selectable there.
21+
*/
22+
selectable?: boolean;
23+
/**
24+
* Recursive array of child `TreeNode` objects.
25+
*/
26+
children?: InitTreeNode[];
27+
/**
28+
* Any additional property, which is available (the component-logic will respect them).
29+
*/
30+
[key: string]: any;
31+
}
32+
/**
33+
* The data representation of each node-object in the tree.
34+
*/
35+
export interface TreeNode extends InitTreeNode {
1336
/**
1437
* Indicates if this node is currently selected and included in the component-value ({@link Instance.getSelected()}).
1538
*/
@@ -36,10 +59,6 @@ export interface TreeNode {
3659
* The unique id-value used internally.
3760
*/
3861
uid: string;
39-
/**
40-
* Any additional property, which is available (the component-logic will respect them).
41-
*/
42-
[key: string]: any;
4362
}
4463
/**
4564
* @ignore

0 commit comments

Comments
 (0)