Skip to content

Commit

Permalink
feat(tree): expand root only if its single
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabauke committed Apr 14, 2022
1 parent 9561fe8 commit 921edab
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
5 changes: 1 addition & 4 deletions addon/components/tree-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ export default class TreeNodeComponent extends Component {
}

get expandedDefault() {
return (
this.args.item.level === 0 ||
this.args.activeItem?.id === this.args.item.id
);
return this.args.activeItem?.id === this.args.item.id;
}

get expanded() {
Expand Down
5 changes: 5 additions & 0 deletions addon/components/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { inject as service } from "@ember/service";
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { timeout, restartableTask } from "ember-concurrency";
import { cached } from "tracked-toolbox";

export default class TreeComponent extends Component {
@service store;
Expand All @@ -15,6 +16,7 @@ export default class TreeComponent extends Component {
return !!this.filterValue;
}

@cached
get expandedItems() {
if (this.filterValue && this.filtered) {
const expanded = [...this.filtered];
Expand All @@ -31,8 +33,11 @@ export default class TreeComponent extends Component {
});
return expanded.map((item) => item.id);
}
const rootNodes = this.args.items?.filter((i) => i.level === 0);
return this.args.activeItem?.findParents
? this.args.activeItem?.findParents().map((item) => item.id)
: rootNodes.length === 1
? rootNodes
: [];
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"qunit": "2.18.0",
"qunit-dom": "2.0.0",
"sass": "1.49.9",
"tracked-toolbox": "1.2.3",
"webpack": "5.69.1"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/components/tree-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module("Integration | Component | tree-node", function (hooks) {
this.set("item", items[0]);
this.set("itemRoute", "/scope/edit");
this.set("activeItem", items[1]);
this.set("expandedItems", [items[1].id]);
this.set("expandedItems", [items[0].id, items[1].id]);
});

test("it renders", async function (assert) {
Expand All @@ -42,7 +42,7 @@ module("Integration | Component | tree-node", function (hooks) {
@item={{this.item}}
@itemRoute={{this.itemRoute}}
@activeItem={{this.activeItem}}
@expandedItems={{[this.expandedItems]}}
@expandedItems={{this.expandedItems}}
/>`);

const item = this.items[0];
Expand Down
28 changes: 27 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,14 @@
"@handlebars/parser" "^1.1.0"
simple-html-tokenizer "^0.5.10"

"@glimmer/tracking@^1.0.0":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@glimmer/tracking/-/tracking-1.1.2.tgz#74e71be07b0a7066518d24044d2665d0cf8281eb"
integrity sha512-cyV32zsHh+CnftuRX84ALZpd2rpbDrhLhJnTXn9W//QpqdRZ5rdMsxSY9fOsj0CKEc706tmEU299oNnDc0d7tA==
dependencies:
"@glimmer/env" "^0.1.7"
"@glimmer/validator" "^0.44.0"

"@glimmer/tracking@^1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@glimmer/tracking/-/tracking-1.0.4.tgz#f1bc1412fe5e2236d0f8d502994a8f88af1bbb21"
Expand Down Expand Up @@ -5831,7 +5839,7 @@ ember-auto-import@^2.3.0, ember-auto-import@^2.4.0:
ember-style-modifier "^0.7.0"
ember-truth-helpers "^2.1.0 || ^3.0.0"

ember-cache-primitive-polyfill@^1.0.1:
ember-cache-primitive-polyfill@^1.0.0, ember-cache-primitive-polyfill@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/ember-cache-primitive-polyfill/-/ember-cache-primitive-polyfill-1.0.1.tgz#a27075443bd87e5af286c1cd8a7df24e3b9f6715"
integrity sha512-hSPcvIKarA8wad2/b6jDd/eU+OtKmi6uP+iYQbzi5TQpjsqV6b4QdRqrLk7ClSRRKBAtdTuutx+m+X+WlEd2lw==
Expand Down Expand Up @@ -14063,6 +14071,24 @@ tr46@~0.0.3:
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=

tracked-maps-and-sets@^2.0.0:
version "2.2.1"
resolved "https://registry.yarnpkg.com/tracked-maps-and-sets/-/tracked-maps-and-sets-2.2.1.tgz#323dd40540c561e8b0ffdec8bf129c68ec5025f9"
integrity sha512-XYrXh6L/GpGmVmG3KcN/qoDyi4FxHh8eZY/BA/RuoxynskV+GZSfwrX3R+5DR2CIkzkCx4zi4kkDRg1AMDfDhg==
dependencies:
"@glimmer/tracking" "^1.0.0"
ember-cli-babel "^7.17.2"

tracked-toolbox@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/tracked-toolbox/-/tracked-toolbox-1.2.3.tgz#66ee74b948b270958f9401e93d8a69635ed294f9"
integrity sha512-0axTjBYinHurXoat9Qk71GbCxR2GZWDbhV3aGQmeGr7FxGXvfz+/mvuG69Nl3omkOdYy5GRjy7lYXJbDz8lPMA==
dependencies:
ember-cache-primitive-polyfill "^1.0.0"
ember-cli-babel "^7.21.0"
ember-cli-htmlbars "^5.3.1"
tracked-maps-and-sets "^2.0.0"

traverse@~0.6.6:
version "0.6.6"
resolved "https://registry.yarnpkg.com/traverse/-/traverse-0.6.6.tgz#cbdf560fd7b9af632502fed40f918c157ea97137"
Expand Down

0 comments on commit 921edab

Please sign in to comment.