diff --git a/addon/components/tree-node.js b/addon/components/tree-node.js index ccb72c3f..c20e580d 100644 --- a/addon/components/tree-node.js +++ b/addon/components/tree-node.js @@ -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() { diff --git a/addon/components/tree.js b/addon/components/tree.js index 255cdf8e..d4541234 100644 --- a/addon/components/tree.js +++ b/addon/components/tree.js @@ -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; @@ -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]; @@ -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 : []; } diff --git a/package.json b/package.json index 907a12a3..0f57e3df 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/tests/integration/components/tree-node-test.js b/tests/integration/components/tree-node-test.js index 47a8daf6..215a638a 100644 --- a/tests/integration/components/tree-node-test.js +++ b/tests/integration/components/tree-node-test.js @@ -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) { @@ -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]; diff --git a/yarn.lock b/yarn.lock index 9b58f4a5..e7db4665 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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== @@ -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"