Skip to content

Commit 9ebf2fc

Browse files
Merge pull request #43 from CodeWithDennis/feature/disable-options
Introduce disabled options
2 parents 60aa02b + 93c8035 commit 9ebf2fc

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ Activate the search functionality
156156
->searchable();
157157
```
158158

159+
Disable specific options in the tree
160+
161+
```PHP
162+
->disabledOptions([2, 3, 4])
163+
```
164+
165+
```PHP
166+
->disabledOptions(function () {
167+
return Category::where('is_disabled', true)
168+
->get()
169+
->pluck('id')
170+
->toArray();
171+
})
172+
```
173+
174+
159175
## Screenshots
160176

161177
<img width="641" alt="light" src="https://github.com/CodeWithDennis/filament-select-tree/assets/23448484/4d348c85-5ee9-45b1-9424-0d8b3efcc02e">

resources/css/custom.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ html.dark .treeselect-list__item-checkbox-container {
183183
}
184184

185185
.treeselect-input__tags-element:hover .treeselect-input__tags-cross svg {
186-
stroke: rgba(var(--gray-950),var(--tw-text-opacity));
186+
stroke: rgba(var(--gray-950), var(--tw-text-opacity));
187187
}
188188

189189
html.dark .treeselect-input__tags-element:hover .treeselect-input__tags-cross svg {
@@ -238,3 +238,11 @@ html.dark .treeselect--disabled {
238238
.treeselect--disabled .treeselect-input__clear {
239239
display: none;
240240
}
241+
242+
.treeselect-list__item--disabled {
243+
cursor: not-allowed !important;
244+
}
245+
246+
html.dark .treeselect-list__item--disabled .treeselect-list__item-checkbox-container {
247+
background-color: hsl(0deg 0% 30.77% / 5%);
248+
}

resources/dist/custom.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SelectTree.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class SelectTree extends Field
4949

5050
protected string $direction = 'auto';
5151

52+
protected Closure|array $disabledOptions = [];
53+
5254
protected function setUp(): void
5355
{
5456
// Load the state from relationships using a callback function.
@@ -141,6 +143,7 @@ private function buildNode($result, $resultMap): array
141143
$node = [
142144
'name' => $result->{$this->getTitleAttribute()},
143145
'value' => $result->id,
146+
'disabled' => in_array($result->id, $this->getDisabledOptions()),
144147
];
145148

146149
// Check if the result has children
@@ -251,6 +254,13 @@ public function independent(bool $independent = true): static
251254
return $this;
252255
}
253256

257+
public function disabledOptions(Closure|array $disabledOptions): static
258+
{
259+
$this->disabledOptions = $disabledOptions;
260+
261+
return $this;
262+
}
263+
254264
public function alwaysOpen(bool $alwaysOpen = true): static
255265
{
256266
$this->alwaysOpen = $alwaysOpen;
@@ -324,4 +334,9 @@ public function getDirection(): string
324334
{
325335
return $this->evaluate($this->direction);
326336
}
337+
338+
public function getDisabledOptions(): array
339+
{
340+
return $this->evaluate($this->disabledOptions);
341+
}
327342
}

0 commit comments

Comments
 (0)