Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tree): expand root only if its solitary #460

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.2",
"qunit-dom": "2.0.0",
"sass": "1.51.0",
"tracked-toolbox": "1.2.3",
"webpack": "5.72.0"
},
"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
36 changes: 27 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1590,18 +1590,18 @@
"@handlebars/parser" "~2.0.0"
simple-html-tokenizer "^0.5.11"

"@glimmer/tracking@^1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@glimmer/tracking/-/tracking-1.0.4.tgz#f1bc1412fe5e2236d0f8d502994a8f88af1bbb21"
integrity sha512-F+oT8I55ba2puSGIzInmVrv/8QA2PcK1VD+GWgFMhF6WC97D+uZX7BFg+a3s/2N4FVBq5KHE+QxZzgazM151Yw==
"@glimmer/tracking@^1.0.0", "@glimmer/tracking@^1.1.2":
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.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@glimmer/tracking/-/tracking-1.1.2.tgz#74e71be07b0a7066518d24044d2665d0cf8281eb"
integrity sha512-cyV32zsHh+CnftuRX84ALZpd2rpbDrhLhJnTXn9W//QpqdRZ5rdMsxSY9fOsj0CKEc706tmEU299oNnDc0d7tA==
"@glimmer/tracking@^1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@glimmer/tracking/-/tracking-1.0.4.tgz#f1bc1412fe5e2236d0f8d502994a8f88af1bbb21"
integrity sha512-F+oT8I55ba2puSGIzInmVrv/8QA2PcK1VD+GWgFMhF6WC97D+uZX7BFg+a3s/2N4FVBq5KHE+QxZzgazM151Yw==
dependencies:
"@glimmer/env" "^0.1.7"
"@glimmer/validator" "^0.44.0"
Expand Down Expand Up @@ -5762,7 +5762,7 @@ ember-auto-import@^2.4.1:
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 @@ -14116,6 +14116,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"

[email protected]:
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