Skip to content

Commit d9040b0

Browse files
Merge pull request #10 from CodeWithDennis/feature/custom-options
Allow custom options
2 parents eabaeff + cd60403 commit d9040b0

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ composer require codewithdennis/filament-select-tree
1919
- ✅ Compatible with dark mode
2020
- ✅ Featuring search functionality
2121
- ✅ Comma seperated multi-select
22+
- ✅ Custom options
23+
- ❌ Disabled options (Planned)
2224
- ❌ Relationships (Planned)
2325

2426
## Usage
@@ -54,9 +56,31 @@ SelectTree::make('category_id')
5456
// Activates the search functionality for the SelectTree.
5557
->searchable()
5658
```
59+
### Custom
60+
If you prefer to create custom options rather than utilizing a pre-existing model, you can achieve this using the following example:
5761

58-
## Changelog
62+
```PHP
63+
->options([
64+
[
65+
'name' => 'Electronics',
66+
'value' => 'electronics',
67+
'children' => [
68+
[
69+
'name' => 'Mobile Devices',
70+
'value' => 'mobile devices',
71+
'children' => [
72+
[
73+
'name' => 'Apple Products',
74+
'value' => 'apple products',
75+
]
76+
]
77+
]
78+
]
79+
],
80+
])
81+
```
5982

83+
## Changelog
6084
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
6185

6286
## Contributing

src/SelectTree.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class SelectTree extends Field
2929

3030
protected bool $disabledBranchNode = false;
3131

32-
protected string $treeModel;
32+
protected ?string $treeModel = null;
33+
34+
protected array $options = [];
3335

3436
protected string $treeParentKey;
3537

@@ -72,14 +74,21 @@ public function multiple(bool $multiple = true): static
7274
return $this;
7375
}
7476

77+
public function options(array $options): static
78+
{
79+
$this->options = $options;
80+
81+
return $this;
82+
}
83+
7584
public function disabledBranchNode(bool $disabledBranchNode = true): static
7685
{
7786
$this->disabledBranchNode = $disabledBranchNode;
7887

7988
return $this;
8089
}
8190

82-
public function getTree(): Collection
91+
public function getTree(): Collection|array
8392
{
8493
return $this->evaluate($this->buildTree());
8594
}
@@ -109,6 +118,11 @@ public function getAlwaysOpen(): bool
109118
return $this->evaluate($this->alwaysOpen);
110119
}
111120

121+
public function getOptions(): array
122+
{
123+
return $this->evaluate($this->options);
124+
}
125+
112126
public function getDisabledBranchNode(): bool
113127
{
114128
return $this->evaluate($this->disabledBranchNode);
@@ -124,13 +138,23 @@ public function tree(string $treeModel, string $treeParentKey, string $titleAttr
124138
return $this;
125139
}
126140

127-
private function buildTree(int $parent = null): Collection
141+
private function buildTree(int $parent = null): array|Collection
128142
{
143+
// Check if custom options are set; if yes, return them.
144+
if ($this->getOptions()) {
145+
return $this->getOptions();
146+
}
147+
148+
// Check if the treeModel is not set; if yes, return an empty collection.
149+
if (! $this->treeModel) {
150+
return collect();
151+
}
152+
129153
// Create a default query to fetch items with the specified parent ID.
130154
$defaultQuery = $this->treeModel::query()
131155
->where($this->treeParentKey, $parent);
132156

133-
// If not a root level query and a modification callback is provided, apply it.
157+
// If we're not at the root level and a modification callback is provided, apply it.
134158
if (! $parent && $this->modifyQueryUsing) {
135159
$defaultQuery = $this->evaluate($this->modifyQueryUsing, ['query' => $defaultQuery]);
136160
}

0 commit comments

Comments
 (0)