Skip to content

Commit

Permalink
Merge pull request #43 from CodeWithDennis/feature/disable-options
Browse files Browse the repository at this point in the history
Introduce disabled options
  • Loading branch information
CodeWithDennis authored Nov 18, 2023
2 parents 60aa02b + 93c8035 commit 9ebf2fc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ Activate the search functionality
->searchable();
```

Disable specific options in the tree

```PHP
->disabledOptions([2, 3, 4])
```

```PHP
->disabledOptions(function () {
return Category::where('is_disabled', true)
->get()
->pluck('id')
->toArray();
})
```


## Screenshots

<img width="641" alt="light" src="https://github.com/CodeWithDennis/filament-select-tree/assets/23448484/4d348c85-5ee9-45b1-9424-0d8b3efcc02e">
Expand Down
10 changes: 9 additions & 1 deletion resources/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ html.dark .treeselect-list__item-checkbox-container {
}

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

html.dark .treeselect-input__tags-element:hover .treeselect-input__tags-cross svg {
Expand Down Expand Up @@ -238,3 +238,11 @@ html.dark .treeselect--disabled {
.treeselect--disabled .treeselect-input__clear {
display: none;
}

.treeselect-list__item--disabled {
cursor: not-allowed !important;
}

html.dark .treeselect-list__item--disabled .treeselect-list__item-checkbox-container {
background-color: hsl(0deg 0% 30.77% / 5%);
}
2 changes: 1 addition & 1 deletion resources/dist/custom.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/SelectTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class SelectTree extends Field

protected string $direction = 'auto';

protected Closure|array $disabledOptions = [];

protected function setUp(): void
{
// Load the state from relationships using a callback function.
Expand Down Expand Up @@ -141,6 +143,7 @@ private function buildNode($result, $resultMap): array
$node = [
'name' => $result->{$this->getTitleAttribute()},
'value' => $result->id,
'disabled' => in_array($result->id, $this->getDisabledOptions()),
];

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

public function disabledOptions(Closure|array $disabledOptions): static
{
$this->disabledOptions = $disabledOptions;

return $this;
}

public function alwaysOpen(bool $alwaysOpen = true): static
{
$this->alwaysOpen = $alwaysOpen;
Expand Down Expand Up @@ -324,4 +334,9 @@ public function getDirection(): string
{
return $this->evaluate($this->direction);
}

public function getDisabledOptions(): array
{
return $this->evaluate($this->disabledOptions);
}
}

0 comments on commit 9ebf2fc

Please sign in to comment.