From a3b7467a1e701a9730ca73534857b2737adc339c Mon Sep 17 00:00:00 2001 From: gp-lnuff Date: Thu, 10 Jul 2025 09:37:47 +0200 Subject: [PATCH 1/6] add append option --- src/SelectTree.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/SelectTree.php b/src/SelectTree.php index 1985283..51c3ce7 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -88,6 +88,8 @@ class SelectTree extends Field implements HasAffixActions protected Closure|array|null $prepend = null; + protected Closure|array|null $append = null; + protected Closure|string|null $treeKey = 'treeKey'; protected function setUp(): void @@ -316,6 +318,19 @@ public function prepend(Closure|array|null $prepend = null): static return $this; } + public function append(Closure|array|null $append = null): static + { + $this->append = $this->evaluate($append); + + if (is_array($this->append) && isset($this->append['name'], $this->append['value'])) { + $this->append['value'] = (string) $this->append['value']; + } else { + throw new \InvalidArgumentException('The provided append value must be an array with "name" and "value" keys.'); + } + + return $this; + } + public function getRelationship(): BelongsToMany|BelongsTo { return $this->getModelInstance()->{$this->evaluate($this->relationship)}(); From 70686dacff8ddf848f0c3304aa398a4709e32c1d Mon Sep 17 00:00:00 2001 From: gp-lnuff Date: Thu, 10 Jul 2025 09:45:09 +0200 Subject: [PATCH 2/6] Update README.md --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 2f56610..88be339 100644 --- a/README.md +++ b/README.md @@ -223,6 +223,25 @@ use CodeWithDennis\FilamentSelectTree\SelectTree; ]) ``` +If you need to append an item to the tree menu, use the `append` method. This method also accepts an array or a closure. + +```php +->schema([ + SelectTree::make('category') + ->relationship('categories', 'name', 'parent_id') + ->enableBranchNode() + ->multiple(false) + ->append([ + 'name' => 'Uncategorized Records', + 'value' => -1, + 'parent' => null, // optional + 'disabled' => false, // optional + 'hidden' => false, // optional + 'children' => [], // optional + ]) + ]) +``` + ## Filters Use the tree in your table filters. Here's an example to show you how. From 3bd57e87ca9b25c42822722929971d3a4e5d136b Mon Sep 17 00:00:00 2001 From: gp-lnuff Date: Thu, 10 Jul 2025 09:48:28 +0200 Subject: [PATCH 3/6] make append option actually do something --- src/SelectTree.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/SelectTree.php b/src/SelectTree.php index 51c3ce7..ae0e2ea 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -437,8 +437,14 @@ public function storeResults(bool $storeResults = true): static public function getTree(): Collection|array { - return $this->evaluate($this->buildTree()->when($this->prepend, - fn (Collection $tree) => $tree->prepend($this->evaluate($this->prepend)))); + return $this->evaluate($this->buildTree() + ->when($this->prepend, + fn (Collection $tree) => $tree->prepend($this->evaluate($this->prepend)) + ) + ->when($this->append, + fn(Collection $tree) => $tree->push($this->evaluate($this->append)) + ) + ); } public function getResults(): Collection|array|null From ef0e13fa789c6f881c7c8145f1348ed9ac734eca Mon Sep 17 00:00:00 2001 From: CodeWithDennis <23448484+CodeWithDennis@users.noreply.github.com> Date: Sat, 19 Jul 2025 10:14:19 +0200 Subject: [PATCH 4/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 88be339..f2d8643 100644 --- a/README.md +++ b/README.md @@ -238,7 +238,7 @@ If you need to append an item to the tree menu, use the `append` method. This me 'disabled' => false, // optional 'hidden' => false, // optional 'children' => [], // optional - ]) + ]) ]) ``` From 70708ac3d233a7babce5fd5f2037f2a6afef0a78 Mon Sep 17 00:00:00 2001 From: CodeWithDennis <23448484+CodeWithDennis@users.noreply.github.com> Date: Sat, 19 Jul 2025 10:14:24 +0200 Subject: [PATCH 5/6] Update src/SelectTree.php --- src/SelectTree.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/SelectTree.php b/src/SelectTree.php index ae0e2ea..048716a 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -438,12 +438,8 @@ public function storeResults(bool $storeResults = true): static public function getTree(): Collection|array { return $this->evaluate($this->buildTree() - ->when($this->prepend, - fn (Collection $tree) => $tree->prepend($this->evaluate($this->prepend)) - ) - ->when($this->append, - fn(Collection $tree) => $tree->push($this->evaluate($this->append)) - ) + ->when($this->prepend, fn (Collection $tree) => $tree->prepend($this->evaluate($this->prepend))) + ->when($this->append, fn(Collection $tree) => $tree->push($this->evaluate($this->append))) ); } From 2751081f0f6a56e7e628fc1841986e585c4657eb Mon Sep 17 00:00:00 2001 From: CodeWithDennis <23448484+CodeWithDennis@users.noreply.github.com> Date: Sat, 19 Jul 2025 10:15:16 +0200 Subject: [PATCH 6/6] Update src/SelectTree.php --- src/SelectTree.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SelectTree.php b/src/SelectTree.php index 048716a..e9588c8 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -439,7 +439,7 @@ public function getTree(): Collection|array { return $this->evaluate($this->buildTree() ->when($this->prepend, fn (Collection $tree) => $tree->prepend($this->evaluate($this->prepend))) - ->when($this->append, fn(Collection $tree) => $tree->push($this->evaluate($this->append))) + ->when($this->append, fn (Collection $tree) => $tree->push($this->evaluate($this->append))) ); }