Skip to content

Commit 69b093f

Browse files
authored
Allow root TreeViewItem to be closed (#388)
* Allow root TreeViewItem to be closed * Update root item in example * Lint fixes * Tweaks * Fix open getter * Open example browser root nodes * Allow selecting of categories to allow opening and closing * Rename _intentedOpenState to _open * Lint fixes * Restore newline
1 parent ba8f909 commit 69b093f

File tree

4 files changed

+26
-50
lines changed

4 files changed

+26
-50
lines changed

examples/elements/treeview.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,20 @@
3636
});
3737

3838
const root = new TreeViewItem({
39-
text: 'Root'
39+
open: true,
40+
text: 'My Drive',
41+
icon: 'E229' // Drive icon
4042
});
4143
treeView.append(root);
4244

4345
const generateChildren = (parent, depth) => {
4446
if (depth > 0) {
4547
for (let i = 0; i < 5; i++) {
48+
const isFolder = depth > 1;
4649
const item = new TreeViewItem({
47-
text: 'Item ' + i
50+
allowDrop: isFolder,
51+
text: (isFolder ? 'Folder ' : 'File ') + i,
52+
icon: isFolder ? 'E139' : 'E208' // Folder or file icon
4853
});
4954
parent.append(item);
5055

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106

107107
for (const category of categories) {
108108
const categoryItem = new TreeViewItem({
109-
allowSelect: false,
109+
open: true,
110110
text: category.categoryName
111111
});
112112
treeView.append(categoryItem);

src/components/TreeView/style.scss

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -134,49 +134,10 @@
134134
}
135135
}
136136

137-
.pcui-treeview {
138-
// direct children of tree
139-
> .pcui-treeview-item {
140-
padding-left: 0;
141-
142-
&::before {
143-
content: none;
144-
}
145-
146-
> .pcui-treeview-item-contents {
147-
margin-left: 0;
148-
149-
> .pcui-treeview-item-icon {
150-
&::before {
151-
content: none;
152-
}
153-
154-
&::after {
155-
margin-left: 0;
156-
}
157-
}
158-
}
159-
160-
> .pcui-treeview-item {
161-
padding-left: 21px;
162-
163-
// top line
164-
&::before {
165-
left: 11px;
166-
}
167-
}
168-
}
169-
}
170-
171137
.pcui-treeview:not(.pcui-treeview-filtering) {
172-
// direct children of tree
173-
> .pcui-treeview-item {
174-
.pcui-treeview-item {
175-
&:not(.pcui-treeview-item-open, .pcui-treeview-item-empty) {
176-
> .pcui-treeview-item {
177-
display: none;
178-
}
179-
}
138+
.pcui-treeview-item:not(.pcui-treeview-item-open, .pcui-treeview-item-empty) {
139+
> .pcui-treeview-item {
140+
display: none;
180141
}
181142
}
182143
}

src/components/TreeViewItem/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ interface TreeViewItemArgs extends ContainerArgs {
2626
*/
2727
allowSelect?: boolean,
2828
/**
29-
* Whether the item is open meaning showing its children.
29+
* Whether the item is open (showing its children). Defaults to `false`.
3030
*/
3131
open?: boolean,
3232
/**
@@ -130,6 +130,8 @@ class TreeViewItem extends Container {
130130

131131
protected _icon: string;
132132

133+
protected _open = false;
134+
133135
/**
134136
* Creates a new TreeViewItem.
135137
*
@@ -174,6 +176,8 @@ class TreeViewItem extends Container {
174176
this.selected = args.selected;
175177
}
176178

179+
this._open = args.open ?? false;
180+
177181
const dom = this._containerContents.dom;
178182
dom.addEventListener('focus', this._onContentFocus);
179183
dom.addEventListener('blur', this._onContentBlur);
@@ -210,8 +214,11 @@ class TreeViewItem extends Container {
210214

211215
if (element instanceof TreeViewItem) {
212216
this._numChildren++;
213-
if (this._parent !== this._treeView) {
214-
this.class.remove(CLASS_EMPTY);
217+
this.class.remove(CLASS_EMPTY);
218+
219+
// Apply intended open state now that we have children
220+
if (this._open) {
221+
this.class.add(CLASS_OPEN);
215222
}
216223

217224
if (this._treeView) {
@@ -472,7 +479,10 @@ class TreeViewItem extends Container {
472479
* Sets whether the item is expanded and showing its children.
473480
*/
474481
set open(value) {
475-
if (this.open === value) return;
482+
if (this._open === value) return;
483+
484+
this._open = value;
485+
476486
if (value) {
477487
if (!this.numChildren) return;
478488

@@ -488,7 +498,7 @@ class TreeViewItem extends Container {
488498
* Gets whether the item is expanded and showing its children.
489499
*/
490500
get open() {
491-
return this.class.contains(CLASS_OPEN) || this.parent === this._treeView;
501+
return this._open;
492502
}
493503

494504
/**

0 commit comments

Comments
 (0)